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

Last change on this file since 0765c00 was 9a5097c, checked in by andyfaff, 8 years ago

MAINT: import numpy as np

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