def txt2inv(fin, **kwargs): """ Return :class:`~obspy.core.inventory.Inventory` from text file. """ col_sta = kwargs.get('col_sta', 0) col_lon = kwargs.get('col_lon', 1) col_lat = kwargs.get('col_lat', 2) col_net = kwargs.get('col_net', 3) source = kwargs.get('source', 'CIEI') chacodes = kwargs.get('chacodes', ['BHZ']) locode = kwargs.get('locode', '01') elev = kwargs.get('elev', 0) depth = kwargs.get('depth', 0) inv = Inventory(networks=[], source=source) ncha = len(chacodes) # These are not used but required by pyasdf creation_date = obspy.core.utcdatetime.UTCDateTime(0) site = inventory.util.Site(name='') with open(fin, 'r') as f: for line in f: meta = line.split() stacode = meta[col_sta] lon = meta[col_lon] lat = meta[col_lat] netcode = meta[col_net] lon = float(lon) if lon > 180: lon -= 360 lat = float(lat) xyz = {'latitude': lat, 'longitude': lon, 'elevation': elev} channels = [] for chacode in chacodes: channels.append( inventory.channel.Channel(code=chacode, location_code=locode, depth=depth, **xyz)) station = inventory.station.Station(code=stacode, channels=channels, total_number_of_channels=ncha, site=site, creation_date=creation_date, **xyz) network = inventory.network.Network( code=netcode, stations=[station], ) inv += Inventory(networks=[network], source=source) return inv
def run(self): """ Make a webservice request for events using the passed in options. """ self.setPriority(QtCore.QThread.LowestPriority) self.clearFutures() self.futures = {} inventory = None LOGGER.info("Making %d station requests" % len(self.request.sub_requests)) with concurrent.futures.ThreadPoolExecutor(5) as executor: for sub_request in self.request.sub_requests: # Dictionary lets us look up argument by result later self.futures[executor.submit(load_stations, self.request.client, sub_request)] = sub_request # Iterate through Futures as they complete for result in concurrent.futures.as_completed(self.futures): LOGGER.debug("Stations loaded") try: if not inventory: inventory = result.result() else: inventory += result.result() self.progress.emit() except Exception: self.progress.emit() self.futures = {} # If no inventory object (ie. no sub-requests were run) create a dummy one if not inventory: inventory = Inventory([], 'INTERNAL') inventory = self.request.process_result(inventory) self.done.emit(inventory)
def select_inventory(stream, inv, starttime=None, endtime=None): """ Convenience function to select inventory from active traces only """ invs = Inventory() for tr in stream: invs += inv.select(network=tr.stats.network, station=tr.stats.station, location=tr.stats.location, channel=tr.stats.channel, starttime=starttime, endtime=endtime) return invs
def sac2asdf(sac_directory, response_directory, cmt_path, output_path): with pyasdf.ASDFDataSet(output_path, mode="w", compression=None, mpi=False) as ds: # read in eventxml event_xml = obspy.read_events(cmt_path) # add eventxml to ds ds.add_quakeml(event_xml) event = ds.events[0] # read in waves files = sorted(glob(join(sac_directory, "*"))) # we should select files files = [item for item in files if isfile(item)] station_xml = Inventory() # pylint: disable=no-value-for-parameter stream_mapper = {} for filename in files: waveform_stream = obspy.read(filename) waveid = waveform_stream[0].id net, sta, _, _ = waveid.split(".") thekey = f"{net}.{sta}" stream_mapper[thekey] = waveform_stream ds.add_waveforms(waveform_stream, tag="raw", event_id=event) # # add stationxml # allfiles = sorted(glob(join(response_directory, "*"))) # station_xml_this_seed = Inventory() # pylint: disable=no-value-for-parameter # for fname in allfiles: # inv_temp = obspy.read_inventory(fname) # # update essencial location information # inv_temp = update_info(inv_temp, waveform_stream) # if(inv_temp == None): # continue # station_xml_this_seed += inv_temp # station_xml += station_xml_this_seed # ds.add_stationxml(station_xml) # add stationxml allfiles = sorted(glob(join(response_directory, "*"))) for fname in allfiles: inv_temp = obspy.read_inventory(fname) rep_usable_channel = inv_temp.get_contents()["channels"][0] net, sta, _, _ = rep_usable_channel.split(".") thekey = f"{net}.{sta}" waveform_stream = stream_mapper[thekey] inv_temp = update_info(inv_temp, waveform_stream) station_xml += inv_temp ds.add_stationxml(station_xml)
def load_stations(client, parameters): """ Execute one query for station metadata. This is a standalone function so we can run it in a separate thread. """ try: LOGGER.info('Loading stations: %s', get_service_url(client, 'station', parameters)) return client.get_stations(**parameters) except Exception as e: # If no results found, the client will raise an exception, we need to trap this # TODO: this should be much cleaner with a fix to https://github.com/obspy/obspy/issues/1656 if str(e).startswith("No data"): LOGGER.warning("No stations found! Your query may be too narrow.") return Inventory([], 'INTERNAL') else: raise
def save_raw(saved: dict, st: Stream, rawloc: str, inv: Inventory, saveasdf: bool): """ Save the raw waveform data in the desired format. The point of this function is mainly that the waveforms will be saved with the correct associations and at the correct locations. :param saved: Dictionary holding information about the original streams to identify them afterwards. :type saved: dict :param st: obspy stream holding all data (from various stations) :type st: Stream :param rawloc: Parental directory (with phase) to save the files in. :type rawloc: str :param inv: The inventory holding all the station information :type inv: Inventory :param saveasdf: If True the data will be saved in asdf format. :type saveasdf: bool """ # Just use the same name for evt, startt, endt, net, stat in zip(saved['event'], saved['startt'], saved['endt'], saved['net'], saved['stat']): # earlier we downloaded all locations, but we don't really want # to have several, so let's just keep one try: sst = st.select(network=net, station=stat) # This might actually be empty if so, let's just skip if sst.count() == 0: logging.debug(f'No trace of {net}.{stat} in Stream.') continue slst = sst.slice(startt, endt) # Only write the prevelant location locs = [tr.stats.location for tr in sst] filtloc = max(set(locs), key=locs.count) sslst = slst.select(location=filtloc) if saveasdf: sinv = inv.select(net, stat, starttime=startt, endtime=endt) write_st(sslst, evt, rawloc, sinv) else: save_raw_mseed(evt, sslst, rawloc, net, stat) except Exception as e: logging.error(e)
def get_filtered_inventory(inventory, stations_iter): """ Given an inventory and an iterator of selected network/station/channel items, return an inventory containing only the selected items. """ networks = {} stations = {} for (network, station, channel) in stations_iter: # Create a station record if necessary, and add this channel to it full_station_code = "%s.%s" % (network.code, station.code) if full_station_code not in stations: f_station = copy.copy(station) f_station.channels = [] stations[full_station_code] = f_station # Create a network record if necessary, and add this station to it if network.code not in networks: f_network = copy.copy(network) f_network.stations = [] networks[network.code] = f_network networks[network.code].stations.append(f_station) stations[full_station_code].channels.append(channel) return Inventory(list(networks.values()), inventory.source, inventory.sender)
longitude=YOUR_LONGITUDE, maxradius=15) except: pass try: cat2 += iris.get_events(starttime=t1, endtime=t2, minmagnitude=6) except: pass print(cat.__str__(print_all=True)) print(cat2.__str__(print_all=True)) cat.write('evtlocal30days.xml', format='QUAKEML') cat2.write('evtmajor30days.xml', format='QUAKEML') inv = Inventory((Network('AM'), ), 'AM') i = True if all( isinstance(stn, basestring) for stn in YOUR_STATIONS ): # check iterable for stringiness of all items. Will raise TypeError if some_object is not iterable for stn in YOUR_STATIONS: try: sta = read_inventory( 'http://raspberryshake.net:8080/fdsnws/station/1/query?network=AM&station=%s&level=resp&format=sc3ml' % stn.upper()) sta.write('sta%s.xml' % stn.upper(), 'STATIONXML') if i: inv = sta else: inv = inv + sta
def acquisition(event_time, lat_ep, lon_ep, depth, data_to_use, eew=False): """ """ t1 = event_time - 3 * 60 t2 = event_time + 81 * 60 t3 = event_time - 60 if not eew else event_time - 5 * 60 t4 = event_time + 5 * 60 if not eew else event_time + 140 * 60 client_iris = Client("IRIS") try: client_gfz = Client("GFZ") #For stations belonging to CX network except: pass inventory = Inventory([], None) if 'strong' in data_to_use: networks = "C,C1,II,IU" inventory = client_iris.get_stations( starttime=event_time - 60, endtime=event_time + 5*60, network=networks, channel="HN*", level="response", maxradius=10, latitude=lat_ep, longitude=lon_ep) networks = "CX" try: inventory = inventory + client_gfz.get_stations( starttime=event_time - 60, endtime=event_time + 5*60, network=networks, channel="HL*", level="response", maxradius=10, latitude=lat_ep, longitude=lon_ep) print('we have the inventory') except: pass inventory_tele = Inventory([], None) if 'tele' in data_to_use: networks = "II,G,IU,GE" max_dist = 90 inventory_tele = client_iris.get_stations( starttime=event_time - 10*60, endtime=event_time + 120*60, network=networks, channel="BH*", level="response", minradius=30, maxradius=max_dist, latitude=lat_ep, longitude=lon_ep) cola_tr = queue.Queue() cola_mt = queue.Queue() iris_client = IRIS_Client() for network in inventory_tele: netwk = network.code for station in network: statn = station.code for canal in station: loc_code = canal.location_code channel = canal.code sac_dict = __get_channel_information_manual( canal, lat_ep, lon_ep, depth) t_tr = threading.Thread( target=lambda q, a, b, c, d, e, f, g, h: q.put( worker2(a, b, c, d, e, f, g, h)), args = (cola_tr, client_iris, netwk, statn, loc_code, channel, sac_dict, t1, t2)) t_mt = threading.Thread( target=lambda q, a, b, c, d, e, f, g: q.put( worker3(a, b, c, d, e, f, g)), args = (cola_mt, iris_client, netwk, statn, loc_code, channel, t1, t2)) t_tr.daemon = True t_tr.start() t_mt.daemon = True t_mt.start() time.sleep(100) cola_tr = queue.Queue() cola_mt = queue.Queue() for network in inventory: netwk = network.code for station in network: statn = station.code if statn in ['AC02']: continue for canal in station: loc_code = canal.location_code channel = canal.code sac_dict = __get_channel_information_manual( canal, lat_ep, lon_ep, depth) # worker1(netwk, statn, loc_code, channel, sac_dict, t3, t4) # worker4(canal, netwk, statn, loc_code, channel, t3, t4) if not netwk=='CX': t_tr = threading.Thread( target=lambda q, a, b, c, d, e, f, g, h:\ q.put(worker2(a, b, c, d, e, f, g, h)), args = (cola_tr, client_iris, netwk, statn, loc_code, channel, sac_dict, t3, t4)) else: t_tr = threading.Thread( target=lambda q, a, b, c, d, e, f, g, h:\ q.put(worker2(a, b, c, d, e, f, g, h)), args = (cola_tr, client_gfz, netwk, statn, loc_code, channel, sac_dict, t3, t4)) t_mt = threading.Thread( target=lambda q, a, b, c, d, e, f, g:\ q.put(worker4(a, b, c, d, e, f, g)), args = (cola_mt, canal, netwk, statn, loc_code, channel, t3, t4)) t_tr.daemon = True t_tr.start() t_mt.daemon = True t_mt.start() time.sleep(100) return
def seed2asdf(seed_directory, cmt_path, output_path): with pyasdf.ASDFDataSet(output_path, mode="w", compression=None) as ds: # read in eventxml event_xml = obspy.read_events(cmt_path) # add eventxml to ds ds.add_quakeml(event_xml) event = ds.events[0] # read in waves files = sorted(glob(join(seed_directory, "*"))) station_xml = Inventory() # pylint: disable=no-value-for-parameter waveform_read_status = None for filename in files: # try to use obspy try: waveform_stream = obspy.read(filename) waveform_read_status = 1 except: # pylint: disable=bare-except dirpath = tempfile.mkdtemp() command = f"rdseed -d -f {filename} -q {dirpath}" subprocess.call(command, shell=True) waveform_stream = obspy.read(join(dirpath, "*SAC")) waveform_read_status = 2 ds.add_waveforms(waveform_stream, tag="raw", event_id=event) # add stationxml (since statinxml may be different for different events, it's better # to store only one event in ds) try: station_xml_file_obj = tempfile.NamedTemporaryFile( delete=False) station_xml_file_obj.close() station_xml_file_path = station_xml_file_obj.name sp = Parser(filename) sp.write_xseed(station_xml_file_path) station_xml_this_seed = obspy.read_inventory( station_xml_file_path) except: # pylint: disable=bare-except # since such an error occurs, we might have to use the except part to read the waveform to get the sac head if (waveform_read_status == 1): # re readin waveform_stream dirpath = tempfile.mkdtemp() command = f"rdseed -d -f {filename} -q {dirpath}" subprocess.call(command, shell=True) waveform_stream = obspy.read(join(dirpath, "*SAC")) else: pass # should already have the head information dirpath = tempfile.mkdtemp() command = f"rdseed -R -f {filename} -q {dirpath}" subprocess.call(command, shell=True) station_xml_this_seed = Inventory() # pylint: disable=no-value-for-parameter allfiles = sorted(glob(join(dirpath, "*"))) for fname in allfiles: inv_temp = obspy.read_inventory(fname) # update essencial location information inv_temp = update_info(inv_temp, waveform_stream) if (inv_temp == None): continue station_xml_this_seed += inv_temp station_xml += station_xml_this_seed ds.add_stationxml(station_xml)