Skip to content

Commit

Permalink
Merge 8e0b583 into 301a1ae
Browse files Browse the repository at this point in the history
  • Loading branch information
megies committed Jun 10, 2016
2 parents 301a1ae + 8e0b583 commit 8bb495b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ master:
- obspy.db:
* Fixed a bug in obspy-indexer command line script (see #1369,
command line script was not working, probably since 0.10.0)
- obspy.io.ascii:
* Fixed a bug that lead to wrong header information in output files when
writing non-integer sampling rate data to SLIST or TSPAIR formats
(see #1447)
- obspy.io.mseed:
* Fixed a bug in obspy-mseed-recordanalyzer (see #1386)
- obspy.io.nlloc:
Expand Down
36 changes: 25 additions & 11 deletions obspy/io/ascii/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,21 @@
from obspy.core.util import AttribDict, loadtxt


HEADER = "TIMESERIES %s_%s_%s_%s_%s, %d samples, %d sps, %.26s, %s, %s, %s\n"
HEADER = ("TIMESERIES {network}_{station}_{location}_{channel}_{dataquality}, "
"{npts:d} samples, {sampling_rate} sps, {starttime!s:.26s}, "
"{format}, {dtype}, {unit}\n")


def _format_header(stats, format, dataquality, dtype, unit):
sampling_rate = str(stats.sampling_rate)
if "." in sampling_rate and "E" not in sampling_rate.upper():
sampling_rate = sampling_rate.rstrip('0').rstrip('.')
header = HEADER.format(
network=stats.network, station=stats.station, location=stats.location,
channel=stats.channel, dataquality=dataquality, npts=stats.npts,
sampling_rate=sampling_rate, starttime=stats.starttime,
format=format, dtype=dtype, unit=unit)
return header


def _is_slist(filename):
Expand Down Expand Up @@ -151,7 +165,10 @@ def _read_slist(filename, headonly=False, **kwargs): # @UnusedVariable
stats.channel = temp[3]
stats.sampling_rate = parts[4]
# quality only used in MSEED
stats.mseed = AttribDict({'dataquality': temp[4]})
# don't put blank quality code into 'mseed' dictionary
# (quality code is mentioned as optional by format specs anyway)
if temp[4]:
stats.mseed = AttribDict({'dataquality': temp[4]})
stats.ascii = AttribDict({'unit': parts[-1]})
stats.starttime = UTCDateTime(parts[6])
stats.npts = parts[2]
Expand Down Expand Up @@ -216,7 +233,10 @@ def _read_tspair(filename, headonly=False, **kwargs): # @UnusedVariable
stats.channel = temp[3]
stats.sampling_rate = parts[4]
# quality only used in MSEED
stats.mseed = AttribDict({'dataquality': temp[4]})
# don't put blank quality code into 'mseed' dictionary
# (quality code is mentioned as optional by format specs anyway)
if temp[4]:
stats.mseed = AttribDict({'dataquality': temp[4]})
stats.ascii = AttribDict({'unit': parts[-1]})
stats.starttime = UTCDateTime(parts[6])
stats.npts = parts[2]
Expand Down Expand Up @@ -314,10 +334,7 @@ def _write_slist(stream, filename, **kwargs): # @UnusedVariable
except:
unit = ''
# write trace header
header = HEADER % (stats.network, stats.station, stats.location,
stats.channel, dataquality, stats.npts,
stats.sampling_rate, stats.starttime, 'SLIST',
dtype, unit)
header = _format_header(stats, 'SLIST', dataquality, dtype, unit)
fh.write(header.encode('ascii', 'strict'))
# write data
rest = stats.npts % 6
Expand Down Expand Up @@ -425,10 +442,7 @@ def _write_tspair(stream, filename, **kwargs): # @UnusedVariable
except:
unit = ''
# write trace header
header = HEADER % (stats.network, stats.station, stats.location,
stats.channel, dataquality, stats.npts,
stats.sampling_rate, stats.starttime, 'TSPAIR',
dtype, unit)
header = _format_header(stats, 'TSPAIR', dataquality, dtype, unit)
fh.write(header.encode('ascii', 'strict'))
# write data
times = np.linspace(stats.starttime.timestamp,
Expand Down
19 changes: 19 additions & 0 deletions obspy/io/ascii/tests/test_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,25 @@ def test_write_small_trace(self):
self.assertEqual(len(st), 1)
self.assertEqual(len(st[0]), num)

def test_float_sampling_rates_write_and_read(self):
"""
Tests writing and reading Traces with floating point and with less than
1 Hz sampling rates.
"""
tr = Trace(np.arange(10))
check_sampling_rates = (0.000000001, 1.000000001, 100.000000001,
99.999999999, 1.5, 1.666666, 10000.0001)
for format in ['SLIST', 'TSPAIR']:
for sps in check_sampling_rates:
tr.stats.sampling_rate = sps
with NamedTemporaryFile() as tf:
tempfile = tf.name
tr.write(tempfile, format=format)
# test results
got = read(tempfile, format=format)[0]
self.assertEqual(tr.stats.sampling_rate,
got.stats.sampling_rate)


def suite():
return unittest.makeSuite(ASCIITestCase, 'test')
Expand Down

0 comments on commit 8bb495b

Please sign in to comment.