Changeset 574adc7 in sasview for src/sas/sascalc/dataloader/readers/cansas_reader.py
- Timestamp:
- Sep 22, 2017 2:01:32 PM (7 years ago)
- 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:
- 34d7b35
- Parents:
- 9706d88
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/cansas_reader.py
rae69c690 r574adc7 1 1 import logging 2 import numpy as np3 2 import os 4 3 import sys 5 4 import datetime 6 5 import inspect 7 # For saving individual sections of data 8 from sas.sascalc.dataloader.data_info import Data1D, Data2D, DataInfo, \ 9 plottable_1D, plottable_2D 10 from sas.sascalc.dataloader.data_info import Collimation, TransmissionSpectrum, \ 11 Detector, Process, Aperture 12 from sas.sascalc.dataloader.data_info import \ 13 combine_data_info_with_plottable as combine_data 14 import sas.sascalc.dataloader.readers.xml_reader as xml_reader 15 from sas.sascalc.dataloader.readers.xml_reader import XMLreader 16 from sas.sascalc.dataloader.readers.cansas_constants import CansasConstants, CurrentLevel 17 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, \ 18 DefaultReaderException, DataReaderException 6 7 import numpy as np 19 8 20 9 # The following 2 imports *ARE* used. Do not remove either. … … 23 12 24 13 from lxml import etree 14 15 from sas.sascalc.data_util.nxsunit import Converter 16 17 # For saving individual sections of data 18 from ..data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D, \ 19 Collimation, TransmissionSpectrum, Detector, Process, Aperture, \ 20 combine_data_info_with_plottable as combine_data 21 from ..loader_exceptions import FileContentsException, DefaultReaderException, \ 22 DataReaderException 23 from . import xml_reader 24 from .xml_reader import XMLreader 25 from .cansas_constants import CansasConstants, CurrentLevel 25 26 26 27 logger = logging.getLogger(__name__) … … 34 35 "as much of the data as possible.\n\n" 35 36 HAS_CONVERTER = True 36 try:37 from sas.sascalc.data_util.nxsunit import Converter38 except ImportError:39 HAS_CONVERTER = False40 37 41 38 CONSTANTS = CansasConstants() … … 163 160 raise fc_exc 164 161 except Exception as e: # Convert all other exceptions to FileContentsExceptions 165 raise FileContentsException(e.message) 162 raise 163 raise FileContentsException(str(e)) 166 164 167 165 … … 632 630 else: 633 631 save_in = "current_datainfo" 634 exec "default_unit = self.{0}.{1}".format(save_in, unitname) 635 if local_unit and default_unit and local_unit.lower() != default_unit.lower() \ 636 and local_unit.lower() != "none": 637 if HAS_CONVERTER == True: 632 exec("default_unit = self.{0}.{1}".format(save_in, unitname)) 633 if (local_unit and default_unit 634 and local_unit.lower() != default_unit.lower() 635 and local_unit.lower() != "none"): 636 if HAS_CONVERTER: 638 637 # Check local units - bad units raise KeyError 639 638 data_conv_q = Converter(local_unit) … … 654 653 err_msg += "expecting [{0}]".format(default_unit) 655 654 value_unit = local_unit 656 except :655 except Exception: 657 656 err_msg = "CanSAS reader: unknown error converting " 658 657 err_msg += "\"{0}\" unit [{1}]" … … 908 907 point = self.create_element("Idata") 909 908 node.append(point) 910 qx = ','.join( [str(datainfo.qx_data[i]) for i in xrange(len(datainfo.qx_data))])911 qy = ','.join( [str(datainfo.qy_data[i]) for i in xrange(len(datainfo.qy_data))])912 intensity = ','.join( [str(datainfo.data[i]) for i in xrange(len(datainfo.data))])909 qx = ','.join(str(v) for v in datainfo.qx_data) 910 qy = ','.join(str(v) for v in datainfo.qy_data) 911 intensity = ','.join(str(v) for v in datainfo.data) 913 912 914 913 self.write_node(point, "Qx", qx, … … 919 918 {'unit': datainfo._zunit}) 920 919 if datainfo.err_data is not None: 921 err = ','.join([str(datainfo.err_data[i]) for i in 922 xrange(len(datainfo.err_data))]) 920 err = ','.join(str(v) for v in datainfo.err_data) 923 921 self.write_node(point, "Idev", err, 924 922 {'unit': datainfo._zunit}) 925 923 if datainfo.dqy_data is not None: 926 dqy = ','.join([str(datainfo.dqy_data[i]) for i in 927 xrange(len(datainfo.dqy_data))]) 924 dqy = ','.join(str(v) for v in datainfo.dqy_data) 928 925 self.write_node(point, "Qydev", dqy, 929 926 {'unit': datainfo._yunit}) 930 927 if datainfo.dqx_data is not None: 931 dqx = ','.join([str(datainfo.dqx_data[i]) for i in 932 xrange(len(datainfo.dqx_data))]) 928 dqx = ','.join(str(v) for v in datainfo.dqx_data) 933 929 self.write_node(point, "Qxdev", dqx, 934 930 {'unit': datainfo._xunit}) 935 931 if datainfo.mask is not None: 936 mask = ','.join( 937 ["1" if datainfo.mask[i] else "0" 938 for i in xrange(len(datainfo.mask))]) 932 mask = ','.join("1" if v else "0" for v in datainfo.mask) 939 933 self.write_node(point, "Mask", mask) 940 934 … … 1280 1274 try: 1281 1275 value = float(entry.text) 1282 except :1276 except ValueError: 1283 1277 value = None 1284 1278 … … 1289 1283 if units is not None: 1290 1284 toks = variable.split('.') 1291 local_unit = None 1292 exec "local_unit = storage.%s_unit" % toks[0] 1285 exec("local_unit = storage.%s_unit" % toks[0]) 1293 1286 if local_unit is not None and units.lower() != local_unit.lower(): 1294 1287 if HAS_CONVERTER == True: 1295 1288 try: 1296 1289 conv = Converter(units) 1297 exec "storage.%s = %g" % \1298 (variable, conv(value, units=local_unit))1299 except :1290 exec("storage.%s = %g" % 1291 - (variable, conv(value, units=local_unit))) 1292 except Exception: 1300 1293 _, exc_value, _ = sys.exc_info() 1301 1294 err_mess = "CanSAS reader: could not convert" … … 1306 1299 logger.info(err_mess) 1307 1300 else: 1308 raise ValueError , err_mess1301 raise ValueError(err_mess) 1309 1302 else: 1310 1303 err_mess = "CanSAS reader: unrecognized %s unit [%s];"\ … … 1315 1308 logger.info(err_mess) 1316 1309 else: 1317 raise ValueError , err_mess1310 raise ValueError(err_mess) 1318 1311 else: 1319 exec "storage.%s = value" % variable1312 exec("storage.%s = value" % variable) 1320 1313 else: 1321 exec "storage.%s = value" % variable1314 exec("storage.%s = value" % variable) 1322 1315 1323 1316 # DO NOT REMOVE - used in saving and loading panel states. … … 1339 1332 entry = get_content(location, node) 1340 1333 if entry is not None and entry.text is not None: 1341 exec "storage.%s = entry.text.strip()" % variable1334 exec("storage.%s = entry.text.strip()" % variable) 1342 1335 1343 1336 # DO NOT REMOVE Called by outside packages:
Note: See TracChangeset
for help on using the changeset viewer.