Loading pyproject.toml +1 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ authors = [ description = "Python tools for the SNS-NSE instrument" readme = "README.md" requires-python = ">=3.7" requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", Loading pysen/config.py +3 −9 Original line number Diff line number Diff line Loading @@ -33,12 +33,6 @@ MIN_PHI = 3.0 # degrees MAX_PHI = dict(zip(INST_POSITIONS,[29.0,42.0,56.0,79.5])) # degrees PHI_LIMS = (3.0, 79.5) # wavelenth bandwitdh # now computed based on Ltot #DLAMBDA = {'p1': 3.6, # lambda in Angstroms # 'p2': 3.1, # 'p3': 2.7, # 'p4': 2.4} # 3. The Detector XDET = 0.300 # [m] detector width Loading Loading @@ -145,8 +139,8 @@ def phi_limits(pos='p2'): """ get instrument scattering angle limits in degrees""" return MIN_PHI, MAX_PHI.get(pos, MIN_PHI) def wavelength_bandwitdh(pos='p2', freq=DEFAULT_ACCELERATOR_FREQ): """ get instrument wavelength bandwitdh limits in Angstroms""" def wavelength_bandwidth(pos='p2', freq=DEFAULT_ACCELERATOR_FREQ): """ get instrument wavelength bandwidth limits in Angstroems""" return HMN/(Ltot.get(pos)*freq*ANGSTROM) Loading Loading @@ -332,7 +326,7 @@ def coverage(lmax, qmin, **kwargs): tbins = kwargs.pop('tbins', None) ntbin = kwargs.pop('ntbin', 42 ) detpos = kwargs.pop('pos', 'p2' ) dlam = kwargs.pop('dlam', wavelength_bandwitdh(detpos)) dlam = kwargs.pop('dlam', wavelength_bandwidth(detpos)) rpixel = kwargs.pop('rpixel', (NXCHAN//2)) check_limits = kwargs.pop('limits', True) # check limits Loading pysen/inout/hdf.py +5 −2 Original line number Diff line number Diff line Loading @@ -211,8 +211,8 @@ def convert_to_hdf(filename, outdir, **kwargs): 'magnetic' : read_magnetic, 'nexus_echo' : read_nexus_echo, # 'application/nse-echo' : read_echo, 'application/nse-xyz' : read_xyz, 'application/taco-nse-echo' : read_echo, 'application/taco-nse-xyz' : read_xyz, 'application/nexus-nse-echo' : read_nexus_echo, } Loading Loading @@ -268,12 +268,15 @@ def convert_files(filenames, outdir, **kwargs): """convert *.nxs.h5 file to/from old TACO style files""" log = logging.getLogger() converter = ConverterType.get(kwargs.pop('converter', 'from_taco')) results = [] for filename in filenames: try: basename = os.path.basename(filename) outfile = converter(filename, outdir, **kwargs) log.info('converted %s to %s', basename, outfile) results.append(outfile) except RuntimeError as exc: log.warning("error converting %s: (%s)", basename, exc) except OSError as exc: log.warning("error converting %s: (%s)", basename, exc.strerror) return results pysen/inout/reader.py +43 −38 Original line number Diff line number Diff line Loading @@ -772,10 +772,12 @@ def read_magnetic(filename): def read_transmission(filename): "read transmission file" result = OrderedDict() #transtab = { '"' : None, # '\n': None, } try: with open(filename, mode='r', encoding='ascii') as _fdesc: lines = iter(_fdesc.readlines()) lines = _fdesc.readlines() except TypeError: lines = filename lines = iter(lines) prevline = None while True: line = next(lines) Loading @@ -788,9 +790,9 @@ def read_transmission(filename): result['proposal' ] = header[0] result['sample' ] = header[1].split()[-1] result['wavelength'] = _evaluate(header[2].split()[-1]) thead = remove_chars(line, delchars='"\n').split() tdata = remove_chars(next(lines), delchars='"\n').split() for k,v in zip(thead, tdata): t_head = remove_chars(line, delchars='"\n').split() t_data = remove_chars(next(lines), delchars='"\n').split() for k,v in zip(t_head, t_data): result[k] = _evaluate(v) tmpkey = None tmparr = [] Loading Loading @@ -824,10 +826,13 @@ def read_diffrun(filename, att_table=None): wlendata = [] wlen_list = None attenuator = 1 try: with open(filename, mode='r', encoding='ascii') as _fdesc: lines = iter(_fdesc.readlines()) lines = _fdesc.readlines() except TypeError: lines = filename # for line in lines: for line in iter(lines): line = line.strip() tokens = line.split() if not tokens: Loading pysen/inout/utils.py +26 −7 Original line number Diff line number Diff line Loading @@ -31,10 +31,14 @@ def get_open_file(filename): NSE_FILE_TYPES = { "application/nse-echo" : '.echo' , "application/nse-xyz" : '.xyz' , "application/nse-magnetic" : '.dat' , "application/taco-nse-echo" : '.echo' , "application/taco-nse-transmission" : '.dat' , "application/taco-nse-diffraction" : '.dat' , "application/taco-nse-xyz" : '.xyz' , "application/taco-nse-magnetic" : '.dat' , # "application/nse-echo-hdf" : '.h5' , # "application/nexus-nse-echo" : '.nxs.h5', "application/nexus-nse-xyz" : '.nxs.h5', "application/nexus-nse-transmission" : '.nxs.h5', Loading Loading @@ -71,18 +75,33 @@ def get_hdf_filetype(filename): def get_dat_filetype(filename): "test if old-style magnetic xyz file" def find_first_line(fdesc): "find first non-empty line" while True: line = fdesc.readline() if line.strip(): return line.lower() return None with open(filename, 'r', encoding='ascii') as fd: header = fd.readline() header = find_first_line(fd) if header.startswith('-------------- physics parameters'): return "application/nse-magnetic" return "application/taco-nse-magnetic" if header.startswith('"transmission'): return "application/taco-nse-transmission" if header.startswith('"##############'): #get next line to see if difraction scan header = fd.readline() if "diffraction" in header: return "application/taco-nse-diffraction" return get_mime_filetype(filename) def get_nsefiletype(filename): "get filetype" _, ext = os.path.splitext(filename) filetype = { '.echo': "application/nse-echo", '.xyz' : "application/nse-xyz", '.echo': "application/taco-nse-echo", '.xyz' : "application/taco-nse-xyz", '.dat' : get_dat_filetype, '.h5' : get_hdf_filetype, }.get(ext, get_mime_filetype) Loading Loading
pyproject.toml +1 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ authors = [ description = "Python tools for the SNS-NSE instrument" readme = "README.md" requires-python = ">=3.7" requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", Loading
pysen/config.py +3 −9 Original line number Diff line number Diff line Loading @@ -33,12 +33,6 @@ MIN_PHI = 3.0 # degrees MAX_PHI = dict(zip(INST_POSITIONS,[29.0,42.0,56.0,79.5])) # degrees PHI_LIMS = (3.0, 79.5) # wavelenth bandwitdh # now computed based on Ltot #DLAMBDA = {'p1': 3.6, # lambda in Angstroms # 'p2': 3.1, # 'p3': 2.7, # 'p4': 2.4} # 3. The Detector XDET = 0.300 # [m] detector width Loading Loading @@ -145,8 +139,8 @@ def phi_limits(pos='p2'): """ get instrument scattering angle limits in degrees""" return MIN_PHI, MAX_PHI.get(pos, MIN_PHI) def wavelength_bandwitdh(pos='p2', freq=DEFAULT_ACCELERATOR_FREQ): """ get instrument wavelength bandwitdh limits in Angstroms""" def wavelength_bandwidth(pos='p2', freq=DEFAULT_ACCELERATOR_FREQ): """ get instrument wavelength bandwidth limits in Angstroems""" return HMN/(Ltot.get(pos)*freq*ANGSTROM) Loading Loading @@ -332,7 +326,7 @@ def coverage(lmax, qmin, **kwargs): tbins = kwargs.pop('tbins', None) ntbin = kwargs.pop('ntbin', 42 ) detpos = kwargs.pop('pos', 'p2' ) dlam = kwargs.pop('dlam', wavelength_bandwitdh(detpos)) dlam = kwargs.pop('dlam', wavelength_bandwidth(detpos)) rpixel = kwargs.pop('rpixel', (NXCHAN//2)) check_limits = kwargs.pop('limits', True) # check limits Loading
pysen/inout/hdf.py +5 −2 Original line number Diff line number Diff line Loading @@ -211,8 +211,8 @@ def convert_to_hdf(filename, outdir, **kwargs): 'magnetic' : read_magnetic, 'nexus_echo' : read_nexus_echo, # 'application/nse-echo' : read_echo, 'application/nse-xyz' : read_xyz, 'application/taco-nse-echo' : read_echo, 'application/taco-nse-xyz' : read_xyz, 'application/nexus-nse-echo' : read_nexus_echo, } Loading Loading @@ -268,12 +268,15 @@ def convert_files(filenames, outdir, **kwargs): """convert *.nxs.h5 file to/from old TACO style files""" log = logging.getLogger() converter = ConverterType.get(kwargs.pop('converter', 'from_taco')) results = [] for filename in filenames: try: basename = os.path.basename(filename) outfile = converter(filename, outdir, **kwargs) log.info('converted %s to %s', basename, outfile) results.append(outfile) except RuntimeError as exc: log.warning("error converting %s: (%s)", basename, exc) except OSError as exc: log.warning("error converting %s: (%s)", basename, exc.strerror) return results
pysen/inout/reader.py +43 −38 Original line number Diff line number Diff line Loading @@ -772,10 +772,12 @@ def read_magnetic(filename): def read_transmission(filename): "read transmission file" result = OrderedDict() #transtab = { '"' : None, # '\n': None, } try: with open(filename, mode='r', encoding='ascii') as _fdesc: lines = iter(_fdesc.readlines()) lines = _fdesc.readlines() except TypeError: lines = filename lines = iter(lines) prevline = None while True: line = next(lines) Loading @@ -788,9 +790,9 @@ def read_transmission(filename): result['proposal' ] = header[0] result['sample' ] = header[1].split()[-1] result['wavelength'] = _evaluate(header[2].split()[-1]) thead = remove_chars(line, delchars='"\n').split() tdata = remove_chars(next(lines), delchars='"\n').split() for k,v in zip(thead, tdata): t_head = remove_chars(line, delchars='"\n').split() t_data = remove_chars(next(lines), delchars='"\n').split() for k,v in zip(t_head, t_data): result[k] = _evaluate(v) tmpkey = None tmparr = [] Loading Loading @@ -824,10 +826,13 @@ def read_diffrun(filename, att_table=None): wlendata = [] wlen_list = None attenuator = 1 try: with open(filename, mode='r', encoding='ascii') as _fdesc: lines = iter(_fdesc.readlines()) lines = _fdesc.readlines() except TypeError: lines = filename # for line in lines: for line in iter(lines): line = line.strip() tokens = line.split() if not tokens: Loading
pysen/inout/utils.py +26 −7 Original line number Diff line number Diff line Loading @@ -31,10 +31,14 @@ def get_open_file(filename): NSE_FILE_TYPES = { "application/nse-echo" : '.echo' , "application/nse-xyz" : '.xyz' , "application/nse-magnetic" : '.dat' , "application/taco-nse-echo" : '.echo' , "application/taco-nse-transmission" : '.dat' , "application/taco-nse-diffraction" : '.dat' , "application/taco-nse-xyz" : '.xyz' , "application/taco-nse-magnetic" : '.dat' , # "application/nse-echo-hdf" : '.h5' , # "application/nexus-nse-echo" : '.nxs.h5', "application/nexus-nse-xyz" : '.nxs.h5', "application/nexus-nse-transmission" : '.nxs.h5', Loading Loading @@ -71,18 +75,33 @@ def get_hdf_filetype(filename): def get_dat_filetype(filename): "test if old-style magnetic xyz file" def find_first_line(fdesc): "find first non-empty line" while True: line = fdesc.readline() if line.strip(): return line.lower() return None with open(filename, 'r', encoding='ascii') as fd: header = fd.readline() header = find_first_line(fd) if header.startswith('-------------- physics parameters'): return "application/nse-magnetic" return "application/taco-nse-magnetic" if header.startswith('"transmission'): return "application/taco-nse-transmission" if header.startswith('"##############'): #get next line to see if difraction scan header = fd.readline() if "diffraction" in header: return "application/taco-nse-diffraction" return get_mime_filetype(filename) def get_nsefiletype(filename): "get filetype" _, ext = os.path.splitext(filename) filetype = { '.echo': "application/nse-echo", '.xyz' : "application/nse-xyz", '.echo': "application/taco-nse-echo", '.xyz' : "application/taco-nse-xyz", '.dat' : get_dat_filetype, '.h5' : get_hdf_filetype, }.get(ext, get_mime_filetype) Loading