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

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 463e7ffc was 463e7ffc, checked in by Ricardo Ferraz Leal <ricleal@…>, 7 years ago

getLogger with module name

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[0997158f]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######################################################################
[daa56d0]8"""
[7d6351e]9    Image reader. Untested.
[daa56d0]10"""
11#TODO: load and check data and orientation of the image (needs rendering)
[7d6351e]12import math
13import logging
14import os
[daa56d0]15import numpy
[b699768]16from sas.sascalc.dataloader.data_info import Data2D
17from sas.sascalc.dataloader.manipulations import reader2D_converter
[c155a16]18
[463e7ffc]19logger = logging.getLogger(__name__)
[c155a16]20
[bb03739]21class Reader:
[11a0319]22    """
[0997158f]23    Example data manipulation
[11a0319]24    """
25    ## File type
[7d6351e]26    type_name = "TIF"
[28caa03]27    ## Wildcards
[daa56d0]28    type = ["TIF files (*.tif)|*.tif",
29            "TIFF files (*.tiff)|*.tiff",
30            ]
[11a0319]31    ## Extension
[7d6351e]32    ext = ['.tif', '.tiff']
[11a0319]33       
34    def read(self, filename=None):
35        """
[0997158f]36        Open and read the data in a file
37       
38        :param file: path of the file
[11a0319]39        """
[daa56d0]40        try:
[11a0319]41            import Image
[a30864b1]42            import TiffImagePlugin
43            Image._initialized=2
[daa56d0]44        except:
[a7a5886]45            msg = "tiff_reader: could not load file. Missing Image module."
46            raise RuntimeError, msg
[11a0319]47       
[daa56d0]48        # Instantiate data object
[9d4e502]49        output = Data2D()
[daa56d0]50        output.filename = os.path.basename(filename)
[11a0319]51           
[daa56d0]52        # Read in the image
53        try:
54            im = Image.open(filename)
[7d6351e]55        except:
56            raise  RuntimeError, "cannot open %s"%(filename)
[28caa03]57        data = im.getdata()
[872785f]58
[daa56d0]59        # Initiazed the output data object
[7d6351e]60        output.data = numpy.zeros([im.size[0], im.size[1]])
61        output.err_data = numpy.zeros([im.size[0], im.size[1]])
[a30864b1]62        output.mask = numpy.ones([im.size[0], im.size[1]], dtype=bool)
[28caa03]63       
[7d6351e]64        # Initialize
[daa56d0]65        x_vals = []
[7d6351e]66        y_vals = []
[daa56d0]67
68        # x and y vectors
[28caa03]69        for i_x in range(im.size[0]):
[daa56d0]70            x_vals.append(i_x)
[11a0319]71           
[28caa03]72        itot = 0
73        for i_y in range(im.size[1]):
[daa56d0]74            y_vals.append(i_y)
[28caa03]75
76        for val in data:
77            try:
78                value = float(val)
79            except:
[c155a16]80                logger.error("tiff_reader: had to skip a non-float point")
[28caa03]81                continue
[8d6440f]82           
[28caa03]83            # Get bin number
[7d6351e]84            if math.fmod(itot, im.size[0]) == 0:
[28caa03]85                i_x = 0
86                i_y += 1
87            else:
88                i_x += 1
[daa56d0]89               
[7d6351e]90            output.data[im.size[1] - 1 - i_y][i_x] = value
[28caa03]91           
92            itot += 1
93               
[7d6351e]94        output.xbins = im.size[0]
95        output.ybins = im.size[1]
96        output.x_bins = x_vals
97        output.y_bins = y_vals
[a30864b1]98        output.qx_data = numpy.array(x_vals)
99        output.qy_data = numpy.array(y_vals)
[7d6351e]100        output.xmin = 0
101        output.xmax = im.size[0] - 1
102        output.ymin = 0
103        output.ymax = im.size[0] - 1
[daa56d0]104       
[fe78c7b]105        # Store loading process information
[7d6351e]106        output.meta_data['loader'] = self.type_name
[a30864b1]107        output = reader2D_converter(output)
[daa56d0]108        return output
Note: See TracBrowser for help on using the repository browser.