[obspy-users] write SAC data to file

Ahu Komec Mutlu komec at boun.edu.tr
Mon Nov 12 16:33:32 CET 2012


Thanks for help, unfortunately  I have another problem with for loop I 
think, will be great if I can get some help.

I run the code below, changed the file names but it writes same SAC 
file more than ones with 01.02.03.. numbers at file name.

code I wrote -------------------------------------------------

from obspy.core import read
from obspy.core import UTCDateTime
from obspy.sac import SacIO
#
st = read("/media/disk-1/OBSPY/cut_SAC/van_dene/2011*", format="SAC")
for tr in st:
         fname = "%s.sac" % tr.id
         print fname
         stname = fname[1:5]
         comp = fname[11:12]
         for comp in fname:
                 if comp == "Z":
                         comp1 = 'BHZ'
                 elif comp == "N":
                         comp1 = 'BHN'
                 elif comp == "E":
                         comp1 = 'BHE'
         newfname = stname + "." + comp1 + "." +'SAC'
         st.resample(50.0)
         EventOriginTime = UTCDateTime("2011-10-23T10:41:19")
         st.trim(EventOriginTime, EventOriginTime+360)
         st.write(newfname, format="SAC")
         print newfname

---------------------------------------------------------------

the out put is :

.AFSR..AFSRE2.sac
AFSR.BHE.SAC
.AFSR..AFSRN2.sac
AFSR.BHN.SAC
.AFSR..AFSRZ2.sac
AFSR.BHZ.SAC
.AKAS..3M59E2.sac
AKAS.BHE.SAC
.AKAS..3M59N2.sac
AKAS.BHN.SAC
.....   seems it works with the file names but when I do "ls" ;


AFSR.BHE01.SAC          AFSR.BHZ23.SAC
AFSR.BHE02.SAC          AFSR.BHZ24.SAC
AFSR.BHE03.SAC          AFSR.BHZ25.SAC
AFSR.BHE04.SAC          AFSR.BHZ26.SAC
AFSR.BHE05.SAC          AFSR.BHZ27.SAC
AFSR.BHE06.SAC          AKAS.BHE01.SAC
AFSR.BHE07.SAC          AKAS.BHE02.SAC
AFSR.BHE08.SAC          AKAS.BHE03.SAC
AFSR.BHE09.SAC          AKAS.BHE04.SAC
AFSR.BHE10.SAC          AKAS.BHE05.SAC
AFSR.BHE11.SAC          AKAS.BHE06.SAC
AFSR.BHE12.SAC          AKAS.BHE07.SAC
AFSR.BHE13.SAC          AKAS.BHE08.SAC
AFSR.BHE14.SAC          AKAS.BHE09.SAC
AFSR.BHE15.SAC          AKAS.BHE10.SAC
AFSR.BHE16.SAC          AKAS.BHE11.SAC  ......
AFSR.BHE17.SAC    .......

I have SAC data from 9 stations in my directory, and creates 27 new SAC 
files from each station ???

Thanks & Regards,

-- 
-------------------------

AHU KOMEC MUTLU
Bogazici University Kandilli Observatory & E.R.I.
Cengelkoy / ISTANBUL

Phone: +(90) 216 516 32 16

-------------------------

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

On 08/11/12 15:16, Ahu Komec Mutlu wrote:
>
> 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
>
> -------------------------
>
> AHU KOMEC MUTLU
> Bogazici University Kandilli Observatory & E.R.I.
> Cengelkoy / ISTANBUL
>
> Phone: +(90) 216 516 32 16
>
> -------------------------
>
>
>
> _______________________________________________
> obspy-users mailing list
> obspy-users at lists.sevor.de
> http://lists.obspy.org/cgi-bin/mailman/listinfo/obspy-users

-- 
Dipl.-Geophys. Tobias Megies

Geophysikalisches Observatorium
Ludwigshöhe 8
82256 Fürstenfeldbruck

Ludwig-Maximilians-Universität
Department für Geo- und Umweltwissenschaften
Sektion Geophysik
Theresienstrasse 41/IV
80333 München

Tel: +49 (0) 89 2180-73981
      +49 (0) 89 2180-4326
Mail: tobias.megies at geophysik.uni-muenchen.de




More information about the obspy-users mailing list