1 | """ |
---|
2 | CanSAS 2D data reader for reading HDF5 formatted CanSAS files. |
---|
3 | """ |
---|
4 | |
---|
5 | import h5py |
---|
6 | import numpy as np |
---|
7 | import re |
---|
8 | import os |
---|
9 | import sys |
---|
10 | |
---|
11 | from sas.sascalc.dataloader.data_info import plottable_1D, plottable_2D, Data1D, Data2D, DataInfo, Process, Aperture |
---|
12 | from sas.sascalc.dataloader.data_info import Collimation, TransmissionSpectrum, Detector |
---|
13 | from sas.sascalc.dataloader.data_info import combine_data_info_with_plottable |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | class Reader(): |
---|
18 | """ |
---|
19 | A class for reading in CanSAS v2.0 data files. The existing iteration opens Mantid generated HDF5 formatted files |
---|
20 | with file extension .h5/.H5. Any number of data sets may be present within the file and any dimensionality of data |
---|
21 | may be used. Currently 1D and 2D SAS data sets are supported, but future implementations will include 1D and 2D |
---|
22 | SESANS data. |
---|
23 | |
---|
24 | Any number of SASdata sets may be present in a SASentry and the data within can be either 1D I(Q) or 2D I(Qx, Qy). |
---|
25 | |
---|
26 | :Dependencies: |
---|
27 | The CanSAS HDF5 reader requires h5py => v2.5.0 or later. |
---|
28 | """ |
---|
29 | |
---|
30 | ## CanSAS version |
---|
31 | cansas_version = 2.0 |
---|
32 | ## Logged warnings or messages |
---|
33 | logging = None |
---|
34 | ## List of errors for the current data set |
---|
35 | errors = None |
---|
36 | ## Raw file contents to be processed |
---|
37 | raw_data = None |
---|
38 | ## Data info currently being read in |
---|
39 | current_datainfo = None |
---|
40 | ## SASdata set currently being read in |
---|
41 | current_dataset = None |
---|
42 | ## List of plottable1D objects that should be linked to the current_datainfo |
---|
43 | data1d = None |
---|
44 | ## List of plottable2D objects that should be linked to the current_datainfo |
---|
45 | data2d = None |
---|
46 | ## Data type name |
---|
47 | type_name = "CanSAS 2.0" |
---|
48 | ## Wildcards |
---|
49 | type = ["CanSAS 2.0 HDF5 Files (*.h5)|*.h5"] |
---|
50 | ## List of allowed extensions |
---|
51 | ext = ['.h5', '.H5'] |
---|
52 | ## Flag to bypass extension check |
---|
53 | allow_all = False |
---|
54 | ## List of files to return |
---|
55 | output = None |
---|
56 | |
---|
57 | def read(self, filename): |
---|
58 | """ |
---|
59 | This is the general read method that all SasView data_loaders must have. |
---|
60 | |
---|
61 | :param filename: A path for an HDF5 formatted CanSAS 2D data file. |
---|
62 | :return: List of Data1D/2D objects and/or a list of errors. |
---|
63 | """ |
---|
64 | |
---|
65 | ## Reinitialize the class when loading a new data file to reset all class variables |
---|
66 | self.reset_class_variables() |
---|
67 | ## Check that the file exists |
---|
68 | if os.path.isfile(filename): |
---|
69 | basename = os.path.basename(filename) |
---|
70 | _, extension = os.path.splitext(basename) |
---|
71 | # If the file type is not allowed, return empty list |
---|
72 | if extension in self.ext or self.allow_all: |
---|
73 | ## Load the data file |
---|
74 | self.raw_data = h5py.File(filename, 'r') |
---|
75 | ## Read in all child elements of top level SASroot |
---|
76 | self.read_children(self.raw_data, []) |
---|
77 | ## Add the last data set to the list of outputs |
---|
78 | self.add_data_set() |
---|
79 | ## Return data set(s) |
---|
80 | return self.output |
---|
81 | |
---|
82 | def reset_class_variables(self): |
---|
83 | """ |
---|
84 | Create the reader object and define initial states for class variables |
---|
85 | """ |
---|
86 | self.current_datainfo = None |
---|
87 | self.current_dataset = None |
---|
88 | self.data1d = [] |
---|
89 | self.data2d = [] |
---|
90 | self.raw_data = None |
---|
91 | self.errors = set() |
---|
92 | self.logging = [] |
---|
93 | self.output = [] |
---|
94 | self.parent_class = u'' |
---|
95 | self.detector = Detector() |
---|
96 | self.collimation = Collimation() |
---|
97 | self.aperture = Aperture() |
---|
98 | self.process = Process() |
---|
99 | self.trans_spectrum = TransmissionSpectrum() |
---|
100 | |
---|
101 | def read_children(self, data, parent_list): |
---|
102 | """ |
---|
103 | A recursive method for stepping through the hierarchical data file. |
---|
104 | |
---|
105 | :param data: h5py Group object of any kind |
---|
106 | :param parent: h5py Group parent name |
---|
107 | """ |
---|
108 | |
---|
109 | ## Loop through each element of the parent and process accordingly |
---|
110 | for key in data.keys(): |
---|
111 | ## Get all information for the current key |
---|
112 | value = data.get(key) |
---|
113 | if value.attrs.get(u'canSAS_class') is not None: |
---|
114 | class_name = value.attrs.get(u'canSAS_class') |
---|
115 | else: |
---|
116 | class_name = value.attrs.get(u'NX_class') |
---|
117 | if class_name is not None: |
---|
118 | class_prog = re.compile(class_name) |
---|
119 | else: |
---|
120 | class_prog = re.compile(value.name) |
---|
121 | |
---|
122 | if isinstance(value, h5py.Group): |
---|
123 | self.parent_class = class_name |
---|
124 | parent_list.append(key) |
---|
125 | ## If this is a new sasentry, store the current data sets and create a fresh Data1D/2D object |
---|
126 | if class_prog.match(u'SASentry'): |
---|
127 | self.add_data_set(key) |
---|
128 | elif class_prog.match(u'SASdata'): |
---|
129 | self._initialize_new_data_set(parent_list) |
---|
130 | ## Recursion step to access data within the group |
---|
131 | self.read_children(value, parent_list) |
---|
132 | self.add_intermediate() |
---|
133 | parent_list.remove(key) |
---|
134 | |
---|
135 | elif isinstance(value, h5py.Dataset): |
---|
136 | ## If this is a dataset, store the data appropriately |
---|
137 | data_set = data[key][:] |
---|
138 | |
---|
139 | for data_point in data_set: |
---|
140 | ## Top Level Meta Data |
---|
141 | unit = self._get_unit(value) |
---|
142 | if key == u'definition': |
---|
143 | self.current_datainfo.meta_data['reader'] = data_point |
---|
144 | elif key == u'run': |
---|
145 | self.current_datainfo.run.append(data_point) |
---|
146 | elif key == u'title': |
---|
147 | self.current_datainfo.title = data_point |
---|
148 | elif key == u'SASnote': |
---|
149 | self.current_datainfo.notes.append(data_point) |
---|
150 | |
---|
151 | ## I and Q Data |
---|
152 | elif key == u'I': |
---|
153 | if type(self.current_dataset) is plottable_2D: |
---|
154 | self.current_dataset.data = np.append(self.current_dataset.data, data_point) |
---|
155 | self.current_dataset.zaxis("Intensity", unit) |
---|
156 | else: |
---|
157 | self.current_dataset.y = np.append(self.current_dataset.y, data_point) |
---|
158 | self.current_dataset.yaxis("Intensity", unit) |
---|
159 | elif key == u'Idev': |
---|
160 | if type(self.current_dataset) is plottable_2D: |
---|
161 | self.current_dataset.err_data = np.append(self.current_dataset.err_data, data_point) |
---|
162 | else: |
---|
163 | self.current_dataset.dy = np.append(self.current_dataset.dy, data_point) |
---|
164 | elif key == u'Q': |
---|
165 | self.current_dataset.xaxis("Q", unit) |
---|
166 | if type(self.current_dataset) is plottable_2D: |
---|
167 | self.current_dataset.q = np.append(self.current_dataset.q, data_point) |
---|
168 | else: |
---|
169 | self.current_dataset.x = np.append(self.current_dataset.x, data_point) |
---|
170 | elif key == u'Qy': |
---|
171 | self.current_dataset.yaxis("Q_y", unit) |
---|
172 | self.current_dataset.qy_data = np.append(self.current_dataset.qy_data, data_point) |
---|
173 | elif key == u'Qydev': |
---|
174 | self.current_dataset.dqy_data = np.append(self.current_dataset.dqy_data, data_point) |
---|
175 | elif key == u'Qx': |
---|
176 | self.current_dataset.xaxis("Q_x", unit) |
---|
177 | self.current_dataset.qx_data = np.append(self.current_dataset.qx_data, data_point) |
---|
178 | elif key == u'Qxdev': |
---|
179 | self.current_dataset.dqx_data = np.append(self.current_dataset.dqx_data, data_point) |
---|
180 | elif key == u'Mask': |
---|
181 | self.current_dataset.mask = np.append(self.current_dataset.mask, data_point) |
---|
182 | |
---|
183 | ## Sample Information |
---|
184 | elif key == u'Title' and self.parent_class == u'SASsample': |
---|
185 | self.current_datainfo.sample.name = data_point |
---|
186 | elif key == u'name' and self.parent_class == u'SASsample': |
---|
187 | self.current_datainfo.sample.name = data_point |
---|
188 | elif key == u'thickness' and self.parent_class == u'SASsample': |
---|
189 | self.current_datainfo.sample.thickness = data_point |
---|
190 | elif key == u'temperature' and self.parent_class == u'SASsample': |
---|
191 | self.current_datainfo.sample.temperature = data_point |
---|
192 | |
---|
193 | ## Instrumental Information |
---|
194 | elif key == u'name' and self.parent_class == u'SASinstrument': |
---|
195 | self.current_datainfo.instrument = data_point |
---|
196 | elif key == u'name' and self.parent_class == u'SASdetector': |
---|
197 | self.detector.name = data_point |
---|
198 | elif key == u'SDD' and self.parent_class == u'SASdetector': |
---|
199 | self.detector.distance = float(data_point) |
---|
200 | self.detector.distance_unit = unit |
---|
201 | elif key == u'SSD' and self.parent_class == u'SAScollimation': |
---|
202 | self.collimation.length = data_point |
---|
203 | self.collimation.length_unit = unit |
---|
204 | elif key == u'name' and self.parent_class == u'SAScollimation': |
---|
205 | self.collimation.name = data_point |
---|
206 | |
---|
207 | ## Process Information |
---|
208 | elif key == u'name' and self.parent_class == u'SASprocess': |
---|
209 | self.process.name = data_point |
---|
210 | elif key == u'Title' and self.parent_class == u'SASprocess': |
---|
211 | self.process.name = data_point |
---|
212 | elif key == u'name' and self.parent_class == u'SASprocess': |
---|
213 | self.process.name = data_point |
---|
214 | elif key == u'description' and self.parent_class == u'SASprocess': |
---|
215 | self.process.description = data_point |
---|
216 | elif key == u'date' and self.parent_class == u'SASprocess': |
---|
217 | self.process.date = data_point |
---|
218 | elif self.parent_class == u'SASprocess': |
---|
219 | self.process.notes.append(data_point) |
---|
220 | |
---|
221 | ## Transmission Spectrum |
---|
222 | elif key == u'T' and self.parent_class == u'SAStransmission_spectrum': |
---|
223 | self.trans_spectrum.transmission.append(data_point) |
---|
224 | elif key == u'Tdev' and self.parent_class == u'SAStransmission_spectrum': |
---|
225 | self.trans_spectrum.transmission_deviation.append(data_point) |
---|
226 | elif key == u'lambda' and self.parent_class == u'SAStransmission_spectrum': |
---|
227 | self.trans_spectrum.wavelength.append(data_point) |
---|
228 | |
---|
229 | ## Other Information |
---|
230 | elif key == u'wavelength' and self.parent_class == u'SASdata': |
---|
231 | self.current_datainfo.source.wavelength = data_point |
---|
232 | self.current_datainfo.source.wavelength.unit = unit |
---|
233 | elif key == u'radiation' and self.parent_class == u'SASsource': |
---|
234 | self.current_datainfo.source.radiation = data_point |
---|
235 | elif key == u'transmission' and self.parent_class == u'SASdata': |
---|
236 | self.current_datainfo.sample.transmission = data_point |
---|
237 | |
---|
238 | ## Everything else goes in meta_data |
---|
239 | else: |
---|
240 | new_key = self._create_unique_key(self.current_datainfo.meta_data, key) |
---|
241 | self.current_datainfo.meta_data[new_key] = data_point |
---|
242 | |
---|
243 | else: |
---|
244 | ## I don't know if this reachable code |
---|
245 | self.errors.add("ShouldNeverHappenException") |
---|
246 | |
---|
247 | def add_intermediate(self): |
---|
248 | """ |
---|
249 | This method stores any intermediate objects within the final data set after fully reading the set. |
---|
250 | |
---|
251 | :param parent: The NXclass name for the h5py Group object that just finished being processed |
---|
252 | """ |
---|
253 | |
---|
254 | if self.parent_class == u'SASprocess': |
---|
255 | self.current_datainfo.process.append(self.process) |
---|
256 | self.process = Process() |
---|
257 | elif self.parent_class == u'SASdetector': |
---|
258 | self.current_datainfo.detector.append(self.detector) |
---|
259 | self.detector = Detector() |
---|
260 | elif self.parent_class == u'SAStransmission_spectrum': |
---|
261 | self.current_datainfo.trans_spectrum.append(self.trans_spectrum) |
---|
262 | self.trans_spectrum = TransmissionSpectrum() |
---|
263 | elif self.parent_class == u'SAScollimation': |
---|
264 | self.current_datainfo.collimation.append(self.collimation) |
---|
265 | self.collimation = Collimation() |
---|
266 | elif self.parent_class == u'SASaperture': |
---|
267 | self.collimation.aperture.append(self.aperture) |
---|
268 | self.aperture = Aperture() |
---|
269 | elif self.parent_class == u'SASdata': |
---|
270 | if type(self.current_dataset) is plottable_2D: |
---|
271 | self.data2d.append(self.current_dataset) |
---|
272 | elif type(self.current_dataset) is plottable_1D: |
---|
273 | self.data1d.append(self.current_dataset) |
---|
274 | |
---|
275 | def final_data_cleanup(self): |
---|
276 | """ |
---|
277 | Does some final cleanup and formatting on self.current_datainfo and all data1D and data2D objects and then |
---|
278 | combines the data and info into Data1D and Data2D objects |
---|
279 | """ |
---|
280 | |
---|
281 | ## Type cast data arrays to float64 |
---|
282 | if len(self.current_datainfo.trans_spectrum) > 0: |
---|
283 | spectrum_list = [] |
---|
284 | for spectrum in self.current_datainfo.trans_spectrum: |
---|
285 | spectrum.transmission = np.delete(spectrum.transmission, [0]) |
---|
286 | spectrum.transmission = spectrum.transmission.astype(np.float64) |
---|
287 | spectrum.transmission_deviation = np.delete(spectrum.transmission_deviation, [0]) |
---|
288 | spectrum.transmission_deviation = spectrum.transmission_deviation.astype(np.float64) |
---|
289 | spectrum.wavelength = np.delete(spectrum.wavelength, [0]) |
---|
290 | spectrum.wavelength = spectrum.wavelength.astype(np.float64) |
---|
291 | if len(spectrum.transmission) > 0: |
---|
292 | spectrum_list.append(spectrum) |
---|
293 | self.current_datainfo.trans_spectrum = spectrum_list |
---|
294 | |
---|
295 | ## Append errors to dataset and reset class errors |
---|
296 | self.current_datainfo.errors = self.errors |
---|
297 | self.errors.clear() |
---|
298 | |
---|
299 | ## Combine all plottables with datainfo and append each to output |
---|
300 | ## Type cast data arrays to float64 and find min/max as appropriate |
---|
301 | for dataset in self.data2d: |
---|
302 | dataset.data = np.delete(dataset.data, [0]) |
---|
303 | dataset.data = dataset.data.astype(np.float64) |
---|
304 | dataset.err_data = np.delete(dataset.err_data, [0]) |
---|
305 | dataset.err_data = dataset.err_data.astype(np.float64) |
---|
306 | dataset.mask = np.delete(dataset.mask, [0]) |
---|
307 | if dataset.qx_data is not None: |
---|
308 | dataset.qx_data = np.delete(dataset.qx_data, [0]) |
---|
309 | dataset.xmin = np.min(dataset.qx_data) |
---|
310 | dataset.xmax = np.max(dataset.qx_data) |
---|
311 | dataset.qx_data = dataset.qx_data.astype(np.float64) |
---|
312 | if dataset.dqx_data is not None: |
---|
313 | dataset.dqx_data = np.delete(dataset.dqx_data, [0]) |
---|
314 | dataset.dqx_data = dataset.dqx_data.astype(np.float64) |
---|
315 | if dataset.qy_data is not None: |
---|
316 | dataset.qy_data = np.delete(dataset.qy_data, [0]) |
---|
317 | dataset.ymin = np.min(dataset.qy_data) |
---|
318 | dataset.ymax = np.max(dataset.qy_data) |
---|
319 | dataset.qy_data = dataset.qy_data.astype(np.float64) |
---|
320 | if dataset.dqy_data is not None: |
---|
321 | dataset.dqy_data = np.delete(dataset.dqy_data, [0]) |
---|
322 | dataset.dqy_data = dataset.dqy_data.astype(np.float64) |
---|
323 | if dataset.q_data is not None: |
---|
324 | dataset.q_data = np.delete(dataset.q_data, [0]) |
---|
325 | dataset.q_data = dataset.q_data.astype(np.float64) |
---|
326 | zeros = np.ones(dataset.data.size, dtype=bool) |
---|
327 | try: |
---|
328 | for i in range (0, dataset.mask.size - 1): |
---|
329 | zeros[i] = dataset.mask[i] |
---|
330 | except: |
---|
331 | self.errors.add(sys.exc_value) |
---|
332 | dataset.mask = zeros |
---|
333 | ## Calculate the actual Q matrix |
---|
334 | try: |
---|
335 | if dataset.q_data.size <= 1: |
---|
336 | dataset.q_data = np.sqrt(dataset.qx_data * dataset.qx_data + dataset.qy_data * dataset.qy_data) |
---|
337 | except: |
---|
338 | dataset.q_data = None |
---|
339 | final_dataset = combine_data_info_with_plottable(dataset, self.current_datainfo) |
---|
340 | self.output.append(final_dataset) |
---|
341 | |
---|
342 | for dataset in self.data1d: |
---|
343 | if dataset.x is not None: |
---|
344 | dataset.x = np.delete(dataset.x, [0]) |
---|
345 | dataset.x = dataset.x.astype(np.float64) |
---|
346 | dataset.xmin = np.min(dataset.x) |
---|
347 | dataset.xmax = np.max(dataset.x) |
---|
348 | if dataset.y is not None: |
---|
349 | dataset.y = np.delete(dataset.y, [0]) |
---|
350 | dataset.y = dataset.y.astype(np.float64) |
---|
351 | dataset.ymin = np.min(dataset.y) |
---|
352 | dataset.ymax = np.max(dataset.y) |
---|
353 | if dataset.dx is not None: |
---|
354 | dataset.dx = np.delete(dataset.dx, [0]) |
---|
355 | dataset.dx = dataset.dx.astype(np.float64) |
---|
356 | if dataset.dxl is not None: |
---|
357 | dataset.dxl = np.delete(dataset.dxl, [0]) |
---|
358 | dataset.dxl = dataset.dxl.astype(np.float64) |
---|
359 | if dataset.dxw is not None: |
---|
360 | dataset.dxw = np.delete(dataset.dxw, [0]) |
---|
361 | dataset.dxw = dataset.dxw.astype(np.float64) |
---|
362 | if dataset.dy is not None: |
---|
363 | dataset.dy = np.delete(dataset.dy, [0]) |
---|
364 | dataset.dy = dataset.dy.astype(np.float64) |
---|
365 | final_dataset = combine_data_info_with_plottable(dataset, self.current_datainfo) |
---|
366 | self.output.append(final_dataset) |
---|
367 | |
---|
368 | def add_data_set(self, key=""): |
---|
369 | """ |
---|
370 | Adds the current_dataset to the list of outputs after preforming final processing on the data and then calls a |
---|
371 | private method to generate a new data set. |
---|
372 | |
---|
373 | :param key: NeXus group name for current tree level |
---|
374 | """ |
---|
375 | |
---|
376 | if self.current_datainfo and self.current_dataset: |
---|
377 | self.final_data_cleanup() |
---|
378 | self.data1d = [] |
---|
379 | self.data2d = [] |
---|
380 | self.current_datainfo = DataInfo() |
---|
381 | |
---|
382 | def write(self, dataset, filename): |
---|
383 | """ |
---|
384 | Write an array of Data1d or Data2D objects to a CanSAS 2.0 file, as |
---|
385 | one SASEntry with multiple SASData elements. The metadata of the first |
---|
386 | elememt in the array will be written as the SASentry metadata |
---|
387 | (detector, instrument, sample, etc) |
---|
388 | |
---|
389 | :param dataset: A list of Data1D or Data2D objects to write |
---|
390 | :param filename: Where to write the CanSAS 2.0 file |
---|
391 | """ |
---|
392 | is_1d = all([isinstance(d, Data1D) for d in dataset]) |
---|
393 | is_2d = all([isinstance(d, Data2D) for d in dataset]) |
---|
394 | |
---|
395 | if not (is_1d or is_2d): |
---|
396 | msg = ("All elements of dataset must be of the same class " |
---|
397 | "(Data1D or Data2D)") |
---|
398 | raise ValueError(msg) |
---|
399 | |
---|
400 | def _h5_string(string): |
---|
401 | """ |
---|
402 | Convert a string to a numpy string in a numpy array. This way it is |
---|
403 | written to the HDF5 file as a fixed length ASCII string and is |
---|
404 | compatible with the Reader read() method. |
---|
405 | """ |
---|
406 | if not isinstance(string, str): |
---|
407 | raise ValueError("String should be of type str") |
---|
408 | |
---|
409 | return np.array([np.string_(string)]) |
---|
410 | |
---|
411 | def _h5_float(x): |
---|
412 | if not (isinstance(x, list)): |
---|
413 | x = [x] |
---|
414 | return np.array(x, dtype=np.float32) |
---|
415 | |
---|
416 | # Get run name and number from first Data object |
---|
417 | data_info = dataset[0] |
---|
418 | run_number = '' |
---|
419 | run_name = '' |
---|
420 | if len(data_info.run) > 0: |
---|
421 | run_number = data_info.run[0] |
---|
422 | if len(data_info.run_name) > 0: |
---|
423 | run_name = data_info.run_name[run_number] |
---|
424 | |
---|
425 | f = h5py.File(filename, 'w') |
---|
426 | sasentry = f.create_group('sasentry01') |
---|
427 | sasentry['definition'] = _h5_string('NXcanSAS') |
---|
428 | sasentry['run'] = _h5_string(run_number) |
---|
429 | sasentry['run'].attrs['name'] = run_name |
---|
430 | sasentry['title'] = _h5_string(data_info.title) |
---|
431 | sasentry.attrs['canSAS_class'] = 'SASentry' |
---|
432 | sasentry.attrs['version'] = '1.0' |
---|
433 | |
---|
434 | i = 1 |
---|
435 | |
---|
436 | if is_1d: |
---|
437 | for data_obj in dataset: |
---|
438 | data_entry = sasentry.create_group("sasdata{0:0=2d}".format(i)) |
---|
439 | data_entry.attrs['canSAS_class'] = 'SASdata' |
---|
440 | data_entry.attrs['signal'] = 'I' |
---|
441 | data_entry.attrs['I_axes'] = 'Q' |
---|
442 | data_entry.attrs['I_uncertainties'] = 'Idev' |
---|
443 | data_entry.attrs['Q_indicies'] = 0 |
---|
444 | Q_data = data_entry.create_dataset('Q', data=data_obj.x) |
---|
445 | I_data = data_entry.create_dataset('I', data=data_obj.y) |
---|
446 | dI_data = data_entry.create_dataset('Idev', data=data_obj.dy) |
---|
447 | i += 1 |
---|
448 | |
---|
449 | data_info = dataset[0] |
---|
450 | sample_entry = sasentry.create_group('sassample') |
---|
451 | sample_entry.attrs['canSAS_class'] = 'SASsample' |
---|
452 | sample_entry['name'] = _h5_string(data_info.sample.name) |
---|
453 | sample_attrs = ['thickness', 'temperature'] |
---|
454 | for key in sample_attrs: |
---|
455 | if getattr(data_info.sample, key) is not None: |
---|
456 | sample_entry.create_dataset(key, data=np.array([getattr(data_info.sample, key)])) |
---|
457 | |
---|
458 | instrument_entry = sasentry.create_group('sasinstrument') |
---|
459 | instrument_entry.attrs['canSAS_class'] = 'SASinstrument' |
---|
460 | instrument_entry['name'] = _h5_string(data_info.instrument) |
---|
461 | |
---|
462 | source_entry = instrument_entry.create_group('sassource') |
---|
463 | source_entry.attrs['canSAS_class'] = 'SASsource' |
---|
464 | if data_info.source.radiation is None: |
---|
465 | source_entry['radiation'] = _h5_string('neutron') |
---|
466 | else: |
---|
467 | source_entry['radiation'] = _h5_string(data_info.source.radiation) |
---|
468 | |
---|
469 | if len(data_info.collimation) > 0: |
---|
470 | i = 1 |
---|
471 | for coll_info in data_info.collimation: |
---|
472 | collimation_entry = instrument_entry.create_group( |
---|
473 | 'sascollimation{0:0=2d}'.format(i)) |
---|
474 | collimation_entry.attrs['canSAS_class'] = 'SAScollimation' |
---|
475 | if coll_info.length is not None: |
---|
476 | collimation_entry['SDD'] = _h5_float(coll_info.length) |
---|
477 | collimation_entry['SDD'].attrs['units'] = coll_info.length_unit |
---|
478 | if coll_info.name is not None: |
---|
479 | collimation_entry['name'] = _h5_string(coll_info.name) |
---|
480 | else: |
---|
481 | collimation_entry = instrument_entry.create_group('sascollimation01') |
---|
482 | |
---|
483 | if len(data_info.detector) > 0: |
---|
484 | i = 1 |
---|
485 | for det_info in data_info.detector: |
---|
486 | detector_entry = instrument_entry.create_group( |
---|
487 | 'sasdetector{0:0=2d}'.format(i)) |
---|
488 | detector_entry.attrs['canSAS_class'] = 'SASdetector' |
---|
489 | if det_info.distance is not None: |
---|
490 | detector_entry['SDD'] = _h5_float(det_info.distance) |
---|
491 | detector_entry['SDD'].attrs['units'] = det_info.distance_unit |
---|
492 | if det_info.name is not None: |
---|
493 | detector_entry['name'] = _h5_string(det_info.name) |
---|
494 | else: |
---|
495 | detector_entry['name'] = _h5_string('') |
---|
496 | i += 1 |
---|
497 | else: |
---|
498 | detector_entry = instrument_entry.create_group('sasdetector01') |
---|
499 | detector_entry.attrs['canSAS_class'] = 'SASdetector' |
---|
500 | detector_entry.attrs['name'] = '' |
---|
501 | |
---|
502 | # TODO: implement writing SASnote |
---|
503 | note_entry = sasentry.create_group('sasnote{0:0=2d}'.format(i)) |
---|
504 | note_entry.attrs['canSAS_class'] = 'SASnote' |
---|
505 | |
---|
506 | elif is_2d: |
---|
507 | f.close() |
---|
508 | raise NotImplementedError("Saving 2D data as CanSAS 2.0 is not yet implemented") |
---|
509 | |
---|
510 | f.close() |
---|
511 | |
---|
512 | |
---|
513 | def _initialize_new_data_set(self, parent_list = None): |
---|
514 | """ |
---|
515 | A private class method to generate a new 1D or 2D data object based on the type of data within the set. |
---|
516 | Outside methods should call add_data_set() to be sure any existing data is stored properly. |
---|
517 | |
---|
518 | :param parent_list: List of names of parent elements |
---|
519 | """ |
---|
520 | |
---|
521 | if parent_list is None: |
---|
522 | parent_list = [] |
---|
523 | if self._find_intermediate(parent_list, "Qx"): |
---|
524 | self.current_dataset = plottable_2D() |
---|
525 | else: |
---|
526 | x = np.array(0) |
---|
527 | y = np.array(0) |
---|
528 | self.current_dataset = plottable_1D(x, y) |
---|
529 | self.current_datainfo.filename = self.raw_data.filename |
---|
530 | |
---|
531 | def _find_intermediate(self, parent_list, basename=""): |
---|
532 | """ |
---|
533 | A private class used to find an entry by either using a direct key or knowing the approximate basename. |
---|
534 | |
---|
535 | :param parent_list: List of parents to the current level in the HDF5 file |
---|
536 | :param basename: Approximate name of an entry to search for |
---|
537 | :return: |
---|
538 | """ |
---|
539 | |
---|
540 | entry = False |
---|
541 | key_prog = re.compile(basename) |
---|
542 | top = self.raw_data |
---|
543 | for parent in parent_list: |
---|
544 | top = top.get(parent) |
---|
545 | for key in top.keys(): |
---|
546 | if (key_prog.match(key)): |
---|
547 | entry = True |
---|
548 | break |
---|
549 | return entry |
---|
550 | |
---|
551 | def _create_unique_key(self, dictionary, name, numb=0): |
---|
552 | """ |
---|
553 | Create a unique key value for any dictionary to prevent overwriting |
---|
554 | Recurses until a unique key value is found. |
---|
555 | |
---|
556 | :param dictionary: A dictionary with any number of entries |
---|
557 | :param name: The index of the item to be added to dictionary |
---|
558 | :param numb: The number to be appended to the name, starts at 0 |
---|
559 | :return: The new name for the dictionary entry |
---|
560 | """ |
---|
561 | if dictionary.get(name) is not None: |
---|
562 | numb += 1 |
---|
563 | name = name.split("_")[0] |
---|
564 | name += "_{0}".format(numb) |
---|
565 | name = self._create_unique_key(dictionary, name, numb) |
---|
566 | return name |
---|
567 | |
---|
568 | def _get_unit(self, value): |
---|
569 | """ |
---|
570 | Find the unit for a particular value within the h5py dictionary |
---|
571 | |
---|
572 | :param value: attribute dictionary for a particular value set |
---|
573 | :return: unit for the value passed to the method |
---|
574 | """ |
---|
575 | unit = value.attrs.get(u'units') |
---|
576 | if unit == None: |
---|
577 | unit = value.attrs.get(u'unit') |
---|
578 | ## Convert the unit formats |
---|
579 | if unit == "1/A": |
---|
580 | unit = "A^{-1}" |
---|
581 | elif unit == "1/cm": |
---|
582 | unit = "cm^{-1}" |
---|
583 | return unit |
---|