1 | |
---|
2 | import Image |
---|
3 | import math |
---|
4 | class ReaderInfo: |
---|
5 | """ |
---|
6 | """ |
---|
7 | ## Wavelength [A] |
---|
8 | wavelength = 0.0 |
---|
9 | ## Number of x bins |
---|
10 | xbins = 128 |
---|
11 | ## Number of y bins |
---|
12 | ybins = 128 |
---|
13 | ## Beam center X [pixel number] |
---|
14 | center_x = 65 |
---|
15 | ## Beam center Y [pixel number] |
---|
16 | center_y = 65 |
---|
17 | ## Distance from sample to detector [m] |
---|
18 | distance = 11.0 |
---|
19 | ## Qx values [A-1] |
---|
20 | x_vals = [] |
---|
21 | ## Qy xalues [A-1] |
---|
22 | y_vals = [] |
---|
23 | ## Qx min |
---|
24 | xmin = 0.0 |
---|
25 | ## Qx max |
---|
26 | xmax = 1.0 |
---|
27 | ## Qy min |
---|
28 | ymin = 0.0 |
---|
29 | ## Qy max |
---|
30 | ymax = 1.0 |
---|
31 | ## Image |
---|
32 | image = None |
---|
33 | ## Pixel size |
---|
34 | pixel_size = 0.5 |
---|
35 | ## Error on each pixel |
---|
36 | error = None |
---|
37 | |
---|
38 | class DataReader: |
---|
39 | """ |
---|
40 | Example data manipulation |
---|
41 | """ |
---|
42 | ## File type |
---|
43 | type = [] |
---|
44 | ## Extension |
---|
45 | ext = ['tif', 'jpg', 'png', 'jpeg', 'tiff', 'gif', 'bmp'] |
---|
46 | |
---|
47 | def read(self, filename=None): |
---|
48 | """ |
---|
49 | Open and read the data in a file |
---|
50 | @param file: path of the file |
---|
51 | """ |
---|
52 | |
---|
53 | read_it = False |
---|
54 | for item in self.ext: |
---|
55 | if filename.lower().find(item)>=0: |
---|
56 | read_it = True |
---|
57 | |
---|
58 | if read_it: |
---|
59 | import Image |
---|
60 | import pylab |
---|
61 | import copy |
---|
62 | import numpy |
---|
63 | |
---|
64 | wavelength, distance, center_x, center_y, pixel = 10.0, 11.0, 65, 65, 1.0 |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | # Initialize |
---|
69 | x_vals = [] |
---|
70 | y_vals = [] |
---|
71 | ymin = None |
---|
72 | ymax = None |
---|
73 | xmin = None |
---|
74 | xmax = None |
---|
75 | Z = None |
---|
76 | |
---|
77 | |
---|
78 | im = Image.open(filename) |
---|
79 | |
---|
80 | # Detector size should be 128x128, the file is 190x190 |
---|
81 | print "-> Image size:", im.size, im.format, im.mode |
---|
82 | data = im.getdata() |
---|
83 | x = numpy.zeros(im.size[0]) |
---|
84 | y = numpy.zeros(im.size[1]) |
---|
85 | X, Y = pylab.meshgrid(x, y) |
---|
86 | Z = copy.deepcopy(X) |
---|
87 | itot = 0 |
---|
88 | i_x = 0 |
---|
89 | i_y = 0 |
---|
90 | |
---|
91 | # Qx and Qy vectors |
---|
92 | for i_x in range(im.size[0]): |
---|
93 | theta = (i_x-center_x+1)*pixel / distance / 100.0 |
---|
94 | qx = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
95 | x_vals.append(qx) |
---|
96 | if xmin==None or qx<xmin: |
---|
97 | xmin = qx |
---|
98 | if xmax==None or qx>xmax: |
---|
99 | xmax = qx |
---|
100 | |
---|
101 | ymin = None |
---|
102 | ymax = None |
---|
103 | for i_y in range(im.size[1]): |
---|
104 | theta = (i_y-center_y+1)*pixel / distance / 100.0 |
---|
105 | qy = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
106 | y_vals.append(qy) |
---|
107 | if ymin==None or qy<ymin: |
---|
108 | ymin = qy |
---|
109 | if ymax==None or qy>ymax: |
---|
110 | ymax = qy |
---|
111 | |
---|
112 | for val in data: |
---|
113 | |
---|
114 | try: |
---|
115 | value = float(val) |
---|
116 | except: |
---|
117 | continue |
---|
118 | |
---|
119 | # Get bin number |
---|
120 | if math.fmod(itot, im.size[0])==0: |
---|
121 | i_x = 0 |
---|
122 | i_y += 1 |
---|
123 | else: |
---|
124 | i_x += 1 |
---|
125 | |
---|
126 | Z[im.size[1]-1-i_y][i_x] = value |
---|
127 | |
---|
128 | itot += 1 |
---|
129 | |
---|
130 | output = ReaderInfo() |
---|
131 | output.wavelength = wavelength |
---|
132 | output.xbins = im.size[0] |
---|
133 | output.ybins = im.size[1] |
---|
134 | output.center_x = center_x |
---|
135 | output.center_y = center_y |
---|
136 | output.distance = distance |
---|
137 | output.x_vals = x_vals |
---|
138 | output.y_vals = y_vals |
---|
139 | output.xmin = xmin |
---|
140 | output.xmax = xmax |
---|
141 | output.ymin = ymin |
---|
142 | output.ymax = ymax |
---|
143 | output.image = Z |
---|
144 | output.pixel_size = pixel |
---|
145 | |
---|
146 | return output |
---|
147 | |
---|
148 | return None |
---|
149 | |
---|
150 | |
---|