Changeset fafe52a in sasview for src/sas/sascalc/dataloader/readers
- Timestamp:
- Jul 31, 2017 4:28:48 AM (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, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 2f85af7
- Parents:
- 9df816b
- Location:
- src/sas/sascalc/dataloader/readers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/anton_paar_saxs_reader.py
ra235f715 rfafe52a 9 9 10 10 from sas.sascalc.dataloader.readers.xml_reader import XMLreader 11 from sas.sascalc.dataloader.data_info import plottable_1D, Data1D, Sample, Source11 from sas.sascalc.dataloader.data_info import plottable_1D, Data1D, DataInfo, Sample, Source 12 12 from sas.sascalc.dataloader.data_info import Process, Aperture, Collimation, TransmissionSpectrum, Detector 13 13 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DataReaderException 14 14 15 15 class Reader(XMLreader): 16 16 """ 17 A class for reading in CanSAS v2.0 data files. The existing iteration opens Mantid generated HDF5 formatted files 18 with file extension .h5/.H5. Any number of data sets may be present within the file and any dimensionality of data 19 may be used. Currently 1D and 2D SAS data sets are supported, but future implementations will include 1D and 2D 20 SESANS data. This class assumes a single data set for each sasentry. 21 22 :Dependencies: 23 The CanSAS HDF5 reader requires h5py v2.5.0 or later. 17 A class for reading in Anton Paar .pdh files 24 18 """ 25 19 … … 30 24 ## Raw file contents to be processed 31 25 raw_data = None 32 ## Data set being modified33 current_dataset = None34 26 ## For recursion and saving purposes, remember parent objects 35 27 parent_list = None … … 42 34 ## Flag to bypass extension check 43 35 allow_all = False 44 ## List of files to return45 output = None46 36 47 37 def reset_state(self): 48 self.current_dataset = Data1D(np.empty(0), np.empty(0),49 np.empty(0), np.empty(0))38 self.current_dataset = plottable_1D(np.empty(0), np.empty(0), np.empty(0), np.empty(0)) 39 self.current_datainfo = DataInfo() 50 40 self.datasets = [] 51 41 self.raw_data = None … … 63 53 self.lower = 5 64 54 65 def read(self, filename):55 def get_file_contents(self): 66 56 """ 67 57 This is the general read method that all SasView data_loaders must have. … … 73 63 ## Reinitialize the class when loading a new data file to reset all class variables 74 64 self.reset_state() 75 ## Check that the file exists 76 if os.path.isfile(filename): 77 basename = os.path.basename(filename) 78 _, extension = os.path.splitext(basename) 79 # If the file type is not allowed, return empty list 80 if extension in self.ext or self.allow_all: 81 ## Load the data file 82 input_f = open(filename, 'r') 83 buff = input_f.read() 84 self.raw_data = buff.splitlines() 85 self.read_data() 86 return self.output 65 buff = self.f_open.read() 66 self.raw_data = buff.splitlines() 67 self.read_data() 87 68 88 69 def read_data(self): 70 correctly_loaded = True 71 error_message = "" 72 89 73 q_unit = "1/nm" 90 74 i_unit = "1/um^2" 91 self.current_dataset.title = self.raw_data[0] 92 self.current_dataset.meta_data["Keywords"] = self.raw_data[1] 93 line3 = self.raw_data[2].split() 94 line4 = self.raw_data[3].split() 95 line5 = self.raw_data[4].split() 96 self.data_points = int(line3[0]) 97 self.lower = 5 98 self.upper = self.lower + self.data_points 99 self.source.radiation = 'x-ray' 100 normal = float(line4[3]) 101 self.current_dataset.source.radiation = "x-ray" 102 self.current_dataset.source.name = "Anton Paar SAXSess Instrument" 103 self.current_dataset.source.wavelength = float(line4[4]) 104 xvals = [] 105 yvals = [] 106 dyvals = [] 107 for i in range(self.lower, self.upper): 108 index = i - self.lower 109 data = self.raw_data[i].split() 110 xvals.insert(index, normal * float(data[0])) 111 yvals.insert(index, normal * float(data[1])) 112 dyvals.insert(index, normal * float(data[2])) 75 try: 76 self.current_datainfo.title = self.raw_data[0] 77 self.current_datainfo.meta_data["Keywords"] = self.raw_data[1] 78 line3 = self.raw_data[2].split() 79 line4 = self.raw_data[3].split() 80 line5 = self.raw_data[4].split() 81 self.data_points = int(line3[0]) 82 self.lower = 5 83 self.upper = self.lower + self.data_points 84 self.source.radiation = 'x-ray' 85 normal = float(line4[3]) 86 self.current_datainfo.source.radiation = "x-ray" 87 self.current_datainfo.source.name = "Anton Paar SAXSess Instrument" 88 self.current_datainfo.source.wavelength = float(line4[4]) 89 xvals = [] 90 yvals = [] 91 dyvals = [] 92 for i in range(self.lower, self.upper): 93 index = i - self.lower 94 data = self.raw_data[i].split() 95 xvals.insert(index, normal * float(data[0])) 96 yvals.insert(index, normal * float(data[1])) 97 dyvals.insert(index, normal * float(data[2])) 98 except Exception as e: 99 error_message = "Couldn't load {}.\n".format(self.f_open.name) 100 error_message += e.message 101 raise FileContentsException(error_message) 113 102 self.current_dataset.x = np.append(self.current_dataset.x, xvals) 114 103 self.current_dataset.y = np.append(self.current_dataset.y, yvals) 115 104 self.current_dataset.dy = np.append(self.current_dataset.dy, dyvals) 116 105 if self.data_points != self.current_dataset.x.size: 117 self.errors.add("Not all data was loaded properly.") 118 if self.current_dataset.dx.size != self.current_dataset.x.size: 119 dxvals = np.zeros(self.current_dataset.x.size) 120 self.current_dataset.dx = dxvals 106 error_message += "Not all data points could be loaded.\n" 107 correctly_loaded = False 121 108 if self.current_dataset.x.size != self.current_dataset.y.size: 122 self.errors.add("The x and y data sets are not the same size.") 109 error_message += "The x and y data sets are not the same size.\n" 110 correctly_loaded = False 123 111 if self.current_dataset.y.size != self.current_dataset.dy.size: 124 self.errors.add("The y and dy datasets are not the same size.") 125 self.current_dataset.errors = self.errors 112 error_message += "The y and dy datasets are not the same size.\n" 113 correctly_loaded = False 114 126 115 self.current_dataset.xaxis("Q", q_unit) 127 116 self.current_dataset.yaxis("Intensity", i_unit) 128 117 xml_intermediate = self.raw_data[self.upper:] 129 118 xml = ''.join(xml_intermediate) 130 self.set_xml_string(xml) 131 dom = self.xmlroot.xpath('/fileinfo') 132 self._parse_child(dom) 133 self.output.append(self.current_dataset) 119 try: 120 self.set_xml_string(xml) 121 dom = self.xmlroot.xpath('/fileinfo') 122 self._parse_child(dom) 123 except Exception as e: 124 # Data loaded but XML metadata has an error 125 error_message += "Data points have been loaded but there was an " 126 error_message += "error reading XML metadata: " + e.message 127 correctly_loaded = False 128 self.send_to_output() 129 if not correctly_loaded: 130 raise DataReaderException(error_message) 134 131 135 132 def _parse_child(self, dom, parent=''): … … 146 143 self._parse_child(node, key) 147 144 if key == "SampleDetector": 148 self.current_data set.detector.append(self.detector)145 self.current_datainfo.detector.append(self.detector) 149 146 self.detector = Detector() 150 147 else: 151 148 if key == "value": 152 149 if parent == "Wavelength": 153 self.current_data set.source.wavelength = value150 self.current_datainfo.source.wavelength = value 154 151 elif parent == "SampleDetector": 155 152 self.detector.distance = value 156 153 elif parent == "Temperature": 157 self.current_data set.sample.temperature = value154 self.current_datainfo.sample.temperature = value 158 155 elif parent == "CounterSlitLength": 159 156 self.detector.slit_length = value … … 161 158 value = value.replace("_", "") 162 159 if parent == "Wavelength": 163 self.current_data set.source.wavelength_unit = value160 self.current_datainfo.source.wavelength_unit = value 164 161 elif parent == "SampleDetector": 165 162 self.detector.distance_unit = value … … 169 166 self.current_dataset.yaxis(self.current_dataset._yaxis, value) 170 167 elif parent == "Temperature": 171 self.current_data set.sample.temperature_unit = value168 self.current_datainfo.sample.temperature_unit = value 172 169 elif parent == "CounterSlitLength": 173 170 self.detector.slit_length_unit = value -
src/sas/sascalc/dataloader/readers/xml_reader.py
rbc570f4 rfafe52a 93 93 except etree.XMLSyntaxError as xml_error: 94 94 logger.info(xml_error) 95 except Exception: 95 raise xml_error 96 except Exception as exc: 96 97 self.xml = None 97 98 self.xmldoc = None 98 99 self.xmlroot = None 100 raise exc 99 101 100 102 def set_schema(self, schema):
Note: See TracChangeset
for help on using the changeset viewer.