@@ -30,7 +30,7 @@ def from_Solis(filepath, name=None, parent=None, verbose=True) -> Data:
3030 Parameters
3131 ----------
3232 filepath : path-like
33- Path to .txt file .
33+ Path to file (should be .asc format) .
3434 Can be either a local or remote file (http/ftp).
3535 Can be compressed with gz/bz2, decompression based on file name.
3636 name : string (optional)
@@ -61,15 +61,32 @@ def from_Solis(filepath, name=None, parent=None, verbose=True) -> Data:
6161 axis0 = []
6262 arr = []
6363 attrs = {}
64- while True :
65- line = f .readline ().strip ()[:- 1 ]
66- if len (line ) == 0 :
67- break
68- else :
69- line = line .split ("," )
70- line = [float (x ) for x in line ]
71- axis0 .append (line .pop (0 ))
72- arr .append (line )
64+
65+ line0 = f .readline ().strip ()[:- 1 ]
66+ line0 = [float (x ) for x in line0 .split ("," )] # TODO: robust to space, tab, comma
67+ axis0 .append (line0 .pop (0 ))
68+ arr .append (line0 )
69+
70+ def get_frames (f , arr , axis0 ):
71+ axis0_written = False
72+ while True :
73+ line = f .readline ().strip ()[:- 1 ]
74+ if len (line ) == 0 :
75+ break
76+ else :
77+ line = [float (x ) for x in line .split ("," )]
78+ # signature of new frames is restart of axis0
79+ if not axis0_written and (line [0 ] == axis0 [0 ]):
80+ axis0_written = True
81+ if axis0_written :
82+ line .pop (0 )
83+ else :
84+ axis0 .append (line .pop (0 ))
85+ arr .append (line )
86+ return arr , axis0
87+
88+ arr , axis0 = get_frames (f , arr , axis0 )
89+ nframes = len (arr ) // len (axis0 )
7390
7491 i = 0
7592 while i < 3 :
@@ -94,20 +111,33 @@ def from_Solis(filepath, name=None, parent=None, verbose=True) -> Data:
94111 data = Data (** kwargs )
95112 else :
96113 data = parent .create_data (** kwargs )
97- arr = np .array (arr )
98- arr /= float (attrs ["Exposure Time (secs)" ])
99- # signal has units of Hz because time normalized
100- arr = data .create_channel (name = "signal" , values = arr , signed = False , units = "Hz" )
114+
101115 axis0 = np .array (axis0 )
102116 if float (attrs ["Grating Groove Density (l/mm)" ]) == 0 :
103117 xname = "xindex"
104118 xunits = None
105119 else :
106120 xname = "wm"
107121 xunits = "nm"
108- data .create_variable (name = xname , values = axis0 [:, None ], units = xunits )
109- data .create_variable (name = "yindex" , values = np .arange (arr .shape [1 ])[None , :], units = None )
110- data .transform (data .variables [0 ].natural_name , "yindex" )
122+ axes = [xname , "yindex" ]
123+
124+ if nframes == 1 :
125+ arr = np .array (arr )
126+ data .create_variable (name = xname , values = axis0 [:, None ], units = xunits )
127+ data .create_variable (name = "yindex" , values = np .arange (arr .shape [- 1 ])[None , :], units = None )
128+ else :
129+ arr = np .array (arr ).reshape (nframes , len (axis0 ), len (arr [0 ]))
130+ data .create_variable (name = "frame" , values = np .arange (nframes )[:, None , None ], units = None )
131+ data .create_variable (name = xname , values = axis0 [None , :, None ], units = xunits )
132+ data .create_variable (
133+ name = "yindex" , values = np .arange (arr .shape [- 1 ])[None , None , :], units = None
134+ )
135+ axes = ["frame" ] + axes
136+
137+ data .transform (* axes )
138+ arr /= float (attrs ["Exposure Time (secs)" ])
139+ # signal has units of Hz because time normalized
140+ data .create_channel (name = "signal" , values = arr , signed = False , units = "Hz" )
111141
112142 for key , val in attrs .items ():
113143 data .attrs [key ] = val
0 commit comments