Changes in / [0a88623:d7fd7be] in sasview
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py
r61f329f0 r61f329f0 111 111 self.errors = set() 112 112 self.logging = [] 113 self.output = [] 114 self.q_name = [] 115 self.mask_name = u'' 116 self.i_name = u'' 117 self.i_node = u'' 118 self.q_uncertainties = u'' 119 self.q_resolutions = u'' 120 self.i_uncertainties = u'' 113 121 self.parent_class = u'' 114 122 self.detector = Detector() … … 147 155 self.add_data_set(key) 148 156 elif class_prog.match(u'SASdata'): 157 self._find_data_attributes(value) 149 158 self._initialize_new_data_set(parent_list) 150 159 # Recursion step to access data within the group … … 159 168 data_set = data[key][:] 160 169 unit = self._get_unit(value) 161 162 # I and Q Data163 if key == u'I':164 if isinstance(self.current_dataset, plottable_2D):165 self.current_dataset.data = data_set166 self.current_dataset.zaxis("Intensity", unit)167 else:168 self.current_dataset.y = data_set.flatten()169 self.current_dataset.yaxis("Intensity", unit)170 continue171 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 continue177 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 continue184 elif key == u'Qdev':185 self.current_dataset.dx = data_set.flatten()186 continue187 elif key == u'dQw':188 self.current_dataset.dxw = data_set.flatten()189 continue190 elif key == u'dQl':191 self.current_dataset.dxl = data_set.flatten()192 continue193 elif key == u'Qy':194 self.current_dataset.yaxis("Q_y", unit)195 self.current_dataset.qy_data = data_set.flatten()196 continue197 elif key == u'Qydev':198 self.current_dataset.dqy_data = data_set.flatten()199 continue200 elif key == u'Qx':201 self.current_dataset.xaxis("Q_x", unit)202 self.current_dataset.qx_data = data_set.flatten()203 continue204 elif key == u'Qxdev':205 self.current_dataset.dqx_data = data_set.flatten()206 continue207 elif key == u'Mask':208 self.current_dataset.mask = data_set.flatten()209 continue210 # Transmission Spectrum211 elif (key == u'T'212 and self.parent_class == u'SAStransmission_spectrum'):213 self.trans_spectrum.transmission = data_set.flatten()214 continue215 elif (key == u'Tdev'216 and self.parent_class == u'SAStransmission_spectrum'):217 self.trans_spectrum.transmission_deviation = \218 data_set.flatten()219 continue220 elif (key == u'lambda'221 and self.parent_class == u'SAStransmission_spectrum'):222 self.trans_spectrum.wavelength = data_set.flatten()223 continue224 170 225 171 for data_point in data_set: … … 232 178 if key == u'definition': 233 179 self.current_datainfo.meta_data['reader'] = data_point 180 # Run 234 181 elif key == u'run': 235 182 self.current_datainfo.run.append(data_point) … … 240 187 except Exception: 241 188 pass 189 # Title 242 190 elif key == u'title': 243 191 self.current_datainfo.title = data_point 192 # Note 244 193 elif key == u'SASnote': 245 194 self.current_datainfo.notes.append(data_point) 246 247 195 # Sample Information 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 196 elif self.parent_class == u'SASsample': 197 self.process_sample(data_point, key) 282 198 # Instrumental Information 283 199 elif (key == u'name' 284 200 and self.parent_class == u'SASinstrument'): 285 201 self.current_datainfo.instrument = data_point 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 202 # Detector 203 elif self.parent_class == u'SASdetector': 204 self.process_detector(data_point, key, unit) 205 # Collimation 206 elif self.parent_class == u'SAScollimation': 207 self.process_collimation(data_point, key, unit) 208 # Aperture 209 elif self.parent_class == u'SASaperture': 210 self.process_aperture(data_point, key) 346 211 # Process Information 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 212 elif self.parent_class == u'SASprocess': # CanSAS 2.0 213 self.process_process(data_point, key) 363 214 # Source 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 215 elif self.parent_class == u'SASsource': 216 self.process_source(data_point, key, unit) 404 217 # Everything else goes in meta_data 218 elif self.parent_class == u'SASdata': 219 self.process_data_object(data_set, key, unit) 220 break 221 elif self.parent_class == u'SAStransmission_spectrum': 222 self.process_trans_spectrum(data_set, key) 223 break 405 224 else: 406 225 new_key = self._create_unique_key( … … 411 230 # I don't know if this reachable code 412 231 self.errors.add("ShouldNeverHappenException") 232 233 def process_data_object(self, data_set, key, unit): 234 """ 235 SASdata processor method 236 :param data_set: data from HDF5 file 237 :param key: canSAS_class attribute 238 :param unit: unit attribute 239 """ 240 # FIXME: check attributes of SASdata for Q, dQ, Mask, etc. 241 if key == self.i_name: 242 if isinstance(self.current_dataset, plottable_2D): 243 self.current_dataset.data = data_set 244 self.current_dataset.zaxis("Intensity", unit) 245 else: 246 self.current_dataset.y = data_set.flatten() 247 self.current_dataset.yaxis("Intensity", unit) 248 elif key == self.i_uncertainties: 249 if isinstance(self.current_dataset, plottable_2D): 250 self.current_dataset.err_data = data_set.flatten() 251 else: 252 self.current_dataset.dy = data_set.flatten() 253 elif key in self.q_name: 254 self.current_dataset.xaxis("Q", unit) 255 if isinstance(self.current_dataset, plottable_2D): 256 self.current_dataset.q = data_set.flatten() 257 else: 258 self.current_dataset.x = data_set.flatten() 259 elif key in self.q_resolutions: 260 if key == u'dQw': 261 self.current_dataset.dxw = data_set.flatten() 262 elif key == u'dQl': 263 self.current_dataset.dxl = data_set.flatten() 264 else: 265 self.current_dataset.dx = data_set.flatten() 266 elif key == u'Qy': 267 self.current_dataset.yaxis("Q_y", unit) 268 self.current_dataset.qy_data = data_set.flatten() 269 elif key == u'Qydev': 270 self.current_dataset.dqy_data = data_set.flatten() 271 elif key == u'Qx': 272 self.current_dataset.xaxis("Q_x", unit) 273 self.current_dataset.qx_data = data_set.flatten() 274 elif key == u'Qxdev': 275 self.current_dataset.dqx_data = data_set.flatten() 276 elif key == self.mask_name: 277 self.current_dataset.mask = data_set.flatten() 278 elif key == u'wavelength': 279 self.current_datainfo.source.wavelength = data_set[0] 280 self.current_datainfo.source.wavelength_unit = unit 281 282 def process_trans_spectrum(self, data_set, key): 283 """ 284 SAStransmission_spectrum processor 285 :param data_set: data from HDF5 file 286 :param key: canSAS_class attribute 287 """ 288 if key == u'T': 289 self.trans_spectrum.transmission = data_set.flatten() 290 elif key == u'Tdev': 291 self.trans_spectrum.transmission_deviation = data_set.flatten() 292 elif key == u'lambda': 293 self.trans_spectrum.wavelength = data_set.flatten() 294 295 def process_sample(self, data_point, key): 296 """ 297 SASsample processor 298 :param data_point: Single point from an HDF5 data file 299 :param key: class name data_point was taken from 300 """ 301 if key == u'Title': 302 self.current_datainfo.sample.name = data_point 303 elif key == u'name': 304 self.current_datainfo.sample.name = data_point 305 elif key == u'ID': 306 self.current_datainfo.sample.name = data_point 307 elif key == u'thickness': 308 self.current_datainfo.sample.thickness = data_point 309 elif key == u'temperature': 310 self.current_datainfo.sample.temperature = data_point 311 elif key == u'transmission': 312 self.current_datainfo.sample.transmission = data_point 313 elif key == u'x_position': 314 self.current_datainfo.sample.position.x = data_point 315 elif key == u'y_position': 316 self.current_datainfo.sample.position.y = data_point 317 elif key == u'pitch': 318 self.current_datainfo.sample.orientation.x = data_point 319 elif key == u'yaw': 320 self.current_datainfo.sample.orientation.y = data_point 321 elif key == u'roll': 322 self.current_datainfo.sample.orientation.z = data_point 323 elif key == u'details': 324 self.current_datainfo.sample.details.append(data_point) 325 326 def process_detector(self, data_point, key, unit): 327 """ 328 SASdetector processor 329 :param data_point: Single point from an HDF5 data file 330 :param key: class name data_point was taken from 331 :param unit: unit attribute from data set 332 """ 333 if key == u'name': 334 self.detector.name = data_point 335 elif key == u'SDD': 336 self.detector.distance = float(data_point) 337 self.detector.distance_unit = unit 338 elif key == u'slit_length': 339 self.detector.slit_length = float(data_point) 340 self.detector.slit_length_unit = unit 341 elif key == u'x_position': 342 self.detector.offset.x = float(data_point) 343 self.detector.offset_unit = unit 344 elif key == u'y_position': 345 self.detector.offset.y = float(data_point) 346 self.detector.offset_unit = unit 347 elif key == u'pitch': 348 self.detector.orientation.x = float(data_point) 349 self.detector.orientation_unit = unit 350 elif key == u'roll': 351 self.detector.orientation.z = float(data_point) 352 self.detector.orientation_unit = unit 353 elif key == u'yaw': 354 self.detector.orientation.y = float(data_point) 355 self.detector.orientation_unit = unit 356 elif key == u'beam_center_x': 357 self.detector.beam_center.x = float(data_point) 358 self.detector.beam_center_unit = unit 359 elif key == u'beam_center_y': 360 self.detector.beam_center.y = float(data_point) 361 self.detector.beam_center_unit = unit 362 elif key == u'x_pixel_size': 363 self.detector.pixel_size.x = float(data_point) 364 self.detector.pixel_size_unit = unit 365 elif key == u'y_pixel_size': 366 self.detector.pixel_size.y = float(data_point) 367 self.detector.pixel_size_unit = unit 368 369 def process_collimation(self, data_point, key, unit): 370 """ 371 SAScollimation processor 372 :param data_point: Single point from an HDF5 data file 373 :param key: class name data_point was taken from 374 :param unit: unit attribute from data set 375 """ 376 if key == u'distance': 377 self.collimation.length = data_point 378 self.collimation.length_unit = unit 379 elif key == u'name': 380 self.collimation.name = data_point 381 382 def process_aperture(self, data_point, key): 383 """ 384 SASaperture processor 385 :param data_point: Single point from an HDF5 data file 386 :param key: class name data_point was taken from 387 """ 388 if key == u'shape': 389 self.aperture.shape = data_point 390 elif key == u'x_gap': 391 self.aperture.size.x = data_point 392 elif key == u'y_gap': 393 self.aperture.size.y = data_point 394 395 def process_source(self, data_point, key, unit): 396 """ 397 SASsource processor 398 :param data_point: Single point from an HDF5 data file 399 :param key: class name data_point was taken from 400 :param unit: unit attribute from data set 401 """ 402 if key == u'incident_wavelength': 403 self.current_datainfo.source.wavelength = data_point 404 self.current_datainfo.source.wavelength_unit = unit 405 elif key == u'wavelength_max': 406 self.current_datainfo.source.wavelength_max = data_point 407 self.current_datainfo.source.wavelength_max_unit = unit 408 elif key == u'wavelength_min': 409 self.current_datainfo.source.wavelength_min = data_point 410 self.current_datainfo.source.wavelength_min_unit = unit 411 elif key == u'incident_wavelength_spread': 412 self.current_datainfo.source.wavelength_spread = data_point 413 self.current_datainfo.source.wavelength_spread_unit = unit 414 elif key == u'beam_size_x': 415 self.current_datainfo.source.beam_size.x = data_point 416 self.current_datainfo.source.beam_size_unit = unit 417 elif key == u'beam_size_y': 418 self.current_datainfo.source.beam_size.y = data_point 419 self.current_datainfo.source.beam_size_unit = unit 420 elif key == u'beam_shape': 421 self.current_datainfo.source.beam_shape = data_point 422 elif key == u'radiation': 423 self.current_datainfo.source.radiation = data_point 424 425 def process_process(self, data_point, key): 426 """ 427 SASprocess processor 428 :param data_point: Single point from an HDF5 data file 429 :param key: class name data_point was taken from 430 """ 431 if key == u'Title': # CanSAS 2.0 432 self.process.name = data_point 433 elif key == u'name': # NXcanSAS 434 self.process.name = data_point 435 elif key == u'description': 436 self.process.description = data_point 437 elif key == u'date': 438 self.process.date = data_point 439 elif key == u'term': 440 self.process.term = data_point 441 else: 442 self.process.notes.append(data_point) 413 443 414 444 def add_intermediate(self): … … 515 545 self.current_datainfo = DataInfo() 516 546 517 518 547 def _initialize_new_data_set(self, parent_list=None): 519 548 """ … … 534 563 self.current_dataset = plottable_1D(x, y) 535 564 self.current_datainfo.filename = self.raw_data.filename 565 566 def _find_data_attributes(self, value): 567 """ 568 A class to find the indices for Q, the name of the Qdev and Idev, and 569 the name of the mask. 570 :param value: SASdata/NXdata HDF5 Group 571 """ 572 attrs = value.attrs 573 signal = attrs.get("signal") 574 575 i_axes = np.array(attrs.get("I_axes").split(",")) 576 q_indices = np.array(attrs.get("Q_indices")) 577 self.mask_name = attrs.get("mask") 578 keys = value.keys() 579 for val in q_indices: 580 self.q_name.append(i_axes[val]) 581 self.i_name = signal 582 self.i_node = value.get(self.i_name) 583 for item in self.q_name: 584 if item in keys: 585 q_vals = value.get(item) 586 self.q_uncertainties = q_vals.attrs.get("uncertainty") 587 self.q_resolutions = q_vals.attrs.get("resolution") 588 if self.i_name in keys: 589 i_vals = value.get(self.i_name) 590 self.i_uncertainties = i_vals.attrs.get("uncertainty") 536 591 537 592 def _find_intermediate(self, parent_list, basename=""):
Note: See TracChangeset
for help on using the changeset viewer.