From 12c5ef238e587f9aca5d3ebe8e833b4903886b22 Mon Sep 17 00:00:00 2001 From: Claudio Satriano Date: Tue, 9 Jul 2024 19:18:46 +0200 Subject: [PATCH] Use the global `config` object in ssp_output.py, ssp_qml_output.py, and ssp_sqlite_output.py --- sourcespec2/source_spec.py | 4 +- sourcespec2/ssp_output.py | 68 +++++++++++++++++++++++--------- sourcespec2/ssp_qml_output.py | 10 ++++- sourcespec2/ssp_sqlite_output.py | 27 ++++++------- 4 files changed, 73 insertions(+), 36 deletions(-) diff --git a/sourcespec2/source_spec.py b/sourcespec2/source_spec.py index b81a3586..d631413c 100644 --- a/sourcespec2/source_spec.py +++ b/sourcespec2/source_spec.py @@ -73,8 +73,8 @@ def main(): # Save output from .ssp_output import write_output, save_spectra - write_output(config, sspec_output) - save_spectra(config, spec_st) + write_output(sspec_output) + save_spectra(spec_st) # Save residuals from .ssp_residuals import spectral_residuals diff --git a/sourcespec2/ssp_output.py b/sourcespec2/ssp_output.py index 1657bc02..0a4f1f71 100644 --- a/sourcespec2/ssp_output.py +++ b/sourcespec2/ssp_output.py @@ -22,13 +22,14 @@ from datetime import datetime from tzlocal import get_localzone import numpy as np +from .config import config from .ssp_qml_output import write_qml from .ssp_sqlite_output import write_sqlite from ._version import get_versions logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1]) -def _write_author_and_agency_to_parfile(config, parfile): +def _write_author_and_agency_to_parfile(parfile): author_str = empty_author_str = '\n*** Author:' if config.author_name is not None: author_str += f' {config.author_name}' @@ -65,7 +66,7 @@ def _value_error_str(value, error, fmt): return s -def _write_parfile(config, sspec_output): +def _write_parfile(sspec_output): """ Write station source parameters to file. @@ -286,13 +287,23 @@ def _write_parfile(config, sspec_output): f'{config.end_of_run_tz}') if config.options.run_id: parfile.write(f'\n*** Run ID: {config.options.run_id}') - _write_author_and_agency_to_parfile(config, parfile) + _write_author_and_agency_to_parfile(parfile) logger.info(f'Output written to file: {parfilename}') def _dict2yaml(dict_like, level=0): - """Serialize a dict-like object into YAML format.""" + """ + Serialize a dict-like object into YAML format. + + :param dict_like: Dict-like object to serialize. + :type dict_like: dict + :param level: Indentation level. + :type level: int + + :return: YAML-formatted string. + :rtype: str + """ if not isinstance(dict_like, Mapping): raise TypeError('dict_like must be a dict-like object') comments = dict_like.get('_comments', {}) @@ -329,8 +340,13 @@ def _dict2yaml(dict_like, level=0): return lines -def _write_yaml(config, sspec_output): - """Write sspec output in a YAML file.""" +def _write_yaml(sspec_output): + """ + Write sspec output in a YAML file. + + :param sspec_output: Output of the spectral inversion. + :type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput` + """ if not os.path.exists(config.options.outdir): os.makedirs(config.options.outdir) evid = config.event.event_id @@ -346,7 +362,13 @@ def _write_yaml(config, sspec_output): logger.info(f'Output written to file: {yamlfilename}') -def _write_hypo71(config, sspec_output): +def _write_hypo71(sspec_output): + """ + Write source parameters to hypo71 file. + + :param sspec_output: Output of the spectral inversion. + :type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput` + """ if not config.options.hypo_file: return if config.hypo_file_format != 'hypo71': @@ -376,7 +398,7 @@ def _write_hypo71(config, sspec_output): logger.info(f'Hypo file written to: {hypo_file_out}') -def _make_symlinks(config): +def _make_symlinks(): """Make symlinks to input files into output directory.""" # Windows does not support symlinks if os.name == 'nt': @@ -406,8 +428,13 @@ def _make_symlinks(config): os.symlink(filename, linkname) -def write_output(config, sspec_output): - """Write results into different formats.""" +def write_output(sspec_output): + """ + Write results into different formats. + + :param sspec_output: Output of the spectral inversion. + :type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput` + """ # Add run info to output object run_info = sspec_output.run_info run_info.SourceSpec_version = get_versions()['version'] @@ -423,21 +450,26 @@ def write_output(config, sspec_output): run_info.agency_short_name = config.agency_short_name run_info.agency_url = config.agency_url # Symlink input files into output directory - _make_symlinks(config) + _make_symlinks() # Write to parfile (deprecated) - _write_parfile(config, sspec_output) + _write_parfile(sspec_output) # Write to YAML file - _write_yaml(config, sspec_output) + _write_yaml(sspec_output) # Write to SQLite database, if requested - write_sqlite(config, sspec_output) + write_sqlite(sspec_output) # Write to hypo file, if requested - _write_hypo71(config, sspec_output) + _write_hypo71(sspec_output) # Write to quakeml file, if requested - write_qml(config, sspec_output) + write_qml(sspec_output) + +def save_spectra(spec_st): + """ + Save spectra to file. -def save_spectra(config, spec_st): - """Save spectra to file.""" + :param spec_st: Stream of spectra. + :type spec_st: :class:`~sourcespec.spectrum.SpectrumStream` + """ if not config.save_spectra: return outfile = os.path.join( diff --git a/sourcespec2/ssp_qml_output.py b/sourcespec2/ssp_qml_output.py index d0bae428..48195a0b 100644 --- a/sourcespec2/ssp_qml_output.py +++ b/sourcespec2/ssp_qml_output.py @@ -19,6 +19,7 @@ MomentTensor, QuantityError, ResourceIdentifier, StationMagnitude, StationMagnitudeContribution, WaveformStreamID) +from .config import config from ._version import get_versions logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1]) @@ -63,8 +64,13 @@ def __init__(self, value=None): self.value = value -def write_qml(config, sspec_output): - """Write QuakeML output.""" +def write_qml(sspec_output): + """ + Write QuakeML output. + + :param sspec_output: Output from spectral inversion. + :type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput` + """ if not config.options.qml_file: config.qml_file_out = None return diff --git a/sourcespec2/ssp_sqlite_output.py b/sourcespec2/ssp_sqlite_output.py index 72189165..9c7aacfc 100644 --- a/sourcespec2/ssp_sqlite_output.py +++ b/sourcespec2/ssp_sqlite_output.py @@ -12,6 +12,7 @@ import os.path import logging import sqlite3 +from .config import config from .ssp_setup import ssp_exit from .ssp_db_definitions import ( DB_VERSION, @@ -38,6 +39,7 @@ def _open_sqlite_db(db_file): :param db_file: SQLite database file :type db_file: str + :return: SQLite connection and cursor :rtype: tuple """ @@ -131,7 +133,7 @@ def _create_stations_table(cursor, db_file): _log_db_write_error(db_err, db_file) -def _write_stations_table(cursor, db_file, sspec_output, config): +def _write_stations_table(cursor, db_file, sspec_output): """ Write station source parameters to database. @@ -141,8 +143,9 @@ def _write_stations_table(cursor, db_file, sspec_output, config): :type db_file: str :param sspec_output: sspec output object :type sspec_output: ssp_data_types.SourceSpecOutput - :param config: sspec configuration object - :type config: config.Config + + :return: Number of observations + :rtype: int """ event = config.event evid = event.event_id @@ -218,7 +221,7 @@ def _create_events_table(cursor, db_file): _log_db_write_error(db_err, db_file) -def _write_events_table(cursor, db_file, sspec_output, config, nobs): +def _write_events_table(cursor, db_file, sspec_output, nobs): """ Write Events table. @@ -228,8 +231,6 @@ def _write_events_table(cursor, db_file, sspec_output, config, nobs): :type db_file: str :param sspec_output: SSP output object :type sspec_output: ssp_data_types.SourceSpecOutput - :param config: SSP configuration object - :type config: config.Config :param nobs: Number of observations :type nobs: int """ @@ -396,14 +397,12 @@ def _write_events_table(cursor, db_file, sspec_output, config, nobs): ssp_exit(1) -def write_sqlite(config, sspec_output): +def write_sqlite(sspec_output): """ - Write SSP output to SQLite database. + Write SourceSpec output to SQLite database. - :param config: SSP configuration object - :type config: config.Config - :param sspec_output: SSP output object - :type sspec_output: ssp_data_types.SourceSpecOutput + :param sspec_output: SourceSpec output object + :type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput` """ db_file = config.get('database_file', None) if not db_file: @@ -419,13 +418,13 @@ def write_sqlite(config, sspec_output): # Create Stations table _create_stations_table(cursor, db_file) # Write station source parameters to database - nobs = _write_stations_table(cursor, db_file, sspec_output, config) + nobs = _write_stations_table(cursor, db_file, sspec_output) # Commit changes conn.commit() # Create Events table _create_events_table(cursor, db_file) # Write event source parameters to database - _write_events_table(cursor, db_file, sspec_output, config, nobs) + _write_events_table(cursor, db_file, sspec_output, nobs) # Commit changes and close database conn.commit() conn.close()