reorganize

This commit is contained in:
Peter McGoron 2021-09-11 13:45:48 -04:00
parent 899acf0309
commit 6760c21870
3 changed files with 37 additions and 26 deletions

17
gwygsf/GSF.py Normal file
View File

@ -0,0 +1,17 @@
"""This is the skeleton for the GSF container class.
Methods in the GSF class are spread out across other files.
"""
import numpy.typing as npt
import numpy as np
class GSF:
"""Container for GSF files."""
from .readfile import fromfile
attr : dict
d : npt.NDArray[np.float32]
def __init__(self):
"""Initialize class attributes to unusable values."""
self.d = None
self.attr = {}

View File

@ -1 +1,2 @@
"""gwygsf: Gwyddion Simple Field File Format Parser"""
from .readfile import * from .readfile import *

View File

@ -1,31 +1,24 @@
import io """This module is code for reading and writing GSF files."""
import numpy as np import numpy as np
class ParseError(Exception): class ParseError(Exception):
def __init__(self, s): """Exception thrown when an error occurs when parsing a GSF file."""
self.s = s
def __str__(self):
return self.s
class GSF: def fromfile(self, filename):
def fromfile(self, s): """Parse the GSF file into the class. Throws ParseError."""
with open(s, 'rb') as f: with open(filename, 'rb') as f:
magic = f.readline() magic = f.readline()
self.dic = {} self.attr = {}
if magic != b"Gwyddion Simple Field 1.0\n": if magic != b"Gwyddion Simple Field 1.0\n":
raise ParseError("Magic line not found") raise ParseError("Magic line not found")
while f.peek(1) != b'\0': while f.peek(1) != b'\0':
l = f.readline().split(b'=') line = f.readline().split(b'=')
dic[str(l[0].strip())] = l[1].strip() if len(line) != 2:
raise ParseError("Malformed attribute line")
self.attr[str(line[0].strip())] = line[1].strip()
while f.read(1) == b'\0': while f.read(1) == b'\0':
pass pass
self.x = int(dic["XRes"]) self.x = int(self.attr["XRes"])
self.y = int(dic["YRes"]) self.y = int(self.attr["YRes"])
self.d = np.reshape(np.fromfile(f,type=np.float32), (self.y, self.x)) self.d = np.reshape(np.fromfile(f,type=np.float32), (self.y, self.x))
def __init__(self):
self.x = None
self.y = None
self.d = None
self.dic = {}