subclass of obspy.core.event.magnitude.Magnitude object (or other event object)

Dear obspy users,

I’m working on some magnitude estimates and I would like do create a subclass inheriting from “magnitude.Magnitude” obspy object. At this point I just want to have the same attributes as the parent class plus some very simple new attributes such as “corner frequency”, “omega_0”…

I just did the following:

class Magnitude_User(_Magnitude):
def init(self,*args, **kwargs):

initialization code

self.fc=kwargs.get(‘fc’)
with warnings.catch_warnings():
warnings.simplefilter(“ignore”)
super().init(*args, **kwargs)

I wanted to get rid of the warning, but I still get the following:

UserWarning: Setting attribute “fc” which is not a default attribute

What would you advise for defining a new attribute for an obspy class without directly modifying the parent class? Is that the best approach? If yes, how can I get rid of the “warnings.warn”?

I thought about using the AttribDict() and the “extra” attribute but I’m not really interested in writing any xml for now and I would prefer a more direct approach.

Thanks for your help,

Christian

Hey Christian,

most event classes are autogenerated and contain some “magic” to make handling them easier. One of these things is to warn on setting unknown attributes (this would otherwise a great source of unspotted errors due to typos).

There are two ways around this - either turn off warning with non-default keys or add it as a default key. Also best inherit from Magnitude instead of _Magnitude.

from obspy.core.event import Magnitude

class Magnitude_User(Magnitude):

Either do this.

warn_on_non_default_key = False
def init(self, fc, *args, **kwargs):
super().init(*args, **kwargs)

Or add it as a default value.

self.defaults[‘fc’] = None
self.fc = fc

Cheers!

Lion

Thanks a lot Lion, it’s great to have a defaults option, I’ll use this one in my case! Perfect!

Cheers,

Ch.