def add_custom_element(obj, custom_name, custom_value, units=None, attribs=None, namespace="MT"): """ Add a custom MT element to Obspy Inventory object :param obj: :class:`~obspy.core.inventory.Inventory` object that will have the element added :type obj: :class:`~obspy.core.inventory.Inventory` :param custom_key: name of custom element, if the key has a '.' it will be recursively split to assure proper nesting. :type custom_key: str :param custom_value: value of custom element :type custom_value: [ int | float | string ] :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') """ if custom_value is None: return if "." in custom_name: custom_category, custom_name = custom_name.split(".", 1) if not hasattr(obj, custom_category): obj[custom_category] = AttribDict({ "namespace": namespace, "value": AttribDict() }) add_custom_element( obj[custom_category].value, custom_name, custom_value, units=units, attribs=attribs, namespace=namespace, ) else: obj[custom_name] = AttribDict({"namespace": namespace}) obj[custom_name].value = custom_value if units: assert isinstance(units, str), "Units must be a string" obj[custom_name].attrib = {"units": units.upper()} if attribs: if not isinstance(obj[custom_name].attrib, dict): obj[custom_name].attrib = {} for key, value in attribs.items(): obj[custom_name].attrib[key] = value