Skip to content

Commit

Permalink
Export a global config object
Browse files Browse the repository at this point in the history
TODO: not yet used in the rest of the code
  • Loading branch information
claudiodsf committed Jul 4, 2024
1 parent 5d11542 commit 262ba75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion sourcespec2/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
CeCILL Free Software License Agreement v2.1
(http://www.cecill.info/licences.en.html)
"""
from .config import configure # noqa
from .config import configure, config # noqa
from .library_versions import library_versions # noqa
53 changes: 32 additions & 21 deletions sourcespec2/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@

class Config(dict):
"""Config class for sourcespec."""
def __init__(self):
# Initialize config object to the default values
configspec = _parse_configspec()
config_obj = _get_default_config_obj(configspec)
self.update(config_obj.dict())
# Additional config values
self.vertical_channel_codes = ['Z']
self.horizontal_channel_codes_1 = ['N', 'R']
self.horizontal_channel_codes_2 = ['E', 'T']
# Empty options object, for compatibility with the command line version
self.options = types.SimpleNamespace()
# A list of warnings to be issued when logger is set up
self.warnings = []
# Create a dict to store figure paths
self.figures = defaultdict(list)
# store the absolute path of the current working directory
self.workdir = os.getcwd()
# SEED standard instrument codes:
# https://ds.iris.edu/ds/nodes/dmc/data/formats/seed-channel-naming/
self.INSTR_CODES_VEL = ['H', 'L']
self.INSTR_CODES_ACC = ['N', ]

def __setitem__(self, key, value):
"""Make Config keys accessible as attributes."""
Expand Down Expand Up @@ -348,14 +369,10 @@ def _check_mandatory_config_params(config_obj):
sys.exit(msg)


def _init_instrument_codes(config):
def _update_instrument_codes(config):
"""
Initialize instrument codes from config file.
Update instrument codes from config file.
"""
# SEED standard instrument codes:
# https://ds.iris.edu/ds/nodes/dmc/data/formats/seed-channel-naming/
config.INSTR_CODES_VEL = ['H', 'L']
config.INSTR_CODES_ACC = ['N', ]
# User-defined instrument codes:
instr_code_acc_user = config.instrument_code_acceleration
instr_code_vel_user = config.instrument_code_velocity
Expand Down Expand Up @@ -461,6 +478,12 @@ def _none_lenght(input_list):
return 1 if input_list is None else len(input_list)


# Global config object, initialized with default values
# API users should use this object to access configuration parameters
# and update them as needed
config = Config()


def configure(options=None, progname='source_spec', config_overrides=None):
"""
Parse command line arguments and read config file.
Expand Down Expand Up @@ -547,20 +570,15 @@ def configure(options=None, progname='source_spec', config_overrides=None):
options.outdir = os.path.join(options.outdir, f'no_evid_{hexstr}')
_write_config(config_obj, progname, options.outdir)

# Create a Config object
config = Config(config_obj.dict().copy())

# Update config object with the contents of the config file
config.update(config_obj.dict())
# Add options to config:
config.options = options

# Override station_metadata config option with command line option
if getattr(options, 'station_metadata', None):
config.station_metadata = options.station_metadata

# Additional config values
config.vertical_channel_codes = ['Z']
config.horizontal_channel_codes_1 = ['N', 'R']
config.horizontal_channel_codes_2 = ['E', 'T']
msc = config.mis_oriented_channels
if msc is not None:
config.vertical_channel_codes.append(msc[0])
Expand Down Expand Up @@ -608,9 +626,6 @@ def configure(options=None, progname='source_spec', config_overrides=None):
except ValueError as msg:
sys.exit(f'Error parsing parameter "Er_freq_range": {msg}')

# A list of warnings to be issued when logger is set up
config.warnings = []

if config.html_report:
if not config.plot_save:
msg = (
Expand Down Expand Up @@ -638,11 +653,7 @@ def configure(options=None, progname='source_spec', config_overrides=None):
except ImportError as err:
sys.exit(err)

_init_instrument_codes(config)
_update_instrument_codes(config)
_init_traceid_map(config)
_init_plotting(config.plot_show)
# Create a dict to store figure paths
config.figures = defaultdict(list)
# store the absolute path of the current working directory
config.workdir = os.getcwd()
return config

0 comments on commit 262ba75

Please sign in to comment.