Changeset 5a8cdbb in sasview for src/sas/sascalc/dataloader/readers/sesans_reader.py
- Timestamp:
- Aug 1, 2017 12:02:35 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, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 511ccb2d
- Parents:
- 248ff73 (diff), bc04647 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/sesans_reader.py
rb2c28a5 r5a8cdbb 24 24 Class to load sesans files (6 columns). 25 25 """ 26 # #File type26 # File type 27 27 type_name = "SESANS" 28 28 … … 30 30 type = ["SESANS files (*.ses)|*.ses", 31 31 "SESANS files (*..sesans)|*.sesans"] 32 # #List of allowed extensions32 # List of allowed extensions 33 33 ext = ['.ses', '.SES', '.sesans', '.SESANS'] 34 34 35 # #Flag to bypass extension check36 allow_all = False35 # Flag to bypass extension check 36 allow_all = True 37 37 38 38 def get_file_contents(self): … … 42 42 self.output = [] 43 43 44 error_message = "" 45 loaded_correctly = True 44 line = self.f_open.readline() 45 params = {} 46 while not line.startswith("BEGIN_DATA"): 47 terms = line.split() 48 if len(terms) >= 2: 49 params[terms[0]] = " ".join(terms[1:]) 50 line = self.f_open.readline() 51 self.params = params 46 52 47 import pdb; pdb.set_trace() 53 if "FileFormatVersion" not in self.params: 54 raise FileContentsException("SES file missing FileFormatVersion") 55 if float(self.params["FileFormatVersion"]) >= 2.0: 56 raise FileContentsException("SASView only supports SES version 1") 48 57 49 buff = self.f_open.read() 50 lines = buff.splitlines() 58 if "SpinEchoLength_unit" not in self.params: 59 raise FileContentsException("SpinEchoLength has no units") 60 if "Wavelength_unit" not in self.params: 61 raise FileContentsException("Wavelength has no units") 62 if params["SpinEchoLength_unit"] != params["Wavelength_unit"]: 63 raise FileContentsException("The spin echo data has rudely used " 64 "different units for the spin echo length " 65 "and the wavelength. While sasview could " 66 "handle this instance, it is a violation " 67 "of the file format and will not be " 68 "handled by other software.") 51 69 52 self.current_datainfo.filename = os.path.basename(self.f_open.name)70 headers = self.f_open.readline().split() 53 71 54 paramnames=[] 55 paramvals=[] 56 zvals=[] 57 dzvals=[] 58 lamvals=[] 59 dlamvals=[] 60 Pvals=[] 61 dPvals=[] 72 self._insist_header(headers, "SpinEchoLength") 73 self._insist_header(headers, "Depolarisation") 74 self._insist_header(headers, "Depolarisation_error") 75 self._insist_header(headers, "Wavelength") 62 76 63 for line in lines: 64 # Initial try for CSV (split on ,) 65 line=line.strip() 66 toks = line.split('\t') 67 if len(toks)==2: 68 paramnames.append(toks[0]) 69 paramvals.append(toks[1]) 70 elif len(toks)>5: 71 zvals.append(toks[0]) 72 dzvals.append(toks[3]) 73 lamvals.append(toks[4]) 74 dlamvals.append(toks[5]) 75 Pvals.append(toks[1]) 76 dPvals.append(toks[2]) 77 data = np.loadtxt(self.f_open) 78 79 if data.shape[1] != len(headers): 80 raise FileContentsException( 81 "File has {} headers, but {} columns".format( 82 len(headers), 83 data.shape[1])) 84 85 if not data.size: 86 raise FileContentsException("{} is empty".format(path)) 87 x = data[:, headers.index("SpinEchoLength")] 88 if "SpinEchoLength_error" in headers: 89 dx = data[:, headers.index("SpinEchoLength_error")] 77 90 else: 78 continue 91 dx = x * 0.05 92 lam = data[:, headers.index("Wavelength")] 93 if "Wavelength_error" in headers: 94 dlam = data[:, headers.index("Wavelength_error")] 95 else: 96 dlam = lam * 0.05 97 y = data[:, headers.index("Depolarisation")] 98 dy = data[:, headers.index("Depolarisation_error")] 79 99 80 x=[] 81 y=[] 82 lam=[] 83 dx=[] 84 dy=[] 85 dlam=[] 86 lam_header = lamvals[0].split() 87 data_conv_z = None 88 default_z_unit = "A" 89 data_conv_P = None 90 default_p_unit = " " # Adjust unit for axis (L^-3) 91 lam_unit = lam_header[1].replace("[","").replace("]","") 92 if lam_unit == 'AA': 93 lam_unit = 'A' 94 varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] 95 valrange=range(1, len(zvals)) 96 try: 97 for i in valrange: 98 x.append(float(zvals[i])) 99 y.append(float(Pvals[i])) 100 lam.append(float(lamvals[i])) 101 dy.append(float(dPvals[i])) 102 dx.append(float(dzvals[i])) 103 dlam.append(float(dlamvals[i])) 104 except ValueError as val_err: 105 err_msg = "Invalid float" 106 err_msg += ":".join(val_err.message.split(":")[1:]) 107 raise FileContentsException(err_msg) 100 lam_unit = self._unit_fetch("Wavelength") 101 x, x_unit = self._unit_conversion(x, "A", 102 self._unit_fetch( 103 "SpinEchoLength")) 104 dx, dx_unit = self._unit_conversion( 105 dx, lam_unit, 106 self._unit_fetch("SpinEchoLength")) 107 dlam, dlam_unit = self._unit_conversion( 108 dlam, lam_unit, 109 self._unit_fetch("Wavelength")) 110 y_unit = self._unit_fetch("Depolarisation") 108 111 109 x, y, lam, dy, dx, dlam = [ 110 np.asarray(v, 'double') 111 for v in (x, y, lam, dy, dx, dlam) 112 ] 112 self.current_dataset.x = x 113 self.current_dataset.y = y 114 self.current_dataset.lam = lam 115 self.current_dataset.dy = dy 116 self.current_dataset.dx = dx 117 self.current_dataset.dlam = dlam 118 self.current_datainfo.isSesans = True 113 119 114 self.f_open.close() 120 self.current_datainfo._yunit = y_unit 121 self.current_datainfo._xunit = x_unit 122 self.current_datainfo.source.wavelength_unit = lam_unit 123 self.current_datainfo.source.wavelength = lam 124 self.current_datainfo.filename = os.basename(self.f_open.name) 125 self.current_dataset.xaxis(r"\rm{z}", x_unit) 126 # Adjust label to ln P/(lam^2 t), remove lam column refs 127 self.current_dataset.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit) 128 # Store loading process information 129 self.current_datainfo.meta_data['loader'] = self.type_name 130 self.current_datainfo.sample.name = params["Sample"] 131 self.current_datainfo.sample.ID = params["DataFileTitle"] 132 self.current_datainfo.sample.thickness = self._unit_conversion( 133 float(params["Thickness"]), "cm", 134 self._unit_fetch("Thickness"))[0] 115 135 116 self.current_dataset.x, self.current_dataset._xunit = self._unit_conversion(x, lam_unit, default_z_unit) 117 self.current_dataset.y = y 118 self.current_dataset._yunit = r'\AA^{-2} cm^{-1}' # output y_unit added 119 self.current_dataset.dx, _ = self._unit_conversion(dx, lam_unit, default_z_unit) 120 self.current_dataset.dy = dy 121 self.current_dataset.lam, _ = self._unit_conversion(lam, lam_unit, default_z_unit) 122 self.current_dataset.dlam, _ = self._unit_conversion(dlam, lam_unit, default_z_unit) 136 self.current_datainfo.sample.zacceptance = ( 137 float(params["Theta_zmax"]), 138 self._unit_fetch("Theta_zmax")) 123 139 124 self.current_dataset.xaxis(r"\rm{z}", self.current_dataset._xunit) 125 self.current_dataset.yaxis(r"\rm{ln(P)/(t \lambda^2)}", self.current_dataset._yunit) # Adjust label to ln P/(lam^2 t), remove lam column refs 140 self.current_datainfo.sample.yacceptance = ( 141 float(params["Theta_ymax"]), 142 self._unit_fetch("Theta_ymax")) 126 143 127 # Store loading process information 128 self.current_datainfo.meta_data['loader'] = self.type_name 129 try: 130 self.current_datainfo.sample.thickness = float(paramvals[6]) 131 except ValueError as val_err: 132 loaded_correctly = False 133 error_message += "\nInvalid sample thickness '{}'".format(paramvals[6]) 144 self.send_to_output() 134 145 135 self.current_datainfo.sample.name = paramvals[1] 136 self.current_datainfo.sample.ID = paramvals[0] 137 zaccept_unit_split = paramnames[7].split("[") 138 zaccept_unit = zaccept_unit_split[1].replace("]","") 139 if zaccept_unit.strip() == r'\AA^-1' or zaccept_unit.strip() == r'\A^-1': 140 zaccept_unit = "1/A" 141 self.current_datainfo.sample.zacceptance=(float(paramvals[7]),zaccept_unit) 146 @staticmethod 147 def _insist_header(headers, name): 148 if name not in headers: 149 raise FileContentsException( 150 "Missing {} column in spin echo data".format(name)) 142 151 143 self.current_datainfo.vars = varheader 152 @staticmethod 153 def _unit_conversion(value, value_unit, default_unit): 154 """ 155 Performs unit conversion on a measurement. 144 156 145 if len(self.current_dataset.x) < 1: 146 raise FileContentsException("No data points in file.") 147 148 self.send_to_output() 149 150 if not loaded_correctly: 151 raise DataReaderException(error_message) 152 153 def _unit_conversion(self, value, value_unit, default_unit): 154 if has_converter == True and value_unit != default_unit: 155 data_conv_q = Converter(value_unit) 156 value = data_conv_q(value, units=default_unit) 157 :param value: The magnitude of the measurement 158 :param value_unit: a string containing the final desired unit 159 :param default_unit: string with the units of the original measurement 160 :return: The magnitude of the measurement in the new units 161 """ 162 # (float, string, string) -> float 163 if has_converter and value_unit != default_unit: 164 data_conv_q = Converter(default_unit) 165 value = data_conv_q(value, units=value_unit) 157 166 new_unit = default_unit 158 167 else: 159 168 new_unit = value_unit 160 169 return value, new_unit 170 171 def _unit_fetch(self, unit): 172 return self.params[unit+"_unit"]
Note: See TracChangeset
for help on using the changeset viewer.