Skip to content

Commit c5b6f31

Browse files
author
Malcolm White
committed
Make Reference creation and retrieval more flexible.
1 parent 73ae2bc commit c5b6f31

File tree

1 file changed

+165
-52
lines changed

1 file changed

+165
-52
lines changed

pyasdf/asdf_data_set.py

Lines changed: 165 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,73 +1231,186 @@ def __parse_waveform_input_and_validate(self, waveform):
12311231
raise NotImplementedError
12321232
return waveform
12331233

1234-
def create_reference(self, ref, net, sta, loc, chan, starttime, endtime,
1235-
tag=None, overwrite=False):
1234+
def create_reference(self, ref, starttime, endtime, net=None, sta=None,
1235+
loc=None, chan=None, tag=None, overwrite=False):
12361236
"""
12371237
Create a region reference for fast lookup of segements of
12381238
continuous data.
12391239
"""
12401240

1241-
# Build the station name
1242-
_station_name = "%s.%s" % (net, sta)
1243-
# Iterate over datasets in waveform group to find matching
1244-
# net:sta:loc:chan combination.
1245-
for _key in self._waveform_group[_station_name]:
1246-
_net, _sta, _loc, _remainder = _key.split(".")
1241+
if isinstance(net, str):
1242+
net = (net,)
1243+
elif isinstance(net, tuple) or isinstance(net, list) or net is None:
1244+
pass
1245+
else:
1246+
raise(TypeError(net))
12471247

1248-
if _net != net or _sta != sta or _loc != loc:
1249-
continue
1248+
if isinstance(sta, str):
1249+
sta = (sta,)
1250+
elif isinstance(sta, tuple) or isinstance(sta, list) or sta is None:
1251+
pass
1252+
else:
1253+
raise(TypeError(sta))
12501254

1251-
_chan, _ts, _te, _tag = _remainder.split("__")
1252-
if _chan != chan or (tag is not None and _tag != tag):
1253-
continue
1255+
if isinstance(loc, str):
1256+
loc = (loc,)
1257+
elif isinstance(loc, tuple) or isinstance(loc, list) or loc is None:
1258+
pass
1259+
else:
1260+
raise(TypeError(loc))
12541261

1255-
_ds = self._waveform_group["%s/%s" % (_station_name, _key)]
1262+
if isinstance(chan, str):
1263+
chan = (chan,)
1264+
elif isinstance(chan, tuple) or isinstance(chan, list) or chan is None:
1265+
pass
1266+
else:
1267+
raise(TypeError(chan))
12561268

1257-
_ts = obspy.UTCDateTime(_ds.attrs["starttime"]*1e-9)
1258-
_samprate = _ds.attrs["sampling_rate"]
1259-
_te = _ts + len(_ds)/_samprate
1260-
if _te < starttime or _ts > endtime:
1261-
continue
1269+
if isinstance(tag, str):
1270+
tag = (tag,)
1271+
elif isinstance(tag, tuple) or isinstance(tag, list) or tag is None:
1272+
pass
1273+
else:
1274+
raise(TypeError(tag))
12621275

1263-
_offset = int((starttime-_ts)*_samprate)
1264-
_nsamp = int((endtime-starttime)*_samprate)
1265-
_ref = _ds.regionref[_offset:_offset+_nsamp+1]
1276+
_predicate_net = lambda _key: net == None\
1277+
or _key.split(".")[0] in net
12661278

1267-
if ref not in self._reference_group:
1268-
_ref_grp = self._reference_group.create_group(ref)
1269-
else:
1270-
_ref_grp = self._reference_group[ref]
1279+
_predicate_sta = lambda _key: sta == None\
1280+
or _key.split(".")[1] in sta
12711281

1272-
_handle = ".".join((_net, _sta, _loc, _chan))
1282+
_predicate_loc = lambda _key: loc == None\
1283+
or _key.split(".")[2] in loc
12731284

1274-
if overwrite is True and _handle in _ref_grp:
1275-
del(_ref_grp[_handle])
1285+
_predicate_chan = lambda _key: chan == None\
1286+
or _key.split(".")[-1].split("__")[0] in chan
12761287

1277-
if _handle not in _ref_grp:
1278-
_ref_ds = _ref_grp.create_dataset(_handle,
1279-
(1,),
1280-
dtype=h5py.special_dtype(ref=h5py.RegionReference))
1281-
_ref_ds.attrs["sampling_rate"] = _ds.attrs["sampling_rate"]
1282-
_ref_ds.attrs["starttime"] = _ds.attrs["starttime"] + int(_offset/_samprate*1.e9)
1283-
_ref_ds[0] = _ref
1284-
else:
1285-
print("Will not overwrite existing reference")
1286-
continue
1288+
_predicate_tag = lambda _key: tag == None\
1289+
or _key.split(".")[-1].split("__")[-1] in tag
1290+
1291+
_predicate_netsta = lambda _key: _predicate_net(_key)\
1292+
and _predicate_sta(_key)
1293+
1294+
_predicate_locchantag = lambda _key: _predicate_loc(_key)\
1295+
and _predicate_chan(_key)\
1296+
and _predicate_tag(_key)
1297+
1298+
for _station_name in itertools.ifilter(_predicate_netsta,
1299+
self._waveform_group.keys()):
1300+
for _key in itertools.ifilter(_predicate_locchantag,
1301+
self._waveform_group[_station_name].keys()):
1302+
1303+
_net, _sta, _loc, _remainder = _key.split(".")
1304+
_chan = _remainder.split("__")[0]
1305+
1306+
_ds = self._waveform_group["%s/%s" % (_station_name, _key)]
1307+
1308+
_ts = obspy.UTCDateTime(_ds.attrs["starttime"]*1e-9)
1309+
_samprate = _ds.attrs["sampling_rate"]
1310+
_te = _ts + len(_ds)/_samprate
1311+
if _te < starttime or _ts > endtime:
1312+
continue
1313+
1314+
_offset = int((starttime-_ts)*_samprate)
1315+
_nsamp = int((endtime-starttime)*_samprate)
1316+
_ref = _ds.regionref[_offset:_offset+_nsamp+1]
1317+
1318+
if ref not in self._reference_group:
1319+
_ref_grp = self._reference_group.create_group(ref)
1320+
else:
1321+
_ref_grp = self._reference_group[ref]
1322+
1323+
_net = "__" if _net == "" else _net
1324+
_sta = "__" if _sta == "" else _sta
1325+
_loc = "__" if _loc == "" else _loc
1326+
_chan = "__" if _chan == "" else _chan
1327+
_handle = "/".join((_net, _sta, _loc, _chan))
1328+
1329+
if overwrite is True and _handle in _ref_grp:
1330+
del(_ref_grp[_handle])
1331+
1332+
if _handle not in _ref_grp:
1333+
_ref_ds = _ref_grp.create_dataset(_handle,
1334+
(1,),
1335+
dtype=h5py.special_dtype(ref=h5py.RegionReference))
1336+
_ref_ds.attrs["sampling_rate"] = _ds.attrs["sampling_rate"]
1337+
_ref_ds.attrs["starttime"] = _ds.attrs["starttime"] + int(_offset/_samprate*1.e9)
1338+
_ref_ds[0] = _ref
1339+
else:
1340+
print("Will not overwrite existing reference")
1341+
continue
1342+
1343+
def get_data_for_reference(self, ref, net=None, sta=None, loc=None,
1344+
chan=None):
1345+
"""
1346+
Create a region reference for fast lookup of segements of
1347+
continuous data.
1348+
"""
1349+
if not isinstance(ref, str):
1350+
raise(TypeError("reference must be type ::str::"))
1351+
if ref not in self._reference_group:
1352+
raise(IOError("reference does not exist: %s" % ref))
12871353

1288-
def get_data_for_reference(self, ref, net, sta, loc, chan):
1289-
_handle = "%s/%s.%s.%s.%s" % (ref, net, sta, loc, chan)
1290-
if _handle not in self._reference_group:
1291-
raise(IOError("Data not found"))
1292-
_ref = self._reference_group[_handle][0]
1293-
tr = obspy.Trace(data=self.__file[_ref][_ref])
1294-
tr.stats.network = net
1295-
tr.stats.station = sta
1296-
tr.stats.location = loc
1297-
tr.stats.channel = chan
1298-
tr.stats.starttime = obspy.UTCDateTime(self._reference_group[_handle].attrs["starttime"]*1e-9)
1299-
tr.stats.delta = 1/self._reference_group[_handle].attrs["sampling_rate"]
1300-
return(tr)
1354+
if isinstance(net, str):
1355+
net = (net,)
1356+
elif isinstance(net, tuple) or isinstance(net, list) or net is None:
1357+
pass
1358+
else:
1359+
raise(TypeError(net))
1360+
1361+
if isinstance(sta, str):
1362+
sta = (sta,)
1363+
elif isinstance(sta, tuple) or isinstance(sta, list) or sta is None:
1364+
pass
1365+
else:
1366+
raise(TypeError(sta))
1367+
1368+
if isinstance(loc, str):
1369+
loc = (loc,)
1370+
elif isinstance(loc, tuple) or isinstance(loc, list) or loc is None:
1371+
pass
1372+
else:
1373+
raise(TypeError(loc))
1374+
1375+
if isinstance(chan, str):
1376+
chan = (chan,)
1377+
elif isinstance(chan, tuple) or isinstance(chan, list) or chan is None:
1378+
pass
1379+
else:
1380+
raise(TypeError(chan))
1381+
1382+
_predicate_net = lambda _key: net == None or _key in net
1383+
_predicate_sta = lambda _key: sta == None or _key in sta
1384+
_predicate_loc = lambda _key: loc == None or _key in loc
1385+
_predicate_chan = lambda _key: chan == None or _key in chan
1386+
1387+
_st = obspy.Stream()
1388+
_ref_grp = self._reference_group[ref]
1389+
for _net in itertools.ifilter(_predicate_net,
1390+
_ref_grp.keys()):
1391+
_net_grp = _ref_grp[_net]
1392+
1393+
for _sta in itertools.ifilter(_predicate_sta,
1394+
_net_grp.keys()):
1395+
_sta_grp = _net_grp[_sta]
1396+
1397+
for _loc in itertools.ifilter(_predicate_loc,
1398+
_sta_grp.keys()):
1399+
_loc_grp = _sta_grp[_loc]
1400+
1401+
for _chan in itertools.ifilter(_predicate_chan,
1402+
_loc_grp.keys()):
1403+
_ds = _loc_grp[_chan]
1404+
_ref = _ds[0]
1405+
_tr = obspy.Trace(data=self.__file[_ref][_ref])
1406+
_tr.stats.network = _net if _net != "__" else ""
1407+
_tr.stats.station = _sta if _sta != "__" else ""
1408+
_tr.stats.location = _loc if _loc != "__" else ""
1409+
_tr.stats.channel = _chan if _chan != "__" else ""
1410+
_tr.stats.starttime = obspy.UTCDateTime(_ds.attrs["starttime"]*1e-9)
1411+
_tr.stats.delta = 1/_ds.attrs["sampling_rate"]
1412+
_st.append(_tr)
1413+
return(_st)
13011414

13021415
def get_provenance_document(self, document_name):
13031416
"""

0 commit comments

Comments
 (0)