[daa56d0] | 1 | """ |
---|
| 2 | Image reader. Untested. |
---|
| 3 | """ |
---|
[11a0319] | 4 | |
---|
[daa56d0] | 5 | """ |
---|
| 6 | This software was developed by the University of Tennessee as part of the |
---|
| 7 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 8 | project funded by the US National Science Foundation. |
---|
[16d8e5f] | 9 | |
---|
[daa56d0] | 10 | See the license text in license.txt |
---|
| 11 | |
---|
| 12 | copyright 2008, University of Tennessee |
---|
| 13 | """ |
---|
| 14 | #TODO: load and check data and orientation of the image (needs rendering) |
---|
| 15 | |
---|
| 16 | import math, logging, os |
---|
| 17 | import numpy |
---|
[9d4e502] | 18 | from DataLoader.data_info import Data2D |
---|
[11a0319] | 19 | |
---|
[bb03739] | 20 | class Reader: |
---|
[11a0319] | 21 | """ |
---|
| 22 | Example data manipulation |
---|
| 23 | """ |
---|
| 24 | ## File type |
---|
[28caa03] | 25 | type_name = "TIF" |
---|
| 26 | ## Wildcards |
---|
[daa56d0] | 27 | type = ["TIF files (*.tif)|*.tif", |
---|
| 28 | "TIFF files (*.tiff)|*.tiff", |
---|
| 29 | ] |
---|
[11a0319] | 30 | ## Extension |
---|
[28caa03] | 31 | ext = ['.tif', '.tiff'] |
---|
[11a0319] | 32 | |
---|
| 33 | def read(self, filename=None): |
---|
| 34 | """ |
---|
| 35 | Open and read the data in a file |
---|
| 36 | @param file: path of the file |
---|
| 37 | """ |
---|
[daa56d0] | 38 | try: |
---|
[11a0319] | 39 | import Image |
---|
[daa56d0] | 40 | except: |
---|
| 41 | raise RuntimeError, "tiff_reader: could not load file. Missing Image module." |
---|
[11a0319] | 42 | |
---|
[daa56d0] | 43 | # Instantiate data object |
---|
[9d4e502] | 44 | output = Data2D() |
---|
[daa56d0] | 45 | output.filename = os.path.basename(filename) |
---|
[11a0319] | 46 | |
---|
[daa56d0] | 47 | # Read in the image |
---|
| 48 | try: |
---|
| 49 | im = Image.open(filename) |
---|
| 50 | except : |
---|
| 51 | raise RuntimeError,"cannot open %s"%(filename) |
---|
[28caa03] | 52 | data = im.getdata() |
---|
[872785f] | 53 | |
---|
[daa56d0] | 54 | # Initiazed the output data object |
---|
[28caa03] | 55 | output.data = numpy.zeros([im.size[0],im.size[1]]) |
---|
| 56 | output.err_data = numpy.zeros([im.size[0],im.size[1]]) |
---|
| 57 | |
---|
[daa56d0] | 58 | # Initialize |
---|
| 59 | x_vals = [] |
---|
| 60 | y_vals = [] |
---|
| 61 | |
---|
| 62 | # x and y vectors |
---|
[28caa03] | 63 | for i_x in range(im.size[0]): |
---|
[daa56d0] | 64 | x_vals.append(i_x) |
---|
[11a0319] | 65 | |
---|
[28caa03] | 66 | itot = 0 |
---|
| 67 | for i_y in range(im.size[1]): |
---|
[daa56d0] | 68 | y_vals.append(i_y) |
---|
[28caa03] | 69 | |
---|
| 70 | for val in data: |
---|
| 71 | try: |
---|
| 72 | value = float(val) |
---|
| 73 | except: |
---|
| 74 | logging.error("tiff_reader: had to skip a non-float point") |
---|
| 75 | continue |
---|
[8d6440f] | 76 | |
---|
[28caa03] | 77 | # Get bin number |
---|
| 78 | if math.fmod(itot, im.size[0])==0: |
---|
| 79 | i_x = 0 |
---|
| 80 | i_y += 1 |
---|
| 81 | else: |
---|
| 82 | i_x += 1 |
---|
[daa56d0] | 83 | |
---|
[28caa03] | 84 | output.data[im.size[1]-1-i_y][i_x] = value |
---|
| 85 | |
---|
| 86 | itot += 1 |
---|
| 87 | |
---|
| 88 | output.xbins = im.size[0] |
---|
| 89 | output.ybins = im.size[1] |
---|
[daa56d0] | 90 | output.x_bins = x_vals |
---|
| 91 | output.y_bins = y_vals |
---|
| 92 | output.xmin = 0 |
---|
[28caa03] | 93 | output.xmax = im.size[0]-1 |
---|
[daa56d0] | 94 | output.ymin = 0 |
---|
[28caa03] | 95 | output.ymax = im.size[0]-1 |
---|
[daa56d0] | 96 | |
---|
[fe78c7b] | 97 | # Store loading process information |
---|
| 98 | output.meta_data['loader'] = self.type_name |
---|
[daa56d0] | 99 | return output |
---|
[11a0319] | 100 | |
---|
[28caa03] | 101 | |
---|
| 102 | if __name__ == "__main__": |
---|
| 103 | reader = Reader() |
---|
| 104 | print reader.read("../test/MP_New.sans") |
---|
| 105 | |
---|
| 106 | |
---|