source: sasview/src/sas/sascalc/dataloader/readers/hfir1d_reader.py @ 959eb01

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 959eb01 was 959eb01, checked in by ajj, 7 years ago

normalising line endings

  • Property mode set to 100644
File size: 4.8 KB
Line 
1"""
2    HFIR 1D 4-column data reader
3"""
4#####################################################################
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
10######################################################################
11import numpy as np
12import os
13from sas.sascalc.dataloader.data_info import Data1D
14
15# Check whether we have a converter available
16has_converter = True
17try:
18    from sas.sascalc.data_util.nxsunit import Converter
19except:
20    has_converter = False
21
22class Reader(object):
23    """
24    Class to load HFIR 1D 4-column files
25    """
26    ## File type
27    type_name = "HFIR 1D"
28    ## Wildcards
29    type = ["HFIR 1D files (*.d1d)|*.d1d"]
30    ## List of allowed extensions
31    ext = ['.d1d']
32   
33    def read(self, path):
34        """
35        Load data file
36       
37        :param path: file path
38       
39        :return: Data1D object, or None
40       
41        :raise RuntimeError: when the file can't be opened
42        :raise ValueError: when the length of the data vectors are inconsistent
43        """
44        if os.path.isfile(path):
45            basename = os.path.basename(path)
46            root, extension = os.path.splitext(basename)
47            if extension.lower() in self.ext:
48                try:
49                    input_f = open(path,'r')
50                except:
51                    raise  RuntimeError, "hfir1d_reader: cannot open %s" % path
52                buff = input_f.read()
53                lines = buff.split('\n')
54                x = np.zeros(0)
55                y = np.zeros(0)
56                dx = np.zeros(0)
57                dy = np.zeros(0)
58                output = Data1D(x, y, dx=dx, dy=dy)
59                self.filename = output.filename = basename
60           
61                data_conv_q = None
62                data_conv_i = None
63               
64                if has_converter == True and output.x_unit != '1/A':
65                    data_conv_q = Converter('1/A')
66                    # Test it
67                    data_conv_q(1.0, output.x_unit)
68                   
69                if has_converter == True and output.y_unit != '1/cm':
70                    data_conv_i = Converter('1/cm')
71                    # Test it
72                    data_conv_i(1.0, output.y_unit)
73                           
74                for line in lines:
75                    toks = line.split()
76                    try:
77                        _x = float(toks[0])
78                        _y = float(toks[1])
79                        _dx = float(toks[3])
80                        _dy = float(toks[2])
81                       
82                        if data_conv_q is not None:
83                            _x = data_conv_q(_x, units=output.x_unit)
84                            _dx = data_conv_q(_dx, units=output.x_unit)
85                           
86                        if data_conv_i is not None:
87                            _y = data_conv_i(_y, units=output.y_unit)
88                            _dy = data_conv_i(_dy, units=output.y_unit)
89                                                   
90                        x = np.append(x, _x)
91                        y = np.append(y, _y)
92                        dx = np.append(dx, _dx)
93                        dy = np.append(dy, _dy)
94                    except:
95                        # Couldn't parse this line, skip it
96                        pass
97                         
98                # Sanity check
99                if not len(y) == len(dy):
100                    msg = "hfir1d_reader: y and dy have different length"
101                    raise RuntimeError, msg
102                if not len(x) == len(dx):
103                    msg = "hfir1d_reader: x and dx have different length"
104                    raise RuntimeError, msg
105
106                # If the data length is zero, consider this as
107                # though we were not able to read the file.
108                if len(x) == 0:
109                    raise RuntimeError, "hfir1d_reader: could not load file"
110               
111                output.x = x
112                output.y = y
113                output.dy = dy
114                output.dx = dx
115                if data_conv_q is not None:
116                    output.xaxis("\\rm{Q}", output.x_unit)
117                else:
118                    output.xaxis("\\rm{Q}", 'A^{-1}')
119                if data_conv_i is not None:
120                    output.yaxis("\\rm{Intensity}", output.y_unit)
121                else:
122                    output.yaxis("\\rm{Intensity}", "cm^{-1}")
123               
124                # Store loading process information
125                output.meta_data['loader'] = self.type_name
126                return output
127        else:
128            raise RuntimeError, "%s is not a file" % path
129        return None
Note: See TracBrowser for help on using the repository browser.