source: sasview/sansdataloader/readers/hfir1d_reader.py @ aab4e15

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 aab4e15 was aab4e15, checked in by Jae Cho <jhjcho@…>, 13 years ago

rename dataloader

  • Property mode set to 100644
File size: 5.1 KB
Line 
1
2#####################################################################
3#This software was developed by the University of Tennessee as part of the
4#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
5#project funded by the US National Science Foundation.
6#See the license text in license.txt
7#copyright 2008, University of Tennessee
8######################################################################
9
10import numpy
11import os
12from DataLoader.data_info import Data1D
13
14# Check whether we have a converter available
15has_converter = True
16try:
17    from data_util.nxsunit import Converter
18except:
19    has_converter = False
20
21class Reader(object):
22    """
23    Class to load HFIR 1D 4-column files
24    """
25    ## File type
26    type_name = "HFIR 1D"   
27    ## Wildcards
28    type = ["HFIR 1D files (*.d1d)|*.d1d"]
29    ## List of allowed extensions
30    ext = ['.d1d'] 
31   
32    def read(self, path):
33        """
34        Load data file
35       
36        :param path: file path
37       
38        :return: Data1D object, or None
39       
40        :raise RuntimeError: when the file can't be opened
41        :raise ValueError: when the length of the data vectors are inconsistent
42        """
43        if os.path.isfile(path):
44            basename  = os.path.basename(path)
45            root, extension = os.path.splitext(basename)
46            if extension.lower() in self.ext:
47                try:
48                    input_f =  open(path,'r')
49                except :
50                    raise  RuntimeError, "hfir1d_reader: cannot open %s" % path
51                buff = input_f.read()
52                lines = buff.split('\n')
53                x  = numpy.zeros(0)
54                y  = numpy.zeros(0)
55                dx = numpy.zeros(0)
56                dy = numpy.zeros(0)
57                output = Data1D(x, y, dx=dx, dy=dy)
58                self.filename = output.filename = basename
59           
60                data_conv_q = None
61                data_conv_i = None
62               
63                if has_converter == True and output.x_unit != '1/A':
64                    data_conv_q = Converter('1/A')
65                    # Test it
66                    data_conv_q(1.0, output.x_unit)
67                   
68                if has_converter == True and output.y_unit != '1/cm':
69                    data_conv_i = Converter('1/cm')
70                    # Test it
71                    data_conv_i(1.0, output.y_unit)
72                           
73                for line in lines:
74                    toks = line.split()
75                    try:
76                        _x = float(toks[0])
77                        _y = float(toks[1])
78                        _dx = float(toks[3])
79                        _dy = float(toks[2])
80                       
81                        if data_conv_q is not None:
82                            _x = data_conv_q(_x, units=output.x_unit)
83                            _dx = data_conv_q(_dx, units=output.x_unit)
84                           
85                        if data_conv_i is not None:
86                            _y = data_conv_i(_y, units=output.y_unit)                       
87                            _dy = data_conv_i(_dy, units=output.y_unit)                       
88                                                   
89                        x   = numpy.append(x, _x) 
90                        y   = numpy.append(y, _y)
91                        dx  = numpy.append(dx, _dx)
92                        dy  = numpy.append(dy, _dy)                       
93                    except:
94                        # Couldn't parse this line, skip it
95                        pass
96                         
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
130   
131if __name__ == "__main__": 
132    reader = Reader()
133    print reader.read("../test/S2-30dq.d1d")
134   
135   
136                       
Note: See TracBrowser for help on using the repository browser.