11import gwyfile
22import gsffile
33import numpy as np
4+ import pandas as pd
45
56class Reader :
67 def __init__ (self , fullfilepath = None ):
@@ -173,7 +174,7 @@ def read(self):
173174 linestring = fid .readline ()
174175 Nlines = 1
175176
176- while 'Row ' not in linestring :
177+ while 'Version ' not in linestring :
177178 Nlines += 1
178179 linestring = fid .readline ()
179180 if Nlines > 1 :
@@ -219,12 +220,107 @@ def read(self):
219220 except :
220221 params [fieldname ] = val .strip ()
221222
222- channels = linestring .split ('\t ' )
223+ if 'Version:' in linestring :
224+ linestring = fid .readline ()
225+ channels = linestring .split ('\t ' )
226+ for i , channel in enumerate (channels ):
227+ channels [i ] = channel .split (' ' )[0 ]
228+ else :
229+ channels = linestring .split ('\t ' )
230+
223231 fid .close ()
224232
225233 C_data = np .genfromtxt (self .filename , skip_header = Nlines )
226234
227235 for i in range (len (channels )- 1 ):
228- data [channels [i ]] = C_data [:,i ]
236+ data [channels [i ]] = C_data [1 :,i ]
229237
238+ return data , params
239+
240+ class NeaSpectrumGeneralReader (Reader ):
241+ def __init__ (self , fullfilepath = None , output = 'dict' ):
242+ super ().__init__ (fullfilepath )
243+ self .output = output
244+
245+ def lineparser (self ,linestring ,params :dict ):
246+
247+ ct = linestring .split ('\t ' )
248+ fieldname = ct [0 ][2 :- 1 ]
249+ fieldname = fieldname .replace (' ' , '' )
250+
251+ if 'Scanner Center Position' in linestring :
252+ fieldname = fieldname [:- 5 ]
253+ params [fieldname ] = [float (ct [2 ]), float (ct [3 ])]
254+
255+ elif 'Scan Area' in linestring :
256+ fieldname = fieldname [:- 7 ]
257+ params [fieldname ] = [float (ct [2 ]), float (ct [3 ]), float (ct [4 ])]
258+
259+ elif 'Pixel Area' in linestring :
260+ fieldname = fieldname [:- 7 ]
261+ params [fieldname ] = [int (ct [2 ]), int (ct [3 ]), int (ct [4 ])]
262+
263+ elif 'Averaging' in linestring :
264+ params [fieldname ] = int (ct [2 ])
265+
266+ elif 'Interferometer Center/Distance' in linestring :
267+ fieldname = fieldname .replace ('/' , '' )
268+ params [fieldname ] = [float (ct [2 ]), float (ct [3 ])]
269+
270+ elif 'Regulator' in linestring :
271+ fieldname = fieldname [:- 7 ]
272+ params [fieldname ] = [float (ct [2 ]), float (ct [3 ]), float (ct [4 ])]
273+
274+ elif 'Q-Factor' in linestring :
275+ fieldname = fieldname .replace ('-' , '' )
276+ params [fieldname ] = float (ct [2 ])
277+
278+ else :
279+ fieldname = ct [0 ][2 :- 1 ]
280+ fieldname = fieldname .replace (' ' , '' )
281+ val = ct [2 ]
282+ val = val .replace (',' ,'' )
283+ try :
284+ params [fieldname ] = float (val )
285+ except :
286+ params [fieldname ] = val .strip ()
287+
288+ return params
289+
290+ def read_header (self ):
291+ params = {}
292+ with open (self .filename , "rt" , encoding = "utf8" ) as f :
293+ # Read www.neaspec.com
294+ line = f .readline ()
295+ count = 1
296+ while f :
297+ line = f .readline ()
298+ count = count + 1
299+ if line [0 ] != '#' :
300+ break
301+ params = self .lineparser (line ,params )
302+ channels = line .split ('\t ' )
303+
304+ return channels , params
305+
306+ def read (self ):
307+ data = {}
308+
309+ channels , params = self .read_header ()
310+
311+ count = len (list (params .keys ()))+ 2
312+
313+ data = pd .read_csv (
314+ self .filename ,
315+ sep = "\t " ,
316+ skiprows = count ,
317+ encoding = "utf-8" ,
318+ names = channels ,
319+ ).dropna (axis = 1 , how = "all" )
320+
321+ if self .output == "dict" :
322+ data = data .to_dict ('list' )
323+ for key in list (data .keys ()):
324+ data [key ] = np .asarray (data [key ])
325+
230326 return data , params
0 commit comments