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