diff --git a/gwygsf/__init__.py b/gwygsf/__init__.py index eb8630b..f5c790b 100644 --- a/gwygsf/__init__.py +++ b/gwygsf/__init__.py @@ -1,2 +1,2 @@ """gwygsf: Gwyddion Simple Field File Format Parser""" -from .readfile import * +from .GSF import * diff --git a/gwygsf/readfile.py b/gwygsf/readfile.py index 765a6f6..299cd2b 100644 --- a/gwygsf/readfile.py +++ b/gwygsf/readfile.py @@ -4,21 +4,28 @@ import numpy as np class ParseError(Exception): """Exception thrown when an error occurs when parsing a GSF file.""" +def skiplen(f): + l = 4 - (f.tell() % 4) + for i in range(0,l): + if f.read(1) != b'\0': + raise ParseError("Truncated null padding") + def fromfile(self, filename): """Parse the GSF file into the class. Throws ParseError.""" + with open(filename, 'rb') as f: magic = f.readline() self.attr = {} if magic != b"Gwyddion Simple Field 1.0\n": raise ParseError("Magic line not found") - while f.peek(1) != b'\0': - line = f.readline().split(b'=') - if len(line) != 2: - raise ParseError("Malformed attribute line") - self.attr[str(line[0].strip())] = line[1].strip() - while f.read(1) == b'\0': - pass + while f.peek(1)[0] != 0: + line = f.readline() + if line.rfind(b'=') < 0: + raise ParseError("Invalid attribute line") + line = line.split(b'=') + self.attr[line[0].strip().decode()] = line[1].strip().decode() + skiplen(f) self.x = int(self.attr["XRes"]) 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,dtype=np.float32), (self.y, self.x))