def test_stream(): inventory = get_inventory() channels = ["HN1", "HN2", "HNZ"] data = np.random.rand(1000) traces = [] network = inventory.networks[0] station = network.stations[0] chlist = station.channels channelcodes = [ch.code for ch in chlist] for channel in channels: chidx = channelcodes.index(channel) channeldata = chlist[chidx] header = { "sampling_rate": channeldata.sample_rate, "npts": len(data), "network": network.code, "location": channeldata.location_code, "station": station.code, "channel": channel, "starttime": UTCDateTime(2010, 1, 1, 0, 0, 0), } trace = Trace(data=data, header=header) traces.append(trace) invstream = StationStream(traces=traces, inventory=inventory) inventory2 = invstream.getInventory() inv2_channel1 = inventory2.networks[0].stations[0].channels[0] inv_channel1 = inventory2.networks[0].stations[0].channels[0] assert inv_channel1.code == inv2_channel1.code # test the streamparam functionality statsdict = {"name": "Fred", "age": 34} invstream.setStreamParam("stats", statsdict) assert invstream.getStreamParamKeys() == ["stats"] cmpdict = invstream.getStreamParam("stats") assert statsdict == cmpdict
def test_stream(): inventory = get_inventory() channels = ['HN1', 'HN2', 'HNZ'] data = np.random.rand(1000) traces = [] network = inventory.networks[0] station = network.stations[0] chlist = station.channels channelcodes = [ch.code for ch in chlist] for channel in channels: chidx = channelcodes.index(channel) channeldata = chlist[chidx] header = { 'sampling_rate': channeldata.sample_rate, 'npts': len(data), 'network': network.code, 'location': channeldata.location_code, 'station': station.code, 'channel': channel, 'starttime': UTCDateTime(2010, 1, 1, 0, 0, 0) } trace = Trace(data=data, header=header) traces.append(trace) invstream = StationStream(traces=traces, inventory=inventory) inventory2 = invstream.getInventory() inv2_channel1 = inventory2.networks[0].stations[0].channels[0] inv_channel1 = inventory2.networks[0].stations[0].channels[0] assert inv_channel1.code == inv2_channel1.code # test the streamparam functionality statsdict = {'name': 'Fred', 'age': 34} invstream.setStreamParam('stats', statsdict) assert invstream.getStreamParamKeys() == ['stats'] cmpdict = invstream.getStreamParam('stats') assert statsdict == cmpdict
def __init__( self, streams=None, config=None, ): """Initialize StreamCollection. Args: streams (list): List of StationStream objects. config (dict): Configuration options. """ self.config = config if not isinstance(streams, list): raise TypeError("streams must be a list of StationStream objects.") newstreams = [] for st in streams: if not isinstance(st, StationStream): raise TypeError( "streams must be a list of StationStream objects.") else: if hasattr(st, "tag"): stream_tag = st.tag else: stream_tag = None stream_params_keys = st.getStreamParamKeys() for tr in st: new_st = StationStream(traces=[tr]) new_st.id = ".".join([ tr.stats.network, tr.stats.station, tr.stats.location, tr.stats.channel, ], ) if stream_tag is not None: new_st.tag = stream_tag if len(stream_params_keys): for k in stream_params_keys: new_st.setStreamParam(k, st.getStreamParam(k)) new_st.use_array = True newstreams.append(new_st) self.streams = newstreams
def getStreams(self, eventid, stations=None, labels=None, config=None): """Get Stream from ASDF file given event id and input tags. Args: eventid (str): Event ID corresponding to an Event in the workspace. stations (list): List of stations to search for. labels (list): List of processing labels to search for. config (dict): Configuration options. Returns: StreamCollection: Object containing list of organized StationStreams. """ trace_auxholder = [] stream_auxholder = [] auxdata = self.dataset.auxiliary_data if 'TraceProcessingParameters' in auxdata: trace_auxholder = auxdata.TraceProcessingParameters if 'StreamProcessingParameters' in auxdata: stream_auxholder = auxdata.StreamProcessingParameters streams = [] if stations is None: stations = self.getStations(eventid) if labels is None: labels = self.getLabels() for waveform in self.dataset.ifilter( self.dataset.q.station == stations, self.dataset.q.tag == ['%s_%s' % (eventid, label) for label in labels]): tags = waveform.get_waveform_tags() for tag in tags: tstream = waveform[tag] inventory = waveform['StationXML'] for ttrace in tstream: if isinstance(ttrace.data[0], np.floating): if ttrace.data[0].nbytes == 4: ttrace.data = ttrace.data.astype('float32') else: ttrace.data = ttrace.data.astype('float64') else: if ttrace.data[0].nbytes == 2: ttrace.data = ttrace.data.astype('int16') elif ttrace.data[0].nbytes == 4: ttrace.data = ttrace.data.astype('int32') else: ttrace.data = ttrace.data.astype('int64') trace = StationTrace(data=ttrace.data, header=ttrace.stats, inventory=inventory, config=config) # get the provenance information provname = format_nslct(trace.stats, tag) if provname in self.dataset.provenance.list(): provdoc = self.dataset.provenance[provname] trace.setProvenanceDocument(provdoc) # get the trace processing parameters top = format_netsta(trace.stats) trace_path = format_nslct(trace.stats, tag) if top in trace_auxholder: root_auxholder = trace_auxholder[top] if trace_path in root_auxholder: bytelist = root_auxholder[ trace_path].data[:].tolist() jsonstr = ''.join([chr(b) for b in bytelist]) jdict = json.loads(jsonstr) for key, value in jdict.items(): trace.setParameter(key, value) # get the trace spectra arrays from auxiliary, # repack into stationtrace object spectra = {} if 'Cache' in auxdata: for aux in auxdata['Cache'].list(): auxarray = auxdata['Cache'][aux] if top not in auxarray.list(): continue auxarray_top = auxarray[top] if trace_path in auxarray_top: specparts = camel_case_split(aux) array_name = specparts[-1].lower() specname = '_'.join(specparts[:-1]).lower() specarray = auxarray_top[trace_path].data[()] if specname in spectra: spectra[specname][array_name] = specarray else: spectra[specname] = {array_name: specarray} for key, value in spectra.items(): trace.setCached(key, value) stream = StationStream(traces=[trace]) stream.tag = tag # testing this out # get the stream processing parameters stream_path = format_nslit( trace.stats, stream.get_inst(), tag) if top in stream_auxholder: top_auxholder = stream_auxholder[top] if stream_path in top_auxholder: auxarray = top_auxholder[stream_path] bytelist = auxarray.data[:].tolist() jsonstr = ''.join([chr(b) for b in bytelist]) jdict = json.loads(jsonstr) for key, value in jdict.items(): stream.setStreamParam(key, value) streams.append(stream) # No need to handle duplicates when retrieving stations from the # workspace file because it must have been handled before putting them # into the workspace file. streams = StreamCollection( streams, handle_duplicates=False, config=config) return streams