mth5.utils package

Submodules

mth5.utils.exceptions module

Exceptions raised by MTH5

Created on Wed May 13 19:07:21 2020

@author: jpeacock

exception mth5.utils.exceptions.MTH5Error[source]

Bases: Exception

exception mth5.utils.exceptions.MTH5TableError[source]

Bases: Exception

exception mth5.utils.exceptions.MTSchemaError[source]

Bases: Exception

exception mth5.utils.exceptions.MTTSError[source]

Bases: Exception

exception mth5.utils.exceptions.MTTimeError[source]

Bases: Exception

mth5.utils.fdsn_tools module

Tools for FDSN standards

Created on Wed Sep 30 11:47:01 2020

author

Jared Peacock

license

MIT

mth5.utils.fdsn_tools.get_location_code(channel_obj)[source]

Get the location code given the components and channel number

Parameters

channel_obj (Channel) – Channel object

Returns

2 character location code

Return type

string

mth5.utils.fdsn_tools.get_measurement_code(measurement)[source]

get SEED sensor code given the measurement type

Parameters

measurement (string) – measurement type, e.g. * temperature * electric * magnetic

Returns

single character SEED sensor code, if the measurement type has not been defined yet Y is returned.

Return type

string

mth5.utils.fdsn_tools.get_orientation_code(azimuth, orientation='horizontal')[source]

Get orientation code given angle and orientation. This is a general code and the true azimuth is stored in channel

Parameters

azimuth (float) – angel assuming 0 is north, 90 is east, 0 is vertical down

Returns

single character SEED orientation code

Return type

string

mth5.utils.fdsn_tools.get_period_code(sample_rate)[source]

Get the SEED sampling rate code given a sample rate

Parameters

sample_rate (float) – sample rate in samples per second

Returns

single character SEED sampling code

Return type

string

mth5.utils.fdsn_tools.make_channel_code(channel_obj)[source]

Make the 3 character SEED channel code

Parameters

channel_obj (Channel) – Channel metadata

Returns

3 character channel code

Type

string

mth5.utils.fdsn_tools.make_mt_channel(code_dict, angle_tol=15)[source]
Parameters

code_dict (TYPE) – DESCRIPTION

Returns

DESCRIPTION

Return type

TYPE

mth5.utils.fdsn_tools.read_channel_code(channel_code)[source]

read FDSN channel code

Parameters

channel_code (TYPE) – DESCRIPTION

Returns

DESCRIPTION

Return type

TYPE

mth5.utils.mth5_logger module

Logger

Setup a logger with two handlers to remove redundancy between logs entries One is a stream handler for any messages to the console. The other is either a file handler or a null handler.

mth5.utils.mth5_logger.load_logging_config(config_fn=PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/mth5/checkouts/stable/mth5/utils/logging_config.yaml'))[source]

configure/setup the logging according to the input configfile

Parameters

configfile – .yml, .ini, .conf, .json, .yaml.

Its default is the logging.yml located in the same dir as this module. It can be modofied to use env variables to search for a log config file.

mth5.utils.mth5_logger.setup_logger(logger_name, fn=None, level='debug')[source]

Create a logger, can write to a separate file. This will write to the logs folder in the mt_metadata directory.

Parameters
  • logger_name (string) – name of the logger, typically __name__

  • fn (TYPE, optional) – file name to write to, defaults to None

  • level (TYPE, optional) – DESCRIPTION, defaults to “debug”

Returns

DESCRIPTION

Return type

TYPE

mth5.utils.stationxml_translator module

This module provides tools to convert MT metadata to a StationXML file.

Created on Tue Jun 9 19:53:32 2020

@author: jpeacock

class mth5.utils.stationxml_translator.MTToStationXML(inventory_object=None)[source]

Bases: object

Translate MT metadata to StationXML using Obspy Inventory classes.

Any metadata that does not fit under the StationXML schema will be added as extra metadata in the namespace MT.

MT metadata is mapped into StationXML

>>> # inventory mapping
Inventory
===========
  |--> Network (MT Survey)
  --------------
    |--> Station (MT Station)
    -------------
      |--> Channel (MT Channel + MT Run)
      -------------
Example

>>> from mth5.utils import translator
>>> from mth import metadata
>>> # survey_dict = metadata for survey
>>> mt2xml = translator.MTToStationXML()
>>> mt_survey = metadata.Survey()
>>> mt_survey.from_dict(survey_dict)
>>> mt2xml.add_network(mt_survey)
Add a station from an xml file with root <station>

>>> from xml.etree import ElementTree as et
>>> mt_station = metadata.Station()
>>> mt_station.from_xml(et.parse("mt_station_xml_fn.xml").getroot())
>>> mt2xml.add_station(mt_station)
Add a channel from an json files with {channel:{}} and {run:{}} format

>>> import json
>>> mt_electric = metadata.Electric()
>>> with open("electric_json_fn.json", 'r') as fid:
>>> ... mt_electric.from_json(json.load(fid))
>>> mt_run = metadata.Run()
>>> with open("run_json_fn.json", 'r') as fid:
>>> ... mt_run.from_json(json.load(fid))
>>> mt2xml.add_channel(mt_electric, mt_run, mt_station.archive_id)
add_channel(mt_channel, mt_run, station, network_code=None)[source]

Add a station from a MT channel and run objects. The run object is needed to fill the datalogger information.

Will fill the appropriate metadata in Inventory.Network[network].station[station], any metadata that does not fit within the StationXML schema will be added as extra.

Parameters
  • mt_channel (Channel, Electric, Magnetic, Auxiliary) – MT channel metadata

  • mt_run – MT run metadata

  • station (5 character string) – Station name to add the channel to

  • network_code (2 character code. optional) – Network code that the station belongs to. Defaults to None which will use Inventory.networks[0]

Dtype mt_run

Run

add_network(mt_survey_obj)[source]

Add a network from an MT survey object. Will fill the appropriate metadata in Inventory.Network, any metadata that does not fit within the StationXML schema will be added as extra.

Parameters

mt_survey_obj (Survey) – MT Survey metadata

add_station(mt_station_obj, network_code=None)[source]

Add a station from an MT station object. Will fill the appropriate metadata in Inventory.Network[network].station, any metadata that does not fit within the StationXML schema will be added as extra.

Parameters
  • mt_station_obj (Station) – MT station metadata

  • network_code (2 character code. optional) – Network code that the station belongs to. Defaults to None which will use Inventory.networks[0]

find_network_index(network)[source]

locate the index of given network

Parameters

network (2 character string) – name of the network to look for

Returns

index of network in inventory.networks

Return type

int

find_station_index(station, network=None)[source]

locate the index of given station in Inventory.networks[network].stations

Parameters
  • station (5 character string) – 5 character SEED station name

  • network (2 character string, optional) – Network name, defaults to None which will use Inventory.networks[0]

Returns

Index of station in Inventory.networks[network].stations

Return type

int

to_stationxml(station_xml_fn)[source]

Write a StationXML file using Inventory.write

Parameters

station_xml_fn (string or Path) – Full path to StationXML file

Returns

path to StationXML

Return type

Path

class mth5.utils.stationxml_translator.StationXMLToMTH5[source]

Bases: object

Translate a station XML to MT metadata standards

class mth5.utils.stationxml_translator.XMLNetworkMTSurvey[source]

Bases: object

translate back and forth between StationXML Network and MT Survey

network_to_survey(network)[source]

Translate a StationXML Network object to MT Survey object

Parameters

network (obspy.core.inventory.Network) – StationXML network element

mth5.utils.stationxml_translator.add_custom_element(obj, custom_name, custom_value, units=None, attribs=None, namespace='MT')[source]

Add a custom MT element to Obspy Inventory object

Parameters
  • obj (Inventory) – Inventory object that will have the element added

  • custom_key (str) – name of custom element, if the key has a ‘.’ it will be recursively split to assure proper nesting.

  • custom_value ([ int | float | string ]) – value of custom element

Example
>>> from obspy.core import inventory
>>> from obspy.util import AttribDict()
>>> channel_01 = inventory.Channel('SQE', "", 39.0, -112.0, 150, 0,

… azimuth=90, … sample_rate=256, dip=0, … types=[‘ELECTRIC POTENTIAL’]) >>> # add custom element >>> channel_01.extra = AttribDict({‘namespace’:’MT’}) >>> channel_01.extra.FieldNotes = AttribDict({‘namespace’:’MT’}) >>> channel_01.extra.FieldNotes.value = AttribDict({‘namespace’:’MT’}) >>> channel_01.extra.FieldNotes = add_custom_element( >>>… channel_01.extra.FieldNotes, >>>… ‘ContactResistanceA’, >>>… 1.2, >>>… units=’kOhm’)

mth5.utils.stationxml_translator.flip_dict(original_dict)[source]

Flip keys and values of the dictionary

Parameters

original_dict (TYPE) – DESCRIPTION

Returns

DESCRIPTION

Return type

TYPE

mth5.utils.stationxml_translator.inventory_network_to_mt_survey(network_obj)[source]

Convert an inventory.Network oject to an mth5.metadata.Survey object

Parameters

network_obj (TYPE) – DESCRIPTION

Returns

DESCRIPTION

Return type

TYPE

mth5.utils.stationxml_translator.inventory_station_to_mt_station(inv_station_obj)[source]
Parameters

inv_station_obj (TYPE) – DESCRIPTION

Returns

DESCRIPTION

Return type

TYPE

mth5.utils.stationxml_translator.mt_channel_to_inventory_channel(channel_obj, run_obj, namespace)[source]

Translate MT channel metadata to inventory channel

Metadata that does not fit under StationXML schema is added as extra.

Parameters
  • channel_obj (Channel, Electric, Magnetic, Auxiliary,) – MT channel metadata

  • run_obj (Run) – MT run metadata to get data logger information

Returns

StationXML channel

Return type

Channel

mth5.utils.stationxml_translator.mt_station_to_inventory_station(station_obj, namespace='MT')[source]

Translate MT station metadata to inventory station

Metadata that does not fit under StationXML schema is added as extra.

Parameters

station_obj (Station) – MT station metadata

Returns

StationXML Station element

Return type

Station

mth5.utils.stationxml_translator.mt_survey_to_inventory_network(survey_obj, namespace='MT')[source]

Translate MT survey metadata to inventory Network in StationXML

Metadata that does not fit under StationXML schema is added as extra.

Parameters

survey_obj (Survey) – MT survey metadata

Returns

DESCRIPTION

Return type

TYPE

mth5.utils.stationxml_translator.read_comment(inv_comment)[source]
Parameters

inv_comment (TYPE) – DESCRIPTION

Returns

DESCRIPTION

Return type

TYPE

Module contents