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