properly import gsf file

This commit is contained in:
Peter McGoron 2021-09-16 13:44:20 -04:00
parent 6760c21870
commit fadb371120
2 changed files with 16 additions and 9 deletions

View File

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

View File

@ -4,21 +4,28 @@ import numpy as np
class ParseError(Exception): class ParseError(Exception):
"""Exception thrown when an error occurs when parsing a GSF file.""" """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): def fromfile(self, filename):
"""Parse the GSF file into the class. Throws ParseError.""" """Parse the GSF file into the class. Throws ParseError."""
with open(filename, 'rb') as f: with open(filename, 'rb') as f:
magic = f.readline() magic = f.readline()
self.attr = {} 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)[0] != 0:
line = f.readline().split(b'=') line = f.readline()
if len(line) != 2: if line.rfind(b'=') < 0:
raise ParseError("Malformed attribute line") raise ParseError("Invalid attribute line")
self.attr[str(line[0].strip())] = line[1].strip() line = line.split(b'=')
while f.read(1) == b'\0': self.attr[line[0].strip().decode()] = line[1].strip().decode()
pass skiplen(f)
self.x = int(self.attr["XRes"]) self.x = int(self.attr["XRes"])
self.y = int(self.attr["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,dtype=np.float32), (self.y, self.x))