What is the replacement for the old SacIO() .fromarray() function?

(corrected version of sample code in this version of this message)

Hi everyone,
I obviously need to re-write quite a few of my tools now that the old SACIO() is gone.
What I have is broken, and I haven’t quite found the right combination for reconstructing it in the new version 1.0.0 of Obspy’s SAC module.

Old code that I used for synthesizing my sac file:

Hi Dan,

Yes, the new class is obspy.io.sac.SACTrace .

Maybe try something like this (untested). If it doesn’t work, feel free to email me directly about it. If any problems arise, we can file a GitHub issue.


This is a great example for use with the SACTrace!

I have expanded it a little, and it worked on the first try. I can now go about adding the modification to the code.
Here is the test code that worked. If the documenters at ObSpy want to use it within the SACTrace documentation as an example of synthesizing a SAC file from an arbitrary list of values, I think it would be quite helpful to other users in similar situations as myself.

I really must learn to review my message before hitting SEND.
The code snippet I sent has a small problem with timing, and in the formation of the 1 Hz waveform.
It’s not really 1 Hz because of the extra quarter-second on the end, and the example did not set
time properly. I missed on adding the hour and minute. I also declare the time and remainder as text fields,
because I frequently get this sort of information when building streams from data sourced from
comma separated variable text files. Now I will get back to fixing my data converter & calibration software.

Thanks again Jon,

-Dan Burk, Michigan State University

If anyone wants the corrected version:


This is a great example for use with the SACTrace!

Agreed, this is pretty common. I intend to update the SAC module documentation shortly, and this may be worthwhile to include.

As an aside, your example may be even simpler if you use “obspy.io.sac.util.utcdatetime_to_sac_nztimes” and “obspy.UTCDateTime”. UTCDateTime can accept a full ISO8601:2004 string, and utcdatetime_to_sac_nztimes will accept that instance and return a tuple (nztimes_dictionary, remainder_microseconds). You can decide what to do with the remainder microseconds, but you can just unpack the nztimes dictionary into the SACTrace initialization. Something like this (using 0.789 milliseconds):

from obspy import UTCDateTime
from obspy.io.sac.util import utcdatetime_to_sac_nztimes

dt = UTCDateTime("2016-04-08T12:34:56.000789”)
nztimes, microseconds = utcdatetime_to_sac_nztimes(dt)
t = SACTrace(data=b, scale=1.0, delta=.01, kcmpnm="BHZ”, knetwk="LM”, kstnm="mytst”, **nztimes)