Commit 9f1e7931 authored by Zolnierczuk, Piotr's avatar Zolnierczuk, Piotr
Browse files

filetype savepoint 2

parent a8fac9db
Loading
Loading
Loading
Loading

bin/check_for_tests.sh

0 → 100755
+8 −0
Original line number Diff line number Diff line
#!/bin/bash

for f in $(find pysen -name '[a-z]*.py'); do 
	tm=$(echo $f | sed -e 's|/|_|g' -e 's|pysen|test|g'); 
	if [ ! -f test/$tm ]; then
		echo "TEST MISSING $f $tm"
	fi
done
+1 −1
Original line number Diff line number Diff line
@@ -110,7 +110,7 @@ def get_attenuator_pos(attenuator_angle):

#
PROTON_CHARGE_RTDL         = 1e-11  # 100 pico Coulomb (RTDL unit)
PROTON_CHARGE_UNIT         = 1e-7   # 0.1 micro Coulomb (NSE control program unit)
PROTON_CHARGE_UNIT         = 1e-07  # 1e-07  # 0.1 micro Coulomb (NSE control program unit)
DEFAULT_ACCELERATOR_FREQ   =   60.0 # Hz
#DEFAULT_ACCELERATOR_POWER  =    1.4 # MW
#DEFAULT_PROTON_ENERGY      = 1000.0 # MeV
+6 −28
Original line number Diff line number Diff line
@@ -7,40 +7,18 @@ import argparse
import logging

from pysen import version, setup_logger
from pysen.inout import convert_to_hdf, get_nsefiletype
from pysen.inout import convert_to_hdf #, get_nsefiletype

def convert_file(filenames, outdir, **kwargs):
    """convert magnetic up/down .dat file into an HDF file """
    """convert a TACO/NeXus file into an pysen/HDF file """
    log = logging.getLogger()
    for filename in filenames:
        # get the file extension and the header
        filetype    = get_nsefiletype(filename)
        basename = os.path.basename(filename)
        #filetype = get_nsefiletype(filename)
        try:
            #if file_ext=='.echo':
            if filetype=='application/nse-echo':
                log.debug('reading echo data %s', filename)
                outfile = convert_to_hdf(filename, outdir, data_type='echo', **kwargs)
            elif filetype=='application/nse-xyz':
                log.debug('reading xyz data %s', filename)
                outfile = convert_to_hdf(filename, outdir, data_type='xyz', **kwargs)
            elif filetype=='application/nexus-nse-echo':
                log.debug('reading NeXus echo data %s', filename)
                outfile = convert_to_hdf(filename, outdir, data_type='nexus_echo', **kwargs)
            else:
                _, file_ext = os.path.splitext(basename)
                if file_ext=='.dat':
                    header=''
                    with open(filename, 'r', encoding='ascii') as _fd:
                        header = _fd.readline()
                        if header.startswith('-------------- physics parameters'):
                            log.debug('reading magnetic data %s', filename)
                            outfile = convert_to_hdf(filename, outdir, data_type='magnetic', **kwargs)
                        else:
                            raise RuntimeError("Unknown file type (.dat)")
                else:
                    raise RuntimeError("Unknown file type %s" % filetype)
            log.info('written HDF5 file %s', outfile)
            outfile = convert_to_hdf(filename, outdir, **kwargs)
            log.info('converted %s to %s', basename, outfile)
        except RuntimeError as exc:
            log.warning("error converting %s: %s", basename, exc)
        except OSError as exc:
+43 −26
Original line number Diff line number Diff line
@@ -33,16 +33,25 @@ def get_open_file(filename):
NSE_FILE_TYPES = {
"application/nse-echo"           : '.echo'  ,
"application/nse-xyz"            : '.xyz'   ,
"application/nse-magnetic"       : '.dat'   ,
"application/nse-echo-hdf"       : '.h5'    ,
"application/nexus-nse-echo"     : '.nxs.h5',
"application/nexus-transmission" : '.nxs.h5',
"application/nexus-diffraction"  : '.nxs.h5',
}

def get_mime_filetype(filename):
    "get mime type if all else fails"
    return mimetypes.guess_type(filename)[0]

def get_nexusfiletype(filename):
    "get nexus filetype"
def get_hdf_filetype(filename):
    "get HDF filetype"
    # read /entry/notes to determine the filetype
    base, _   = os.path.splitext(os.path.basename(filename))
    _   , ext = os.path.splitext(base) # get the "second extension"
    if not ext: # no second extension
        return "application/nse-echo-hdf"
    if ext == '.nxs': # .nxs.h5
        with h5py.File(filename, 'r') as nxsfile:
            try:
                notes   = nxsfile['/entry/notes']
@@ -55,18 +64,26 @@ def get_nexusfiletype(filename):
                    return "application/nexus-nse-echo"
            except KeyError:
                pass
    return mimetypes.guess_type(filename)[0]
    return get_mime_filetype(filename)

def get_dat_filetype(filename):
    "test if old-style magnetic xyz file"
    with open(filename, 'r', encoding='ascii') as fd:
        header = fd.readline()
        if header.startswith('-------------- physics parameters'):
            return "application/nse-magnetic"
    return get_mime_filetype(filename)

def get_nsefiletype(filename):
    "get filetype"
    base, ext1 = os.path.splitext(os.path.basename(filename))
    if ext1=='.echo':
        return "application/nse-echo"
    if ext1=='.xyz':
        return "application/nse-xyz"
    if ext1=='.h5':
        base, ext2 = os.path.splitext(base)
        if not ext2:
            return "application/nse-echo-hdf"
        return get_nexusfiletype(filename)
    return mimetypes.guess_type(filename)[0]
    _, ext = os.path.splitext(filename)
    filetype =  {
            '.echo': "application/nse-echo",
            '.xyz' : "application/nse-xyz",
            '.dat' : get_dat_filetype,
            '.h5'  : get_hdf_filetype,
            }.get(ext, get_mime_filetype)

    if callable(filetype):
        return filetype(filename)
    return filetype
Loading