Skip to content

Commit bed89ce

Browse files
author
Malcolm White
committed
Add I/O for masked Traces
Make backwards compatible with older ASDF versions I/O of masked arrays of float/integer data. Ignore some test files.
1 parent fb88b35 commit bed89ce

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
*.egg-info
22
doc/_build/
33
.cache
4+
*.pyc
5+
.coverage
6+
.pytest_cache

pyasdf/asdf_data_set.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import prov
3838
import prov.model
3939

40-
4140
# Minimum compatibility wrapper between Python 2 and 3.
4241
try:
4342
filter = itertools.ifilter
@@ -811,7 +810,13 @@ def _get_waveform(self, waveform_name, starttime=None, endtime=None):
811810
data = self.__file["Waveforms"]["%s.%s" % (network, station)][
812811
waveform_name]
813812

814-
tr = obspy.Trace(data=data[idx_start: idx_end])
813+
if "mask" in data.attrs and data.attrs["mask"] is not False:
814+
_data = np.ma.masked_values(data[idx_start: idx_end],
815+
data.attrs["mask"])
816+
else:
817+
_data = data[idx_start: idx_end]
818+
819+
tr = obspy.Trace(data=_data)
815820
tr.stats.starttime = data_starttime
816821
tr.stats.sampling_rate = data.attrs["sampling_rate"]
817822
tr.stats.network = network
@@ -1167,6 +1172,8 @@ def add_waveforms(self, waveform, tag, event_id=None, origin_id=None,
11671172

11681173
# Actually add the data.
11691174
for trace in waveform:
1175+
if isinstance(trace.data, np.ma.masked_array):
1176+
self.__set_masked_array_fill_value(trace)
11701177
# Complicated multi-step process but it enables one to use
11711178
# parallel I/O with the same functions.
11721179
info = self._add_trace_get_collective_information(
@@ -1179,6 +1186,16 @@ def add_waveforms(self, waveform, tag, event_id=None, origin_id=None,
11791186
self._add_trace_write_collective_information(info)
11801187
self._add_trace_write_independent_information(info, trace)
11811188

1189+
def __set_masked_array_fill_value(self, trace):
1190+
if trace.data.dtype.kind in ("i", "u"):
1191+
_info = np.iinfo
1192+
elif trace.data.dtype.kind == "f" :
1193+
_info = np.finfo
1194+
else:
1195+
raise(NotImplementedError("fill value for dtype %s not defined"\
1196+
% trace.data.dtype))
1197+
trace.data.set_fill_value(_info(trace.data.dtype).min)
1198+
11821199
def __parse_and_validate_tag(self, tag):
11831200
tag = tag.strip()
11841201
if tag.lower() == "stationxml":
@@ -1295,7 +1312,7 @@ def _add_trace_write_independent_information(self, info, trace):
12951312
:param trace:
12961313
:return:
12971314
"""
1298-
self._waveform_group[info["data_name"]][:] = trace.data
1315+
self._waveform_group[info["data_name"]][:] = np.ma.filled(trace.data)
12991316

13001317
def _add_trace_write_collective_information(self, info):
13011318
"""
@@ -1367,6 +1384,12 @@ def _add_trace_get_collective_information(
13671384
else:
13681385
fletcher32 = True
13691386

1387+
# Determine appropriate mask value.
1388+
if not isinstance(trace.data, np.ma.masked_array):
1389+
_mask = np.bool(False)
1390+
else:
1391+
_mask = trace.data.fill_value
1392+
13701393
info = {
13711394
"station_name": station_name,
13721395
"data_name": group_name,
@@ -1384,7 +1407,8 @@ def _add_trace_get_collective_information(
13841407
# Starttime is the epoch time in nanoseconds.
13851408
"starttime":
13861409
int(round(trace.stats.starttime.timestamp * 1.0E9)),
1387-
"sampling_rate": trace.stats.sampling_rate
1410+
"sampling_rate": trace.stats.sampling_rate,
1411+
"mask": _mask
13881412
}
13891413
}
13901414

0 commit comments

Comments
 (0)