def getWaveform(self, network, station, location, channel, starttime, endtime, format="MSEED"): """ Retrieves waveform data from the NERIES Web service and returns a ObsPy Stream object. :type network: str :param network: Network code, e.g. ``'BW'``. :type station: str :param station: Station code, e.g. ``'MANZ'``. :type location: str :param location: Location code, e.g. ``'01'``. Location code may contain wild cards. :type channel: str :param channel: Channel code, e.g. ``'EHE'``. . Channel code may contain wild cards. :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime` :param starttime: Start date and time. :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime` :param endtime: End date and time. :type format: ``'FSEED'`` or ``'MSEED'``, optional :param format: Output format. Either as full SEED (``'FSEED'``) or Mini-SEED (``'MSEED'``) volume. Defaults to ``'MSEED'``. :return: ObsPy :class:`~obspy.core.stream.Stream` object. .. rubric:: Example >>> from obspy.neries import Client >>> client = Client(user='******') >>> dt = UTCDateTime("2009-04-01T00:00:00") >>> st = client.getWaveform("NL", "WIT", "", "BH*", dt, dt+30) >>> print st # doctest: +ELLIPSIS 3 Trace(s) in Stream: NL.WIT..BHZ | 2009-04-01T00:00:00.010200Z - ... | 40.0 Hz, 1201 samples NL.WIT..BHN | 2009-04-01T00:00:00.010200Z - ... | 40.0 Hz, 1201 samples NL.WIT..BHE | 2009-04-01T00:00:00.010200Z - ... | 40.0 Hz, 1201 samples """ tf = NamedTemporaryFile() self.saveWaveform(tf._fileobj, network, station, location, channel, starttime, endtime, format=format) # read stream using obspy.mseed tf.seek(0) try: stream = read(tf.name, 'MSEED') except: stream = Stream() tf.close() # remove temporary file: try: os.remove(tf.name) except: pass # trim stream stream.trim(starttime, endtime) return stream
def make_irisrequest(station, begin, end, channel, loc): print 'IRISREQUEST FOR STATION ', station net, sta = station.split('_') url = 'http://service.iris.edu/fdsnws/dataselect/1/query?' st = Stream() for i in channel: parameter = urllib.urlencode({ 'net': net, 'sta': sta, 'loc': loc, #'cha': i, #hs v2 'starttime': begin, 'endtime': end, 'nodata': '404', }) u = ('%s%s') % (url, parameter) saveUrl(station, u) #data = urllib.urlopen(u).read() #hs try: data = urllib.urlopen(u).read() #hs except: continue #tf = NamedTemporaryFile() #hs tf = NamedTemporaryFile(suffix='xtmp') #hs tf.write(data) tf.seek(0) t = isMSEED(tf.name) if t == True: st += read(tf.name, 'MSEED') tf.close() os.remove(tf.name) break #hs v2 # if t == True: size = proof_file_v3(st,channel) #hs if len(st) > 0: size = proof_file_v3(st, channel) #hs else: size = 0 print 'SIZE: ----> ', size return size
def make_irisrequest(station, begin, end, channel, loc): logger.info('\033[31m IRISREQUEST \033[0m\n' % ()) net, sta = station.split('_') url = 'http://service.iris.edu/fdsnws/dataselect/1/query?' st = Stream() for i in channel: parameter = urllib.urlencode({ 'net': net, 'sta': sta, 'loc': loc, 'cha': i, 'starttime': begin, 'endtime': end, 'nodata': '404', }) u = ('%s%s') % (url, parameter) data = urllib.urlopen(u).read() tf = NamedTemporaryFile() tf.write(data) tf.seek(0) st += read(tf.name, 'MSEED') tf.close() print st output = ('%s.%s-IRIS.mseed') % (net, sta) st.write(output, format='MSEED') size = proof_file_v2(output, channel) #print 'SIZE: ----> ',size try: #os.remove(fname) os.remove(output) except: print '' return size
def process_GET(self, request): """ Function that will be called upon receiving a GET request for the aforementioned URL. """ # Parse the given parameters. event_id = request.args0.get("event", None) channel_id = request.args0.get("channel_id", None) station_id = request.args0.get("station_id", None) tag = request.args0.get("tag", "") format = request.args0.get("format", None) # An event id is obviously needed. if event_id is None: msg = ("No event parameter passed. Every waveform " "is bound to an existing event.") raise InvalidParameterError(msg) if event_id is not None and not event_exists(event_id, self.env): msg = "The given event resource name '%s' " % event_id msg += "is not known to SeisHub." raise InvalidParameterError(msg) # Returns different things based on parameter combinations. # Return all waveforms available for a given event. if channel_id is None and station_id is None and event_id is not None: return self.getListForEvent(event_id, request) # Return all waveforms available for a given event and station id. elif station_id is not None and event_id is not None: return self.getListForStationAndEvent(event_id, station_id, request) # At this step format will mean a waveform output format. acceptable_formats = ["mseed", "sac", "gse2", "segy", "raw", "json"] if format and format.lower() not in acceptable_formats: msg = "'%s' is an unsupported format. Supported formats: %s" % \ (format, ", ".join(acceptable_formats)) raise InvalidParameterError(msg) if channel_id is None: msg = ("To download a waveform, 'channel_id' to be specified.") raise InvalidParameterError(msg) split_channel = channel_id.split(".") if len(split_channel) != 4: msg = "Invalid 'channel_id'. Needs to be NET.STA.LOC.CHAN." raise InvalidParameterError(msg) network, station, location, channel = split_channel session = self.env.db.session(bind=self.env.db.engine) station_id = get_station_id(network, station, session) if station_id is False: session.close() msg = "Could not find station %s.%s in the database" % \ (network, station) raise InvalidParameterError(msg) query = session.query(WaveformChannelObject)\ .join(ChannelObject)\ .filter(WaveformChannelObject.event_resource_id == event_id)\ .filter(WaveformChannelObject.tag == tag)\ .filter(ChannelObject.location == location)\ .filter(ChannelObject.channel == channel)\ .filter(ChannelObject.station_id == station_id) try: result = query.one() except sqlalchemy.orm.exc.NoResultFound: session.close() msg = "No matching data found in the database." raise NotFoundError(msg) if format and format.lower() == "raw": with open(result.filepath.filepath, "rb") as open_file: data = open_file.read() # Set the corresponding headers. request.setHeader("content-type", "application/octet-stream") filename = os.path.basename(result.filepath.filepath)\ .encode("utf-8") request.setHeader("content-disposition", "attachment; filename=%s" % filename) return data chan = result.channel stat = chan.station network = stat.network station = stat.station location = chan.location channel = chan.channel starttime = UTCDateTime(result.starttime) if result.starttime else None endtime = UTCDateTime(result.endtime) if result.endtime else None default_format = result.format # Read and filter the file. st = read(result.filepath.filepath).select(network=network, station=station, location=location, channel=channel) session.close() # Now attempt to find the correct trace in case of more then one trace. # This should enable multicomponent files. selected_trace = None for tr in st: if (starttime and abs(tr.stats.starttime - starttime) > 1) or \ (endtime and abs(tr.stats.endtime - endtime) > 1): continue selected_trace = tr break if selected_trace is None: msg = "Could not find the corresponding waveform file." raise InternalServerError(msg) # Deal with json format conversion. if format and format == "json": output = { "channel": selected_trace.id, "sampling_rate": selected_trace.stats.sampling_rate, "npts": selected_trace.stats.npts, "data": [] } time = selected_trace.stats.starttime delta = selected_trace.stats.delta for value in selected_trace.data: output["data"].append([time.isoformat(), float(value)]) time += delta request.setHeader('content-type', 'application/json; charset=UTF-8') return json.dumps(output) # XXX: Fix some ObsPy modules to be able to write to memory files. tempfile = NamedTemporaryFile() if format: default_format = format selected_trace.write(tempfile.name, format=default_format) with open(tempfile.name, "rb") as open_file: data = open_file.read() tempfile.close() os.remove(tempfile.name) # Set the corresponding headers. request.setHeader("content-type", "application/octet-stream") filename = "%s.%s" % (selected_trace.id, default_format.lower()) filename = filename.encode("utf-8") request.setHeader("content-disposition", "attachment; filename=%s" % filename) return data