shared-snippets/python/seedconverter/unit.py
2024-08-28 17:22:21 +02:00

96 lines
3.5 KiB
Python

def determine_instrument_type_from_blockette(parser, channel_id):
"""
Determines type of instrument used to record a channel with provided
channel_id.
:type parser: obspy.io.xseed.parser.Parser
:param parser: Parser object parsed from Full SEED or dataless or
StationXML.
:type channel_id: str
:param channel_id: Channel_id object generated by obspy.trace.Trace.id
:rtype: str
:return: Returns str determining the type of instrument
"""
return translate_unit_to_instrument_type(determine_unit_from_blockette(parser, channel_id))
def determine_unit_from_blockette(parser, channel_id):
"""
Determines the unit used to record a channel with provided
channel_id.
:type parser: obspy.io.xseed.parser.Parser
:param parser: Parser object parsed from Full SEED or dataless or
StationXML.
:type channel_id: str
:param channel_id: Channel_id object generated by obspy.trace.Trace.id
:rtype: str
:return: Returns str containing a real unit name that can be passed to
translate_unit_to_instrument_type method
to obtain a str compatible with response removal procedure
"""
for blkt in parser._select(channel_id):
if not blkt.id == 52:
continue
for bl in parser.blockettes[34]:
if bl.unit_lookup_code == blkt.units_of_signal_response:
return bl.unit_name
def determine_instrument_type_from_inventory(inv, channel_id, time):
"""
Determines type of instrument used to record a channel with provided channel_id at the provided time.
:type inv: obspy.core.inventory.inventory.Inventory
:param inv: ObsPy Inventory object parsed from a file
:type channel_id: str
:param channel_id: Channel_id object generated by obspy.trace.Trace.id
:type time: obspy.core.utcdatetime.UTCDateTime
:param time: time for which the unit should be determined in the inventory (e.g. start time of a trace)
:rtype: str
:return: Returns str determining the type of instrument
"""
return translate_unit_to_instrument_type(determine_unit_from_inventory(inv, channel_id, time))
def determine_unit_from_inventory(inv, channel_id, time):
"""
Determines unit used to record a channel with provided channel_id at the provided time.
:type inv: obspy.core.inventory.inventory.Inventory
:param inv: ObsPy Inventory object parsed from a file
:type channel_id: str
:param channel_id: Channel_id object generated by obspy.trace.Trace.id
:type time: obspy.core.utcdatetime.UTCDateTime
:param time: time for which the unit should be determined in the inventory (e.g. start time of a trace)
:rtype: str
:return: Returns str containing a real unit name that can be passed to
translate_unit_to_instrument_type method
to obtain a str compatible with response removal procedure
"""
resp = inv.get_response(channel_id, time)
return resp.instrument_sensitivity.input_units
def translate_unit_to_instrument_type(unit_name):
if unit_name == "M":
return "DISP"
elif unit_name == "M/S":
return "VEL"
elif unit_name == "M/S**2":
return "ACC"
else:
raise TypeError("Unknown unit code ", unit_name)
def translate_instrument_type_to_unit(unit_type):
if unit_type == "DISP":
return "M"
elif unit_type == "VEL":
return "M/S"
elif unit_type == "ACC":
return "M/S**2"
else:
raise TypeError("Unknown unit code ", unit_type)