Skip to content

Commit

Permalink
Handle kwarg channel_metadata to set _sample_rate
Browse files Browse the repository at this point in the history
- self._sample_rate is now set on __init__()
- because it can be None, dict, or mt_metadata.timeseries.Electric
mt_metadata.timeseries.Magnetic, or mt_metadata.timeseries.Auxiliary
and the dict can be flat or nested, a little bit of care was needed
- created a method get_sample_rate_supplied_at_init() of ChannelTS to
  handle this
  • Loading branch information
kkappler committed Nov 30, 2023
1 parent 1577535 commit e6c812a
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion mth5/timeseries/channel_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def __init__(
self.station_metadata = station_metadata
self.run_metadata = run_metadata
self.channel_metadata = channel_metadata
self._sample_rate = None
self._sample_rate = self.get_sample_rate_supplied_at_init(channel_metadata)
# input data
if data is not None:
self.ts = data
Expand All @@ -134,6 +134,43 @@ def __init__(
for key in list(kwargs.keys()):
setattr(self, key, kwargs[key])

def get_sample_rate_supplied_at_init(self, channel_metadata):
"""
Interrogate the channel_metadata argument supplied at init
to see if sample_rate is specified.
channel_metadata can be one of 5 types:
None,
dict
mt_metadata.timeseries.Electric,
mt_metadata.timeseries.Magnetic,
mt_metadata.timeseries.Auxiliary
In case it is a dict, we want to allow the sample_rate to
be at the top layer, adn one layer down, to support, for exmaple
{"electric":{"sample_rate":8.0,}}
"""
sr = None
if channel_metadata is None:
sr = None
elif isinstance(channel_metadata, dict):
#check first two layers for sample_rate key
if "sample_rate" in channel_metadata.keys():
sr = channel_metadata["sample_rate"]
else:
for k, v in channel_metadata.items():
if isinstance(v, dict):
if "sample_rate" in v.keys():
sr = v["sample_rate"]
else:
try:
# if an mt_metadata.timeseries access attr
sr = channel_metadata.sample_rate
except AttributeError:
sr = None
return sr

def __str__(self):
lines = [
f"Survey: {self.survey_metadata.id}",
Expand Down

0 comments on commit e6c812a

Please sign in to comment.