Skip to content

Commit

Permalink
Do not pass the config object to functions anymore
Browse files Browse the repository at this point in the history
This simplifies the code overall!
  • Loading branch information
claudiodsf committed Jun 19, 2024
1 parent 9bec96b commit 0b63ca5
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 266 deletions.
10 changes: 3 additions & 7 deletions requake/catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""
import numpy as np
from obspy import UTCDateTime
from ..config.rq_setup import config
from ..formulas.conversion import float_or_none
from ..waveforms.station_metadata import get_traceid_coords

Expand Down Expand Up @@ -248,13 +249,10 @@ def filter(
return outcat


def read_stored_catalog(config):
def read_stored_catalog():
"""
Read the catalog stored in the output directory.
:param config: Configuration object.
:type config: config.Config
:return: Catalog object.
:rtype: RequakeCatalog
Expand All @@ -278,14 +276,12 @@ def read_stored_catalog(config):
) from m


def fix_non_locatable_events(catalog, config):
def fix_non_locatable_events(catalog):
"""
Fix non-locatable events in catalog.
:param catalog: a RequakeCatalog object
:type catalog: RequakeCatalog
:param config: a Config object
:type config: config.Config
"""
if not any(ev.lat is None or ev.lon is None for ev in catalog):
return
Expand Down
10 changes: 4 additions & 6 deletions requake/catalog/print_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@
(https://www.gnu.org/licenses/gpl-3.0-standalone.html)
"""
import logging
from ..config.rq_setup import config
from ..config.rq_setup import rq_exit
from .catalog import read_stored_catalog
from ..config.generic_printer import generic_printer
from ..config.rq_setup import rq_exit
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])


def print_catalog(config):
def print_catalog():
"""
Print the event catalog to screen.
:param config: Configuration object.
:type config: config.Config
"""
try:
catalog = read_stored_catalog(config)
catalog = read_stored_catalog()
except (ValueError, FileNotFoundError) as m:
logger.error(m)
rq_exit(1)
Expand Down
21 changes: 8 additions & 13 deletions requake/catalog/read_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
import os
import logging
import contextlib
from ..catalog.catalog import RequakeCatalog
from ..config.rq_setup import config
from ..config.rq_setup import rq_exit
from ..catalog.catalog import RequakeCatalog
from .read_catalog_from_fdsnws import read_catalog_from_fdsnws
from .read_catalog_from_quakeml import read_catalog_from_quakeml
from .read_catalog_from_csv import read_catalog_from_csv
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])


def _read_catalog_from_file(config):
def _read_catalog_from_file():
"""
Read an event catalog from a file.
Supported formats are QuakeML, FDSN text and CSV.
:param config: Configuration object.
:type config: requake.rq_setup.RequakeConfig
:return: Event catalog.
:rtype: requake.catalog.RequakeCatalog
Expand All @@ -54,15 +53,13 @@ def _read_catalog_from_file(config):
return read_catalog_from_csv(catalog_file)


def _filter_catalog(catalog, config):
def _filter_catalog(catalog):
"""
Filter an event catalog, based on the criteria specified
in the configuration.
:param catalog: Event catalog.
:type catalog: requake.catalog.RequakeCatalog
:param config: Configuration object.
:type config: requake.rq_setup.RequakeConfig
:return: Filtered event catalog.
:rtype: requake.catalog.RequakeCatalog
"""
Expand Down Expand Up @@ -93,14 +90,12 @@ def _filter_catalog(catalog, config):
return outcat


def read_catalog(config):
def read_catalog():
"""
Read an event catalog from web services or from a file.
Write the catalog to the output directory.
:param config: Configuration object.
:type config: requake.rq_setup.RequakeConfig
:return: Event catalog.
:rtype: requake.catalog.RequakeCatalog
"""
Expand All @@ -117,17 +112,17 @@ def read_catalog(config):
input_cat_file = config.args.catalog_file
if input_cat_file is not None:
try:
catalog += _read_catalog_from_file(config)
catalog += _read_catalog_from_file()
# Filter catalog based on configuration
catalog = _filter_catalog(catalog, config)
catalog = _filter_catalog(catalog)
except FileNotFoundError:
logger.error(f'File "{input_cat_file}" not found')
rq_exit(1)
except ValueError as m:
logger.error(f'Error reading catalog file "{input_cat_file}": {m}')
rq_exit(1)
else:
catalog += read_catalog_from_fdsnws(config)
catalog += read_catalog_from_fdsnws()
if not catalog:
logger.error('No event read')
rq_exit(1)
Expand Down
5 changes: 2 additions & 3 deletions requake/catalog/read_catalog_from_fdsnws.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import urllib.request
from obspy import UTCDateTime
from obspy.clients.fdsn.header import URL_MAPPINGS
from ..config.rq_setup import config
from ..catalog.catalog import RequakeCatalog, RequakeEvent
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])

Expand Down Expand Up @@ -100,12 +101,10 @@ def _get_events_from_fdsnws(
return cat


def read_catalog_from_fdsnws(config):
def read_catalog_from_fdsnws():
"""
Read an event catalog from FDSN web services.
:param config: Configuration object.
:type config: requake.rq_setup.RequakeConfig
:return: Event catalog.
:rtype: requake.catalog.RequakeCatalog
"""
Expand Down
5 changes: 2 additions & 3 deletions requake/config/generic_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
import contextlib
import logging
from tabulate import tabulate
from ..config.rq_setup import config
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])


def generic_printer(config, rows, headers_fmt, print_headers=True):
def generic_printer(rows, headers_fmt, print_headers=True):
"""
A generic printer function for Requake.
:param config: Configuration object.
:type config: config.Config
:param rows: Rows to print.
:type rows: list of rows
:param headers_fmt: Headers and format strings.
Expand Down
35 changes: 23 additions & 12 deletions requake/config/rq_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
# pylint: disable=global-statement,import-outside-toplevel

config = None # pylint: disable=invalid-name
logger = None # pylint: disable=invalid-name
PYTHON_VERSION_STR = None
NUMPY_VERSION_STR = None
Expand Down Expand Up @@ -64,7 +65,7 @@ def _make_outdir(outdir):
''')


def _setup_logging(config, progname, action_name):
def _setup_logging(progname, action_name):
"""Set up the logging infrastructure."""
global logger

Expand Down Expand Up @@ -120,7 +121,7 @@ def emit(self, record):
logger.debug(' '.join(sys.argv))


def _connect_station_dataselect(config):
def _connect_station_dataselect():
"""
Connect to station and dataselect services.
Expand All @@ -139,7 +140,7 @@ def _connect_station_dataselect(config):
f'Connected to FDSN station server: {config.fdsn_station_url}'
)
if config.waveform_data_path is not None:
_connect_sds(config)
_connect_sds()
else:
config.dataselect_client = FDSNClient(config.fdsn_dataselect_url)
logger.info(
Expand All @@ -148,7 +149,7 @@ def _connect_station_dataselect(config):
)


def _connect_sds(config):
def _connect_sds():
"""
Connect to a local SeisComP Data Structure (SDS) archive.
"""
Expand All @@ -167,7 +168,7 @@ def _connect_sds(config):
logger.info(f'Found the following NSLC codes:\n{all_nslc_str}')


def _parse_catalog_options(config):
def _parse_catalog_options():
"""Parse catalog options into lists."""
config.catalog_start_times = []
config.catalog_end_times = []
Expand All @@ -189,7 +190,7 @@ def _parse_catalog_options(config):
config.catalog_end_times.append(UTCDateTime(end_time))


def _connect_fdsn_catalog(config):
def _connect_fdsn_catalog():
"""Connect to FDSN catalog services."""
config.catalog_fdsn_event_clients = []
for url in config.catalog_fdsn_event_urls:
Expand All @@ -198,7 +199,18 @@ def _connect_fdsn_catalog(config):


def configure(args):
"""Read command line arguments. Read config file. Set up logging."""
"""
Configure Requake.
This function is called by the main script to set up the configuration
object and the logging infrastructure.
:param args: The parsed command-line arguments.
:type args: argparse.Namespace
"""
global config
if config is not None:
return
configspec = parse_configspec()
if args.action == 'sample_config':
write_sample_config(configspec, 'requake')
Expand Down Expand Up @@ -251,26 +263,25 @@ def configure(args):
# Check library versions
_check_library_versions()
# Set up logging
_setup_logging(config, 'requake', args.action)
_setup_logging('requake', args.action)
# save config to output dir
shutil.copy(args.configfile, args.outdir)
_parse_catalog_options(config)
_parse_catalog_options()
actions_needing_fdsn_station_dataselect = (
'scan_catalog', 'plot_pair', 'plot_families', 'build_templates',
'scan_templates'
)
try:
if args.action in actions_needing_fdsn_station_dataselect:
_connect_station_dataselect(config)
_connect_station_dataselect()
if args.action == 'read_catalog' and not args.catalog_file:
_connect_fdsn_catalog(config)
_connect_fdsn_catalog()
except (FileNotFoundError, ValueError, FDSNNoServiceException) as m:
logger.error(m)
rq_exit(1)
# Template times must be UTCDateTime objects
config.template_start_time = UTCDateTime(config.template_start_time)
config.template_end_time = UTCDateTime(config.template_end_time)
return config


def rq_exit(retval=0, abort=False, progname='requake'):
Expand Down
23 changes: 8 additions & 15 deletions requake/families/build_families.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
import csv
from itertools import combinations
from scipy.cluster.hierarchy import average, fcluster
from ..config.rq_setup import config
from ..config.rq_setup import rq_exit
from .pairs import read_events_from_pairs_file
from .families import Family
from ..config.rq_setup import rq_exit
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])


def _check_options(config):
def _check_options():
"""
Check the consistency of the configuration options.
:param config: configuration object
:type config: config.Config
:raises ValueError: if the configuration options are inconsistent
"""
sort_by = config.sort_families_by
Expand Down Expand Up @@ -112,12 +110,10 @@ def _build_families_from_upgma(events, cc_min):
return families


def _write_families(config, families):
def _write_families(families):
"""
Write families to file.
:param config: configuration object
:type config: config.Config
:param families: list of families
:type families: list
"""
Expand Down Expand Up @@ -147,21 +143,18 @@ def _write_families(config, families):
])


def build_families(config):
def build_families():
"""
Build families of repeating earthquakes from a catalog of pairs.
:param config: configuration object
:type config: config.Config
"""
try:
_check_options(config)
_check_options()
except ValueError as e:
logger.error(e)
rq_exit(1)
try:
logger.info('Reading events from pairs file...')
events = read_events_from_pairs_file(config)
events = read_events_from_pairs_file()
except FileNotFoundError:
logger.error(
'Unable to find event pairs file: '
Expand All @@ -174,5 +167,5 @@ def build_families(config):
elif config.clustering_algorithm == 'UPGMA':
logger.info('Building families using UPGMA...')
families = _build_families_from_upgma(events, config.cc_min)
_write_families(config, families)
_write_families(families)
logger.info(f'Done! Output written to: {config.build_families_outfile}')
Loading

0 comments on commit 0b63ca5

Please sign in to comment.