[959eb01] | 1 | """ |
---|
[ad92c5a] | 2 | IGOR 1D data reader |
---|
[959eb01] | 3 | """ |
---|
| 4 | ##################################################################### |
---|
[ad92c5a] | 5 | # This software was developed by the University of Tennessee as part of the |
---|
| 6 | # Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 7 | # project funded by the US National Science Foundation. |
---|
| 8 | # See the license text in license.txt |
---|
| 9 | # copyright 2008, University of Tennessee |
---|
[959eb01] | 10 | ###################################################################### |
---|
| 11 | |
---|
[ad92c5a] | 12 | import logging |
---|
[46cf4c9] | 13 | |
---|
[959eb01] | 14 | import numpy as np |
---|
[46cf4c9] | 15 | |
---|
| 16 | from sas.sascalc.data_util.nxsunit import Converter |
---|
| 17 | from ..file_reader_base_class import FileReader |
---|
| 18 | from ..data_info import DataInfo, plottable_1D, Data1D, Detector |
---|
| 19 | from ..loader_exceptions import FileContentsException, DefaultReaderException |
---|
[ad92c5a] | 20 | |
---|
| 21 | logger = logging.getLogger(__name__) |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | class Reader(FileReader): |
---|
[959eb01] | 25 | """ |
---|
| 26 | Class to load IGOR reduced .ABS files |
---|
| 27 | """ |
---|
[ad92c5a] | 28 | # File type |
---|
[959eb01] | 29 | type_name = "IGOR 1D" |
---|
[ad92c5a] | 30 | # Wildcards |
---|
[959eb01] | 31 | type = ["IGOR 1D files (*.abs)|*.abs"] |
---|
[ad92c5a] | 32 | # List of allowed extensions |
---|
| 33 | ext = ['.abs'] |
---|
[574adc7] | 34 | |
---|
[ad92c5a] | 35 | def get_file_contents(self): |
---|
[574adc7] | 36 | """ |
---|
[ad92c5a] | 37 | Get the contents of the file |
---|
[574adc7] | 38 | |
---|
[959eb01] | 39 | :raise RuntimeError: when the file can't be opened |
---|
| 40 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
| 41 | """ |
---|
[46cf4c9] | 42 | buff = self.readall() |
---|
[ad92c5a] | 43 | filepath = self.f_open.name |
---|
| 44 | lines = buff.splitlines() |
---|
| 45 | self.output = [] |
---|
| 46 | self.current_datainfo = DataInfo() |
---|
| 47 | self.current_datainfo.filename = filepath |
---|
| 48 | self.reset_data_list(len(lines)) |
---|
| 49 | detector = Detector() |
---|
| 50 | data_line = 0 |
---|
| 51 | self.reset_data_list(len(lines)) |
---|
| 52 | self.current_datainfo.detector.append(detector) |
---|
| 53 | self.current_datainfo.filename = filepath |
---|
| 54 | |
---|
| 55 | is_info = False |
---|
| 56 | is_center = False |
---|
| 57 | is_data_started = False |
---|
| 58 | |
---|
| 59 | base_q_unit = '1/A' |
---|
| 60 | base_i_unit = '1/cm' |
---|
| 61 | data_conv_q = Converter(base_q_unit) |
---|
| 62 | data_conv_i = Converter(base_i_unit) |
---|
| 63 | |
---|
| 64 | for line in lines: |
---|
| 65 | # Information line 1 |
---|
| 66 | if is_info: |
---|
| 67 | is_info = False |
---|
| 68 | line_toks = line.split() |
---|
| 69 | |
---|
| 70 | # Wavelength in Angstrom |
---|
[959eb01] | 71 | try: |
---|
[ad92c5a] | 72 | value = float(line_toks[1]) |
---|
[46cf4c9] | 73 | if self.current_datainfo.source.wavelength_unit != 'A': |
---|
[ad92c5a] | 74 | conv = Converter('A') |
---|
| 75 | self.current_datainfo.source.wavelength = conv(value, |
---|
| 76 | units=self.current_datainfo.source.wavelength_unit) |
---|
| 77 | else: |
---|
| 78 | self.current_datainfo.source.wavelength = value |
---|
| 79 | except KeyError: |
---|
| 80 | msg = "ABSReader cannot read wavelength from %s" % filepath |
---|
| 81 | self.current_datainfo.errors.append(msg) |
---|
| 82 | |
---|
| 83 | # Detector distance in meters |
---|
| 84 | try: |
---|
| 85 | value = float(line_toks[3]) |
---|
[46cf4c9] | 86 | if detector.distance_unit != 'm': |
---|
[ad92c5a] | 87 | conv = Converter('m') |
---|
| 88 | detector.distance = conv(value, |
---|
| 89 | units=detector.distance_unit) |
---|
| 90 | else: |
---|
| 91 | detector.distance = value |
---|
[46cf4c9] | 92 | except Exception: |
---|
[ad92c5a] | 93 | msg = "ABSReader cannot read SDD from %s" % filepath |
---|
| 94 | self.current_datainfo.errors.append(msg) |
---|
| 95 | |
---|
| 96 | # Transmission |
---|
| 97 | try: |
---|
| 98 | self.current_datainfo.sample.transmission = \ |
---|
| 99 | float(line_toks[4]) |
---|
| 100 | except ValueError: |
---|
| 101 | # Transmission isn't always in the header |
---|
| 102 | pass |
---|
| 103 | |
---|
| 104 | # Sample thickness in mm |
---|
| 105 | try: |
---|
[9e6aeaf] | 106 | # ABS writer adds 'C' with no space to the end of the |
---|
| 107 | # thickness column. Remove it if it is there before |
---|
| 108 | # converting the thickness. |
---|
[1efbc190] | 109 | if line_toks[5][-1] not in '012345679.': |
---|
[9e6aeaf] | 110 | value = float(line_toks[5][:-1]) |
---|
| 111 | else: |
---|
| 112 | value = float(line_toks[5]) |
---|
[46cf4c9] | 113 | if self.current_datainfo.sample.thickness_unit != 'cm': |
---|
[ad92c5a] | 114 | conv = Converter('cm') |
---|
| 115 | self.current_datainfo.sample.thickness = conv(value, |
---|
| 116 | units=self.current_datainfo.sample.thickness_unit) |
---|
| 117 | else: |
---|
| 118 | self.current_datainfo.sample.thickness = value |
---|
| 119 | except ValueError: |
---|
| 120 | # Thickness is not a mandatory entry |
---|
| 121 | pass |
---|
| 122 | |
---|
| 123 | # MON CNT LAMBDA DET ANG DET DIST TRANS THICK AVE STEP |
---|
| 124 | if line.count("LAMBDA") > 0: |
---|
| 125 | is_info = True |
---|
| 126 | |
---|
| 127 | # Find center info line |
---|
| 128 | if is_center: |
---|
[959eb01] | 129 | is_center = False |
---|
[ad92c5a] | 130 | line_toks = line.split() |
---|
| 131 | # Center in bin number |
---|
| 132 | center_x = float(line_toks[0]) |
---|
| 133 | center_y = float(line_toks[1]) |
---|
| 134 | |
---|
| 135 | # Bin size |
---|
[46cf4c9] | 136 | if detector.pixel_size_unit != 'mm': |
---|
[ad92c5a] | 137 | conv = Converter('mm') |
---|
| 138 | detector.pixel_size.x = conv(5.08, |
---|
| 139 | units=detector.pixel_size_unit) |
---|
| 140 | detector.pixel_size.y = conv(5.08, |
---|
| 141 | units=detector.pixel_size_unit) |
---|
[959eb01] | 142 | else: |
---|
[ad92c5a] | 143 | detector.pixel_size.x = 5.08 |
---|
| 144 | detector.pixel_size.y = 5.08 |
---|
| 145 | |
---|
| 146 | # Store beam center in distance units |
---|
| 147 | # Det 640 x 640 mm |
---|
[46cf4c9] | 148 | if detector.beam_center_unit != 'mm': |
---|
[ad92c5a] | 149 | conv = Converter('mm') |
---|
| 150 | detector.beam_center.x = conv(center_x * 5.08, |
---|
| 151 | units=detector.beam_center_unit) |
---|
| 152 | detector.beam_center.y = conv(center_y * 5.08, |
---|
[46cf4c9] | 153 | units=detector.beam_center_unit) |
---|
[959eb01] | 154 | else: |
---|
[ad92c5a] | 155 | detector.beam_center.x = center_x * 5.08 |
---|
| 156 | detector.beam_center.y = center_y * 5.08 |
---|
| 157 | |
---|
| 158 | # Detector type |
---|
| 159 | try: |
---|
| 160 | detector.name = line_toks[7] |
---|
| 161 | except: |
---|
| 162 | # Detector name is not a mandatory entry |
---|
| 163 | pass |
---|
| 164 | |
---|
| 165 | # BCENT(X,Y) A1(mm) A2(mm) A1A2DIST(m) DL/L BSTOP(mm) DET_TYP |
---|
| 166 | if line.count("BCENT") > 0: |
---|
| 167 | is_center = True |
---|
| 168 | |
---|
| 169 | # Parse the data |
---|
| 170 | if is_data_started: |
---|
| 171 | toks = line.split() |
---|
| 172 | |
---|
| 173 | try: |
---|
| 174 | _x = float(toks[0]) |
---|
| 175 | _y = float(toks[1]) |
---|
| 176 | _dy = float(toks[2]) |
---|
| 177 | _dx = float(toks[3]) |
---|
| 178 | |
---|
| 179 | if data_conv_q is not None: |
---|
| 180 | _x = data_conv_q(_x, units=base_q_unit) |
---|
| 181 | _dx = data_conv_q(_dx, units=base_q_unit) |
---|
| 182 | |
---|
| 183 | if data_conv_i is not None: |
---|
| 184 | _y = data_conv_i(_y, units=base_i_unit) |
---|
| 185 | _dy = data_conv_i(_dy, units=base_i_unit) |
---|
| 186 | |
---|
| 187 | self.current_dataset.x[data_line] = _x |
---|
| 188 | self.current_dataset.y[data_line] = _y |
---|
| 189 | self.current_dataset.dy[data_line] = _dy |
---|
| 190 | self.current_dataset.dx[data_line] = _dx |
---|
| 191 | data_line += 1 |
---|
| 192 | |
---|
| 193 | except ValueError: |
---|
| 194 | # Could not read this data line. If we are here |
---|
| 195 | # it is because we are in the data section. Just |
---|
| 196 | # skip it. |
---|
| 197 | pass |
---|
| 198 | |
---|
| 199 | # The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. |
---|
| 200 | # I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor| |
---|
| 201 | if line.count("The 6 columns") > 0: |
---|
| 202 | is_data_started = True |
---|
| 203 | |
---|
[7b07fbe] | 204 | self.remove_empty_q_values() |
---|
[ad92c5a] | 205 | |
---|
| 206 | # Sanity check |
---|
| 207 | if not len(self.current_dataset.y) == len(self.current_dataset.dy): |
---|
| 208 | self.set_all_to_none() |
---|
| 209 | msg = "abs_reader: y and dy have different length" |
---|
| 210 | raise ValueError(msg) |
---|
| 211 | # If the data length is zero, consider this as |
---|
| 212 | # though we were not able to read the file. |
---|
| 213 | if len(self.current_dataset.x) == 0: |
---|
| 214 | self.set_all_to_none() |
---|
| 215 | raise ValueError("ascii_reader: could not load file") |
---|
| 216 | |
---|
| 217 | if data_conv_q is not None: |
---|
| 218 | self.current_dataset.xaxis("\\rm{Q}", base_q_unit) |
---|
[959eb01] | 219 | else: |
---|
[ad92c5a] | 220 | self.current_dataset.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 221 | if data_conv_i is not None: |
---|
| 222 | self.current_dataset.yaxis("\\rm{Intensity}", base_i_unit) |
---|
| 223 | else: |
---|
| 224 | self.current_dataset.yaxis("\\rm{Intensity}", "cm^{-1}") |
---|
| 225 | |
---|
| 226 | # Store loading process information |
---|
| 227 | self.current_datainfo.meta_data['loader'] = self.type_name |
---|
| 228 | self.send_to_output() |
---|