Package fabio :: Module fit2dspreadsheetimage
[hide private]
[frames] | no frames]

Source Code for Module fabio.fit2dspreadsheetimage

 1   
 2   
 3  """ 
 4  Read the fit2d ascii image output 
 5          + Jon Wright, ESRF 
 6  """ 
 7   
 8  import numpy 
 9   
10  from fabioimage import fabioimage 
11   
12   
13   
14   
15 -class fit2dspreadsheetimage(fabioimage):
16 """ 17 Read a fit2d ascii format 18 """ 19
20 - def _readheader(self, infile):
21 """ 22 23 TODO : test for minimal attributes? 24 """ 25 line = infile.readline() 26 try: 27 items = line.split() 28 xdim = int(items[0]) 29 ydim = int(items[1]) 30 except: 31 raise 32 self.header['title'] = line 33 self.header['Dim_1'] = xdim 34 self.header['Dim_2'] = ydim
35
36 - def read(self, fname):
37 """ 38 Read in header into self.header and 39 the data into self.data 40 """ 41 self.header = {} 42 self.resetvals() 43 infile = self._open(fname) 44 self._readheader(infile) 45 # Compute image size 46 try: 47 self.dim1 = int(self.header['Dim_1']) 48 self.dim2 = int(self.header['Dim_2']) 49 except: 50 raise Exception("file", str(fname) + \ 51 "is corrupt, cannot read it") 52 bytecode = numpy.float32 53 54 self.bpp = len(numpy.array(0, bytecode).tostring()) 55 56 #now read the data into the array 57 try: 58 vals = [] 59 for line in infile.readlines(): 60 try: 61 vals.append([float(x) for x in line.split()]) 62 except: 63 pass 64 self.data = numpy.array(vals).astype(bytecode) 65 assert self.data.shape == (self.dim2, self.dim1) 66 67 except: 68 raise IOError, "Error reading ascii" 69 70 self.resetvals() 71 # ensure the PIL image is reset 72 self.pilimage = None 73 return self
74 75 76 if __name__ == "__main__": 77 import sys, time 78 start = time.time() 79 img = fit2dspreadsheetimage() 80 img.read(sys.argv[1]) 81 print time.time() - start 82 print img.dim1, img.dim2, img.data.shape 83 from matplotlib.pylab import imshow, show 84 imshow(img.data.T) 85 show() 86