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