# write SAC data to file

**URL:** <https://discourse.obspy.org/t/write-sac-data-to-file/148>\
**Category:** Users Mailing List Archive\
**Tags:** archive\
**Created:** [November 8, 2012, 2:16pm UTC](https://discourse.obspy.org/t/write-sac-data-to-file/148 "2012-11-08T14:16:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahu\_Komec\_Mutlu](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@Ahu\_Komec\_Mutlu](https://discourse.obspy.org/u/Ahu_Komec_Mutlu)\
**Post date:** [November 8, 2012, 2:16pm UTC](https://discourse.obspy.org/t/write-sac-data-to-file/148/1 "2012-11-08T14:16:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![megies](https://discourse.obspy.org/user_avatar/discourse.obspy.org/megies/32/9_2.png) [@megies](https://discourse.obspy.org/u/megies)\
**Post date:** [November 8, 2012, 2:29pm UTC](https://discourse.obspy.org/t/write-sac-data-to-file/148/2 "2012-11-08T14:29:28Z")

</div>

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:  
&nbsp;&nbsp;&nbsp;&nbsp;fname = "%s.sac" % tr.id  
&nbsp;&nbsp;&nbsp;&nbsp;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:  
&nbsp;&nbsp;&nbsp;&nbsp;fname = "%s.sac" % id  
&nbsp;&nbsp;&nbsp;&nbsp;st.select(id=id).write(fname, format="SAC")

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

best,  
Tobias
