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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 59dfb53 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
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 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']
33
34    def read(self, filename=None):
35        """
36        Open and read the data in a file
37
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."
46            raise RuntimeError(msg)
47
48        # Instantiate data object
49        output = Data2D()
50        output.filename = os.path.basename(filename)
51
52        # Read in the image
53        try:
54            im = Image.open(filename)
55        except:
56            raise  RuntimeError("cannot open %s"%(filename))
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)
63
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)
71
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
82
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
89
90            output.data[im.size[1] - 1 - i_y][i_x] = value
91
92            itot += 1
93
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
104
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.