Skip to content

Commit

Permalink
Use the global config object in ssp_output.py, ssp_qml_output.py, a…
Browse files Browse the repository at this point in the history
…nd ssp_sqlite_output.py
  • Loading branch information
claudiodsf committed Jul 9, 2024
1 parent 298d33e commit bb3da95
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
4 changes: 2 additions & 2 deletions sourcespec2/source_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 50 additions & 18 deletions sourcespec2/ssp_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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', {})
Expand Down Expand Up @@ -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
Expand All @@ -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':
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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']
Expand All @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions sourcespec2/ssp_qml_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -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
Expand Down
27 changes: 13 additions & 14 deletions sourcespec2/ssp_sqlite_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down

0 comments on commit bb3da95

Please sign in to comment.