Skip to content

Commit

Permalink
Towards uniform unit handling
Browse files Browse the repository at this point in the history
- remove nT <--> T handling from inventory_review in sandbox
- replace with triage_mt_units_magnetic_field
- make triage_units a list, not a string, that can handle electrics an magnetics
- make MT2SI_MAGNETIC_FIELD_FILTER
  • Loading branch information
kkappler committed Jan 6, 2024
1 parent 048720f commit 4f16e68
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
28 changes: 16 additions & 12 deletions aurora/sandbox/io_helpers/inventory_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def describe_inventory_stages(inventory, assign_names=False, verbose=False):
new_name = f"{station.code}_{channel.code}_{i}"
stage.name = new_name
if verbose:
logger.info(f"ASSIGNING stage {stage}, name {stage.name}")
logger.info(

Check warning on line 41 in aurora/sandbox/io_helpers/inventory_review.py

View check run for this annotation

Codecov / codecov/patch

aurora/sandbox/io_helpers/inventory_review.py#L41

Added line #L41 was not covered by tests
f"ASSIGNING stage {stage}, name {stage.name}"
)
if hasattr(stage, "symmetry"):
pass
# import matplotlib.pyplot as plt
Expand Down Expand Up @@ -116,15 +118,17 @@ def scan_inventory_for_nonconformity(inventory, verbose=False):
# print("Applied unstable fix to magnetic channel names")
logger.info("{T1,T2,T3} --> {F1, F2, F3}")

# Tesla to nanoTesla
for channel in station:
response = channel.response
for stage in response.response_stages:
if verbose:
msg = f"{channel.code} {stage.stage_sequence_number}"
msg = f"{msg} {stage.input_units}"
logger.info(msg)
if stage.input_units == "T":
stage.input_units = "nT"
stage.stage_gain *= 1e-9
# Below represents correct syntax for a valid, but not conventional way to correct units
# leaving the code (commented) for future reference
# # Tesla to nanoTesla
# for channel in station:
# response = channel.response
# for stage in response.response_stages:
# if verbose:
# msg = f"{channel.code} {stage.stage_sequence_number}"
# msg = f"{msg} {stage.input_units}"
# logger.info(msg)
# if stage.input_units == "T":
# stage.input_units = "nT"
# stage.stage_gain *= 1e-9
return inventory
7 changes: 5 additions & 2 deletions aurora/sandbox/io_helpers/make_mth5_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from aurora.sandbox.obspy_helpers import trim_streams_to_acquisition_run
from aurora.sandbox.triage_metadata import triage_missing_coil_hollister
from aurora.sandbox.triage_metadata import triage_mt_units_electric_field
from aurora.sandbox.triage_metadata import triage_mt_units_magnetic_field
from mt_metadata.timeseries.stationxml import XMLInventoryMTExperiment
from mth5.utils.helpers import initialize_mth5
from mth5.timeseries import RunTS
Expand All @@ -18,7 +19,7 @@ def create_from_server_multistation(
target_folder=Path(),
run_id="001",
force_align_streams=True,
triage_units=None,
triage_units=[],
triage_missing_coil=False,
**kwargs,
):
Expand Down Expand Up @@ -54,8 +55,10 @@ def create_from_server_multistation(

# TRIAGE ONE-OFF ISSUE WITH UNITS
if triage_units:
if triage_units == "V/m to mV/km":
if "V/m to mV/km" in triage_units:
experiment = triage_mt_units_electric_field(experiment)
if "T to nT" in triage_units:
experiment = triage_mt_units_magnetic_field(experiment)
if triage_missing_coil:
experiment = triage_missing_coil_hollister(experiment)

Expand Down
33 changes: 33 additions & 0 deletions aurora/sandbox/triage_metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from aurora.time_series.filters.filter_helpers import MT2SI_ELECTRIC_FIELD_FILTER
from aurora.time_series.filters.filter_helpers import MT2SI_MAGNETIC_FIELD_FILTER

from loguru import logger


Expand Down Expand Up @@ -33,6 +35,37 @@ def triage_mt_units_electric_field(experiment):
return experiment


def triage_mt_units_magnetic_field(experiment):
"""
One-off example of adding a filter to an mth5 in the case where the electric
field data are given in V/m, but they were expected in mV/km. This adds the
correct filter to the metadata so that the calibrated data have units of
mV/km.
Parameters
----------
experiment ;
Returns
-------
"""
logger.info(
f"Add MT2SI_MAGNETIC_FIELD_FILTER to magnetic channels for parkfield"
f" {MT2SI_MAGNETIC_FIELD_FILTER} "
)
filter_name = MT2SI_MAGNETIC_FIELD_FILTER.name
survey = experiment.surveys[0]
survey.filters[filter_name] = MT2SI_MAGNETIC_FIELD_FILTER
stations = survey.stations
for station in stations:
channels = station.runs[0].channels
for channel in channels:
if channel.component[0] == "h":
channel.filter.name.insert(0, filter_name)
channel.filter.applied.insert(0, True)
return experiment


def triage_missing_coil_hollister(experiment):
"""
One off for hollister missing hy metadata for no reason I can tell
Expand Down
2 changes: 1 addition & 1 deletion aurora/test_utils/parkfield/make_parkfield_mth5.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def make_pkdsao_mth5(fdsn_dataset):
h5_path = create_from_server_multistation(
fdsn_dataset,
target_folder=PARKFIELD_PATHS["data"],
triage_units="V/m to mV/km",
triage_units=["V/m to mV/km", "T to nT"],
)

for station in fdsn_dataset.station.split(","):
Expand Down
18 changes: 18 additions & 0 deletions aurora/time_series/filters/filter_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,25 @@ def make_volt_per_meter_to_millivolt_per_km_converter():
return coeff_filter


def make_tesla_to_nanotesla_converter():
"""
This represents a filter that converts from nt to T.
Returns
-------
"""
coeff_filter = make_coefficient_filter(
gain=1e-9,
units_in="nanotesla",
units_out="tesla",
name="MT to SI magnetic field conversion",
)
return coeff_filter


MT2SI_ELECTRIC_FIELD_FILTER = make_volt_per_meter_to_millivolt_per_km_converter()
MT2SI_MAGNETIC_FIELD_FILTER = make_tesla_to_nanotesla_converter()


def main():
Expand Down

0 comments on commit 4f16e68

Please sign in to comment.