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

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 f72333f was fe78c7b, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

DataLoader?: exception no longer raised when units are wrong for an entry; the entry is not loaded and an error is logged instead. Loader info is now stored.

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