17. Export Seismograms to ASCII

17.1. Built-in Formats

You may directly export waveform data to any ASCII format available by ObsPy using the write() method on the generated Stream object.

>>> from obspy.core import read
>>> stream = read('https://examples.obspy.org/RJOB20090824.ehz')
>>> stream.write('outfile.ascii', format='SLIST')

The following ASCII formats are currently supported:

  • SLIST, a ASCII time series format represented with a header line followed by a sample lists (see also SLIST format description):

    TIMESERIES BW_RJOB__EHZ_D, 6001 samples, 200 sps, 2009-08-24T00:20:03.000000, SLIST, INTEGER,
    288 300 292 285 265 287
    279 250 278 278 268 258
    ...
    
  • TSPAIR, a ASCII format where data is written in time-sample pairs (see also TSPAIR format description):

    TIMESERIES BW_RJOB__EHZ_D, 6001 samples, 200 sps, 2009-08-24T00:20:03.000000, TSPAIR, INTEGER,
    2009-08-24T00:20:03.000000  288
    2009-08-24T00:20:03.005000  300
    2009-08-24T00:20:03.010000  292
    2009-08-24T00:20:03.015000  285
    2009-08-24T00:20:03.020000  265
    2009-08-24T00:20:03.025000  287
    ...
    
  • SH_ASC, ASCII format supported by Seismic Handler:

    DELTA: 5.000000e-03
    LENGTH: 6001
    START: 24-AUG-2009_00:20:03.000
    COMP: Z
    CHAN1: E
    CHAN2: H
    STATION: RJOB
    CALIB: 1.000000e+00
    2.880000e+02 3.000000e+02 2.920000e+02 2.850000e+02
    2.650000e+02 2.870000e+02 2.790000e+02 2.500000e+02
    ...
    

17.2. Custom Format

In the following, a small Python script is shown which converts each Trace of a seismogram file to an ASCII file with a custom header. Waveform data will be multiplied by a given calibration factor and written using NumPy‘s savetxt() function.

"""
USAGE: export_seismograms_to_ascii.py in_file out_file calibration
"""
from __future__ import print_function

import sys

import numpy as np
import obspy


try:
    in_file = sys.argv[1]
    out_file = sys.argv[2]
    calibration = float(sys.argv[3])
except:
    print(__doc__)
    raise

st = obspy.read(in_file)
for i, tr in enumerate(st):
    f = open("%s_%d" % (out_file, i), "w")
    f.write("# STATION %s\n" % (tr.stats.station))
    f.write("# CHANNEL %s\n" % (tr.stats.channel))
    f.write("# START_TIME %s\n" % (str(tr.stats.starttime)))
    f.write("# SAMP_FREQ %f\n" % (tr.stats.sampling_rate))
    f.write("# NDAT %d\n" % (tr.stats.npts))
    np.savetxt(f, tr.data * calibration, fmt="%f")
    f.close()