[1b0b3ca] | 1 | # class Loader to load any king of file |
---|
| 2 | import wx |
---|
| 3 | import string,numpy |
---|
[8d6440f] | 4 | import logging |
---|
[16d8e5f] | 5 | class ReaderInfo: |
---|
| 6 | """ |
---|
| 7 | This class is a container of data. It stores values read from files |
---|
| 8 | """ |
---|
| 9 | ## Wavelength [A] |
---|
| 10 | wavelength = 0.0 |
---|
| 11 | ## Number of x bins |
---|
| 12 | xbins = 128 |
---|
| 13 | ## Number of y bins |
---|
| 14 | ybins = 128 |
---|
| 15 | ## Beam center X [pixel number] |
---|
| 16 | center_x = 65 |
---|
| 17 | ## Beam center Y [pixel number] |
---|
| 18 | center_y = 65 |
---|
| 19 | ## Distance from sample to detector [m] |
---|
| 20 | distance = 11.0 |
---|
| 21 | ## Qx values [A-1] |
---|
| 22 | x_vals = [] |
---|
| 23 | ## Qy xalues [A-1] |
---|
| 24 | y_vals = [] |
---|
| 25 | ## Qx min |
---|
| 26 | xmin = 0.0 |
---|
| 27 | ## Qx max |
---|
| 28 | xmax = 1.0 |
---|
| 29 | ## Qy min |
---|
| 30 | ymin = 0.0 |
---|
| 31 | ## Qy max |
---|
| 32 | ymax = 1.0 |
---|
| 33 | ## Image |
---|
| 34 | image = None |
---|
| 35 | ## Pixel size |
---|
| 36 | pixel_size = 0.5 |
---|
| 37 | ## x coordinate |
---|
| 38 | x=[] |
---|
| 39 | ##y coordinate |
---|
| 40 | y=[] |
---|
| 41 | ##dx error on y |
---|
| 42 | dx=None |
---|
| 43 | ## dy error on y |
---|
| 44 | dy=None |
---|
| 45 | ## Error on each pixel |
---|
| 46 | error = None |
---|
| 47 | ##type of data |
---|
| 48 | type="" |
---|
| 49 | ##z |
---|
| 50 | Z=None |
---|
[1b0b3ca] | 51 | class Reader: |
---|
| 52 | """ |
---|
| 53 | This class is loading values from given file or value giving by the user |
---|
| 54 | """ |
---|
| 55 | ext=['.txt','.dat'] |
---|
| 56 | def _init_(self,filename=None,x=None,y=None,dx=None,dy=None): |
---|
| 57 | # variable to store loaded values |
---|
| 58 | self.x = x |
---|
| 59 | self.y = y |
---|
| 60 | self.dx = dx |
---|
| 61 | self.dy = dy |
---|
| 62 | self.filename = filename |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | def read(self,path, format=None): |
---|
| 66 | """ Store the values loaded from file in local variables |
---|
| 67 | can read 3 columns of data |
---|
| 68 | """ |
---|
| 69 | |
---|
| 70 | if not path == None: |
---|
| 71 | read_it = False |
---|
| 72 | |
---|
| 73 | for item in self.ext: |
---|
| 74 | if path.lower().find(item)>=0: |
---|
| 75 | read_it = True |
---|
| 76 | #print "this is the flag",read_it, path.lower() |
---|
| 77 | if read_it==False: |
---|
| 78 | return None |
---|
| 79 | else: |
---|
[8d6440f] | 80 | try: |
---|
| 81 | input_f = open(path,'r') |
---|
| 82 | except : |
---|
| 83 | raise RuntimeError,"TXT3_Reader cannot open %s"%(path) |
---|
[1b0b3ca] | 84 | buff = input_f.read() |
---|
| 85 | lines = buff.split('\n') |
---|
| 86 | self.x=[] |
---|
| 87 | self.y=[] |
---|
| 88 | self.dx = [] |
---|
| 89 | self.dy=[] |
---|
| 90 | for line in lines: |
---|
| 91 | toks = line.split() |
---|
| 92 | try: |
---|
| 93 | x = float(toks[0]) |
---|
| 94 | y = float(toks[1]) |
---|
| 95 | dy = float(toks[2]) |
---|
| 96 | |
---|
| 97 | self.x.append(x) |
---|
| 98 | self.y.append(y) |
---|
| 99 | self.dy.append(dy) |
---|
| 100 | |
---|
| 101 | except: |
---|
| 102 | pass |
---|
| 103 | #print "READ ERROR", line |
---|
| 104 | |
---|
| 105 | self.dx = numpy.zeros(len(self.x)) |
---|
| 106 | # Sanity check |
---|
| 107 | if not len(self.x) == len(self.dx): |
---|
| 108 | raise ValueError, "x and dx have different length" |
---|
| 109 | if not len(self.y) == len(self.dy): |
---|
| 110 | raise ValueError, "y and dy have different length" |
---|
| 111 | |
---|
| 112 | if (self.x==[] or self.y==[])and (self.dy==[]): |
---|
| 113 | raise ValueError, "TXT3_Reader can't read %s"%path |
---|
| 114 | else: |
---|
[16d8e5f] | 115 | |
---|
[8d6440f] | 116 | logging.info ("TXT3_Reader reading %s \n" %(path)) |
---|
[16d8e5f] | 117 | |
---|
| 118 | output = ReaderInfo() |
---|
| 119 | output.x = self.x |
---|
| 120 | output.y = self.y |
---|
| 121 | output.dy = self.dy |
---|
| 122 | output.type = "1D" |
---|
| 123 | |
---|
| 124 | return output |
---|
[1b0b3ca] | 125 | |
---|
| 126 | return None |
---|
| 127 | if __name__ == "__main__": |
---|
| 128 | read= Reader() |
---|
| 129 | #read= Reader(filename="testdata_line.txt") |
---|
| 130 | print read.load("test.dat") |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | |
---|