[5e326a6] | 1 | """ |
---|
[edfc8ac] | 2 | SESANS reader (based on ASCII reader) |
---|
| 3 | |
---|
| 4 | Reader for .ses or .sesans file format |
---|
| 5 | |
---|
| 6 | Jurrian Bakker |
---|
[5e326a6] | 7 | """ |
---|
| 8 | import numpy |
---|
| 9 | import os |
---|
[1b82623] | 10 | from sas.sascalc.dataloader.data_info import Data1D |
---|
[5e326a6] | 11 | |
---|
| 12 | # Check whether we have a converter available |
---|
| 13 | has_converter = True |
---|
| 14 | try: |
---|
[b699768] | 15 | from sas.sascalc.data_util.nxsunit import Converter |
---|
[5e326a6] | 16 | except: |
---|
| 17 | has_converter = False |
---|
| 18 | _ZERO = 1e-16 |
---|
| 19 | |
---|
| 20 | class Reader: |
---|
| 21 | """ |
---|
| 22 | Class to load sesans files (6 columns). |
---|
| 23 | """ |
---|
| 24 | ## File type |
---|
| 25 | type_name = "SESANS" |
---|
| 26 | |
---|
| 27 | ## Wildcards |
---|
| 28 | type = ["SESANS files (*.ses)|*.ses", |
---|
| 29 | "SESANS files (*..sesans)|*.sesans"] |
---|
| 30 | ## List of allowed extensions |
---|
| 31 | ext = ['.ses', '.SES', '.sesans', '.SESANS'] |
---|
| 32 | |
---|
| 33 | ## Flag to bypass extension check |
---|
| 34 | allow_all = True |
---|
| 35 | |
---|
| 36 | def read(self, path): |
---|
| 37 | |
---|
| 38 | # print "reader triggered" |
---|
| 39 | |
---|
| 40 | """ |
---|
| 41 | Load data file |
---|
| 42 | |
---|
| 43 | :param path: file path |
---|
| 44 | |
---|
| 45 | :return: SESANSData1D object, or None |
---|
| 46 | |
---|
| 47 | :raise RuntimeError: when the file can't be opened |
---|
| 48 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
| 49 | """ |
---|
| 50 | if os.path.isfile(path): |
---|
| 51 | basename = os.path.basename(path) |
---|
| 52 | _, extension = os.path.splitext(basename) |
---|
| 53 | if self.allow_all or extension.lower() in self.ext: |
---|
| 54 | try: |
---|
| 55 | # Read in binary mode since GRASP frequently has no-ascii |
---|
| 56 | # characters that brakes the open operation |
---|
| 57 | input_f = open(path,'rb') |
---|
| 58 | except: |
---|
| 59 | raise RuntimeError, "sesans_reader: cannot open %s" % path |
---|
| 60 | buff = input_f.read() |
---|
| 61 | # print buff |
---|
| 62 | lines = buff.splitlines() |
---|
| 63 | # print lines |
---|
| 64 | #Jae could not find python universal line spliter: |
---|
| 65 | #keep the below for now |
---|
| 66 | # some ascii data has \r line separator, |
---|
| 67 | # try it when the data is on only one long line |
---|
| 68 | # if len(lines) < 2 : |
---|
| 69 | # lines = buff.split('\r') |
---|
| 70 | |
---|
| 71 | x = numpy.zeros(0) |
---|
| 72 | y = numpy.zeros(0) |
---|
| 73 | dy = numpy.zeros(0) |
---|
| 74 | lam = numpy.zeros(0) |
---|
| 75 | dlam = numpy.zeros(0) |
---|
| 76 | dx = numpy.zeros(0) |
---|
| 77 | |
---|
| 78 | #temp. space to sort data |
---|
| 79 | tx = numpy.zeros(0) |
---|
| 80 | ty = numpy.zeros(0) |
---|
| 81 | tdy = numpy.zeros(0) |
---|
| 82 | tlam = numpy.zeros(0) |
---|
| 83 | tdlam = numpy.zeros(0) |
---|
| 84 | tdx = numpy.zeros(0) |
---|
| 85 | # print "all good" |
---|
[1fac6c0] | 86 | output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, isSesans=True ) |
---|
[5e326a6] | 87 | # print output |
---|
| 88 | self.filename = output.filename = basename |
---|
[26d4864] | 89 | |
---|
[5e326a6] | 90 | # #Initialize counters for data lines and header lines. |
---|
| 91 | # is_data = False # Has more than 5 lines |
---|
| 92 | # # More than "5" lines of data is considered as actual |
---|
| 93 | # # data unless that is the only data |
---|
| 94 | # mum_data_lines = 5 |
---|
| 95 | # # To count # of current data candidate lines |
---|
| 96 | # i = -1 |
---|
| 97 | # # To count total # of previous data candidate lines |
---|
| 98 | # i1 = -1 |
---|
| 99 | # # To count # of header lines |
---|
| 100 | # j = -1 |
---|
| 101 | # # Helps to count # of header lines |
---|
| 102 | # j1 = -1 |
---|
| 103 | # #minimum required number of columns of data; ( <= 4). |
---|
| 104 | # lentoks = 2 |
---|
| 105 | paramnames=[] |
---|
| 106 | paramvals=[] |
---|
| 107 | zvals=[] |
---|
| 108 | dzvals=[] |
---|
| 109 | lamvals=[] |
---|
| 110 | dlamvals=[] |
---|
| 111 | Pvals=[] |
---|
| 112 | dPvals=[] |
---|
| 113 | # print x |
---|
| 114 | # print zvals |
---|
| 115 | for line in lines: |
---|
| 116 | # Initial try for CSV (split on ,) |
---|
| 117 | line=line.strip() |
---|
| 118 | toks = line.split('\t') |
---|
| 119 | if len(toks)==2: |
---|
| 120 | paramnames.append(toks[0]) |
---|
| 121 | paramvals.append(toks[1]) |
---|
| 122 | if len(toks)>5: |
---|
[1fac6c0] | 123 | #zvals.append(toks[0]) |
---|
| 124 | #dzvals.append(toks[1]) |
---|
| 125 | #lamvals.append(toks[2]) |
---|
| 126 | #dlamvals.append(toks[3]) |
---|
| 127 | #Pvals.append(toks[4]) |
---|
| 128 | #dPvals.append(toks[5]) |
---|
| 129 | |
---|
[5e326a6] | 130 | zvals.append(toks[0]) |
---|
[1fac6c0] | 131 | dzvals.append(toks[3]) |
---|
| 132 | lamvals.append(toks[4]) |
---|
| 133 | dlamvals.append(toks[5]) |
---|
| 134 | Pvals.append(toks[1]) |
---|
| 135 | dPvals.append(toks[2]) |
---|
[5e326a6] | 136 | else: |
---|
| 137 | continue |
---|
[26d4864] | 138 | |
---|
[5e326a6] | 139 | x=[] |
---|
| 140 | y=[] |
---|
| 141 | lam=[] |
---|
| 142 | dx=[] |
---|
| 143 | dy=[] |
---|
| 144 | dlam=[] |
---|
[26d4864] | 145 | lam_header = lamvals[0].split() |
---|
| 146 | data_conv_z = None |
---|
| 147 | default_z_unit = "A" |
---|
| 148 | data_conv_P = None |
---|
[1fac6c0] | 149 | default_p_unit = " " # Adjust unit for axis (L^-3) |
---|
[26d4864] | 150 | lam_unit = lam_header[1].replace("[","").replace("]","") |
---|
[1fac6c0] | 151 | if lam_unit == 'AA': |
---|
| 152 | lam_unit = 'A' |
---|
[5e326a6] | 153 | varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] |
---|
[26d4864] | 154 | valrange=range(1, len(zvals)) |
---|
[5e326a6] | 155 | for i in valrange: |
---|
[26d4864] | 156 | x.append(float(zvals[i])) |
---|
| 157 | y.append(float(Pvals[i])) |
---|
| 158 | lam.append(float(lamvals[i])) |
---|
| 159 | dy.append(float(dPvals[i])) |
---|
| 160 | dx.append(float(dzvals[i])) |
---|
| 161 | dlam.append(float(dlamvals[i])) |
---|
| 162 | |
---|
[5e326a6] | 163 | x,y,lam,dy,dx,dlam = [ |
---|
| 164 | numpy.asarray(v, 'double') |
---|
| 165 | for v in (x,y,lam,dy,dx,dlam) |
---|
| 166 | ] |
---|
| 167 | |
---|
| 168 | input_f.close() |
---|
[26d4864] | 169 | |
---|
[1c0e3b0] | 170 | output.x, output.x_unit = self._unit_conversion(x, lam_unit, default_z_unit) |
---|
| 171 | output.y = y |
---|
[392056d] | 172 | output.y_unit = '\AA^{-2} cm^{-1}' # output y_unit erbij |
---|
[1c0e3b0] | 173 | output.dx, output.dx_unit = self._unit_conversion(dx, lam_unit, default_z_unit) |
---|
[5e326a6] | 174 | output.dy = dy |
---|
[1c0e3b0] | 175 | output.lam, output.lam_unit = self._unit_conversion(lam, lam_unit, default_z_unit) |
---|
| 176 | output.dlam, output.dlam_unit = self._unit_conversion(dlam, lam_unit, default_z_unit) |
---|
[5e326a6] | 177 | |
---|
[1fac6c0] | 178 | output.xaxis("\\rm{z}", output.x_unit) |
---|
[392056d] | 179 | output.yaxis("\\rm{ln(P)/(t \lambda^2)}", output.y_unit) # Adjust label to ln P/(lam^2 t), remove lam column refs |
---|
[5e326a6] | 180 | # Store loading process information |
---|
| 181 | output.meta_data['loader'] = self.type_name |
---|
[1fac6c0] | 182 | #output.sample.thickness = float(paramvals[6]) |
---|
[5e326a6] | 183 | output.sample.name = paramvals[1] |
---|
| 184 | output.sample.ID = paramvals[0] |
---|
[3689302] | 185 | zaccept_unit_split = paramnames[7].split("[") |
---|
| 186 | zaccept_unit = zaccept_unit_split[1].replace("]","") |
---|
[1fac6c0] | 187 | if zaccept_unit.strip() == '\AA^-1' or zaccept_unit.strip() == '\A^-1': |
---|
[3689302] | 188 | zaccept_unit = "1/A" |
---|
[1c0e3b0] | 189 | output.sample.zacceptance=(float(paramvals[7]),zaccept_unit) |
---|
[edfc8ac] | 190 | output.vars=varheader |
---|
[5e326a6] | 191 | |
---|
| 192 | if len(output.x) < 1: |
---|
| 193 | raise RuntimeError, "%s is empty" % path |
---|
| 194 | return output |
---|
[26d4864] | 195 | |
---|
[5e326a6] | 196 | else: |
---|
| 197 | raise RuntimeError, "%s is not a file" % path |
---|
| 198 | return None |
---|
[26d4864] | 199 | |
---|
| 200 | def _unit_conversion(self, value, value_unit, default_unit): |
---|
| 201 | if has_converter == True and value_unit != default_unit: |
---|
| 202 | data_conv_q = Converter(value_unit) |
---|
| 203 | value = data_conv_q(value, units=default_unit) |
---|
| 204 | new_unit = default_unit |
---|
| 205 | else: |
---|
| 206 | new_unit = value_unit |
---|
| 207 | return value, new_unit |
---|