[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] | 12 | import math |
---|
| 13 | import logging |
---|
| 14 | import os |
---|
[9a5097c] | 15 | import numpy as np |
---|
[b699768] | 16 | from sas.sascalc.dataloader.data_info import Data2D |
---|
| 17 | from sas.sascalc.dataloader.manipulations import reader2D_converter |
---|
[c155a16] | 18 | |
---|
[463e7ffc] | 19 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 20 | |
---|
[bb03739] | 21 | class 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 |
---|
[9a5097c] | 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) |
---|
[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 |
---|
[9a5097c] | 98 | output.qx_data = np.array(x_vals) |
---|
| 99 | output.qy_data = np.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 |
---|