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