Loading pysen/inout/__init__.py +1 −0 Original line number Diff line number Diff line Loading @@ -7,5 +7,6 @@ from .reader import ( read_echo, read_magnetic, read_xyz, # NOQA read_datreat , read_transmission ) # NOQA from .writer import EchoWriter, DiffractionWriter # NOQA from .scans import EchoScan, DiffractionScan, XYZScan # NOQA from .utils import get_nsefiletype # from .legacy_writer import write_csv, generate_echo # NOQA # from .legacy_history import extract_data, list_variables, parse_date # NOQA pysen/inout/convert2hdf.py +20 −14 Original line number Diff line number Diff line Loading @@ -7,23 +7,29 @@ import argparse import logging from pysen import version, setup_logger from pysen.inout import convert_to_hdf 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 """ log = logging.getLogger() for filename in filenames: # get the file extension and the header filetype = get_nsefiletype(filename) basename = os.path.basename(filename) _, file_ext = os.path.splitext(filename) try: if file_ext=='.echo': #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 file_ext=='.xyz': elif filetype=='application/nse-xyz': log.debug('reading xyz data %s', filename) outfile = convert_to_hdf(filename, outdir, data_type='xyz', **kwargs) elif file_ext=='.dat': 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() Loading @@ -31,9 +37,9 @@ def convert_file(filenames, outdir, **kwargs): log.debug('reading magnetic data %s', filename) outfile = convert_to_hdf(filename, outdir, data_type='magnetic', **kwargs) else: raise RuntimeError("Unknown file type") raise RuntimeError("Unknown file type (.dat)") else: raise RuntimeError("Unknown file type") raise RuntimeError("Unknown file type %s" % filetype) log.info('written HDF5 file %s', outfile) except RuntimeError as exc: log.warning("error converting %s: %s", basename, exc) Loading pysen/inout/hdf.py +13 −14 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ import numpy as np from ..revision import version as version_info from ..iostrings import encode from .reader import read_echo, read_magnetic, read_xyz from .writer import EchoWriter, make_echofilename from .writer import EchoWriter from .utils import get_nsefiletype #FIXME: we need to work on the idea of a schema (or not???) Loading Loading @@ -178,11 +178,11 @@ class HdfConverter: def read_nexus_echo(filename, outdir='.'): "read nexus echo file" echo = EchoWriter(make_echofilename(filename)) echo = EchoWriter() if not echo.read_nexus(filename): raise RuntimeError("Error reading NeXus file {filename}") echo.to_echo(outdir) return read_echo(echo.echofile) echofile = echo.save(outdir) return read_echo(echofile) def _make_outfilename(filename, filetype=None, outdir='.'): Loading Loading @@ -222,13 +222,6 @@ def convert_to_hdf(filename, outdir, **kwargs): # outfile filetype = get_nsefiletype(filename) basename = os.path.basename(filename) if data_type is None: # guess data type based on file reader = data_reader.get(filetype) else: # data type given reader = data_reader.get(data_type) data = reader(filename) outfile = _make_outfilename(basename, outdir=outdir, filetype=filetype) _log.info('converting %s to HDF5', basename) Loading @@ -238,6 +231,12 @@ def convert_to_hdf(filename, outdir, **kwargs): if t_out > t_src: _log.info('file %s is newer than %s', outfile, filename) return outfile if data_type is None: # guess data type based on file reader = data_reader.get(filetype) else: # data type given reader = data_reader.get(data_type) data = reader(filename) converter = HdfConverter(outfile, mode=mode, version=version_info()) converter.write(data, compression=compression, schema=schema) return outfile Loading pysen/inout/writer.py +2 −0 Original line number Diff line number Diff line Loading @@ -119,6 +119,7 @@ class DiffractionWriter(DiffractionScan): fd.write(f"\"flip ratio\" {fr:g}\n") self.log.info("%s: done", diffrun) return diffrun class EchoWriter(EchoScan): Loading Loading @@ -318,5 +319,6 @@ class EchoWriter(EchoScan): for phase in self.data[tau]: fd.write(self.phase_point(phase)) self.log.info("%s: done", echofile) return echofile # EOF pysen/plot/nseplot.py +9 −3 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ import matplotlib.pyplot as plt from pysen import version, setup_logger, DEFAULT_LOG_LEVEL from pysen.config import INST_POSITIONS, INST_MODES, DEFAULT_ACCELERATOR_POWER, coverage from pysen.inout import convert_to_hdf from pysen.inout import convert_to_hdf, get_nsefiletype from pysen.echo import get_symmetry_phase, make_phase_table from pysen.plot import ( plot_echo, plot_atari, plot_map, plot_xyz, plot_xyz_nexus, plot_magnetic_fields, Loading @@ -33,9 +33,15 @@ def action_default(filenames, **kwargs): log = logging.getLogger() for filename in filenames: try: basename , ext = os.path.splitext(os.path.basename(filename)) if ext != ".h5": filetype = get_nsefiletype(filename) #basename , ext = os.path.splitext(os.path.basename(filename)) print(filetype, filename) if filetype=='application/nse-echo': filename = convert_to_hdf(filename, outdir, data_type='echo') elif filetype=='application/nexus-nse-echo': filename = convert_to_hdf(filename, outdir, data_type='nexus_echo') with h5py.File(filename, 'r') as hdf5file: action(hdf5file, taus, **kwargs) if savefig: Loading Loading
pysen/inout/__init__.py +1 −0 Original line number Diff line number Diff line Loading @@ -7,5 +7,6 @@ from .reader import ( read_echo, read_magnetic, read_xyz, # NOQA read_datreat , read_transmission ) # NOQA from .writer import EchoWriter, DiffractionWriter # NOQA from .scans import EchoScan, DiffractionScan, XYZScan # NOQA from .utils import get_nsefiletype # from .legacy_writer import write_csv, generate_echo # NOQA # from .legacy_history import extract_data, list_variables, parse_date # NOQA
pysen/inout/convert2hdf.py +20 −14 Original line number Diff line number Diff line Loading @@ -7,23 +7,29 @@ import argparse import logging from pysen import version, setup_logger from pysen.inout import convert_to_hdf 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 """ log = logging.getLogger() for filename in filenames: # get the file extension and the header filetype = get_nsefiletype(filename) basename = os.path.basename(filename) _, file_ext = os.path.splitext(filename) try: if file_ext=='.echo': #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 file_ext=='.xyz': elif filetype=='application/nse-xyz': log.debug('reading xyz data %s', filename) outfile = convert_to_hdf(filename, outdir, data_type='xyz', **kwargs) elif file_ext=='.dat': 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() Loading @@ -31,9 +37,9 @@ def convert_file(filenames, outdir, **kwargs): log.debug('reading magnetic data %s', filename) outfile = convert_to_hdf(filename, outdir, data_type='magnetic', **kwargs) else: raise RuntimeError("Unknown file type") raise RuntimeError("Unknown file type (.dat)") else: raise RuntimeError("Unknown file type") raise RuntimeError("Unknown file type %s" % filetype) log.info('written HDF5 file %s', outfile) except RuntimeError as exc: log.warning("error converting %s: %s", basename, exc) Loading
pysen/inout/hdf.py +13 −14 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ import numpy as np from ..revision import version as version_info from ..iostrings import encode from .reader import read_echo, read_magnetic, read_xyz from .writer import EchoWriter, make_echofilename from .writer import EchoWriter from .utils import get_nsefiletype #FIXME: we need to work on the idea of a schema (or not???) Loading Loading @@ -178,11 +178,11 @@ class HdfConverter: def read_nexus_echo(filename, outdir='.'): "read nexus echo file" echo = EchoWriter(make_echofilename(filename)) echo = EchoWriter() if not echo.read_nexus(filename): raise RuntimeError("Error reading NeXus file {filename}") echo.to_echo(outdir) return read_echo(echo.echofile) echofile = echo.save(outdir) return read_echo(echofile) def _make_outfilename(filename, filetype=None, outdir='.'): Loading Loading @@ -222,13 +222,6 @@ def convert_to_hdf(filename, outdir, **kwargs): # outfile filetype = get_nsefiletype(filename) basename = os.path.basename(filename) if data_type is None: # guess data type based on file reader = data_reader.get(filetype) else: # data type given reader = data_reader.get(data_type) data = reader(filename) outfile = _make_outfilename(basename, outdir=outdir, filetype=filetype) _log.info('converting %s to HDF5', basename) Loading @@ -238,6 +231,12 @@ def convert_to_hdf(filename, outdir, **kwargs): if t_out > t_src: _log.info('file %s is newer than %s', outfile, filename) return outfile if data_type is None: # guess data type based on file reader = data_reader.get(filetype) else: # data type given reader = data_reader.get(data_type) data = reader(filename) converter = HdfConverter(outfile, mode=mode, version=version_info()) converter.write(data, compression=compression, schema=schema) return outfile Loading
pysen/inout/writer.py +2 −0 Original line number Diff line number Diff line Loading @@ -119,6 +119,7 @@ class DiffractionWriter(DiffractionScan): fd.write(f"\"flip ratio\" {fr:g}\n") self.log.info("%s: done", diffrun) return diffrun class EchoWriter(EchoScan): Loading Loading @@ -318,5 +319,6 @@ class EchoWriter(EchoScan): for phase in self.data[tau]: fd.write(self.phase_point(phase)) self.log.info("%s: done", echofile) return echofile # EOF
pysen/plot/nseplot.py +9 −3 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ import matplotlib.pyplot as plt from pysen import version, setup_logger, DEFAULT_LOG_LEVEL from pysen.config import INST_POSITIONS, INST_MODES, DEFAULT_ACCELERATOR_POWER, coverage from pysen.inout import convert_to_hdf from pysen.inout import convert_to_hdf, get_nsefiletype from pysen.echo import get_symmetry_phase, make_phase_table from pysen.plot import ( plot_echo, plot_atari, plot_map, plot_xyz, plot_xyz_nexus, plot_magnetic_fields, Loading @@ -33,9 +33,15 @@ def action_default(filenames, **kwargs): log = logging.getLogger() for filename in filenames: try: basename , ext = os.path.splitext(os.path.basename(filename)) if ext != ".h5": filetype = get_nsefiletype(filename) #basename , ext = os.path.splitext(os.path.basename(filename)) print(filetype, filename) if filetype=='application/nse-echo': filename = convert_to_hdf(filename, outdir, data_type='echo') elif filetype=='application/nexus-nse-echo': filename = convert_to_hdf(filename, outdir, data_type='nexus_echo') with h5py.File(filename, 'r') as hdf5file: action(hdf5file, taus, **kwargs) if savefig: Loading