[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 |
---|
[b699768] | 10 | from sas.sascalc.dataloader.data_info import SESANSData1D |
---|
[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" |
---|
| 86 | output = SESANSData1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam) |
---|
| 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: |
---|
| 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 | else: |
---|
| 130 | continue |
---|
[26d4864] | 131 | |
---|
[5e326a6] | 132 | x=[] |
---|
| 133 | y=[] |
---|
| 134 | lam=[] |
---|
| 135 | dx=[] |
---|
| 136 | dy=[] |
---|
| 137 | dlam=[] |
---|
[26d4864] | 138 | lam_header = lamvals[0].split() |
---|
| 139 | data_conv_z = None |
---|
| 140 | default_z_unit = "A" |
---|
| 141 | data_conv_P = None |
---|
| 142 | default_p_unit = " " |
---|
| 143 | lam_unit = lam_header[1].replace("[","").replace("]","") |
---|
| 144 | for i,x_val in output.x: |
---|
| 145 | output.x[i], output.x_unit = self._unit_conversion(x_val, lam_unit, default_z_unit) |
---|
| 146 | for i,y_val in output.y: |
---|
| 147 | output.y[i], output.y_unit = self._unit_conversion(y_val, " ", default_p_unit) |
---|
[5e326a6] | 148 | varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] |
---|
[26d4864] | 149 | valrange=range(1, len(zvals)) |
---|
[5e326a6] | 150 | for i in valrange: |
---|
[26d4864] | 151 | x.append(float(zvals[i])) |
---|
| 152 | y.append(float(Pvals[i])) |
---|
| 153 | lam.append(float(lamvals[i])) |
---|
| 154 | dy.append(float(dPvals[i])) |
---|
| 155 | dx.append(float(dzvals[i])) |
---|
| 156 | dlam.append(float(dlamvals[i])) |
---|
| 157 | |
---|
[5e326a6] | 158 | x,y,lam,dy,dx,dlam = [ |
---|
| 159 | numpy.asarray(v, 'double') |
---|
| 160 | for v in (x,y,lam,dy,dx,dlam) |
---|
| 161 | ] |
---|
| 162 | |
---|
| 163 | input_f.close() |
---|
[26d4864] | 164 | |
---|
[5e326a6] | 165 | #Data |
---|
| 166 | output.x = x #[x != 0] |
---|
| 167 | output.y = y #[x != 0] |
---|
| 168 | output.dy = dy |
---|
| 169 | output.dx = dx |
---|
| 170 | output.lam = lam |
---|
| 171 | output.dlam = dlam |
---|
| 172 | |
---|
[26d4864] | 173 | output.xaxis("\rm{z}", output.x_unit) |
---|
| 174 | output.yaxis("\\rm{P/P0}", output.y_unit) |
---|
[5e326a6] | 175 | # Store loading process information |
---|
| 176 | output.meta_data['loader'] = self.type_name |
---|
| 177 | output.sample.thickness = float(paramvals[6]) |
---|
| 178 | output.sample.name = paramvals[1] |
---|
| 179 | output.sample.ID = paramvals[0] |
---|
[3689302] | 180 | zaccept_unit_split = paramnames[7].split("[") |
---|
| 181 | zaccept_unit = zaccept_unit_split[1].replace("]","") |
---|
| 182 | if zaccept_unit.strip() == '\AA^-1': |
---|
| 183 | zaccept_unit = "1/A" |
---|
[5e326a6] | 184 | output.sample.zacceptance=float(paramvals[7]) |
---|
[3689302] | 185 | output.sample.zacceptance=self._unit_conversion(output.sample.zacceptance, |
---|
| 186 | zaccept_unit, "1/" + default_z_unit) |
---|
[edfc8ac] | 187 | output.vars=varheader |
---|
[5e326a6] | 188 | |
---|
| 189 | if len(output.x) < 1: |
---|
| 190 | raise RuntimeError, "%s is empty" % path |
---|
| 191 | return output |
---|
[26d4864] | 192 | |
---|
[5e326a6] | 193 | else: |
---|
| 194 | raise RuntimeError, "%s is not a file" % path |
---|
| 195 | return None |
---|
[26d4864] | 196 | |
---|
| 197 | def _unit_conversion(self, value, value_unit, default_unit): |
---|
| 198 | if has_converter == True and value_unit != default_unit: |
---|
| 199 | data_conv_q = Converter(value_unit) |
---|
| 200 | value = data_conv_q(value, units=default_unit) |
---|
| 201 | new_unit = default_unit |
---|
| 202 | else: |
---|
| 203 | new_unit = value_unit |
---|
| 204 | return value, new_unit |
---|