Writing Response information to File

Dear ObsPy Users,

I am trying to save the response information for a station or channel to file. Ultimately I need that to be a RESP file, but XML-SEED would be ok as well. Ideally, this would be done on the fly while my python code is running, rather than downloading a dataless seed volume for a whole network beforehand.

Currently I’m getting the instrument response using “attach_response=True” when using client.get_waveforms. I’m then able to succesfully remove the instrument response using remove_response on each stream or trace.

However, I would like to have the RESP file as well, as I have some external programs that use it. Is there a way to write this response information to file from the inventory object (get_stations with level=“response”) or from Stream[0].stats.response?

From the looking around I’ve done so far, I can generate a stationxml file from the inventory, but that doesn’t convert easily to xml-seed (and therefore RESP).

Thanks in advance for any suggestions,

Hi Andrew et al.,

I recently wrote a simple subroutine to do what I think you’re asking for; it outputs a sac response textfile from the response information contained within a channel object obtained by an IRIS waveform request through python using client.get_waveforms etc.

You can use the response-file writing tool in two ways: from the command line (slow) or within a python script that loops over channels/stations etc.

Let’s say you requested data within a python script, dowloaded an inventory object and then saved it:

Get the inventory of stations

inv = client.get_stations( network=net_dos,channel=chan_dos,level="response”)

Save IRIS inventory (inc. response files) as MYinventory.xml"

inv.write(dbdir+’MYinventory.xml’, format='STATIONXML’)

  1. You can run this tool from the command line, by giving the path to the inventory and the indices of the network, station and channel that you want (counting from zero - as a MATLAB person, I still feel like I have to specify this…)

For example, for the third channel for the second station in the first network, the usage from the command line is as follows:

write_sac_response_file.py MYinventory.xml name_of_response_file 0 1 2

where it’s hopefully obvious that the numbers are the indices of the network, station, and channel, respectively (see code for details).
This usage is pretty slow, because (depending on the size) the inventory file takes a long time to load.

  1. You can also call the subroutine within ipython, which will probably be a lot faster if you want to do this for several channels, because you only have to load the inventory file once, and the rest of the process takes a trivial amount of time. In this case your workflow would be something like:

In [1]: import write_sac_response_file
In [2]: from obspy import read_inventory
In [3]: inv = read_inventory(“MYinventory.xml”)

(here you could loop over networks/stations/channels, replacing the 0,1,2 by the appropriate indices in the line below - the slow loading of the inventory object need only be done once)

In [4]: write_sac_response_file.write_response(inv[0][1][2],”name_of_response_file",inv[0],inv[0][1])

again, see the code for details.

I’m a bit of a novice at both Obspy and python in general, so apologies if any of that is not clear, and I’m hoping that the community will point out any errors in the code (or this description of it!).

write_sac_response_file.py (5.1 KB)

Hi Zach, Andrew,

there is builtin SACPZ output support for Inventory objects in ObsPy (it
still has some quirks for unexpected/missing units etc. but is
tested+validated for some channels):

inv = read_inventory("/path/to/IU_ANMO_00_BHZ.xml")
inv.write("/tmp/mysac_pz.txt", "SACPZ")

Please consider using the builtin routines and if you encounter problems
help improve them.

Anyway, Andrew was talking about RESP files, and that's a different
matter. Andrew, you're right, there is no conversion routine from
Inventory objects /StationXML to SEED/RESP or vice versa. It's a
non-trivial task..
For this conversion I recommend using the IRIS java tool
unfortunately as its Java we cannot wrap it easily in obspy.


Hi Tobias,
Thanks very much. I’ll look into the SACPZ method as well, and resort to the java tool as a last resort.