write SAC data to file

Hello,

I read SAC data and did some changes on that data then I want to write it to a file, here my code:

from obspy.core import read
from obspy.core import UTCDateTime
st = read("20111023_1041/2011*", format="SAC")
print(st.__str__(extended=True))
st.resample(50.0)
EventOriginTime = UTCDateTime("2011-10-23T10:41:19")
st.trim(EventOriginTime, EventOriginTime+360)
st.write("van.sac", format="SAC")

it works but output file names are not include station name and component information, they are like :

van01.sac van04.sac
van02.sac van05.sac

How can I write the SAC data to new file with name like
AFSR.BHE.sac
AFSR.BHN.sac
AFSR.BHZ.sac

(stationname.component.sac)

Thanks & Regards

Hello Ahu,

SAC stores one trace per file, so a st.write() cannot just use the given
name but has to add generic suffixes. To have more control over
filenames just loop over the traces and build your own filenames, e.g.:

for tr in st:
    fname = "%s.sac" % tr.id
    st.write(fname, format="SAC")

Beware, this could overwrite the file again if more traces with same ID
are present. Or try:

ids = set([tr.id for tr in st])
for id in ids:
    fname = "%s.sac" % id
    st.select(id=id).write(fname, format="SAC")

Which would enumerate filenames for multiple traces with the same ID.

best,
Tobias