1 | """ |
---|
2 | Test plug-in |
---|
3 | """ |
---|
4 | |
---|
5 | import math |
---|
6 | import pylab |
---|
7 | import copy |
---|
8 | import numpy |
---|
9 | import logging |
---|
10 | class ReaderInfo: |
---|
11 | """ |
---|
12 | """ |
---|
13 | ## Wavelength [A] |
---|
14 | wavelength = 0.0 |
---|
15 | ## Number of x bins |
---|
16 | xbins = 128 |
---|
17 | ## Number of y bins |
---|
18 | ybins = 128 |
---|
19 | ## Beam center X [pixel number] |
---|
20 | center_x = 65 |
---|
21 | ## Beam center Y [pixel number] |
---|
22 | center_y = 65 |
---|
23 | ## Distance from sample to detector [m] |
---|
24 | distance = 11.0 |
---|
25 | ## Qx values [A-1] |
---|
26 | x_vals = [] |
---|
27 | ## Qy xalues [A-1] |
---|
28 | y_vals = [] |
---|
29 | ## Qx min |
---|
30 | xmin = 0.0 |
---|
31 | ## Qx max |
---|
32 | xmax = 1.0 |
---|
33 | ## Qy min |
---|
34 | ymin = 0.0 |
---|
35 | ## Qy max |
---|
36 | ymax = 1.0 |
---|
37 | ## Image |
---|
38 | image = None |
---|
39 | ## Pixel size |
---|
40 | pixel_size = 0.5 |
---|
41 | ## Error on each pixel |
---|
42 | error = None |
---|
43 | class Reader: |
---|
44 | """ |
---|
45 | Example data manipulation |
---|
46 | """ |
---|
47 | ## File type |
---|
48 | type = ["DANSE files (*.sans)|*.sans"] |
---|
49 | ## Extension |
---|
50 | ext = ['.sans'] |
---|
51 | |
---|
52 | def read(self, filename=None): |
---|
53 | """ |
---|
54 | Open and read the data in a file |
---|
55 | @param file: path of the file |
---|
56 | """ |
---|
57 | |
---|
58 | read_it = False |
---|
59 | for item in self.ext: |
---|
60 | if filename.lower().find(item)>=0: |
---|
61 | read_it = True |
---|
62 | |
---|
63 | if read_it: |
---|
64 | try: |
---|
65 | datafile = open(filename, 'r') |
---|
66 | except : |
---|
67 | raise RuntimeError,"danse_reader cannot open %s"%(filename) |
---|
68 | |
---|
69 | |
---|
70 | # defaults |
---|
71 | # wavelength in Angstrom |
---|
72 | wavelength = 10.0 |
---|
73 | # Distance in meter |
---|
74 | distance = 11.0 |
---|
75 | # Pixel number of center in x |
---|
76 | center_x = 65 |
---|
77 | # Pixel number of center in y |
---|
78 | center_y = 65 |
---|
79 | # Pixel size [mm] |
---|
80 | pixel = 5.0 |
---|
81 | # Size in x, in pixels |
---|
82 | size_x = 128 |
---|
83 | # Size in y, in pixels |
---|
84 | size_y = 128 |
---|
85 | # Format version |
---|
86 | fversion = 1.0 |
---|
87 | |
---|
88 | read_on = True |
---|
89 | while read_on: |
---|
90 | line = datafile.readline() |
---|
91 | if line.find("DATA:")>=0: |
---|
92 | read_on = False |
---|
93 | break |
---|
94 | toks = line.split(':') |
---|
95 | if toks[0]=="FORMATVERSION": |
---|
96 | fversion = float(toks[1]) |
---|
97 | if toks[0]=="WAVELENGTH": |
---|
98 | wavelength = float(toks[1]) |
---|
99 | elif toks[0]=="DISTANCE": |
---|
100 | distance = float(toks[1]) |
---|
101 | elif toks[0]=="CENTER_X": |
---|
102 | center_x = float(toks[1]) |
---|
103 | elif toks[0]=="CENTER_Y": |
---|
104 | center_y = float(toks[1]) |
---|
105 | elif toks[0]=="PIXELSIZE": |
---|
106 | pixel = float(toks[1]) |
---|
107 | elif toks[0]=="SIZE_X": |
---|
108 | size_x = int(toks[1]) |
---|
109 | elif toks[0]=="SIZE_Y": |
---|
110 | size_y = int(toks[1]) |
---|
111 | |
---|
112 | # Read the data |
---|
113 | data = [] |
---|
114 | error = [] |
---|
115 | if fversion==1.0: |
---|
116 | data_str = datafile.readline() |
---|
117 | data = data_str.split(' ') |
---|
118 | else: |
---|
119 | read_on = True |
---|
120 | while read_on: |
---|
121 | data_str = datafile.readline() |
---|
122 | if len(data_str)==0: |
---|
123 | read_on = False |
---|
124 | else: |
---|
125 | toks = data_str.split() |
---|
126 | try: |
---|
127 | val = float(toks[0]) |
---|
128 | err = float(toks[1]) |
---|
129 | data.append(val) |
---|
130 | error.append(err) |
---|
131 | except: |
---|
132 | logging.info("Skipping line:%s,%s" %( data_str,sys.exc_value)) |
---|
133 | #print "Skipping line:%s" % data_str |
---|
134 | #print sys.exc_value |
---|
135 | |
---|
136 | # Initialize |
---|
137 | x_vals = [] |
---|
138 | y_vals = [] |
---|
139 | ymin = None |
---|
140 | ymax = None |
---|
141 | xmin = None |
---|
142 | xmax = None |
---|
143 | Z = None |
---|
144 | |
---|
145 | |
---|
146 | x = numpy.zeros(size_x) |
---|
147 | y = numpy.zeros(size_y) |
---|
148 | X, Y = pylab.meshgrid(x, y) |
---|
149 | Z = copy.deepcopy(X) |
---|
150 | E = copy.deepcopy(X) |
---|
151 | itot = 0 |
---|
152 | i_x = 0 |
---|
153 | i_y = 0 |
---|
154 | |
---|
155 | # Qx and Qy vectors |
---|
156 | for i_x in range(size_x): |
---|
157 | theta = (i_x-center_x+1)*pixel / distance / 100.0 |
---|
158 | qx = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
159 | x_vals.append(qx) |
---|
160 | if xmin==None or qx<xmin: |
---|
161 | xmin = qx |
---|
162 | if xmax==None or qx>xmax: |
---|
163 | xmax = qx |
---|
164 | |
---|
165 | ymin = None |
---|
166 | ymax = None |
---|
167 | for i_y in range(size_y): |
---|
168 | theta = (i_y-center_y+1)*pixel / distance / 100.0 |
---|
169 | qy = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
170 | y_vals.append(qy) |
---|
171 | if ymin==None or qy<ymin: |
---|
172 | ymin = qy |
---|
173 | if ymax==None or qy>ymax: |
---|
174 | ymax = qy |
---|
175 | |
---|
176 | for i_pt in range(len(data)): |
---|
177 | val = data[i_pt] |
---|
178 | try: |
---|
179 | value = float(val) |
---|
180 | except: |
---|
181 | continue |
---|
182 | |
---|
183 | # Get bin number |
---|
184 | if math.fmod(itot, size_x)==0: |
---|
185 | i_x = 0 |
---|
186 | i_y += 1 |
---|
187 | else: |
---|
188 | i_x += 1 |
---|
189 | |
---|
190 | Z[size_y-1-i_y][i_x] = value |
---|
191 | if fversion>1.0: |
---|
192 | E[size_y-1-i_y][i_x] = error[i_pt] |
---|
193 | |
---|
194 | itot += 1 |
---|
195 | |
---|
196 | output = ReaderInfo() |
---|
197 | output.wavelength = wavelength |
---|
198 | output.xbins = size_x |
---|
199 | output.ybins = size_y |
---|
200 | output.center_x = center_x |
---|
201 | output.center_y = center_y |
---|
202 | # Store the distance in [mm] |
---|
203 | output.distance = distance*1000.0 |
---|
204 | output.x_vals = x_vals |
---|
205 | output.y_vals = y_vals |
---|
206 | output.xmin = xmin |
---|
207 | output.xmax = xmax |
---|
208 | output.ymin = ymin |
---|
209 | output.ymax = ymax |
---|
210 | output.pixel_size = pixel |
---|
211 | output.image = Z |
---|
212 | if not fversion==1.1: |
---|
213 | #output.error = E |
---|
214 | raise ValueError,"Danse_reader can't read this file %s"%filename |
---|
215 | else: |
---|
216 | logging.info("Danse_reader Reading %s \n"%filename) |
---|
217 | return output |
---|
218 | |
---|
219 | return None |
---|