Changeset daa56d0 in sasview for DataLoader/readers
- Timestamp:
- Sep 3, 2008 9:47:12 AM (16 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- d831626
- Parents:
- e390933
- Location:
- DataLoader/readers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/readers/ascii_reader.py
r99d1af6 rdaa56d0 13 13 from DataLoader.data_info import Data1D 14 14 15 # Check whether we have a converter available 15 16 has_converter = True 16 17 try: … … 116 117 # Sanity check 117 118 if has_error == True and not len(y) == len(dy): 118 raise ValueError, "ascii_reader: y and dy have different length"119 raise RuntimeError, "ascii_reader: y and dy have different length" 119 120 120 121 # If the data length is zero, consider this as 121 122 # though we were not able to read the file. 122 123 if len(x)==0: 123 raise ValueError, "ascii_reader: could not load file"124 raise RuntimeError, "ascii_reader: could not load file" 124 125 125 126 output.x = x -
DataLoader/readers/tiff_reader.py
r8bd8ea4 rdaa56d0 1 """ 2 Image reader. Untested. 3 """ 1 4 2 import math,logging 5 """ 6 This software was developed by the University of Tennessee as part of the 7 Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 8 project funded by the US National Science Foundation. 3 9 10 See the license text in license.txt 11 12 copyright 2008, University of Tennessee 13 """ 14 #TODO: load and check data and orientation of the image (needs rendering) 15 16 import math, logging, os 17 import numpy 18 from DataLoader.data_info import Data2D 4 19 5 20 class Reader: … … 8 23 """ 9 24 ## File type 10 type = [] 25 type = ["TIF files (*.tif)|*.tif", 26 "JPG files (*.jpg)|*.jpg", 27 "JPEG files (*.jpeg)|*.jpeg", 28 "PNG files (*.png)|*.png", 29 "TIFF files (*.tiff)|*.tiff", 30 "BMP files (*.bmp)|*.bmp", 31 "GIF files (*.gif)|*.gif", 32 ] 11 33 ## Extension 12 ext = [' tif', 'jpg', '.png', 'jpeg', '.tiff', 'gif', 'bmp']34 ext = ['.tif', '.jpg', '.png', '.jpeg', '.tiff', '.gif', '.bmp'] 13 35 14 36 def read(self, filename=None): 15 import Image16 print "in tiff"17 37 """ 18 38 Open and read the data in a file 19 39 @param file: path of the file 20 40 """ 41 try: 42 import Image 43 except: 44 raise RuntimeError, "tiff_reader: could not load file. Missing Image module." 21 45 22 read_it = False 23 for item in self.ext: 24 if filename.lower().find(item)>=0: 25 read_it = True 46 # Instantiate data object 47 output = Data2D() 48 output.filename = os.path.basename(filename) 49 50 # Read in the image 51 try: 52 im = Image.open(filename) 53 except : 54 raise RuntimeError,"cannot open %s"%(filename) 55 data = im.getdata() 56 57 # Initiazed the output data object 58 output.data = numpy.zeros([im.size[0],im.size[1]]) 59 60 # Initialize 61 x_vals = [] 62 y_vals = [] 63 64 # x and y vectors 65 for i_x in range(im.size[0]): 66 x_vals.append(i_x) 67 68 itot = 0 69 for i_y in range(im.size[1]): 70 y_vals.append(i_y) 71 72 for val in data: 73 try: 74 value = float(val) 75 except: 76 logging.error("tiff_reader: had to skip a non-float point") 77 continue 78 79 # Get bin number 80 if math.fmod(itot, im.size[0])==0: 81 i_x = 0 82 i_y += 1 83 else: 84 i_x += 1 26 85 27 if read_it: 28 import Image 29 import pylab 30 import copy 31 import numpy 86 output.data[im.size[1]-1-i_y][i_x] = value 32 87 33 wavelength, distance, center_x, center_y, pixel = 10.0, 11.0, 65, 65, 1.0 34 35 36 37 # Initialize 38 x_vals = [] 39 y_vals = [] 40 ymin = None 41 ymax = None 42 xmin = None 43 xmax = None 44 Z = None 88 itot += 1 89 90 output.xbins = im.size[0] 91 output.ybins = im.size[1] 92 output.x_bins = x_vals 93 output.y_bins = y_vals 94 output.xmin = 0 95 output.xmax = im.size[0]-1 96 output.ymin = 0 97 output.ymax = im.size[0]-1 45 98 46 try: 47 im = Image.open(filename) 48 except : 49 raise RuntimeError,"cannot open %s"%(filename) 50 # Detector size should be 128x128, the file is 190x190 51 print "-> Image size:", im.size, im.format, im.mode 52 data = im.getdata() 53 x = numpy.zeros(im.size[0]) 54 y = numpy.zeros(im.size[1]) 55 X, Y = pylab.meshgrid(x, y) 56 Z = copy.deepcopy(X) 57 itot = 0 58 i_x = 0 59 i_y = 0 60 61 # Qx and Qy vectors 62 for i_x in range(im.size[0]): 63 theta = (i_x-center_x+1)*pixel / distance / 100.0 64 qx = 4.0*math.pi/wavelength * math.sin(theta/2.0) 65 x_vals.append(qx) 66 if xmin==None or qx<xmin: 67 xmin = qx 68 if xmax==None or qx>xmax: 69 xmax = qx 70 71 ymin = None 72 ymax = None 73 for i_y in range(im.size[1]): 74 theta = (i_y-center_y+1)*pixel / distance / 100.0 75 qy = 4.0*math.pi/wavelength * math.sin(theta/2.0) 76 y_vals.append(qy) 77 if ymin==None or qy<ymin: 78 ymin = qy 79 if ymax==None or qy>ymax: 80 ymax = qy 81 82 for val in data: 83 84 try: 85 value = float(val) 86 except: 87 continue 88 89 # Get bin number 90 if math.fmod(itot, im.size[0])==0: 91 i_x = 0 92 i_y += 1 93 else: 94 i_x += 1 95 96 Z[im.size[1]-1-i_y][i_x] = value 97 98 itot += 1 99 from readInfo import ReaderInfo 100 output = ReaderInfo() 101 output.wavelength = wavelength 102 output.xbins = im.size[0] 103 output.ybins = im.size[1] 104 output.center_x = center_x 105 output.center_y = center_y 106 output.distance = distance 107 output.x_vals = x_vals 108 output.y_vals = y_vals 109 output.xmin = xmin 110 output.xmax = xmax 111 output.ymin = ymin 112 output.ymax = ymax 113 output.image = Z 114 output.pixel_size = pixel 115 output.x = x_vals 116 output.y = y_vals 117 output.type ="2D" 118 logging.info("tiff_reader reading %s"%(filename)) 119 120 return output 99 return output 121 100 122 return None123 101 124 102
Note: See TracChangeset
for help on using the changeset viewer.