Obspy: AttributeError when trying to export inventory in stationxml format

Created on 15 Jan 2014  路  10Comments  路  Source: obspy/obspy

Dear developers,

I downloaded an inventory of stations (including instrument response) using the new obspy.fdsn client, and then tried to export it in a stationxml file:

from obspy.fdsn import Client
from obspy import UTCDateTime
client = Client("IRIS")
t1 = UTCDateTime(2000, 1, 1)
t2 = UTCDateTime(2001, 1, 1)
inv = client.get_stations(t1, t2, network='G', level='response')
inv.write("test.xml", "STATIONXML")

...but I got the following error:

AttributeError: 'NoneType' object has no attribute 'resource_id'

The problem seems to be with function _write_equipment in file stationxml.py, which cannot deal with equipment = None. I tried simple fixes by adding some checks there and there in the function, but they did not work.

Could you please help?

Thanks
BG

bug .clients.fdsn

Most helpful comment

Additionally, sensor, equipment, and data_logger are all attributes of Channel which are expected to be Equipment instances. You wouldn't, for example, set chan.equipment.data_logger to anything, but rather should do chan.equipment = Equipment(...) and chan.data_logger = Equipment(...)

All 10 comments

This seems to be a bug. One simple solution would be to directly save your request to a file using the filename="..." option of get_stations(). If you plan to permanently store the requested inventories locally, I would go with that option anyway, because unmodified data saved directly from the raw data acquired from the data centers are always more failsafe compared to parsing and serializing the data again.

great! I did not know about the filename option. (I just started to play with the 0.9 version today.) This worked indeed.
thanks

There'll probably be a couple more bugs like this one in there. We have some real world test files and a file generated from the FDSN schema but far from all possible cases are covered by that, of course.
Thanks for reporting.

bumping this bug as i am still being affected by it in 1.1.1

what I am doing is loading a populated inventory, cycling all stations do to each channel, then changing data_logger info for a few sites

chan.data_logger = "data logger 5000"

then trying to save

inv.write(stationxmlfile,format='STATIONXML')

~/.local/lib/python3.6/site-packages/obspy/io/stationxml/core.py in _write_channel(parent, channel, level)
   1136     _write_equipment(channel_elem, channel.sensor, "Sensor")
   1137     _write_equipment(channel_elem, channel.pre_amplifier, "PreAmplifier")
-> 1138     _write_equipment(channel_elem, channel.data_logger, "DataLogger")
   1139     _write_equipment(channel_elem, channel.equipment, "Equipment")
   1140 

~/.local/lib/python3.6/site-packages/obspy/io/stationxml/core.py in _write_equipment(parent, equipment, tag)
   1338         return
   1339     attr = {}
-> 1340     if equipment.resource_id is not None:
   1341         attr["resourceId"] = equipment.resource_id
   1342     equipment_elem = etree.SubElement(parent, tag, attr)

AttributeError: 'str' object has no attribute 'resource_id'

unclear if this is the result of me changing these settings unwisely or bug was never fixed or?

data_logger should be an Equipment instance, not a string, like it says in the constructor

thanks for the lightning-fast response!

i'm still a bit confused however. there is obviously a chan.data_logger, but chan.equipment is more or less empty and unused. is one of these being phased out? there's also sta.equipmentS parameter which makes more sense overall but that also appears to not really be in use.

Equipment is a _class_, not a Channel attribute.

yes i (think) i understand that. the same as sensor is a class.

however whereas chan.sensor.model = "blah" works, chan.equipment.data_logger gives me

In [197]: chan.equipment.data_logger = "uh"                                                                                                     
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-197-8b5920f83c8b> in <module>
----> 1 chan.equipment.data_logger = "uh"

AttributeError: 'NoneType' object has no attribute 'data_logger'

appreciate your replies greatly!

You need to set up an Equipment instance, bind it to channel as .equipment and then you can set it's properties.

Additionally, sensor, equipment, and data_logger are all attributes of Channel which are expected to be Equipment instances. You wouldn't, for example, set chan.equipment.data_logger to anything, but rather should do chan.equipment = Equipment(...) and chan.data_logger = Equipment(...)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ThomasLecocq picture ThomasLecocq  路  5Comments

comoglu picture comoglu  路  9Comments

EvaEibl picture EvaEibl  路  10Comments

Earthscience picture Earthscience  路  8Comments

FadelI picture FadelI  路  3Comments