[0fc05f9] | 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 Reader: |
---|
| 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 | |
---|
[3dd7cce] | 77 | try: |
---|
| 78 | im = Image.open(filename) |
---|
| 79 | except : |
---|
| 80 | raise RuntimeError,"cannot open %s"%(filename) |
---|
[0fc05f9] | 81 | # Detector size should be 128x128, the file is 190x190 |
---|
| 82 | print "-> Image size:", im.size, im.format, im.mode |
---|
| 83 | data = im.getdata() |
---|
| 84 | x = numpy.zeros(im.size[0]) |
---|
| 85 | y = numpy.zeros(im.size[1]) |
---|
| 86 | X, Y = pylab.meshgrid(x, y) |
---|
| 87 | Z = copy.deepcopy(X) |
---|
| 88 | itot = 0 |
---|
| 89 | i_x = 0 |
---|
| 90 | i_y = 0 |
---|
| 91 | |
---|
| 92 | # Qx and Qy vectors |
---|
| 93 | for i_x in range(im.size[0]): |
---|
| 94 | theta = (i_x-center_x+1)*pixel / distance / 100.0 |
---|
| 95 | qx = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
| 96 | x_vals.append(qx) |
---|
| 97 | if xmin==None or qx<xmin: |
---|
| 98 | xmin = qx |
---|
| 99 | if xmax==None or qx>xmax: |
---|
| 100 | xmax = qx |
---|
| 101 | |
---|
| 102 | ymin = None |
---|
| 103 | ymax = None |
---|
| 104 | for i_y in range(im.size[1]): |
---|
| 105 | theta = (i_y-center_y+1)*pixel / distance / 100.0 |
---|
| 106 | qy = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
| 107 | y_vals.append(qy) |
---|
| 108 | if ymin==None or qy<ymin: |
---|
| 109 | ymin = qy |
---|
| 110 | if ymax==None or qy>ymax: |
---|
| 111 | ymax = qy |
---|
| 112 | |
---|
| 113 | for val in data: |
---|
| 114 | |
---|
| 115 | try: |
---|
| 116 | value = float(val) |
---|
| 117 | except: |
---|
| 118 | continue |
---|
| 119 | |
---|
| 120 | # Get bin number |
---|
| 121 | if math.fmod(itot, im.size[0])==0: |
---|
| 122 | i_x = 0 |
---|
| 123 | i_y += 1 |
---|
| 124 | else: |
---|
| 125 | i_x += 1 |
---|
| 126 | |
---|
| 127 | Z[im.size[1]-1-i_y][i_x] = value |
---|
| 128 | |
---|
| 129 | itot += 1 |
---|
| 130 | |
---|
| 131 | output = ReaderInfo() |
---|
| 132 | output.wavelength = wavelength |
---|
| 133 | output.xbins = im.size[0] |
---|
| 134 | output.ybins = im.size[1] |
---|
| 135 | output.center_x = center_x |
---|
| 136 | output.center_y = center_y |
---|
| 137 | output.distance = distance |
---|
| 138 | output.x_vals = x_vals |
---|
| 139 | output.y_vals = y_vals |
---|
| 140 | output.xmin = xmin |
---|
| 141 | output.xmax = xmax |
---|
| 142 | output.ymin = ymin |
---|
| 143 | output.ymax = ymax |
---|
| 144 | output.image = Z |
---|
| 145 | output.pixel_size = pixel |
---|
| 146 | |
---|
| 147 | return output |
---|
| 148 | |
---|
| 149 | return None |
---|
| 150 | |
---|
| 151 | |
---|