diff --git a/.gitignore b/.gitignore index cd7b10125feecf3ca039b1da3ad14c85d922981f..fab8217807820cda3542c7129623794180e03e18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.swp *.xml -*.wpu +*.wp? *.ipynb diff --git a/nexus.py b/nexus.py index 73ddc236624a1375551a14d5c27ed1e1183f60ad..9fef93401008a7376d4699d9791a0a958e3fe559 100755 --- a/nexus.py +++ b/nexus.py @@ -1130,8 +1130,112 @@ class InventoryIm(obspy.Inventory): def clear_new(self): self.set_new(bool=False) - + def populate_from_streams(self, streams, new=False): + self.clear_new() + for trace in (traces for stream in streams for traces in stream): + net = trace.stats.network + sta = trace.stats.station + chan = trace.stats.channel + loc = trace.stats.location + start = trace.stats.starttime + end = trace.stats.endtime + sr = trace.stats.sampling_rate + + # Find what exists in current inventory + #Network + existing = self.select(network=net) + # remove for debugging + assert isinstance(existing, InventoryIm) + + inv_net = self.get_closest_epoch(existing.networks, + start, end) + if inv_net is None: + inv_net = self.new_network(code=net) + inv_net.start_date = start + inv_net.end_date = end + else: + inv_net = self.networks[self.networks.index(inv_net)] + self.extend_epoch(inv_net, start, end) + inv_net._new = new + + # Station + existing = inv_net.select(station=sta) + inv_sta = self.get_closest_epoch(existing.stations, + start, end) + if inv_sta is None: + inv_sta = self.new_station(inv_net, code=sta) + inv_sta.start_date = start + inv_sta.end_date = end + else: + inv_sta = inv_net.stations[inv_net.stations.index(inv_sta)] + self.extend_epoch(inv_sta, start, end) + inv_sta._new = new + + # Channel + existing = inv_sta.select(channel=chan, + location=loc, + sampling_rate=sr) + inv_cha = self.get_closest_epoch(existing.channels, + start, end) + if inv_cha is None: + inv_cha = self.new_channel(inv_sta, code=chan,loc=loc, sr=sr) + inv_cha.start_date = start + inv_cha.end_date = end + else: + inv_cha = inv_sta.channels[inv_sta.channels.index(inv_cha)] + self.extend_epoch(inv_cha, start, end) + inv_cha._new = new + + def extend_epoch(self, inv, start, end): + if inv.start_date > start: + inv.start_date = start + if inv.end_date < end: + inv.end_date = end + + def get_closest_epoch(self, inv, start, end): + ''' + typically for finding the correct object to extend + inv can be a networks, stations or channels list + returns the closest by time or None with the same + short circuts and returns object if timespans overlap + type as inv + ''' + if len(inv) < 1: + return None + elif len(inv) == 1: + #return the 1 match + return inv[0] + elif len(inv) > 1: + # return the closest by time + # minimun of end-start and start-end + ret = inv[0] + if self.overlap(ret, start, end): + return ret + dist = min(abs(ret.start_date - end), + abs(ret.end_date - start)) + for obj in inv[1:]: + if self.overlap(obj, start, end): + return obj + new_dist = min(abs(obj.start_date - end), + abs(obj.end_date - start)) + if new_dist < dist: + ret = obj + dist = new_dist + return ret + + def overlap(self, inv, start, end): + ''' + inv must have a start_date end_date + returns True if start-end overlap + ''' + # other is before with overlap + if inv.start_date <= end and inv.end_date >= start: + return True + return False + + + def populate_from_streams_old(self, streams, new=False): self.clear_new() for stream in streams: for trace in stream: diff --git a/nexus.wpr b/nexus.wpr index f038b28a79b0a8d13f0fbe251ce876fc759b1d20..c84db4866e822d50a48ea0507b5742fbb5b48e33 100644 --- a/nexus.wpr +++ b/nexus.wpr @@ -332,25 +332,25 @@ proj.launch-config = {loc('../../obspy/ABCmetaTest.py'): ('project', loc('../../obspy/testdl.py'): ('project', (u'', 'launch-0oKSwVd7xSObl21v')), - loc('../../../../../opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py'): ('p'\ + loc('../../../../opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py'): ('p'\ 'roject', (u'', 'launch-0oKSwVd7xSObl21v')), - loc('../../../../../opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pkg_resources/__init__.py'): ('p'\ + loc('../../../../opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pkg_resources/__init__.py'): ('p'\ 'roject', (u'', 'launch-0oKSwVd7xSObl21v')), - loc('../../../../../opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py'): ('p'\ + loc('../../../../opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py'): ('p'\ 'roject', (u'', 'launch-0oKSwVd7xSObl21v')), - loc('unknown:<untitled> #12'): ('project', - (u'', - 'launch-2PbMbG6O0zLB8HYF')), - loc('unknown:<untitled> #10'): ('project', + loc('unknown:<untitled> #3'): ('project', (u'', 'launch-0oKSwVd7xSObl21v')), - loc('unknown:<untitled> #11'): ('project', + loc('unknown:<untitled> #1'): ('project', + (u'', + 'launch-2PbMbG6O0zLB8HYF')), + loc('unknown:<untitled> #2'): ('project', (u'', 'launch-0oKSwVd7xSObl21v'))} testing.auto-test-file-specs = (('glob',