source: sasview/src/sas/sascalc/dataloader/readers/tiff_reader.py @ ef2cdb3

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.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since ef2cdb3 was b699768, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Initial commit of the refactored SasCalc? module.

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