Changeset ac3353d in sasview for src/sas/sascalc/file_converter


Ignore:
Timestamp:
Aug 23, 2016 7:11:28 AM (8 years ago)
Author:
lewis
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
9ddffe0
Parents:
0fcb097
Message:

Write more metadata to NXcanSAS files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/file_converter/nxcansas_writer.py

    ra14fa99 rac3353d  
    4444            return np.array([np.string_(string)]) 
    4545 
     46        def _write_h5_string(entry, value, key): 
     47            entry[key] = _h5_string(value) 
     48 
     49 
    4650        def _h5_float(x): 
    4751            if not (isinstance(x, list)): 
    4852                x = [x] 
    4953            return np.array(x, dtype=np.float32) 
     54 
     55        def _write_h5_float(entry, value, key): 
     56            entry.create_dataset(key, data=_h5_float(value)) 
     57 
     58        def _write_h5_vector(entry, vector, names=['x_position', 'y_position'], 
     59            units=None, write_fn=_write_h5_string): 
     60            if len(names) < 2: 
     61                raise ValueError("Length of names must be >= 2.") 
     62 
     63            if vector.x is not None: 
     64                write_fn(entry, vector.x, names[0]) 
     65                if units is not None: 
     66                    entry[names[0]].attrs['units'] = units 
     67            if vector.y is not None: 
     68                write_fn(entry, vector.y, names[1]) 
     69                if units is not None: 
     70                    entry[names[1]].attrs['units'] = units 
     71            if len(names) == 3 and vector.z is not None: 
     72                write_fn(entry, vector.z, names[2]) 
     73                if units is not None: 
     74                    entry[names[2]].attrs['units'] = units 
    5075 
    5176        valid_data = all([issubclass(d.__class__, (Data1D, Data2D)) for d in dataset]) 
     
    83108 
    84109        data_info = dataset[0] 
     110        # Sample metadata 
    85111        sample_entry = sasentry.create_group('sassample') 
    86112        sample_entry.attrs['canSAS_class'] = 'SASsample' 
    87         sample_entry['name'] = _h5_string(data_info.sample.name) 
    88         sample_attrs = ['thickness', 'temperature'] 
     113        sample_entry['ID'] = _h5_string(data_info.sample.name) 
     114        sample_attrs = ['thickness', 'temperature', 'transmission'] 
    89115        for key in sample_attrs: 
    90116            if getattr(data_info.sample, key) is not None: 
    91117                sample_entry.create_dataset(key, 
    92118                    data=_h5_float(getattr(data_info.sample, key))) 
    93  
     119        _write_h5_vector(sample_entry, data_info.sample.position) 
     120        _write_h5_vector(sample_entry, data_info.sample.orientation, 
     121            names=['polar_angle', 'azimuthal_angle']) 
     122 
     123        # Instrumment metadata 
    94124        instrument_entry = sasentry.create_group('sasinstrument') 
    95125        instrument_entry.attrs['canSAS_class'] = 'SASinstrument' 
    96126        instrument_entry['name'] = _h5_string(data_info.instrument) 
    97127 
     128        # Source metadata 
    98129        source_entry = instrument_entry.create_group('sassource') 
    99130        source_entry.attrs['canSAS_class'] = 'SASsource' 
     
    103134            source_entry['radiation'] = _h5_string(data_info.source.radiation) 
    104135 
     136        # Collimation metadata 
    105137        if len(data_info.collimation) > 0: 
    106138            i = 1 
     
    110142                collimation_entry.attrs['canSAS_class'] = 'SAScollimation' 
    111143                if coll_info.length is not None: 
    112                     collimation_entry['SDD'] = _h5_float(coll_info.length) 
     144                    _write_h5_float(collimation_entry, coll_info.length, 'SDD') 
    113145                    collimation_entry['SDD'].attrs['units'] = coll_info.length_unit 
    114146                if coll_info.name is not None: 
    115147                    collimation_entry['name'] = _h5_string(coll_info.name) 
    116148        else: 
     149            # Create a blank one - at least 1 set of collimation metadata 
     150            # required by format 
    117151            collimation_entry = instrument_entry.create_group('sascollimation01') 
    118152 
     153        # Detector metadata 
    119154        if len(data_info.detector) > 0: 
    120155            i = 1 
     
    124159                detector_entry.attrs['canSAS_class'] = 'SASdetector' 
    125160                if det_info.distance is not None: 
    126                     detector_entry['SDD'] = _h5_float(det_info.distance) 
     161                    _write_h5_float(detector_entry, det_info.distance, 'SDD') 
    127162                    detector_entry['SDD'].attrs['units'] = det_info.distance_unit 
    128163                if det_info.name is not None: 
     
    130165                else: 
    131166                    detector_entry['name'] = _h5_string('') 
     167                if det_info.slit_length is not None: 
     168                    _write_h5_float(detector_entry, det_info.slit_length, 'slit_length') 
     169                    detector_entry['slit_length'].attrs['units'] = det_info.slit_length_unit 
     170                _write_h5_vector(detector_entry, det_info.offset) 
     171                _write_h5_vector(detector_entry, det_info.orientation, 
     172                    names=['polar_angle', 'azimuthal_angle']) 
     173                _write_h5_vector(detector_entry, det_info.beam_center, 
     174                    names=['beam_center_x', 'beam_center_y'], 
     175                    write_fn=_write_h5_float, units=det_info.beam_center_unit) 
     176                _write_h5_vector(detector_entry, det_info.pixel_size, 
     177                    names=['x_pixel_size', 'y_pixel_size'], 
     178                    write_fn=_write_h5_float, units=det_info.pixel_size_unit) 
     179 
    132180                i += 1 
    133181        else: 
     182            # Create a blank one - at least 1 detector required by format 
    134183            detector_entry = instrument_entry.create_group('sasdetector01') 
    135184            detector_entry.attrs['canSAS_class'] = 'SASdetector' 
Note: See TracChangeset for help on using the changeset viewer.