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