diff --git a/sourcespec2/setup/config.py b/sourcespec2/setup/config.py index fab2e322..4fd38472 100644 --- a/sourcespec2/setup/config.py +++ b/sourcespec2/setup/config.py @@ -112,6 +112,12 @@ class _Config(dict): Import the global config object instead. """ def __init__(self): + """Initialize the config object with default values.""" + super().__init__() + self._set_defaults() + + def _set_defaults(self): + """Set the default values for the config object.""" # Additional config values that must exist for the code to run without # errors. They must be defined using the dict syntax. self['running_from_command_line'] = False @@ -136,6 +142,11 @@ def __init__(self): config_obj = get_default_config_obj(configspec) self.update(config_obj.dict()) + def reset(self): + """Reset the config object to the default values.""" + super().clear() + self._set_defaults() + def __setitem__(self, key, value): """Make Config keys accessible as attributes.""" super().__setattr__(key, value) diff --git a/sourcespec2/source_spec.py b/sourcespec2/source_spec.py index 5c5782cc..e7d40f7c 100644 --- a/sourcespec2/source_spec.py +++ b/sourcespec2/source_spec.py @@ -17,6 +17,44 @@ """ +def ssp_clear_state(reset_config=False, clear_options=True): + """ + Clear state from a previous run. + Some submodules contain global variables caching information + about the current run, which need to be cleared before + launching a new run. + + :param reset_config: whether to reset global config as well + :type reset_config: bool + :param clear_options: whether to clear options in global config + :type clear_options: bool + """ + # pylint: disable=import-outside-toplevel + from .setup import logging + logging.OLDLOGFILE = None + # Not sure if we should reset LOGGER? + # logging.LOGGER = None + + from . import ssp_wave_arrival + ssp_wave_arrival.add_arrival_to_trace.pick_cache = dict() + ssp_wave_arrival.add_arrival_to_trace.travel_time_cache = dict() + ssp_wave_arrival.add_arrival_to_trace.angle_cache = dict() + + from . import ssp_plot_traces + ssp_plot_traces.SAVED_FIGURE_CODES = [] + ssp_plot_traces.BBOX = None + + from . import ssp_plot_spectra + ssp_plot_spectra.SAVED_FIGURE_CODES = [] + ssp_plot_spectra.BBOX = None + + from .setup import config + if reset_config: + config.reset() + elif clear_options: + config.options.clear() + + def ssp_run(st, inventory, ssp_event, picks, allow_exit=False): """ Run source_spec as function with collected traces, station inventory, @@ -50,6 +88,9 @@ def ssp_run(st, inventory, ssp_event, picks, allow_exit=False): if not allow_exit: ssp_exit.SSP_EXIT_CALLED = True + # Clear state from possible previous run + ssp_clear_state(reset_config=False, clear_options=False) + # Create output folder if required, save config and setup logging from .setup import get_outdir_path, save_config, setup_logging if getattr(config.options, 'outdir', None):