[obspy-users] write SAC data to file

Yannik Behr yannik at yanmail.de
Mon Nov 12 17:19:50 CET 2012


Hi Ahu,

since you are looping over the traces of your stream object (st) you 
should replace st.write with tr.write. You can do the st.resample and 
st.trim outside of the loop as this will act on all traces contained in 
your stream object. So, something like this should work:

st = read("/media/disk-1/OBSPY/cut_SAC/van_dene/2011*", format="SAC")
st.resample(50.0)
EventOriginTime = UTCDateTime("2011-10-23T10:41:19")
st.trim(EventOriginTime, EventOriginTime+360)
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'
	  tr.write(newfname, format="SAC")


Cheers,
Yannik


On 11/12/2012 04:33 PM, Ahu Komec Mutlu wrote:
>
> 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,
>

-- 
Hofwiesenstrasse 251
CH-8057 Zürich
ph: +41 (0)43 5375118
mobil: +41 (0)76 2384399




More information about the obspy-users mailing list