Changeset 5c5e7fd in sasview for src/sas


Ignore:
Timestamp:
Sep 22, 2017 7:06:16 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
af3e9f5
Parents:
a5bd87a
Message:

convert bytes to string when reading from HDF

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py

    rcd57c7d4 r5c5e7fd  
    1616from sas.sascalc.dataloader.file_reader_base_class import FileReader 
    1717 
     18def decode(s): 
     19    return s.decode() if isinstance(s, bytes) else s 
     20 
     21def h5attr(node, key, default=None): 
     22    return decode(node.attrs.get(key, default)) 
    1823 
    1924class Reader(FileReader): 
     
    130135            # Get all information for the current key 
    131136            value = data.get(key) 
    132             if value.attrs.get(u'canSAS_class') is not None: 
    133                 class_name = value.attrs.get(u'canSAS_class') 
    134             else: 
    135                 class_name = value.attrs.get(u'NX_class') 
     137            classname = h5attr(value, u'canSAS_class') 
     138            if classname is None: 
     139                class_name = h5attr(value, u'NX_class') 
    136140            if class_name is not None: 
    137141                class_prog = re.compile(class_name) 
     
    225229 
    226230                for data_point in data_set: 
     231                    if data_point.dtype.char == 'S': 
     232                        data_point = decode(bytes(data_point)) 
    227233                    # Top Level Meta Data 
    228234                    if key == u'definition': 
     
    231237                        self.current_datainfo.run.append(data_point) 
    232238                        try: 
    233                             run_name = value.attrs['name'] 
     239                            run_name = h5attr(value, 'name') 
    234240                            run_dict = {data_point: run_name} 
    235241                            self.current_datainfo.run_name = run_dict 
     
    576582        :return: unit for the value passed to the method 
    577583        """ 
    578         unit = value.attrs.get(u'units') 
     584        unit = h5attr(value, u'units') 
    579585        if unit is None: 
    580             unit = value.attrs.get(u'unit') 
     586            unit = h5attr(value, u'unit') 
    581587        # Convert the unit formats 
    582588        if unit == "1/A": 
Note: See TracChangeset for help on using the changeset viewer.