Converting Multiple .MSEED Data into .SAC Format

Dear Obspy Users,

I am trying to convert multiple .MSEED files in a directory into .SAC files. My code loops over all the .MSEED files and read all of them, but it converts just the first .MSEED file into .SAC files. Is there anyway that I can write all of the .MSEED files into .SAC files at once? My code is attached below:

import os
import obspy
import glob

path="/home/rauf/Desktop/quant-data/"
readmseed=glob.glob(os.path.join(path, “*”))

my_data=[]

for data in readmseed:
print(data)
st=obspy.read(data, format=“MSEED”)
my_data.append(st)

for name in os.listdir(path):
print(name)

for msdata in my_data:
st=msdata.select(channel=“BH*”)
st+=msdata.select(channel=“HH*”)
print(st.str(extended=True))
for tr in st:
tr.write("/home/rauf/Desktop/SAC_data/"+name+"-"+tr.id, format=“SAC”)

Thanks,
Rauf

Hello Rauf,

I suspect the “tr.id” in your final write loop is not unique for each waveform in your stream. I would append a integer increasing segment number to the file name for each trace in the stream, and see if you’re writing them all.

Best,
Jon

Hi Rauf,

It seems you missed the indentations. It should be like

for msdata in my_data:
st=msdata.select(channel=“BH*”)
st+=msdata.select(channel=“HH*”)
print(st.str(extended=True))
for tr in st:
tr.write("/home/rauf/Desktop/SAC_data/"+name+"-"+tr.id, format=“SAC”)

Sincerely,

Hi Rauf,

simply concatenate all miniSEED files into one single file, read it in one line of code, write it out in another line of code-. All done!

cat * > file.mseed

There is no need to loop over the files. miniSEED is like a set of containers which you can stack (concatenate).

Cheers,
Dirk

Senior geophysicist
gempa GmbH, Potsdam, Germany