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) |
---|
12 | import math |
---|
13 | import logging |
---|
14 | import os |
---|
15 | import numpy |
---|
16 | from sans.dataloader.data_info import Data2D |
---|
17 | |
---|
18 | |
---|
19 | class Reader: |
---|
20 | """ |
---|
21 | Example data manipulation |
---|
22 | """ |
---|
23 | ## File type |
---|
24 | type_name = "TIF" |
---|
25 | ## Wildcards |
---|
26 | type = ["TIF files (*.tif)|*.tif", |
---|
27 | "TIFF files (*.tiff)|*.tiff", |
---|
28 | ] |
---|
29 | ## Extension |
---|
30 | ext = ['.tif', '.tiff'] |
---|
31 | |
---|
32 | def read(self, filename=None): |
---|
33 | """ |
---|
34 | Open and read the data in a file |
---|
35 | |
---|
36 | :param file: path of the file |
---|
37 | """ |
---|
38 | try: |
---|
39 | import Image |
---|
40 | except: |
---|
41 | msg = "tiff_reader: could not load file. Missing Image module." |
---|
42 | raise RuntimeError, msg |
---|
43 | |
---|
44 | # Instantiate data object |
---|
45 | output = Data2D() |
---|
46 | output.filename = os.path.basename(filename) |
---|
47 | |
---|
48 | # Read in the image |
---|
49 | try: |
---|
50 | im = Image.open(filename) |
---|
51 | except: |
---|
52 | raise RuntimeError, "cannot open %s"%(filename) |
---|
53 | data = im.getdata() |
---|
54 | |
---|
55 | # Initiazed the output data object |
---|
56 | output.data = numpy.zeros([im.size[0], im.size[1]]) |
---|
57 | output.err_data = numpy.zeros([im.size[0], im.size[1]]) |
---|
58 | |
---|
59 | # Initialize |
---|
60 | x_vals = [] |
---|
61 | y_vals = [] |
---|
62 | |
---|
63 | # x and y vectors |
---|
64 | for i_x in range(im.size[0]): |
---|
65 | x_vals.append(i_x) |
---|
66 | |
---|
67 | itot = 0 |
---|
68 | for i_y in range(im.size[1]): |
---|
69 | y_vals.append(i_y) |
---|
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 |
---|
77 | |
---|
78 | # Get bin number |
---|
79 | if math.fmod(itot, im.size[0]) == 0: |
---|
80 | i_x = 0 |
---|
81 | i_y += 1 |
---|
82 | else: |
---|
83 | i_x += 1 |
---|
84 | |
---|
85 | output.data[im.size[1] - 1 - i_y][i_x] = value |
---|
86 | |
---|
87 | itot += 1 |
---|
88 | |
---|
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 |
---|
97 | |
---|
98 | # Store loading process information |
---|
99 | output.meta_data['loader'] = self.type_name |
---|
100 | return output |
---|