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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 7fb471d was 574adc7, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

convert sascalc to python 2/3 syntax

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[959eb01]1#####################################################################
2#This software was developed by the University of Tennessee as part of the
3#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
[574adc7]4#project funded by the US National Science Foundation.
[959eb01]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 as np
16from sas.sascalc.dataloader.data_info import Data2D
17from sas.sascalc.dataloader.manipulations import reader2D_converter
18
19logger = logging.getLogger(__name__)
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']
[574adc7]33
[959eb01]34    def read(self, filename=None):
35        """
36        Open and read the data in a file
[574adc7]37
[959eb01]38        :param file: path of the file
39        """
40        try:
41            import Image
42            import TiffImagePlugin
43            Image._initialized=2
44        except:
45            msg = "tiff_reader: could not load file. Missing Image module."
[574adc7]46            raise RuntimeError(msg)
47
[959eb01]48        # Instantiate data object
49        output = Data2D()
50        output.filename = os.path.basename(filename)
[574adc7]51
[959eb01]52        # Read in the image
53        try:
54            im = Image.open(filename)
55        except:
[574adc7]56            raise  RuntimeError("cannot open %s"%(filename))
[959eb01]57        data = im.getdata()
58
59        # Initiazed the output data object
60        output.data = np.zeros([im.size[0], im.size[1]])
61        output.err_data = np.zeros([im.size[0], im.size[1]])
62        output.mask = np.ones([im.size[0], im.size[1]], dtype=bool)
[574adc7]63
[959eb01]64        # Initialize
65        x_vals = []
66        y_vals = []
67
68        # x and y vectors
69        for i_x in range(im.size[0]):
70            x_vals.append(i_x)
[574adc7]71
[959eb01]72        itot = 0
73        for i_y in range(im.size[1]):
74            y_vals.append(i_y)
75
76        for val in data:
77            try:
78                value = float(val)
79            except:
80                logger.error("tiff_reader: had to skip a non-float point")
81                continue
[574adc7]82
[959eb01]83            # Get bin number
84            if math.fmod(itot, im.size[0]) == 0:
85                i_x = 0
86                i_y += 1
87            else:
88                i_x += 1
[574adc7]89
[959eb01]90            output.data[im.size[1] - 1 - i_y][i_x] = value
[574adc7]91
[959eb01]92            itot += 1
[574adc7]93
[959eb01]94        output.xbins = im.size[0]
95        output.ybins = im.size[1]
96        output.x_bins = x_vals
97        output.y_bins = y_vals
98        output.qx_data = np.array(x_vals)
99        output.qy_data = np.array(y_vals)
100        output.xmin = 0
101        output.xmax = im.size[0] - 1
102        output.ymin = 0
103        output.ymax = im.size[0] - 1
[574adc7]104
[959eb01]105        # Store loading process information
106        output.meta_data['loader'] = self.type_name
107        output = reader2D_converter(output)
108        return output
Note: See TracBrowser for help on using the repository browser.