From ea217e8bb60819ba5d3434f54c7bb4dc54319a43 Mon Sep 17 00:00:00 2001 From: Kris Vanneste Date: Fri, 10 Jan 2025 13:32:46 +0100 Subject: [PATCH 1/3] Added ssp_clear_state function. Clear state from possible previous run at beginning of ssp_run function. --- sourcespec2/source_spec.py | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/sourcespec2/source_spec.py b/sourcespec2/source_spec.py index 5c5782cc..fa77a375 100644 --- a/sourcespec2/source_spec.py +++ b/sourcespec2/source_spec.py @@ -17,6 +17,45 @@ """ +def ssp_clear_state(clear_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 clear_config: whether to clear global config as well + :type clear_config: bool + :param clear_options: whether to clear options in global config + :type clear_options: bool + """ + 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 clear_config: + # This clears the entire config, which is not what we want + #config.clear() + config.__init__() + 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 +89,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(clear_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): From 9f8718423725e8ed310d834aea78e3877dd11e97 Mon Sep 17 00:00:00 2001 From: Claudio Satriano Date: Thu, 25 Sep 2025 12:01:07 +0200 Subject: [PATCH 2/3] A bit of linting cleanup --- sourcespec2/source_spec.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sourcespec2/source_spec.py b/sourcespec2/source_spec.py index fa77a375..bfe5fa1f 100644 --- a/sourcespec2/source_spec.py +++ b/sourcespec2/source_spec.py @@ -29,10 +29,11 @@ def ssp_clear_state(clear_config=False, clear_options=True): :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 + # logging.LOGGER = None from . import ssp_wave_arrival ssp_wave_arrival.add_arrival_to_trace.pick_cache = dict() @@ -50,7 +51,7 @@ def ssp_clear_state(clear_config=False, clear_options=True): from .setup import config if clear_config: # This clears the entire config, which is not what we want - #config.clear() + # config.clear() config.__init__() elif clear_options: config.options.clear() From b319216b12b267a293e7a6e79c6ea7ba63865726 Mon Sep 17 00:00:00 2001 From: Claudio Satriano Date: Thu, 25 Sep 2025 12:05:11 +0200 Subject: [PATCH 3/3] Add a reset method to the config class to reset to default values --- sourcespec2/setup/config.py | 11 +++++++++++ sourcespec2/source_spec.py | 14 ++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) 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 bfe5fa1f..e7d40f7c 100644 --- a/sourcespec2/source_spec.py +++ b/sourcespec2/source_spec.py @@ -17,15 +17,15 @@ """ -def ssp_clear_state(clear_config=False, clear_options=True): +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 clear_config: whether to clear global config as well - :type clear_config: bool + :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 """ @@ -49,10 +49,8 @@ def ssp_clear_state(clear_config=False, clear_options=True): ssp_plot_spectra.BBOX = None from .setup import config - if clear_config: - # This clears the entire config, which is not what we want - # config.clear() - config.__init__() + if reset_config: + config.reset() elif clear_options: config.options.clear() @@ -91,7 +89,7 @@ def ssp_run(st, inventory, ssp_event, picks, allow_exit=False): ssp_exit.SSP_EXIT_CALLED = True # Clear state from possible previous run - ssp_clear_state(clear_config=False, clear_options=False) + 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