source: sasview/DataLoader/readers/tiff_reader.py @ 1c95d15

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 1c95d15 was a7a5886, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working pylint

  • Property mode set to 100644
File size: 3.0 KB
Line 
1
2
3#####################################################################
4#This software was developed by the University of Tennessee as part of the
5#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
6#project funded by the US National Science Foundation.
7#See the license text in license.txt
8#copyright 2008, University of Tennessee
9######################################################################
10"""
11    Image reader. Untested.
12"""
13
14
15#TODO: load and check data and orientation of the image (needs rendering)
16
17import math, logging, os
18import numpy
19from DataLoader.data_info import Data2D
20   
21class Reader:
22    """
23    Example data manipulation
24    """
25    ## File type
26    type_name = "TIF"   
27    ## Wildcards
28    type = ["TIF files (*.tif)|*.tif",
29            "TIFF files (*.tiff)|*.tiff",
30            ]
31    ## Extension
32    ext  = ['.tif', '.tiff']   
33       
34    def read(self, filename=None):
35        """
36        Open and read the data in a file
37       
38        :param file: path of the file
39        """
40        try:
41            import Image
42        except:
43            msg = "tiff_reader: could not load file. Missing Image module."
44            raise RuntimeError, msg
45       
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        output.err_data = numpy.zeros([im.size[0],im.size[1]])
60       
61        # Initialize
62        x_vals = []
63        y_vals = [] 
64
65        # x and y vectors
66        for i_x in range(im.size[0]):
67            x_vals.append(i_x)
68           
69        itot = 0
70        for i_y in range(im.size[1]):
71            y_vals.append(i_y)
72
73        for val in data:
74            try:
75                value = float(val)
76            except:
77                logging.error("tiff_reader: had to skip a non-float point")
78                continue
79           
80            # Get bin number
81            if math.fmod(itot, im.size[0])==0:
82                i_x = 0
83                i_y += 1
84            else:
85                i_x += 1
86               
87            output.data[im.size[1]-1-i_y][i_x] = value
88           
89            itot += 1
90               
91        output.xbins      = im.size[0]
92        output.ybins      = im.size[1]
93        output.x_bins     = x_vals
94        output.y_bins     = y_vals
95        output.xmin       = 0
96        output.xmax       = im.size[0]-1
97        output.ymin       = 0
98        output.ymax       = im.size[0]-1
99       
100        # Store loading process information
101        output.meta_data['loader'] = self.type_name
102        return output
103       
104
105if __name__ == "__main__": 
106    reader = Reader()
107    print reader.read("../test/MP_New.sans")
108   
109
Note: See TracBrowser for help on using the repository browser.