source: sasview/src/sas/sascalc/dataloader/readers/sesans_reader.py @ 35009a0

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 35009a0 was 35009a0, checked in by Adam Washington <adam.washington@…>, 7 years ago

Remove redundant code from sesans_reader

I left in an old conditional from the aborted attempt at reading the
file with a generator

  • Property mode set to 100644
File size: 6.3 KB
Line 
1"""
2    SESANS reader (based on ASCII reader)
3
4    Reader for .ses or .sesans file format
5
6    Jurrian Bakker
7"""
8import numpy as np
9import os
10from sas.sascalc.dataloader.data_info import Data1D
11
12# Check whether we have a converter available
13has_converter = True
14try:
15    from sas.sascalc.data_util.nxsunit import Converter
16except:
17    has_converter = False
18_ZERO = 1e-16
19
20
21class Reader:
22    """
23    Class to load sesans files (6 columns).
24    """
25    # File type
26    type_name = "SESANS"
27
28    # Wildcards
29    type = ["SESANS files (*.ses)|*.ses",
30            "SESANS files (*..sesans)|*.sesans"]
31    # List of allowed extensions
32    ext = ['.ses', '.SES', '.sesans', '.SESANS']
33
34    # Flag to bypass extension check
35    allow_all = True
36
37    def read(self, path):
38        """
39        Load data file
40
41        :param path: file path
42
43        :return: SESANSData1D object, or None
44
45        :raise RuntimeError: when the file can't be opened
46        :raise ValueError: when the length of the data vectors are inconsistent
47        """
48        if os.path.isfile(path):
49            basename = os.path.basename(path)
50            _, extension = os.path.splitext(basename)
51            if self.allow_all or extension.lower() in self.ext:
52                with open(path, 'r') as input_f:
53                    # Read in binary mode since GRASP frequently has no-ascii
54                    # characters that brakes the open operation
55                    line = input_f.readline()
56                    params = {}
57                    while line.strip() != "":
58                        terms = line.strip().split("\t")
59                        params[terms[0].strip()] = " ".join(terms[1:]).strip()
60                        line = input_f.readline()
61                    headers_temp = input_f.readline().strip().split("\t")
62                    headers = {}
63                    for h in headers_temp:
64                        temp = h.strip().split()
65                        headers[h[:-1].strip()] = temp[-1][1:-1]
66                    data = np.loadtxt(input_f)
67                    x = data[:, 0]
68                    dx = data[:, 3]
69                    lam = data[:, 4]
70                    dlam = data[:, 5]
71                    y = data[:, 1]
72                    dy = data[:, 2]
73
74                    lam_unit = self._header_fetch(headers, "wavelength")
75                    if lam_unit == "AA":
76                        lam_unit = "A"
77
78                    x, x_unit = self._unit_conversion(
79                        x, lam_unit,
80                        self._fetch_unit(headers, "spin echo length"))
81                    dx, dx_unit = self._unit_conversion(
82                        dx, lam_unit,
83                        self._fetch_unit(headers, "error SEL"))
84                    dlam, dlam_unit = self._unit_conversion(
85                        dlam, lam_unit,
86                        self._fetch_unit(headers, "error wavelength"))
87                    y_unit = r'\AA^{-2} cm^{-1}'
88
89                    output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam,
90                                    isSesans=True)
91                    self.filename = output.filename = basename
92                    output.xaxis(r"\rm{z}", x_unit)
93                    # Adjust label to ln P/(lam^2 t), remove lam column refs
94                    output.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit)
95                    # Store loading process information
96                    output.meta_data['loader'] = self.type_name
97                    output.sample.name = params["Sample"]
98                    output.sample.ID = params["DataFileTitle"]
99
100                    output.sample.zacceptance = (
101                        float(self._header_fetch(params, "Q_zmax")),
102                        self._fetch_unit(params, "Q_zmax"))
103
104                    output.sample.yacceptance = (
105                        float(self._header_fetch(params, "Q_ymax")),
106                        self._fetch_unit(params, "Q_ymax"))
107
108                if len(output.x) < 1:
109                    raise RuntimeError("%s is empty" % path)
110                return output
111
112        else:
113            raise RuntimeError("%s is not a file" % path)
114        return None
115
116    @staticmethod
117    def _unit_conversion(value, value_unit, default_unit):
118        """
119        Performs unit conversion on a measurement.
120
121        :param value: The magnitude of the measurement
122        :type value: Number
123        :param value_unit: a string containing the final desired unit
124        :type value_unit: String
125        :param default_unit: a string containing the units of the original measurement
126        :type default_unit: String
127        :return: The magnitude of the measurement in the new units
128        """
129        if has_converter and value_unit != default_unit:
130            data_conv_q = Converter(value_unit)
131            value = data_conv_q(value, units=default_unit)
132            new_unit = default_unit
133        else:
134            new_unit = value_unit
135        return value, new_unit
136
137    @staticmethod
138    def _header_fetch(headers, key):
139        """
140        Pull the value of a unit defined header from a dict. Example::
141
142         d = {"Length [m]": 17}
143         self._header_fetch(d, "Length") == 17
144
145        :param header: A dictionary of values
146        :type header: Dictionary
147        :param key: A string which is a prefix for one of the keys in the dict
148        :type key: String
149        :return: The value of the dictionary for the specified key
150        """
151        index = [k for k in headers.keys()
152                 if k.startswith(key)][0]
153        return headers[index]
154
155    @staticmethod
156    def _fetch_unit(params, key):
157        """
158        Pull the unit off of a dictionary header. Example::
159
160         d = {"Length [m]": 17}
161         self._fetch_unit(d, "Length") == "m"
162
163        :param header: A dictionary of values, where the keys are strings
164        with the units for the values appended onto the string within square
165        brackets (See the example above)
166        :type header: Dictionary
167        :param key: A string with the prefix of the dictionary key whose unit
168        is being fetched
169        :type key: String
170        :return: A string containing the unit specifed in the header
171        """
172        index = [k for k in params.keys()
173                 if k.startswith(key)][0]
174        unit = index.strip().split()[-1][1:-1]
175        if unit.startswith(r"\A"):
176            unit = "1/A"
177        return unit
Note: See TracBrowser for help on using the repository browser.