adding missing information in SAC header file

Hi all,

is there an easy way to add some missing information to a SAC header file using Obspy?

Specifically, I would like to save a trace in a SAC-file and add some missing information to the header file.
I noticed that if I get the trace using get_waveforms (FDSN client) and save it as SAC-file, the header does not automatically contain the station coordinates.

Cheers,
and many thanks!

Lucia

Hi Lucia,

There is a relatively easy way to add this information to data you download using get_waveforms.
In obspy.core there is AttribDict. When you read a Sac file from a disk, this SAC attribute dictionary is filled with the common header values.
However, when you download the data and write it as a sac file, the attribute dictionary isn’t generated automatically.

You can populate this yourself fairly easily though.

You need to import the AttribDict from obspy.core:
from obspy.core import AttribDict

Then you need to create the dictionary and populate it with those values you’d like. Their names are the same as the sac header variables (in most cases at least):
sacd=AttribDict()

#-- Station Parameters
sacd.stla=stla
sacd.stlo=stlo
sacd.stel=stel
sacd.cmpaz=cmpaz
sacd.cmpinc=cmpinc

sacd.az=az
sacd.baz=baz
sacd.dist=distm/1000.
sacd.gcarc=distd
sacd.lcalda=0
#-- Event Parameters
sacd.evla=evla
sacd.evlo=evlo
sacd.evdp=evdp
sacd.user1=evmg
#-- Timing Parameters
sacd.iztype=11 #-- Set as Event Origin Time
sacd.b = tr.stats.starttime - evOtime
#-- File Type
sacd.iftype=1
#-- Dependent Variable Type
sacd.idep=05

You can compute locally and fill as many of the values as you’d like. Note that if you want to fill the azimuth and inclination, you’ll need to make a call to get_stations and parse the inventory object (conventions for 0 and 90 are different between sac and obspy).

After this, you then attach your attribute dictionary to each trace object:
stream[0].stats.sac=sacd

When you write this stream/trace to a sac file, and load it in sac, you’ll see the header values populated.

Cheers,
Andrew

Hey Lucia,

the waveform data you have does not know its station coordinates as they are not stored in the MiniSEED file served by the FDSN web server. So you need to get it from somewhere else - the get_stations() method of the client is a possibility.

To write to the sac headers do the following:

import obspy

st = …

for tr in st:
��� tr.stats.sac = obspy.core.AttribDict()
��� # Set any header you want - the names are the same as in the sac documentation
��� tr.stats.stla = 11.2
��� …

st.write(filename, format=‘sac’)

Cheers!

Lion

1 Like

Hi Lion,

yes, I was already getting the coordinates from get_stations().
Your suggestion for writing the header works very well if instead of using “tr.stats.stla" I use “tr.stats.sac.stla”.

Thanks a lot!

Cheers,
Lucia