Skip to content

Commit

Permalink
Adding tests + small bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
krischer committed Mar 12, 2018
1 parent d28afd6 commit b32f116
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyasdf/asdf_data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,8 +1313,8 @@ def __get_waveform_ds_name(self, net, sta, loc, cha, start, end, tag):
# Add nanoseconds if two waveforms start in the same second. Only do
# so for recent ASDF versions.
if s == e and self._loose_asdf_format_version >= LooseVersion("1.0.2"):
s = "." + "%09i" % (start._ns % int(1e9))
e = "." + "%09i" % (end._ns % int(1e9))
s += "." + "%09i" % (start._ns % int(1e9))
e += "." + "%09i" % (end._ns % int(1e9))

return "{net}.{sta}.{loc}.{cha}__{start}__{end}__{tag}".format(
net=net, sta=sta, loc=loc, cha=cha,
Expand Down
51 changes: 51 additions & 0 deletions pyasdf/tests/test_asdf_data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -3022,3 +3022,54 @@ def test_get_waveform_attributes(example_data_set):
'sampling_rate': 40.0,
'starttime': 1369374000000000000}
}


def test_datesets_with_less_then_1_second_length(tmpdir):
asdf_filename = os.path.join(tmpdir.strpath, "test.h5")

tr = obspy.Trace(np.linspace(0, 1, 777),
header={"network": "AA", "station": "BB", "location": "",
"channel": "000",
"starttime": obspy.UTCDateTime(38978345.3445843)})

# Don't use nano-seconds if longer than one second.
with ASDFDataSet(asdf_filename) as ds:
tr.stats.sampling_rate = 1.0
ds.add_waveforms(tr, tag="test")
dataset_name = ds.waveforms["AA.BB"].list()[0]
os.remove(asdf_filename)

assert dataset_name == \
"AA.BB..000__1971-03-28T03:19:05__1971-03-28T03:32:01__test"

# Do, if shorter than one second.
with ASDFDataSet(asdf_filename) as ds:
tr.stats.sampling_rate = 474505737
ds.add_waveforms(tr, tag="test")
dataset_name = ds.waveforms["AA.BB"].list()[0]
os.remove(asdf_filename)

assert dataset_name == (
"AA.BB..000__1971-03-28T03:19:05.344584304__"
"1971-03-28T03:19:05.344585939__test")

# Don't do it for older versions to not write invalid files.
with ASDFDataSet(asdf_filename, format_version="1.0.1") as ds:
tr.stats.sampling_rate = 474505737
ds.add_waveforms(tr, tag="test")
dataset_name = ds.waveforms["AA.BB"].list()[0]
os.remove(asdf_filename)

assert dataset_name == (
"AA.BB..000__1971-03-28T03:19:05__1971-03-28T03:19:05__test")

# Check that leading nulls are also written.
with ASDFDataSet(asdf_filename) as ds:
tr.stats.starttime = obspy.UTCDateTime(0)
ds.add_waveforms(tr, tag="test")
dataset_name = ds.waveforms["AA.BB"].list()[0]
os.remove(asdf_filename)

assert dataset_name == (
"AA.BB..000__1970-01-01T00:00:00.000000000__"
"1970-01-01T00:00:00.000001635__test")

0 comments on commit b32f116

Please sign in to comment.