Changeset c0793dd in sasview for src


Ignore:
Timestamp:
Apr 5, 2017 11:21:10 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:
610af69
Parents:
bfd34a7 (diff), 98c44f3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of github.com:SasView/sasview

Location:
src/sas
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/guiframe/media/data_formats_help.rst

    r48b8f6d r98c44f3  
    44.. by S King, ISIS, during SasView CodeCamp-III in Feb 2015. 
    55.. WG Bouwman, DUT, added during CodeCamp-V in Oct 2016 the SESANS data format 
     6.. WG Bouwman, DUT, updated during CodeCamp-VI in Apr 2017 the SESANS data format 
    67 
    78.. _Formats: 
     
    8283The file format has a list of name-value pairs at the top of the file which detail the general experimental parameters necessary for fitting and analyzing data. This list should contain all the information necessary for the file to be 'portable' between users. 
    8384 
    84 Following the header is a 6 column list of instrument experimental variables: 
     85Following the header is a 8 (only the first 4 are really needed) column list of instrument experimental variables: 
    8586 
    8687- Spin echo length (z, in Angstroms) 
     88- depolarization (:math:`log(P/P_0)/(lambda^2 * thickness)`, in Angstrom^-1 cm^-1) 
     89- depolarization error in the same unit) (measurement error) 
    8790- Spin echo length error (:math:`\Delta`\ z, in Angstroms) (experimental resolution) 
    88 - Neutron wavelength (:math:`\lambda`, in Angstroms) (essential for ToF instruments) 
     91- Neutron wavelength (:math:`\lambda`, in Angstroms) 
    8992- Neutron wavelength error (:math:`\Delta \lambda`, in Angstroms) 
    9093- Normalized polarization (:math:`P/P_0`, unitless) 
     
    9396.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 
    9497 
    95 .. note::  This help document was last changed by Steve King, 07Oct2016 
     98.. note::  This help document was last changed by Wim Bouwman, 05Apr2017 
  • src/sas/sascalc/dataloader/readers/sesans_reader.py

    r9a5097c r2b310602  
    11""" 
    22    SESANS reader (based on ASCII reader) 
    3      
     3 
    44    Reader for .ses or .sesans file format 
    5      
    6     Jurrian Bakker  
     5 
     6    Jurrian Bakker 
    77""" 
    88import numpy as np 
     
    1818_ZERO = 1e-16 
    1919 
     20 
    2021class Reader: 
    2122    """ 
    2223    Class to load sesans files (6 columns). 
    2324    """ 
    24     ## File type 
     25    # File type 
    2526    type_name = "SESANS" 
    26      
    27     ## Wildcards 
     27 
     28    # Wildcards 
    2829    type = ["SESANS files (*.ses)|*.ses", 
    2930            "SESANS files (*..sesans)|*.sesans"] 
    30     ## List of allowed extensions 
     31    # List of allowed extensions 
    3132    ext = ['.ses', '.SES', '.sesans', '.SESANS'] 
    32      
    33     ## Flag to bypass extension check 
     33 
     34    # Flag to bypass extension check 
    3435    allow_all = True 
    35      
     36 
    3637    def read(self, path): 
    37          
    38 #        print "reader triggered" 
    39          
    4038        """ 
    4139        Load data file 
    42          
     40 
    4341        :param path: file path 
    44          
     42 
    4543        :return: SESANSData1D object, or None 
    46          
     44 
    4745        :raise RuntimeError: when the file can't be opened 
    4846        :raise ValueError: when the length of the data vectors are inconsistent 
     
    5149            basename = os.path.basename(path) 
    5250            _, 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  = np.zeros(0) 
    63                 y  = np.zeros(0) 
    64                 dy = np.zeros(0) 
    65                 lam  = np.zeros(0) 
    66                 dlam = np.zeros(0) 
    67                 dx = np.zeros(0) 
    68                  
    69                #temp. space to sort data 
    70                 tx  = np.zeros(0) 
    71                 ty  = np.zeros(0) 
    72                 tdy = np.zeros(0) 
    73                 tlam  = np.zeros(0) 
    74                 tdlam = np.zeros(0) 
    75                 tdx = np.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 
     51            if not (self.allow_all or extension.lower() in self.ext): 
     52                raise RuntimeError("{} has an unrecognized file extension".format(path)) 
     53        else: 
     54            raise RunetimeError("{} is not a file".format(path)) 
     55        with open(path, 'r') as input_f: 
     56            # Read in binary mode since GRASP frequently has no-ascii 
     57            # characters that brakes the open operation 
     58            line = input_f.readline() 
     59            params = {} 
     60            while not line.startswith("BEGIN_DATA"): 
     61                terms = line.split() 
     62                if len(terms) >= 2: 
     63                    params[terms[0]] = " ".join(terms[1:]) 
     64                line = input_f.readline() 
     65            self.params = params 
     66            headers = input_f.readline().split() 
    7867 
    79                 paramnames=[] 
    80                 paramvals=[] 
    81                 zvals=[] 
    82                 dzvals=[] 
    83                 lamvals=[] 
    84                 dlamvals=[] 
    85                 Pvals=[] 
    86                 dPvals=[] 
     68            data = np.loadtxt(input_f) 
     69            if data.size < 1: 
     70                raise RuntimeError("{} is empty".format(path)) 
     71            x = data[:, headers.index("SpinEchoLength")] 
     72            dx = data[:, headers.index("SpinEchoLength_error")] 
     73            lam = data[:, headers.index("Wavelength")] 
     74            dlam = data[:, headers.index("Wavelength_error")] 
     75            y = data[:, headers.index("Depolarisation")] 
     76            dy = data[:, headers.index("Depolarisation_error")] 
    8777 
    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 
     78            lam_unit = self._unit_fetch("Wavelength") 
     79            x, x_unit = self._unit_conversion(x, "A", self._unit_fetch("SpinEchoLength")) 
     80            dx, dx_unit = self._unit_conversion( 
     81                dx, lam_unit, 
     82                self._unit_fetch("SpinEchoLength")) 
     83            dlam, dlam_unit = self._unit_conversion( 
     84                dlam, lam_unit, 
     85                self._unit_fetch("Wavelength")) 
     86            y_unit = self._unit_fetch("Depolarisation") 
    10487 
    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])) 
     88            output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, 
     89                            isSesans=True) 
    12890 
    129                 x,y,lam,dy,dx,dlam = [ 
    130                     np.asarray(v, 'double') 
    131                    for v in (x,y,lam,dy,dx,dlam) 
    132                 ] 
     91            output.y_unit = y_unit 
     92            output.x_unit = x_unit 
     93            self.filename = output.filename = basename 
     94            output.xaxis(r"\rm{z}", x_unit) 
     95            # Adjust label to ln P/(lam^2 t), remove lam column refs 
     96            output.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit) 
     97            # Store loading process information 
     98            output.meta_data['loader'] = self.type_name 
     99            output.sample.name = params["Sample"] 
     100            output.sample.ID = params["DataFileTitle"] 
     101            output.sample.thickness = float( 
     102                self._unit_conversion( 
     103                    params["Thickness"], "cm", self._unit_fetch("Thickness"))[0]) 
    133104 
    134                 input_f.close() 
     105            output.sample.zacceptance = ( 
     106                float(params["Theta_zmax"]), 
     107                self._unit_fetch("Theta_zmax")) 
    135108 
    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 
     109            output.sample.yacceptance = ( 
     110                float(params["Theta_ymax"]), 
     111                self._unit_fetch("Theta_ymax")) 
     112            return output 
    146113 
    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 
     114    @staticmethod 
     115    def _unit_conversion(value, value_unit, default_unit): 
     116        """ 
     117        Performs unit conversion on a measurement. 
    158118 
    159                 if len(output.x) < 1: 
    160                     raise RuntimeError, "%s is empty" % path 
    161                 return output 
    162  
    163         else: 
    164             raise RuntimeError, "%s is not a file" % path 
    165         return None 
    166  
    167     def _unit_conversion(self, value, value_unit, default_unit): 
    168         if has_converter == True and value_unit != default_unit: 
     119        :param value: The magnitude of the measurement 
     120        :param value_unit: a string containing the final desired unit 
     121        :param default_unit: a string containing the units of the original measurement 
     122        :return: The magnitude of the measurement in the new units 
     123        """ 
     124        # (float, string, string) -> float 
     125        if has_converter and value_unit != default_unit: 
    169126            data_conv_q = Converter(value_unit) 
    170127            value = data_conv_q(value, units=default_unit) 
     
    173130            new_unit = value_unit 
    174131        return value, new_unit 
     132 
     133    def _unit_fetch(self, unit): 
     134        return self.params[unit+"_unit"] 
Note: See TracChangeset for help on using the changeset viewer.