Ignore:
Timestamp:
Apr 4, 2017 8:15:03 AM (7 years ago)
Author:
Adam Washington <adam.washington@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
ecc8d1a8
Parents:
7cbbacd
git-author:
Adam Washington <adam.washington@…> (04/04/17 08:11:46)
git-committer:
Adam Washington <adam.washington@…> (04/04/17 08:15:03)
Message:

Condensed sesans reader code

The sesans reader code is smaller and now supports loading units from
the file. Additionally, the units are now loaded properly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/sesans_reader.py

    r7caf3e5 re935ddb1  
    66    Jurrian Bakker  
    77""" 
    8 import numpy 
     8import numpy as np 
    99import os 
    1010from sas.sascalc.dataloader.data_info import Data1D 
     
    5252            _, extension = os.path.splitext(basename) 
    5353            if self.allow_all or extension.lower() in self.ext: 
    54                 try: 
     54                with open(path, 'r') as input_f: 
    5555                    # Read in binary mode since GRASP frequently has no-ascii 
    5656                    # 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) 
    76                 output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, isSesans=True) 
    77                 self.filename = output.filename = basename 
     57                    line = input_f.readline() 
     58                    params = {} 
     59                    while line.strip() != "": 
     60                        if line.strip() == "": 
     61                            break 
     62                        terms = line.strip().split("\t") 
     63                        params[terms[0].strip()] = " ".join(terms[1:]).strip() 
     64                        line = input_f.readline() 
     65                    headers_temp = input_f.readline().strip().split("\t") 
     66                    headers = {} 
     67                    for h in headers_temp: 
     68                        temp = h.strip().split() 
     69                        headers[h[:-1].strip()] = temp[-1][1:-1] 
     70                    data = np.loadtxt(input_f) 
     71                    x = data[:, 0] 
     72                    dx = data[:, 3] 
     73                    lam = data[:, 4] 
     74                    dlam = data[:, 5] 
     75                    y = data[:, 1] 
     76                    dy = data[:, 2] 
    7877 
    79                 paramnames=[] 
    80                 paramvals=[] 
    81                 zvals=[] 
    82                 dzvals=[] 
    83                 lamvals=[] 
    84                 dlamvals=[] 
    85                 Pvals=[] 
    86                 dPvals=[] 
     78                    lam_unit = _header_fetch(headers, "wavelength") 
     79                    if lam_unit == "AA": 
     80                        lam_unit = "A" 
     81                    p_unit = headers["polarisation"] 
    8782 
    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]) 
    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]) 
    102                     else: 
    103                         continue 
     83                    x, x_unit = self._unit_conversion(x, lam_unit, 
     84                                                      _fetch_unit(headers, 
     85                                                                  "spin echo length")) 
     86                    dx, dx_unit = self._unit_conversion(dx, lam_unit, 
     87                                                        _fetch_unit(headers, 
     88                                                                    "error SEL")) 
     89                    dlam, dlam_unit = self._unit_conversion(dlam, lam_unit, 
     90                                                            _fetch_unit(headers, 
     91                                                                        "error wavelength")) 
     92                    y_unit = r'\AA^{-2} cm^{-1}' 
    10493 
    105                 x=[] 
    106                 y=[] 
    107                 lam=[] 
    108                 dx=[] 
    109                 dy=[] 
    110                 dlam=[] 
    111                 lam_header = lamvals[0].split() 
    112                 data_conv_z = None 
    113                 default_z_unit = "A" 
    114                 data_conv_P = None 
    115                 default_p_unit = " " # Adjust unit for axis (L^-3) 
    116                 lam_unit = lam_header[1].replace("[","").replace("]","") 
    117                 if lam_unit == 'AA': 
    118                     lam_unit = 'A' 
    119                 varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] 
    120                 valrange=range(1, len(zvals)) 
    121                 for i in valrange: 
    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])) 
     94                    output = Data1D(x=x, y=y, lam=lam, dy = dy, dx=dx, dlam=dlam, isSesans=True) 
     95                    self.filename = output.filename = basename 
     96                    output.xaxis(r"\rm{z}", x_unit) 
     97                    output.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit)  # Adjust label to ln P/(lam^2 t), remove lam column refs 
     98                    # Store loading process information 
     99                    output.meta_data['loader'] = self.type_name 
     100                    #output.sample.thickness = float(paramvals[6]) 
     101                    output.sample.name = params["Sample"] 
     102                    output.sample.ID = params["DataFileTitle"] 
    128103 
    129                 x,y,lam,dy,dx,dlam = [ 
    130                    numpy.asarray(v, 'double') 
    131                    for v in (x,y,lam,dy,dx,dlam) 
    132                 ] 
     104                    output.sample.zacceptance = (float(_header_fetch(params, "Q_zmax")), 
     105                                                 _fetch_unit(params, "Q_zmax")) 
    133106 
    134                 input_f.close() 
    135  
    136                 output.x, output.x_unit = self._unit_conversion(x, lam_unit, default_z_unit) 
    137                 output.y = y 
    138                 output.y_unit = r'\AA^{-2} cm^{-1}'  # output y_unit added 
    139                 output.dx, output.dx_unit = self._unit_conversion(dx, lam_unit, default_z_unit) 
    140                 output.dy = dy 
    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) 
    143                  
    144                 output.xaxis(r"\rm{z}", output.x_unit) 
    145                 output.yaxis(r"\rm{ln(P)/(t \lambda^2)}", output.y_unit)  # Adjust label to ln P/(lam^2 t), remove lam column refs 
    146  
    147                 # Store loading process information 
    148                 output.meta_data['loader'] = self.type_name 
    149                 #output.sample.thickness = float(paramvals[6]) 
    150                 output.sample.name = paramvals[1] 
    151                 output.sample.ID = paramvals[0] 
    152                 zaccept_unit_split = paramnames[7].split("[") 
    153                 zaccept_unit = zaccept_unit_split[1].replace("]","") 
    154                 if zaccept_unit.strip() == r'\AA^-1' or zaccept_unit.strip() == r'\A^-1': 
    155                     zaccept_unit = "1/A" 
    156                 output.sample.zacceptance=(float(paramvals[7]),zaccept_unit) 
    157                 output.vars = varheader 
     107                    output.sample.yacceptance = (float(_header_fetch(params, "Q_ymax")), 
     108                                                 _fetch_unit(params, "Q_ymax")) 
    158109 
    159110                if len(output.x) < 1: 
     
    173124            new_unit = value_unit 
    174125        return value, new_unit 
     126 
     127 
     128def _header_fetch(headers, key): 
     129    index = [k for k in headers.keys() 
     130             if k.startswith(key)][0] 
     131    return headers[index] 
     132 
     133def _fetch_unit(params, key): 
     134    index = [k for k in params.keys() 
     135             if k.startswith(key)][0] 
     136    unit = index.strip().split()[-1][1:-1] 
     137    if unit.startswith(r"\A"): 
     138        unit = "1/A" 
     139    return unit 
Note: See TracChangeset for help on using the changeset viewer.