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