[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 |
---|
[b5db35d] | 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 | lines = buff.splitlines() |
---|
| 62 | x = numpy.zeros(0) |
---|
| 63 | y = numpy.zeros(0) |
---|
| 64 | dy = numpy.zeros(0) |
---|
| 65 | lam = numpy.zeros(0) |
---|
| 66 | dlam = numpy.zeros(0) |
---|
| 67 | dx = numpy.zeros(0) |
---|
| 68 | |
---|
| 69 | #temp. space to sort data |
---|
| 70 | tx = numpy.zeros(0) |
---|
| 71 | ty = numpy.zeros(0) |
---|
| 72 | tdy = numpy.zeros(0) |
---|
| 73 | tlam = numpy.zeros(0) |
---|
| 74 | tdlam = numpy.zeros(0) |
---|
| 75 | tdx = numpy.zeros(0) |
---|
[a9f579c] | 76 | output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, isSesans=True) |
---|
[5e326a6] | 77 | self.filename = output.filename = basename |
---|
[26d4864] | 78 | |
---|
[5e326a6] | 79 | paramnames=[] |
---|
| 80 | paramvals=[] |
---|
| 81 | zvals=[] |
---|
| 82 | dzvals=[] |
---|
| 83 | lamvals=[] |
---|
| 84 | dlamvals=[] |
---|
| 85 | Pvals=[] |
---|
| 86 | dPvals=[] |
---|
[a9f579c] | 87 | |
---|
[5e326a6] | 88 | for line in lines: |
---|
| 89 | # Initial try for CSV (split on ,) |
---|
| 90 | line=line.strip() |
---|
| 91 | toks = line.split('\t') |
---|
| 92 | if len(toks)==2: |
---|
| 93 | paramnames.append(toks[0]) |
---|
| 94 | paramvals.append(toks[1]) |
---|
| 95 | if len(toks)>5: |
---|
| 96 | zvals.append(toks[0]) |
---|
[a9f579c] | 97 | dzvals.append(toks[3]) |
---|
| 98 | lamvals.append(toks[4]) |
---|
| 99 | dlamvals.append(toks[5]) |
---|
| 100 | Pvals.append(toks[1]) |
---|
| 101 | dPvals.append(toks[2]) |
---|
[5e326a6] | 102 | else: |
---|
| 103 | continue |
---|
[26d4864] | 104 | |
---|
[5e326a6] | 105 | x=[] |
---|
| 106 | y=[] |
---|
| 107 | lam=[] |
---|
| 108 | dx=[] |
---|
| 109 | dy=[] |
---|
| 110 | dlam=[] |
---|
[26d4864] | 111 | lam_header = lamvals[0].split() |
---|
| 112 | data_conv_z = None |
---|
| 113 | default_z_unit = "A" |
---|
| 114 | data_conv_P = None |
---|
[a9f579c] | 115 | default_p_unit = " " # Adjust unit for axis (L^-3) |
---|
[26d4864] | 116 | lam_unit = lam_header[1].replace("[","").replace("]","") |
---|
[a9f579c] | 117 | if lam_unit == 'AA': |
---|
| 118 | lam_unit = 'A' |
---|
[5e326a6] | 119 | varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] |
---|
[26d4864] | 120 | valrange=range(1, len(zvals)) |
---|
[5e326a6] | 121 | for i in valrange: |
---|
[26d4864] | 122 | x.append(float(zvals[i])) |
---|
| 123 | y.append(float(Pvals[i])) |
---|
| 124 | lam.append(float(lamvals[i])) |
---|
| 125 | dy.append(float(dPvals[i])) |
---|
| 126 | dx.append(float(dzvals[i])) |
---|
| 127 | dlam.append(float(dlamvals[i])) |
---|
| 128 | |
---|
[5e326a6] | 129 | x,y,lam,dy,dx,dlam = [ |
---|
| 130 | numpy.asarray(v, 'double') |
---|
| 131 | for v in (x,y,lam,dy,dx,dlam) |
---|
| 132 | ] |
---|
| 133 | |
---|
| 134 | input_f.close() |
---|
[26d4864] | 135 | |
---|
[1c0e3b0] | 136 | output.x, output.x_unit = self._unit_conversion(x, lam_unit, default_z_unit) |
---|
| 137 | output.y = y |
---|
[a9f579c] | 138 | output.y_unit = '\AA^{-2} cm^{-1}' # output y_unit added |
---|
[1c0e3b0] | 139 | output.dx, output.dx_unit = self._unit_conversion(dx, lam_unit, default_z_unit) |
---|
[5e326a6] | 140 | output.dy = dy |
---|
[1c0e3b0] | 141 | output.lam, output.lam_unit = self._unit_conversion(lam, lam_unit, default_z_unit) |
---|
| 142 | output.dlam, output.dlam_unit = self._unit_conversion(dlam, lam_unit, default_z_unit) |
---|
[5e326a6] | 143 | |
---|
[a9f579c] | 144 | output.xaxis("\\rm{z}", output.x_unit) |
---|
| 145 | output.yaxis("\\rm{ln(P)/(t \lambda^2)}", output.y_unit) # Adjust label to ln P/(lam^2 t), remove lam column refs |
---|
[5e326a6] | 146 | # Store loading process information |
---|
| 147 | output.meta_data['loader'] = self.type_name |
---|
[a9f579c] | 148 | #output.sample.thickness = float(paramvals[6]) |
---|
[5e326a6] | 149 | output.sample.name = paramvals[1] |
---|
| 150 | output.sample.ID = paramvals[0] |
---|
[3689302] | 151 | zaccept_unit_split = paramnames[7].split("[") |
---|
| 152 | zaccept_unit = zaccept_unit_split[1].replace("]","") |
---|
[a9f579c] | 153 | if zaccept_unit.strip() == '\AA^-1' or zaccept_unit.strip() == '\A^-1': |
---|
[3689302] | 154 | zaccept_unit = "1/A" |
---|
[1c0e3b0] | 155 | output.sample.zacceptance=(float(paramvals[7]),zaccept_unit) |
---|
[a9f579c] | 156 | output.vars = varheader |
---|
[5e326a6] | 157 | |
---|
| 158 | if len(output.x) < 1: |
---|
| 159 | raise RuntimeError, "%s is empty" % path |
---|
| 160 | return output |
---|
[26d4864] | 161 | |
---|
[5e326a6] | 162 | else: |
---|
| 163 | raise RuntimeError, "%s is not a file" % path |
---|
| 164 | return None |
---|
[26d4864] | 165 | |
---|
| 166 | def _unit_conversion(self, value, value_unit, default_unit): |
---|
| 167 | if has_converter == True and value_unit != default_unit: |
---|
| 168 | data_conv_q = Converter(value_unit) |
---|
| 169 | value = data_conv_q(value, units=default_unit) |
---|
| 170 | new_unit = default_unit |
---|
| 171 | else: |
---|
| 172 | new_unit = value_unit |
---|
| 173 | return value, new_unit |
---|