source: sasview/src/sas/dataloader/readers/sesans_reader.py @ 5e326a6

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 5e326a6 was 5e326a6, checked in by jhbakker, 9 years ago

added sesans_reader for SESANS analysis

  • Property mode set to 100644
File size: 9.8 KB
Line 
1"""
2    SESANS reader
3"""
4
5import numpy
6import os
7from sas.dataloader.data_info import SESANSData1D
8
9# Check whether we have a converter available
10has_converter = True
11try:
12    from sas.data_util.nxsunit import Converter
13except:
14    has_converter = False
15_ZERO = 1e-16
16
17class Reader:
18    """
19    Class to load sesans files (6 columns).
20    """
21    ## File type
22    type_name = "SESANS"
23   
24    ## Wildcards
25    type = ["SESANS files (*.ses)|*.ses",
26            "SESANS files (*..sesans)|*.sesans"]
27    ## List of allowed extensions
28    ext = ['.ses', '.SES', '.sesans', '.SESANS']
29   
30    ## Flag to bypass extension check
31    allow_all = True
32   
33    def read(self, path):
34       
35#        print "reader triggered"
36       
37        """
38        Load data file
39       
40        :param path: file path
41       
42        :return: SESANSData1D object, or None
43       
44        :raise RuntimeError: when the file can't be opened
45        :raise ValueError: when the length of the data vectors are inconsistent
46        """
47        if os.path.isfile(path):
48            basename = os.path.basename(path)
49            _, extension = os.path.splitext(basename)
50            if self.allow_all or extension.lower() in self.ext:
51                try:
52                    # Read in binary mode since GRASP frequently has no-ascii
53                    # characters that brakes the open operation
54                    input_f = open(path,'rb')
55                except:
56                    raise  RuntimeError, "sesans_reader: cannot open %s" % path
57                buff = input_f.read()
58#                print buff
59                lines = buff.splitlines()
60#                print lines
61                #Jae could not find python universal line spliter:
62                #keep the below for now
63                # some ascii data has \r line separator,
64                # try it when the data is on only one long line
65#                if len(lines) < 2 :
66#                    lines = buff.split('\r')
67                 
68                x  = numpy.zeros(0)
69                y  = numpy.zeros(0)
70                dy = numpy.zeros(0)
71                lam  = numpy.zeros(0)
72                dlam = numpy.zeros(0)
73                dx = numpy.zeros(0)
74               
75               #temp. space to sort data
76                tx  = numpy.zeros(0)
77                ty  = numpy.zeros(0)
78                tdy = numpy.zeros(0)
79                tlam  = numpy.zeros(0)
80                tdlam = numpy.zeros(0)
81                tdx = numpy.zeros(0)
82#                print "all good"
83                output = SESANSData1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam)
84#                print output               
85                self.filename = output.filename = basename
86               
87               
88                data_conv_z = None
89                data_conv_P = None
90#                print "passing"
91                if has_converter == True and output.x_unit != 'A':
92                    data_conv_z = Converter('nm')
93                    # Test it
94                    data_conv_z(1.0, output.x_unit)
95#                    print data_conv_z
96#                    print data_conv_z(1.0, output.x_unit)
97                if has_converter == True and output.y_unit != ' ':
98                    data_conv_P = Converter('a.u.')
99                    # Test it
100                    data_conv_P(1.0, output.y_unit)
101#                    print data_conv_P
102#                    print data_conv_P(1.0, output.y_unit)
103                # The first good line of data will define whether
104                # we have 2-column or 3-column ascii
105#                print output.x_unit
106#                print output.y_unit
107               
108#                print "past output"
109               
110#                has_error_dx = None
111#                has_error_dy = None
112               
113#                #Initialize counters for data lines and header lines.
114#                is_data = False  # Has more than 5 lines
115#                # More than "5" lines of data is considered as actual
116#                # data unless that is the only data
117#                mum_data_lines = 5
118#                # To count # of current data candidate lines
119#                i = -1
120#                # To count total # of previous data candidate lines
121#                i1 = -1
122#                # To count # of header lines
123#                j = -1
124#                # Helps to count # of header lines
125#                j1 = -1
126#                #minimum required number of columns of data; ( <= 4).
127#                lentoks = 2
128                paramnames=[]
129                paramvals=[]
130                zvals=[]
131                dzvals=[]
132                lamvals=[]
133                dlamvals=[]
134                Pvals=[]
135                dPvals=[]
136#                print x
137#                print zvals
138                for line in lines:
139                    # Initial try for CSV (split on ,)
140#                    print line
141                    line=line.strip()
142#                    print line
143                    toks = line.split('\t')
144#                    print toks
145                    if len(toks)==2:
146                        paramnames.append(toks[0])
147#                        print paramnames
148                        paramvals.append(toks[1])
149#                        print paramvals
150                    if len(toks)>5:
151                        zvals.append(toks[0])
152                        dzvals.append(toks[1])
153                        lamvals.append(toks[2])
154                        dlamvals.append(toks[3])
155                        Pvals.append(toks[4])
156                        dPvals.append(toks[5])
157                    else:
158                        continue
159#                print varheaders
160#                print paramnames
161#                print paramvals
162#                print zvals 
163#                print len(zvals)
164               
165                x=[]
166                y=[]
167                lam=[]
168                dx=[]
169                dy=[]
170                dlam=[]
171                varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]]
172#                print varheader
173                valrange=range(len(zvals)-1)
174#                print valrange
175                for i in valrange:
176                    x.append(float(zvals[i+1]))
177                    y.append(float(Pvals[i+1]))
178                    lam.append(float(lamvals[i+1]))
179                    dy.append(float(dPvals[i+1]))
180                    dx.append(float(dzvals[i+1]))
181                    dlam.append(float(dlamvals[i+1]))
182                   
183                x,y,lam,dy,dx,dlam = [
184                   numpy.asarray(v, 'double')
185                   for v in (x,y,lam,dy,dx,dlam)
186                ]
187
188#                print x
189#                print y
190#                print dx
191#                print dy
192#                print len(x)
193#                print len(y)
194#                print len(dx)
195#                print len(dy)
196               
197               
198                input_f.close()
199                # Sanity check
200#                if has_error_dy == True and not len(y) == len(dy):
201#                    msg = "sesans_reader: y and dy have different length"
202#                    raise RuntimeError, msg
203#                if has_error_dx == True and not len(x) == len(dx):
204#                    msg = "sesans_reader: y and dy have different length"
205#                    raise RuntimeError, msg
206#                # If the data length is zero, consider this as
207#                # though we were not able to read the file.
208#                if len(x) == 0:
209#                    raise RuntimeError, "sesans_reader: could not load file"
210#                print "alive"
211                #Let's re-order the data to make cal.
212                # curve look better some cases
213#                ind = numpy.lexsort((ty, tx))
214#                for i in ind:
215#                    x[i] = tx[ind[i]]
216#                    y[i] = ty[ind[i]]
217#                    if has_error_dy == True:
218#                        dy[i] = tdy[ind[i]]
219#                    if has_error_dx == True:
220#                        dx[i] = tdx[ind[i]]
221                # Zeros in dx, dy
222#                if has_error_dx:
223#                    dx[dx == 0] = _ZERO
224#                if has_error_dy:
225#                    dy[dy == 0] = _ZERO
226                #Data
227                output.x = x #[x != 0]
228#                print output.x
229                output.y = y #[x != 0]
230#                print output.y
231#                output.dy = dy[x != 0] if has_error_dy == True\
232#                    else numpy.zeros(len(output.y))
233#                output.dx = dx[x != 0] if has_error_dx == True\
234#                    else numpy.zeros(len(output.x))
235                output.dy = dy
236                output.dx = dx
237                output.lam = lam
238                output.dlam = dlam
239
240
241#                print "still alive"               
242#                if data_conv_z is not None:
243#                    output.xaxis("\\rm{z}", output.x_unit)
244#                else:
245#                    output.xaxis("\\rm{z}", 'nm')
246#                if data_conv_P is not None:
247#                    output.yaxis("\\rm{P/P0}", output.y_unit)
248#                else:
249#                    output.yaxis("\\rm{P/P0}", "a.u.")
250                output.xaxis("\\rm{z}", 'A')   
251                output.yaxis("\\rm{P/P0}", " ")
252                # Store loading process information
253                output.meta_data['loader'] = self.type_name
254                output.sample.thickness = float(paramvals[6])
255                output.sample.name = paramvals[1]
256                output.sample.ID = paramvals[0]
257                output.sample.zacceptance=float(paramvals[7])
258#                print output               
259
260#                print "sesans_reader end"
261               
262                if len(output.x) < 1:
263                    raise RuntimeError, "%s is empty" % path
264#                print output
265               
266                return output
267           
268        else:
269            raise RuntimeError, "%s is not a file" % path
270        return None
Note: See TracBrowser for help on using the repository browser.