Skip to content
Snippets Groups Projects
Commit ade737bd authored by Laurent Mercadier's avatar Laurent Mercadier
Browse files

Use extra-data read machinery

parent c56d2302
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,7 @@ import pandas as pd
import extra_data as ed
from ..constants import mnemonics as _mnemonics
from ..mnemonics_machinery import mnemonics_for_run
from .dssc_data import save_xarray
log = logging.getLogger(__name__)
......@@ -125,7 +125,8 @@ def _load_chunk_xgm(sel, xgm_mnemonic, stride):
-------
xarray.DataArray
"""
d = sel.get_array(*_mnemonics[xgm_mnemonic].values())
mnemonics = mnemonics_for_run(sel)
d = sel.get_array(*mnemonics[xgm_mnemonic].values())
d = d.rename(new_name_or_name_dict={'XGMbunchId': 'pulse'})
frame_coords = np.linspace(0, (d.shape[1]-1)*stride, d.shape[1], dtype=int)
d = d.assign_coords({'pulse': frame_coords})
......
......@@ -9,15 +9,16 @@
"""
import logging
import os
import numpy as np
import xarray as xr
import extra_data as ed
from extra_data.read_machinery import find_proposal
from .constants import mnemonics as _mnemonics
from .mnemonics_machinery import mnemonics_for_run
from .util.exceptions import ToolBoxValueError, ToolBoxPathError
from .util.data_access import find_run_dir
from .util.exceptions import ToolBoxValueError
import toolbox_scs.detectors as tbdet
log = logging.getLogger(__name__)
......@@ -104,11 +105,11 @@ def load(proposalNB=None, runNB=None,
>>> run, data = tb.load(2212, 208, ['SCS_SA3', 'MCP2apd', 'nrj'])
"""
try:
runFolder = find_run_dir(proposalNB, runNB)
except ToolBoxPathError as err:
log.error(f"{err.message}")
raise
if isinstance(runNB, int):
runNB = 'r{:04d}'.format(runNB)
if isinstance(proposalNB, int):
proposalNB = 'p{:06d}'.format(proposalNB)
runFolder = os.path.join(find_proposal(proposalNB), subFolder, runNB)
run = ed.RunDirectory(runFolder).select_trains(subset)
if fields is None:
return run, xr.Dataset()
......
'''
Extensions to the extra_data package.
contributions should comply with pep8 code structure guidelines.
'''
import os
import logging
import extra_data as ed
from extra_data.read_machinery import find_proposal
from ..util.exceptions import ToolBoxPathError
log = logging.getLogger(__name__)
def find_run_dir(proposal, run):
"""
Get run directory for given run.
This method is an extension to 'find_proposal' in the extra_data
package and should eventually be transferred to the latter.
Parameters
----------
proposal: str, int
Proposal number
run: str, int
Run number
Returns
-------
rdir : str
Run directory as a string
Raises
------
ToolBoxPathError: Exception
Error raised if the constructed path does not exist. This may
happen when entering a non-valid run number, or the folder has
been renamed/removed.
Comment
-------
The rather unspecified Exeption raised, when entering a invalid proposal
number stems from the ed package -> to be fixed externally.
"""
rdir = None
try:
pdir = find_proposal(f'p{proposal:06d}')
rdir = os.path.join(pdir, f'raw/r{run:04d}')
if os.path.isdir(rdir) is False:
log.warning("Invalid directory: raise ToolBoxPathError.")
msg = 'The constructed path does not exist.'
raise ToolBoxPathError(msg, rdir)
except ToolBoxPathError:
raise
except Exception as err:
log.error("Unexpected error:", exc_info=True)
raise
return rdir
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment