source: sasview/DataLoader/readers/tiff_reader.py @ c45976b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c45976b was 8d6440f, checked in by Gervaise Alina <gervyh@…>, 16 years ago

readers printing in a log file text_log.txt now.testload modified

  • Property mode set to 100644
File size: 4.3 KB
Line 
1
2import Image
3import math,logging
4class 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   
38class 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        print "in tiff"
49        """
50            Open and read the data in a file
51            @param file: path of the file
52        """
53       
54        read_it = False
55        for item in self.ext:
56            if filename.lower().find(item)>=0:
57                read_it = True
58               
59        if read_it:
60            import Image
61            import pylab
62            import copy
63            import numpy
64           
65            wavelength, distance, center_x, center_y, pixel = 10.0, 11.0, 65, 65, 1.0
66           
67           
68           
69            # Initialize
70            x_vals = []
71            y_vals = [] 
72            ymin = None
73            ymax = None
74            xmin = None
75            xmax = None
76            Z = None
77       
78            try:
79                im = Image.open(filename)
80            except :
81                raise  RuntimeError,"cannot open %s"%(filename)
82            # Detector size should be 128x128, the file is 190x190
83            print "-> Image size:", im.size, im.format, im.mode
84            data = im.getdata()
85            x = numpy.zeros(im.size[0])
86            y = numpy.zeros(im.size[1])
87            X, Y = pylab.meshgrid(x, y)
88            Z = copy.deepcopy(X)
89            itot = 0
90            i_x = 0
91            i_y = 0
92           
93            # Qx and Qy vectors
94            for i_x in range(im.size[0]):
95                theta = (i_x-center_x+1)*pixel / distance / 100.0
96                qx = 4.0*math.pi/wavelength * math.sin(theta/2.0)
97                x_vals.append(qx)
98                if xmin==None or qx<xmin:
99                    xmin = qx
100                if xmax==None or qx>xmax:
101                    xmax = qx
102           
103            ymin = None
104            ymax = None
105            for i_y in range(im.size[1]):
106                theta = (i_y-center_y+1)*pixel / distance / 100.0
107                qy = 4.0*math.pi/wavelength * math.sin(theta/2.0)
108                y_vals.append(qy)
109                if ymin==None or qy<ymin:
110                    ymin = qy
111                if ymax==None or qy>ymax:
112                    ymax = qy
113           
114            for val in data:
115               
116                try:
117                    value = float(val)
118                except:
119                    continue
120               
121                # Get bin number
122                if math.fmod(itot, im.size[0])==0:
123                    i_x = 0
124                    i_y += 1
125                else:
126                    i_x += 1
127                   
128                Z[im.size[1]-1-i_y][i_x] = value
129               
130                itot += 1
131               
132            output = ReaderInfo()
133            output.wavelength = wavelength
134            output.xbins      = im.size[0]
135            output.ybins      = im.size[1]
136            output.center_x   = center_x
137            output.center_y   = center_y
138            output.distance   = distance
139            output.x_vals     = x_vals
140            output.y_vals     = y_vals
141            output.xmin       = xmin
142            output.xmax       = xmax
143            output.ymin       = ymin
144            output.ymax       = ymax
145            output.image      = Z
146            output.pixel_size = pixel
147            logging.info("tiff_reader reading %s"%(filename))
148           
149            return output
150       
151        return None
152
153
Note: See TracBrowser for help on using the repository browser.