diff --git a/sourcespec2/source_spec.py b/sourcespec2/source_spec.py index 5fb7fa3d..706cc567 100644 --- a/sourcespec2/source_spec.py +++ b/sourcespec2/source_spec.py @@ -54,14 +54,14 @@ def ssp_run(st, inventory, ssp_event, picks, allow_exit=False): # Create output folder if required, save config and setup logging from .ssp_setup import (get_outdir_path, save_config, setup_logging) if config.options.outdir: - outdir = get_outdir_path() + outdir = get_outdir_path(ssp_event.event_id) if not os.path.exists(outdir): os.makedirs(outdir) # Setup logging # (it is assumed this is already done if outdir exists) - setup_logging(config.event.event_id) + setup_logging(ssp_event.event_id) # Save config to out dir - save_config() + save_config(ssp_event.event_id) # Preprocessing from .ssp_read_traces import (augment_event, augment_traces, @@ -159,7 +159,7 @@ def main(): options = parse_args(progname='source_spec') # Setup stage - from .config import config, configure_cli + from .config import configure_cli configure_cli(options, progname='source_spec') from .ssp_setup import ( move_outdir, remove_old_outdir, setup_logging, @@ -176,8 +176,8 @@ def main(): ssp_event, picks = read_event_and_picks(trace1) # Now that we have an evid, we can rename the outdir and the log file - move_outdir() - setup_logging(config.event.event_id) + move_outdir(ssp_event.event_id) + setup_logging(ssp_event.event_id) remove_old_outdir() # Run sourcespec function diff --git a/sourcespec2/ssp_setup.py b/sourcespec2/ssp_setup.py index 2c220581..d8596aee 100644 --- a/sourcespec2/ssp_setup.py +++ b/sourcespec2/ssp_setup.py @@ -43,30 +43,39 @@ SSP_EXIT_CALLED = False -def save_config(): +def save_config(eventid=None): """Save config file to output dir.""" + if eventid is None: + try: + eventid = config.event.event_id + except AttributeError as err: + raise RuntimeError( + 'No event ID found. Cannot save config file.' + ) from err # Actually, it renames the file already existing. src = os.path.join(config.options.outdir, 'source_spec.conf') - evid = config.event.event_id - dst = os.path.join(config.options.outdir, f'{evid}.ssp.conf') + dst = os.path.join(config.options.outdir, f'{eventid}.ssp.conf') # On Windows, dst file must not exist with contextlib.suppress(Exception): os.remove(dst) os.rename(src, dst) -def get_outdir_path(): +def get_outdir_path(eventid=None): """Construct full path to output directory""" - try: - evid = config.event.event_id - except Exception: - return + if eventid is None: + try: + eventid = config.event.event_id + except AttributeError as err: + raise RuntimeError( + 'No event ID found. Cannot create output directory.' + ) from err src = config.options.outdir run_id = config.options.run_id run_id_subdir = config.options.run_id_subdir # TODO: does next line also work if no tmpdir has been created first? outdir = os.path.split(src)[0] - outdir = os.path.join(outdir, str(evid)) + outdir = os.path.join(outdir, str(eventid)) if run_id and run_id_subdir: outdir = os.path.join(outdir, str(run_id)) elif run_id: @@ -75,10 +84,10 @@ def get_outdir_path(): return outdir -def move_outdir(): +def move_outdir(eventid=None): """Move outdir to a new dir named from evid (and optional run_id).""" src = config.options.outdir - dst = get_outdir_path() + dst = get_outdir_path(eventid) # Create destination if not os.path.exists(dst): os.makedirs(dst)