source: sasview/DataLoader/readers/hfir1d_reader.py @ bdd71f4

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.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since bdd71f4 was ca10d8e, checked in by Gervaise Alina <gervyh@…>, 15 years ago

reverse code from version 1237 with modification on output axis unit

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