[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 |
---|
[daa56d0] | 15 | import numpy |
---|
[ad8034f] | 16 | from sans.dataloader.data_info import Data2D |
---|
[11a0319] | 17 | |
---|
[7d6351e] | 18 | |
---|
[bb03739] | 19 | class 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 |
---|
[daa56d0] | 40 | except: |
---|
[a7a5886] | 41 | msg = "tiff_reader: could not load file. Missing Image module." |
---|
| 42 | raise RuntimeError, msg |
---|
[11a0319] | 43 | |
---|
[daa56d0] | 44 | # Instantiate data object |
---|
[9d4e502] | 45 | output = Data2D() |
---|
[daa56d0] | 46 | output.filename = os.path.basename(filename) |
---|
[11a0319] | 47 | |
---|
[daa56d0] | 48 | # Read in the image |
---|
| 49 | try: |
---|
| 50 | im = Image.open(filename) |
---|
[7d6351e] | 51 | except: |
---|
| 52 | raise RuntimeError, "cannot open %s"%(filename) |
---|
[28caa03] | 53 | data = im.getdata() |
---|
[872785f] | 54 | |
---|
[daa56d0] | 55 | # Initiazed the output data object |
---|
[7d6351e] | 56 | output.data = numpy.zeros([im.size[0], im.size[1]]) |
---|
| 57 | output.err_data = numpy.zeros([im.size[0], im.size[1]]) |
---|
[28caa03] | 58 | |
---|
[7d6351e] | 59 | # Initialize |
---|
[daa56d0] | 60 | x_vals = [] |
---|
[7d6351e] | 61 | y_vals = [] |
---|
[daa56d0] | 62 | |
---|
| 63 | # x and y vectors |
---|
[28caa03] | 64 | for i_x in range(im.size[0]): |
---|
[daa56d0] | 65 | x_vals.append(i_x) |
---|
[11a0319] | 66 | |
---|
[28caa03] | 67 | itot = 0 |
---|
| 68 | for i_y in range(im.size[1]): |
---|
[daa56d0] | 69 | y_vals.append(i_y) |
---|
[28caa03] | 70 | |
---|
| 71 | for val in data: |
---|
| 72 | try: |
---|
| 73 | value = float(val) |
---|
| 74 | except: |
---|
| 75 | logging.error("tiff_reader: had to skip a non-float point") |
---|
| 76 | continue |
---|
[8d6440f] | 77 | |
---|
[28caa03] | 78 | # Get bin number |
---|
[7d6351e] | 79 | if math.fmod(itot, im.size[0]) == 0: |
---|
[28caa03] | 80 | i_x = 0 |
---|
| 81 | i_y += 1 |
---|
| 82 | else: |
---|
| 83 | i_x += 1 |
---|
[daa56d0] | 84 | |
---|
[7d6351e] | 85 | output.data[im.size[1] - 1 - i_y][i_x] = value |
---|
[28caa03] | 86 | |
---|
| 87 | itot += 1 |
---|
| 88 | |
---|
[7d6351e] | 89 | output.xbins = im.size[0] |
---|
| 90 | output.ybins = im.size[1] |
---|
| 91 | output.x_bins = x_vals |
---|
| 92 | output.y_bins = y_vals |
---|
| 93 | output.xmin = 0 |
---|
| 94 | output.xmax = im.size[0] - 1 |
---|
| 95 | output.ymin = 0 |
---|
| 96 | output.ymax = im.size[0] - 1 |
---|
[daa56d0] | 97 | |
---|
[fe78c7b] | 98 | # Store loading process information |
---|
[7d6351e] | 99 | output.meta_data['loader'] = self.type_name |
---|
[daa56d0] | 100 | return output |
---|