Changes in / [c7634fd:316b9c1] in sasview
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py
r18af6d2 r61f329f0 38 38 # CanSAS version 39 39 cansas_version = 2.0 40 # Logged warnings or messages 41 logging = None 42 # List of errors for the current data set 43 errors = None 44 # Raw file contents to be processed 45 raw_data = None 46 # List of plottable1D objects that should be linked to the current_datainfo 47 data1d = None 48 # List of plottable2D objects that should be linked to the current_datainfo 49 data2d = None 40 50 # Data type name 41 51 type_name = "CanSAS 2.0" … … 101 111 self.errors = set() 102 112 self.logging = [] 103 self.q_name = []104 self.mask_name = u''105 self.i_name = u''106 self.i_node = u''107 self.q_uncertainties = u''108 self.q_resolutions = u''109 self.i_uncertainties = u''110 113 self.parent_class = u'' 111 114 self.detector = Detector() … … 144 147 self.add_data_set(key) 145 148 elif class_prog.match(u'SASdata'): 146 self._find_data_attributes(value)147 149 self._initialize_new_data_set(parent_list) 148 150 # Recursion step to access data within the group … … 157 159 data_set = data[key][:] 158 160 unit = self._get_unit(value) 161 162 # I and Q Data 163 if key == u'I': 164 if isinstance(self.current_dataset, plottable_2D): 165 self.current_dataset.data = data_set 166 self.current_dataset.zaxis("Intensity", unit) 167 else: 168 self.current_dataset.y = data_set.flatten() 169 self.current_dataset.yaxis("Intensity", unit) 170 continue 171 elif key == u'Idev': 172 if isinstance(self.current_dataset, plottable_2D): 173 self.current_dataset.err_data = data_set.flatten() 174 else: 175 self.current_dataset.dy = data_set.flatten() 176 continue 177 elif key == u'Q': 178 self.current_dataset.xaxis("Q", unit) 179 if isinstance(self.current_dataset, plottable_2D): 180 self.current_dataset.q = data_set.flatten() 181 else: 182 self.current_dataset.x = data_set.flatten() 183 continue 184 elif key == u'Qdev': 185 self.current_dataset.dx = data_set.flatten() 186 continue 187 elif key == u'dQw': 188 self.current_dataset.dxw = data_set.flatten() 189 continue 190 elif key == u'dQl': 191 self.current_dataset.dxl = data_set.flatten() 192 continue 193 elif key == u'Qy': 194 self.current_dataset.yaxis("Q_y", unit) 195 self.current_dataset.qy_data = data_set.flatten() 196 continue 197 elif key == u'Qydev': 198 self.current_dataset.dqy_data = data_set.flatten() 199 continue 200 elif key == u'Qx': 201 self.current_dataset.xaxis("Q_x", unit) 202 self.current_dataset.qx_data = data_set.flatten() 203 continue 204 elif key == u'Qxdev': 205 self.current_dataset.dqx_data = data_set.flatten() 206 continue 207 elif key == u'Mask': 208 self.current_dataset.mask = data_set.flatten() 209 continue 210 # Transmission Spectrum 211 elif (key == u'T' 212 and self.parent_class == u'SAStransmission_spectrum'): 213 self.trans_spectrum.transmission = data_set.flatten() 214 continue 215 elif (key == u'Tdev' 216 and self.parent_class == u'SAStransmission_spectrum'): 217 self.trans_spectrum.transmission_deviation = \ 218 data_set.flatten() 219 continue 220 elif (key == u'lambda' 221 and self.parent_class == u'SAStransmission_spectrum'): 222 self.trans_spectrum.wavelength = data_set.flatten() 223 continue 159 224 160 225 for data_point in data_set: … … 167 232 if key == u'definition': 168 233 self.current_datainfo.meta_data['reader'] = data_point 169 # Run170 234 elif key == u'run': 171 235 self.current_datainfo.run.append(data_point) … … 176 240 except Exception: 177 241 pass 178 # Title179 242 elif key == u'title': 180 243 self.current_datainfo.title = data_point 181 # Note182 244 elif key == u'SASnote': 183 245 self.current_datainfo.notes.append(data_point) 246 184 247 # Sample Information 185 elif self.parent_class == u'SASsample': 186 self.process_sample(data_point, key) 248 # CanSAS 2.0 format 249 elif key == u'Title' and self.parent_class == u'SASsample': 250 self.current_datainfo.sample.name = data_point 251 # NXcanSAS format 252 elif key == u'name' and self.parent_class == u'SASsample': 253 self.current_datainfo.sample.name = data_point 254 # NXcanSAS format 255 elif key == u'ID' and self.parent_class == u'SASsample': 256 self.current_datainfo.sample.name = data_point 257 elif (key == u'thickness' 258 and self.parent_class == u'SASsample'): 259 self.current_datainfo.sample.thickness = data_point 260 elif (key == u'temperature' 261 and self.parent_class == u'SASsample'): 262 self.current_datainfo.sample.temperature = data_point 263 elif (key == u'transmission' 264 and self.parent_class == u'SASsample'): 265 self.current_datainfo.sample.transmission = data_point 266 elif (key == u'x_position' 267 and self.parent_class == u'SASsample'): 268 self.current_datainfo.sample.position.x = data_point 269 elif (key == u'y_position' 270 and self.parent_class == u'SASsample'): 271 self.current_datainfo.sample.position.y = data_point 272 elif key == u'pitch' and self.parent_class == u'SASsample': 273 self.current_datainfo.sample.orientation.x = data_point 274 elif key == u'yaw' and self.parent_class == u'SASsample': 275 self.current_datainfo.sample.orientation.y = data_point 276 elif key == u'roll' and self.parent_class == u'SASsample': 277 self.current_datainfo.sample.orientation.z = data_point 278 elif (key == u'details' 279 and self.parent_class == u'SASsample'): 280 self.current_datainfo.sample.details.append(data_point) 281 187 282 # Instrumental Information 188 283 elif (key == u'name' 189 284 and self.parent_class == u'SASinstrument'): 190 285 self.current_datainfo.instrument = data_point 191 # Detector 192 elif self.parent_class == u'SASdetector': 193 self.process_detector(data_point, key, unit) 194 # Collimation 195 elif self.parent_class == u'SAScollimation': 196 self.process_collimation(data_point, key, unit) 197 # Aperture 198 elif self.parent_class == u'SASaperture': 199 self.process_aperture(data_point, key) 286 elif key == u'name' and self.parent_class == u'SASdetector': 287 self.detector.name = data_point 288 elif key == u'SDD' and self.parent_class == u'SASdetector': 289 self.detector.distance = float(data_point) 290 self.detector.distance_unit = unit 291 elif (key == u'slit_length' 292 and self.parent_class == u'SASdetector'): 293 self.detector.slit_length = float(data_point) 294 self.detector.slit_length_unit = unit 295 elif (key == u'x_position' 296 and self.parent_class == u'SASdetector'): 297 self.detector.offset.x = float(data_point) 298 self.detector.offset_unit = unit 299 elif (key == u'y_position' 300 and self.parent_class == u'SASdetector'): 301 self.detector.offset.y = float(data_point) 302 self.detector.offset_unit = unit 303 elif (key == u'pitch' 304 and self.parent_class == u'SASdetector'): 305 self.detector.orientation.x = float(data_point) 306 self.detector.orientation_unit = unit 307 elif key == u'roll' and self.parent_class == u'SASdetector': 308 self.detector.orientation.z = float(data_point) 309 self.detector.orientation_unit = unit 310 elif key == u'yaw' and self.parent_class == u'SASdetector': 311 self.detector.orientation.y = float(data_point) 312 self.detector.orientation_unit = unit 313 elif (key == u'beam_center_x' 314 and self.parent_class == u'SASdetector'): 315 self.detector.beam_center.x = float(data_point) 316 self.detector.beam_center_unit = unit 317 elif (key == u'beam_center_y' 318 and self.parent_class == u'SASdetector'): 319 self.detector.beam_center.y = float(data_point) 320 self.detector.beam_center_unit = unit 321 elif (key == u'x_pixel_size' 322 and self.parent_class == u'SASdetector'): 323 self.detector.pixel_size.x = float(data_point) 324 self.detector.pixel_size_unit = unit 325 elif (key == u'y_pixel_size' 326 and self.parent_class == u'SASdetector'): 327 self.detector.pixel_size.y = float(data_point) 328 self.detector.pixel_size_unit = unit 329 elif (key == u'distance' 330 and self.parent_class == u'SAScollimation'): 331 self.collimation.length = data_point 332 self.collimation.length_unit = unit 333 elif (key == u'name' 334 and self.parent_class == u'SAScollimation'): 335 self.collimation.name = data_point 336 elif (key == u'shape' 337 and self.parent_class == u'SASaperture'): 338 self.aperture.shape = data_point 339 elif (key == u'x_gap' 340 and self.parent_class == u'SASaperture'): 341 self.aperture.size.x = data_point 342 elif (key == u'y_gap' 343 and self.parent_class == u'SASaperture'): 344 self.aperture.size.y = data_point 345 200 346 # Process Information 201 elif self.parent_class == u'SASprocess': # CanSAS 2.0 202 self.process_process(data_point, key) 347 elif (key == u'Title' 348 and self.parent_class == u'SASprocess'): # CanSAS 2.0 349 self.process.name = data_point 350 elif (key == u'name' 351 and self.parent_class == u'SASprocess'): # NXcanSAS 352 self.process.name = data_point 353 elif (key == u'description' 354 and self.parent_class == u'SASprocess'): 355 self.process.description = data_point 356 elif key == u'date' and self.parent_class == u'SASprocess': 357 self.process.date = data_point 358 elif key == u'term' and self.parent_class == u'SASprocess': 359 self.process.term = data_point 360 elif self.parent_class == u'SASprocess': 361 self.process.notes.append(data_point) 362 203 363 # Source 204 elif self.parent_class == u'SASsource': 205 self.process_source(data_point, key, unit) 364 elif (key == u'wavelength' 365 and self.parent_class == u'SASdata'): 366 self.current_datainfo.source.wavelength = data_point 367 self.current_datainfo.source.wavelength_unit = unit 368 elif (key == u'incident_wavelength' 369 and self.parent_class == 'SASsource'): 370 self.current_datainfo.source.wavelength = data_point 371 self.current_datainfo.source.wavelength_unit = unit 372 elif (key == u'wavelength_max' 373 and self.parent_class == u'SASsource'): 374 self.current_datainfo.source.wavelength_max = data_point 375 self.current_datainfo.source.wavelength_max_unit = unit 376 elif (key == u'wavelength_min' 377 and self.parent_class == u'SASsource'): 378 self.current_datainfo.source.wavelength_min = data_point 379 self.current_datainfo.source.wavelength_min_unit = unit 380 elif (key == u'incident_wavelength_spread' 381 and self.parent_class == u'SASsource'): 382 self.current_datainfo.source.wavelength_spread = \ 383 data_point 384 self.current_datainfo.source.wavelength_spread_unit = \ 385 unit 386 elif (key == u'beam_size_x' 387 and self.parent_class == u'SASsource'): 388 self.current_datainfo.source.beam_size.x = data_point 389 self.current_datainfo.source.beam_size_unit = unit 390 elif (key == u'beam_size_y' 391 and self.parent_class == u'SASsource'): 392 self.current_datainfo.source.beam_size.y = data_point 393 self.current_datainfo.source.beam_size_unit = unit 394 elif (key == u'beam_shape' 395 and self.parent_class == u'SASsource'): 396 self.current_datainfo.source.beam_shape = data_point 397 elif (key == u'radiation' 398 and self.parent_class == u'SASsource'): 399 self.current_datainfo.source.radiation = data_point 400 elif (key == u'transmission' 401 and self.parent_class == u'SASdata'): 402 self.current_datainfo.sample.transmission = data_point 403 206 404 # Everything else goes in meta_data 207 elif self.parent_class == u'SASdata':208 self.process_data_object(data_set, key, unit)209 break210 elif self.parent_class == u'SAStransmission_spectrum':211 self.process_trans_spectrum(data_set, key)212 break213 405 else: 214 406 new_key = self._create_unique_key( … … 219 411 # I don't know if this reachable code 220 412 self.errors.add("ShouldNeverHappenException") 221 222 def process_data_object(self, data_set, key, unit):223 """224 SASdata processor method225 :param data_set: data from HDF5 file226 :param key: canSAS_class attribute227 :param unit: unit attribute228 """229 if key == self.i_name:230 if isinstance(self.current_dataset, plottable_2D):231 self.current_dataset.data = data_set232 self.current_dataset.zaxis("Intensity", unit)233 else:234 self.current_dataset.y = data_set.flatten()235 self.current_dataset.yaxis("Intensity", unit)236 elif key == self.i_uncertainties:237 if isinstance(self.current_dataset, plottable_2D):238 self.current_dataset.err_data = data_set.flatten()239 else:240 self.current_dataset.dy = data_set.flatten()241 elif key in self.q_name:242 self.current_dataset.xaxis("Q", unit)243 if isinstance(self.current_dataset, plottable_2D):244 self.current_dataset.q = data_set.flatten()245 else:246 self.current_dataset.x = data_set.flatten()247 elif key in self.q_resolutions:248 if key == u'dQw':249 self.current_dataset.dxw = data_set.flatten()250 elif key == u'dQl':251 self.current_dataset.dxl = data_set.flatten()252 else:253 self.current_dataset.dx = data_set.flatten()254 elif key == u'Qy':255 self.current_dataset.yaxis("Q_y", unit)256 self.current_dataset.qy_data = data_set.flatten()257 elif key == u'Qydev':258 self.current_dataset.dqy_data = data_set.flatten()259 elif key == u'Qx':260 self.current_dataset.xaxis("Q_x", unit)261 self.current_dataset.qx_data = data_set.flatten()262 elif key == u'Qxdev':263 self.current_dataset.dqx_data = data_set.flatten()264 elif key == self.mask_name:265 self.current_dataset.mask = data_set.flatten()266 elif key == u'wavelength':267 self.current_datainfo.source.wavelength = data_set[0]268 self.current_datainfo.source.wavelength_unit = unit269 270 def process_trans_spectrum(self, data_set, key):271 """272 SAStransmission_spectrum processor273 :param data_set: data from HDF5 file274 :param key: canSAS_class attribute275 """276 if key == u'T':277 self.trans_spectrum.transmission = data_set.flatten()278 elif key == u'Tdev':279 self.trans_spectrum.transmission_deviation = data_set.flatten()280 elif key == u'lambda':281 self.trans_spectrum.wavelength = data_set.flatten()282 283 def process_sample(self, data_point, key):284 """285 SASsample processor286 :param data_point: Single point from an HDF5 data file287 :param key: class name data_point was taken from288 """289 if key == u'Title':290 self.current_datainfo.sample.name = data_point291 elif key == u'name':292 self.current_datainfo.sample.name = data_point293 elif key == u'ID':294 self.current_datainfo.sample.name = data_point295 elif key == u'thickness':296 self.current_datainfo.sample.thickness = data_point297 elif key == u'temperature':298 self.current_datainfo.sample.temperature = data_point299 elif key == u'transmission':300 self.current_datainfo.sample.transmission = data_point301 elif key == u'x_position':302 self.current_datainfo.sample.position.x = data_point303 elif key == u'y_position':304 self.current_datainfo.sample.position.y = data_point305 elif key == u'pitch':306 self.current_datainfo.sample.orientation.x = data_point307 elif key == u'yaw':308 self.current_datainfo.sample.orientation.y = data_point309 elif key == u'roll':310 self.current_datainfo.sample.orientation.z = data_point311 elif key == u'details':312 self.current_datainfo.sample.details.append(data_point)313 314 def process_detector(self, data_point, key, unit):315 """316 SASdetector processor317 :param data_point: Single point from an HDF5 data file318 :param key: class name data_point was taken from319 :param unit: unit attribute from data set320 """321 if key == u'name':322 self.detector.name = data_point323 elif key == u'SDD':324 self.detector.distance = float(data_point)325 self.detector.distance_unit = unit326 elif key == u'slit_length':327 self.detector.slit_length = float(data_point)328 self.detector.slit_length_unit = unit329 elif key == u'x_position':330 self.detector.offset.x = float(data_point)331 self.detector.offset_unit = unit332 elif key == u'y_position':333 self.detector.offset.y = float(data_point)334 self.detector.offset_unit = unit335 elif key == u'pitch':336 self.detector.orientation.x = float(data_point)337 self.detector.orientation_unit = unit338 elif key == u'roll':339 self.detector.orientation.z = float(data_point)340 self.detector.orientation_unit = unit341 elif key == u'yaw':342 self.detector.orientation.y = float(data_point)343 self.detector.orientation_unit = unit344 elif key == u'beam_center_x':345 self.detector.beam_center.x = float(data_point)346 self.detector.beam_center_unit = unit347 elif key == u'beam_center_y':348 self.detector.beam_center.y = float(data_point)349 self.detector.beam_center_unit = unit350 elif key == u'x_pixel_size':351 self.detector.pixel_size.x = float(data_point)352 self.detector.pixel_size_unit = unit353 elif key == u'y_pixel_size':354 self.detector.pixel_size.y = float(data_point)355 self.detector.pixel_size_unit = unit356 357 def process_collimation(self, data_point, key, unit):358 """359 SAScollimation processor360 :param data_point: Single point from an HDF5 data file361 :param key: class name data_point was taken from362 :param unit: unit attribute from data set363 """364 if key == u'distance':365 self.collimation.length = data_point366 self.collimation.length_unit = unit367 elif key == u'name':368 self.collimation.name = data_point369 370 def process_aperture(self, data_point, key):371 """372 SASaperture processor373 :param data_point: Single point from an HDF5 data file374 :param key: class name data_point was taken from375 """376 if key == u'shape':377 self.aperture.shape = data_point378 elif key == u'x_gap':379 self.aperture.size.x = data_point380 elif key == u'y_gap':381 self.aperture.size.y = data_point382 383 def process_source(self, data_point, key, unit):384 """385 SASsource processor386 :param data_point: Single point from an HDF5 data file387 :param key: class name data_point was taken from388 :param unit: unit attribute from data set389 """390 if key == u'incident_wavelength':391 self.current_datainfo.source.wavelength = data_point392 self.current_datainfo.source.wavelength_unit = unit393 elif key == u'wavelength_max':394 self.current_datainfo.source.wavelength_max = data_point395 self.current_datainfo.source.wavelength_max_unit = unit396 elif key == u'wavelength_min':397 self.current_datainfo.source.wavelength_min = data_point398 self.current_datainfo.source.wavelength_min_unit = unit399 elif key == u'incident_wavelength_spread':400 self.current_datainfo.source.wavelength_spread = data_point401 self.current_datainfo.source.wavelength_spread_unit = unit402 elif key == u'beam_size_x':403 self.current_datainfo.source.beam_size.x = data_point404 self.current_datainfo.source.beam_size_unit = unit405 elif key == u'beam_size_y':406 self.current_datainfo.source.beam_size.y = data_point407 self.current_datainfo.source.beam_size_unit = unit408 elif key == u'beam_shape':409 self.current_datainfo.source.beam_shape = data_point410 elif key == u'radiation':411 self.current_datainfo.source.radiation = data_point412 413 def process_process(self, data_point, key):414 """415 SASprocess processor416 :param data_point: Single point from an HDF5 data file417 :param key: class name data_point was taken from418 """419 if key == u'Title': # CanSAS 2.0420 self.process.name = data_point421 elif key == u'name': # NXcanSAS422 self.process.name = data_point423 elif key == u'description':424 self.process.description = data_point425 elif key == u'date':426 self.process.date = data_point427 elif key == u'term':428 self.process.term = data_point429 else:430 self.process.notes.append(data_point)431 413 432 414 def add_intermediate(self): … … 533 515 self.current_datainfo = DataInfo() 534 516 517 535 518 def _initialize_new_data_set(self, parent_list=None): 536 519 """ … … 551 534 self.current_dataset = plottable_1D(x, y) 552 535 self.current_datainfo.filename = self.raw_data.filename 553 self.mask_name = ""554 self.i_name = ""555 self.i_node = ""556 self.q_name = []557 self.q_uncertainties = ""558 self.q_resolutions = ""559 self.i_uncertainties = ""560 561 def _find_data_attributes(self, value):562 """563 A class to find the indices for Q, the name of the Qdev and Idev, and564 the name of the mask.565 :param value: SASdata/NXdata HDF5 Group566 """567 attrs = value.attrs568 signal = attrs.get("signal")569 i_axes = np.array(str(attrs.get("I_axes")).split(","))570 q_indices = np.int_(attrs.get("Q_indices").split(","))571 keys = value.keys()572 self.mask_name = attrs.get("mask")573 for val in q_indices:574 self.q_name.append(i_axes[val])575 self.i_name = signal576 self.i_node = value.get(self.i_name)577 for item in self.q_name:578 if item in keys:579 q_vals = value.get(item)580 self.q_uncertainties = q_vals.attrs.get("uncertainty")581 self.q_resolutions = q_vals.attrs.get("resolution")582 if self.i_name in keys:583 i_vals = value.get(self.i_name)584 self.i_uncertainties = i_vals.attrs.get("uncertainty")585 536 586 537 def _find_intermediate(self, parent_list, basename=""):
Note: See TracChangeset
for help on using the changeset viewer.