Skip to content

Commit

Permalink
Explicitly pass eventid to setup functions
Browse files Browse the repository at this point in the history
Since the `event` object might not be yet in the `config` object.
  • Loading branch information
claudiodsf committed Jul 15, 2024
1 parent be0b7b6 commit 0aa332b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
12 changes: 6 additions & 6 deletions sourcespec2/source_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
31 changes: 20 additions & 11 deletions sourcespec2/ssp_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 0aa332b

Please sign in to comment.