Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to specify complex, time-dependent station combinations #73

Merged
merged 1 commit into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions obspyck/example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ S = blue
[station_combinations]
EXAMPLE1 = II.PFO.00.LH?,GE.APE..LH?,GE.PSZ..LH?,CI.BBR..LH*,IV.PRMA..LH?,G.RER..LH*
EXAMPLE2 = 7A.W01..LH?,CI.BBR..LH*
# for complex, time-dependent station combinations use an exclamation mark
# followed by a corresponding section title where the station combination is
# defined
EXAMPLE3 = !station_combination_BW

[station_combination_BW]
# this is an example section for a complex, time-dependent station combination.
# this can be useful e.g. in case of instrumentation changes that go along with
# a change in channel naming or when stations get added or removed in the
# field. each entry consists of a stream identifier as the key and zero, one or
# two timestamps to denote for which times the stream should be fetched.
# All times interpreted as UTC.
#
# if value is empty, always fetch stream, regardless of requested time
GR.FUR..HH? =
# otherwise, value can be..
# - two timestamps separated by a comma, to fetch stream only for times in
# between these two timestamps
BW.RTBE..HH? = 2017-07-01,2017-10-01
# - a timestamp preceded by a "larger than" sign, to fetch stream only for times after the
# given timestamp (including it, so this is actually evaluated as >=)
BW.RMOA..HH? = >2017-07-04T15:00:00
# - a comma followed by a timestamp, to fetch stream only for times before the
# given timestamp (including it, so this is actually evaluated as <=)
BW.RMOA..EH? = <2017-07-04T11:40:00

[seed_id_lookup]
# wildcards ("?", "*") and regex (e.g. "[EH]*") are only allowed in channel!
Expand Down
50 changes: 49 additions & 1 deletion obspyck/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,47 @@ def check_keybinding_conflicts(keys):
raise Exception(err)


def resolve_complex_station_combination(config, section_title, time):
"""
Resolve complex time-dependent station combination from config file
"""
seed_ids = set()
for key, value in config.items(section_title):
# no value: always use that stream
if not value:
seed_ids.add(key)
# two comma separated time stamps: only use stream if requested time is
# in between given timestamps
elif ',' in value:
start, end = map(UTCDateTime, value.split(','))
if end < start:
msg = ("Invalid configuration in section [{}], stream '{}'. "
"End time larger than start time for used time "
"span.").format(section_title, key)
raise ValueError(msg)
if start <= time <= end:
seed_ids.add(key)
# timestamp preceded by smaller-than sign: only use stream if requested
# time is smaller than specified timestamp
elif value[0] == '<':
end = UTCDateTime(value[1:])
if time <= end:
seed_ids.add(key)
# timestamp preceded by larger-than sign: only use stream if requested
# time is after the specified timestamp
elif value[0] == '>':
start = UTCDateTime(value[1:])
if time >= start:
seed_ids.add(key)
else:
msg = ("Invalid configuration in section [{}], stream '{}'. "
"Value can be empty, two comma-separated time stamps or "
"one timestamp preceded by either '>' or '<'.").format(
section_title, key)
raise ValueError(msg)
return seed_ids


def fetch_waveforms_with_metadata(options, args, config):
"""
Sets up obspy clients and fetches waveforms and metadata according to
Expand Down Expand Up @@ -269,7 +310,14 @@ def fetch_waveforms_with_metadata(options, args, config):
seed_ids_to_fetch = set()
for station_combination_key in options.station_combinations:
seed_ids = config.get("station_combinations", station_combination_key)
for seed_id in seed_ids.split(","):
# starts with an exclamation mark: lookup seed ids from that section
if seed_ids[0] == '!':
seed_ids = resolve_complex_station_combination(
config, seed_ids[1:], t1)
# otherwise a list of comma-separated stream labels
else:
seed_ids = seed_ids.split(",")
for seed_id in seed_ids:
seed_ids_to_fetch.add(seed_id)
for seed_id in options.seed_ids:
seed_ids_to_fetch.add(seed_id)
Expand Down