def data_download(stations, starttime, endtime, event_name): print "\n========================================" print "event:", event_name print "time:", starttime, endtime waveforms_folder = "waveforms/" + event_name stationxml_folder = "stationxml/" + event_name c = Client("IRIS") if not os.path.exists(waveforms_folder): os.makedirs(waveforms_folder) if not os.path.exists(stationxml_folder): os.makedirs(stationxml_folder) for network, station in stations: ### First download waveforms. filename = os.path.join(waveforms_folder, "%s.%s.mseed" % (network, station)) if os.path.exists(filename): continue try: c.get_waveforms(network=network, station=station, location="*", channel="BH?", starttime=starttime, endtime=endtime, filename=filename) except Exception as e: print("Failed to download %s.%s due to %s" % (network, station, str(e))) continue print("Successfully downloaded %s." % filename) ### ### Then download stationxml file stationxml_filename = os.path.join(stationxml_folder, "%s.%s.xml" % (network, station)) if os.path.exists(stationxml_filename): continue try: c.get_stations(network=network, station=station, location="*", channel="BH?", starttime=starttime, endtime=endtime, filename=stationxml_filename, level="response") except Exception as e: print("Failed to download %s.%s StationXML due to %s" % (network, station, str(e))) continue print("Successfully downloaded %s." % stationxml_filename)
def data_download(stations, starttime, endtime, event_name): print "\n========================================" print "event:", event_name print "time:", starttime, endtime waveforms_folder = "waveforms/" + event_name stationxml_folder = "stationxml/" + event_name c = Client("IRIS") if not os.path.exists(waveforms_folder): os.makedirs(waveforms_folder) if not os.path.exists(stationxml_folder): os.makedirs(stationxml_folder) for network, station in stations: ### First download waveforms. filename = os.path.join(waveforms_folder, "%s.%s.mseed" % (network, station)) if os.path.exists(filename): continue try: c.get_waveforms(network=network, station=station, location="*", channel="BH?", starttime=starttime, endtime=endtime, filename=filename) except Exception as e: print("Failed to download %s.%s due to %s" % (network, station, str(e))) continue print("Successfully downloaded %s." % filename) ### ### Then download stationxml file stationxml_filename = os.path.join(stationxml_folder, "%s.%s.xml" % (network, station)) if os.path.exists(stationxml_filename): continue try: c.get_stations(network=network, station=station, location="*", channel="BH?", starttime=starttime, endtime=endtime, filename=stationxml_filename, level="response") except Exception as e: print("Failed to download %s.%s StationXML due to %s" % ( network, station, str(e))) continue print("Successfully downloaded %s." % stationxml_filename)
def getepidata(event_lat, event_lon, event_time, tstart=-5., tend=200., minradiuskm=0., maxradiuskm=20., channels='*', location='*', source='IRIS'): """ Automatically pull existing data within a certain distance of the epicenter (or any lat/lon coordinates) and attach station coordinates to data USAGE st = getepidata(event_lat, event_lon, event_time, tstart=-5., tend=200., minradiuskm=0., maxradiuskm=20., channels='*', location='*', source='IRIS') INPUTS event_lat = latitude of event in decimal degrees event_lon = longitude of event in decimal degrees event_time = Event time in UTC in any format obspy's UTCDateTime can parse - e.g. '2016-02-05T19:57:26' tstart = number of seconds to add to event time for start time of data (use negative number to start before event_time) tend = number of seconds to add to event time for end time of data radiuskm = radius to search for data channels = 'strong motion' to get all strong motion channels (excluding low sample rate ones), 'broadband' to get all broadband instruments, 'short period' for all short period channels, otherwise a single line of comma separated channel codes, * wildcards are okay, e.g. channels = '*N*,*L*' location = comma separated list of location codes allowed, or '*' for all location codes source = FDSN source, 'IRIS', 'NCEDC', 'GEONET' etc., see list here http://docs.obspy.org/archive/0.10.2/packages/obspy.fdsn.html OUTPUTS st = obspy stream containing data from within requested area """ event_time = UTCDateTime(event_time) client = FDSN_Client(source) if channels.lower() == 'strong motion': channels = 'EN*,HN*,BN*,EL*,HL*,BL*' elif channels.lower() == 'broadband': channels = 'BH*,HH*' elif channels.lower() == 'short period': channels = 'EH*' else: channels = channels.replace(' ', '') # Get rid of spaces t1 = UTCDateTime(event_time) + tstart t2 = UTCDateTime(event_time) + tend inventory = client.get_stations(latitude=event_lat, longitude=event_lon, minradius=minradiuskm/111.32, maxradius=maxradiuskm/111.32, channel=channels, level='channel', startbefore=t1, endafter=t2) temp = inventory.get_contents() netnames = temp['networks'] stas = temp['stations'] stanames = [n.split('.')[1].split()[0] for n in stas] st = getdata(','.join(unique_list(netnames)), ','.join(unique_list(stanames)), location, channels, t1, t2, attach_response=True, clientname=source) if st is None: print('No data returned') return for trace in st: try: coord = inventory.get_coordinates(trace.id) trace.stats.coordinates = AttribDict({'latitude': coord['latitude'], 'longitude': coord['longitude'], 'elevation': coord['elevation']}) except: print('Could not attach coordinates for %s' % trace.id) return st
def get_inventory(): try: return read_inventory(invname) except: pass client = Client('GFZ') net, sta, loc, cha = seed_id.split('.') inv = client.get_stations(starttime=t1, endtime=t2, network=net, station=sta, location=loc, channel=cha, level='channel') # latitude=lat, longitude=lon, maxradius=10) inv.write(invname, 'STATIONXML') return inv
def get_inventory(): print('Read inventory file') try: return read_inventory(invname, 'STATIONXML') except: pass print('Create inventory file...') client = FSDNClient('ORFEUS') inv = client.get_stations(**inventory_kwargs) for net in inv: for sta in net[:]: if sta.code not in stations: net.stations.remove(sta) inv.write(invname, 'STATIONXML') return inv
def getSlowestStation(lat,lon,depth,calc): client = Client("IRIS") inventory = client.get_stations(latitude=lat, longitude=lon,maxradius=1.5) lats = [] lons = [] codes = [] for network in inventory.networks: for station in network.stations: lats.append(station.latitude) lons.append(station.longitude) codes.append(station.code) lats = np.array(lats) lons = np.array(lons) codes = np.array(codes) distances = [] times = [] for i in range(0,len(lats)): slat = lats[i] slon = lons[i] distance = locations2degrees(lat,lon,slat,slon) distances.append(distance) ptime,stime = calc.getTravelTimes(distance,depth) times.append(ptime) times = np.array(times) distances = np.array(distances) sortidx = np.argsort(distances) distances = distances[sortidx] times = times[sortidx] lats = lats[sortidx] lons = lons[sortidx] codes = codes[sortidx] distances = distances[0:4] times = times[0:4] + TELEMETRY_DELAY + PROCESSING_DELAY lats = lats[0:4] lons = lons[0:4] codes = codes[0:4] idx = times.argmax() sdict = {'lat':lats[idx],'lon':lons[idx],'time':times[idx],'code':codes[idx]} return sdict
def download_data(params, station_list, with_waveforms, recording_time, padding_time): """ Still a work in progress (perhaps never finished). Sorts a text file obtained from IRIS (see manual), and parses out the STS and KS instruments (apparently the best ones). Then passes """ # Domain boundaries min_lat = -65 max_lat = 45 min_lon = -47.5 max_lon = 75 # Set up paths and such. lasif_data_path = os.path.join(params['lasif_path'], 'DOWNLOADED_DATA') iteration_xml_path = params['iteration_xml_path'] event_xml_directory = os.path.join(params['lasif_path'], 'EVENTS') event_list = params['event_list'] lasif_stations_path = os.path.join(params['lasif_path'], 'STATIONS', 'StationXML') # Set up station tuple and allowable instruments. station = namedtuple('station', ['network', 'station', 'location', 'sensor', 's_time', 'e_time']) # Read the data and parse out the important components. stations_list = pd.read_csv(station_list, delimiter='|') stations_list.fillna('00', inplace=True) # Filter based on domain boundaries stations_list = stations_list[stations_list.Latitude > min_lat] stations_list = stations_list[stations_list.Latitude < max_lat] stations_list = stations_list[stations_list.Longitude > min_lon] stations_list = stations_list[stations_list.Longitude < max_lon] stations_list = stations_list[stations_list.Location == '00'] stations_list['StartTime'] = \ stations_list['StartTime'].astype(obspy.UTCDateTime) stations_list['EndTime'] = \ stations_list['EndTime'].astype(obspy.UTCDateTime) # Number of events. num_events = len(os.listdir(event_xml_directory)) event_names = sorted([x[:-4] for x in os.listdir(event_xml_directory)]) # Event arrays. networks = stations_list.Network stations = stations_list.Station start_time = stations_list.StartTime end_time = stations_list.EndTime # Waveforms. pool = Pool(processes=NUM_THREADS) pool.map(_download_bulk_waveforms, zip( event_names, repeat(networks), repeat(stations), repeat(start_time), repeat(end_time), repeat(lasif_data_path), repeat(event_xml_directory), repeat(recording_time), repeat(padding_time))) if with_waveforms: return # Get stations. c = Client("IRIS") for x in stations_filt: station_filename = os.path.join( lasif_stations_path, 'station.%s_%s.xml' % (x.network, x.station)) if os.path.exists(station_filename): continue utils.print_ylw( "Downloading StationXML for: %s.%s" % (x.network, x.station)) try: c.get_stations( network=x.network, station=x.station, location="*", channel="*", level="response", filename=station_filename) except: utils.print_red("No data for %s" % (station_filename))
if __name__ == '__main__': client = Client("IRIS") network = "XF" starttime = UTCDateTime("2003-06-01") endtime = UTCDateTime("2003-11-01") # endtime = UTCDateTime("1999-05-19") # endtime = UTCDateTime("1998-12-04") # endtime = UTCDateTime("1998-12-05") events = client.get_events(starttime=starttime, endtime=endtime, minmagnitude=5.5, catalog="ISC") # events.plot() stations = client.get_stations(network=network, station="H*", starttime=starttime, endtime=endtime, level="response") # stations.plot() # stations.plot(projection=u'local', resolution=u'f') default_dir = os.getcwd() + '/data_60_120/' for event in events: origin = event.origins[0] print origin event_latitude = origin.latitude event_longitude = origin.longitude event_depth = origin.depth # km event_time = origin.time event_time_str = str(event_time.year) + '.' + str(event_time.julday).zfill(3) + '.' +\ str(event_time.hour).zfill(2) + str(event_time.minute).zfill(2) + \ str(event_time.second).zfill(2) event_dir = default_dir + '/' + event_time_str
from obspy.fdsn import Client client = Client("IRIS") from obspy.fdsn.header import URL_MAPPINGS for key in sorted(URL_MAPPINGS.keys()): print("{0:<7} {1}".format(key, URL_MAPPINGS[key])) # Inventory (from link above) -------------------------------------------------- starttime = UTCDateTime("2002-01-01") endtime = UTCDateTime("2002-01-02") inventory = client.get_stations(network="IU", station="A*", starttime=starttime, endtime=endtime) print(inventory) # NCEDC channels request ------------------------------------------------------- client = Client("NCEDC") starttime = UTCDateTime("2015-07-07") endtime = UTCDateTime("2015-07-08") # NOTE: Good documentation on STATIONXML and the ObsPy "Inventory" object is here: # NOTE:
output_dir = "StationXML" c = Client() inv = read_inventory("./all_stations.xml") def print_error(msg): print colorama.Fore.RED + msg + colorama.Fore.RESET def print_ok(msg): print colorama.Fore.GREEN + msg + colorama.Fore.RESET for network in inv.networks: for station in network.stations: output_filename = os.path.join(output_dir, "%s.%s.xml" % (network.code, station.code)) if os.path.exists(output_filename): continue try: out = c.get_stations(network=network.code, station=station.code, level="response") except: print_error("Failed to download %s.%s." % (network.code, station.code)) continue with open(output_filename, "w") as fh: fh.write(out) print_ok("Downloaded %s.%s." % (network.code, station.code))
station = "*" location = '*' channel = '*H*' file_name = 'list_stas_created.txt' # ########################## END INPUT client = Client(req_client) if starttime: starttime = UTCDateTime(starttime) if endtime: endtime = UTCDateTime(endtime) inv = client.get_stations(network=network, station=station, location=location, channel=channel, starttime=starttime, endtime=endtime, level='channel') content = inv.get_contents() chans = list(set(content['channels'])) chans.sort() net_inv = inv.networks[0] fio = open(file_name, 'w') for _i in range(len(chans)): net, sta, loc, cha = chans[_i].split('.') coord_chan = get_coordinates(net_inv, chans[_i], None) fio.writelines( '%s %s %s %s %s %s %s %s\n' %
def test_download_urls_for_custom_mapping(self, download_url_mock): """ Tests the downloading of data with custom mappings. """ base_url = "http://example.com" # More extensive mock setup simulation service discovery. def custom_side_effects(*args, **kwargs): if "version" in args[0]: return 200, "1.0.200" elif "event" in args[0]: with open(os.path.join( self.datapath, "2014-01-07_iris_event.wadl"), "rb") as fh: return 200, fh.read() elif "station" in args[0]: with open(os.path.join( self.datapath, "2014-01-07_iris_station.wadl"), "rb") as fh: return 200, fh.read() elif "dataselect" in args[0]: with open(os.path.join( self.datapath, "2014-01-07_iris_dataselect.wadl"), "rb") as fh: return 200, fh.read() return 404, None download_url_mock.side_effect = custom_side_effects # Some custom urls base_url_event = "http://other_url.com/beta/event_service/11" base_url_station = "http://some_url.com/beta2/station/7" base_url_ds = "http://new.com/beta3/dataselect/8" # An exception will be raised if not actual WADLs are returned. # Catch warnings to avoid them being raised for the tests. with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") c = Client(base_url=base_url, service_mappings={ "event": base_url_event, "station": base_url_station, "dataselect": base_url_ds, }) for warning in w: self.assertTrue("Could not parse" in str(warning) or "cannot deal with" in str(warning)) # Test the dataselect downloading. download_url_mock.reset_mock() download_url_mock.side_effect = None download_url_mock.return_value = 404, None try: c.get_waveforms("A", "B", "C", "D", UTCDateTime() - 100, UTCDateTime()) except: pass self.assertTrue( base_url_ds in download_url_mock.call_args_list[0][0][0]) # Test the station downloading. download_url_mock.reset_mock() download_url_mock.side_effect = None download_url_mock.return_value = 404, None try: c.get_stations() except: pass self.assertTrue( base_url_station in download_url_mock.call_args_list[0][0][0]) # Test the event downloading. download_url_mock.reset_mock() download_url_mock.side_effect = None download_url_mock.return_value = 404, None try: c.get_events() except: pass self.assertTrue( base_url_event in download_url_mock.call_args_list[0][0][0])
# Normalize traces and find absolute max value (for plotting) # stalist_obtained = [] absmax = -999999 st.normalize() for tr in st: stalist_obtained.append(tr.stats.station) tr_abs_max = np.max(np.abs(tr.data)) if tr_abs_max > absmax: absmax = tr_abs_max # Get station inventory so we have lats/lons stalist_obtained_string = str.join(",", stalist_obtained) inventory = client.get_stations(network=netlist, station=stalist_obtained_string, location=loclist, channel=chanlist, starttime=starttime, endtime=endtime) # Normalize the color map (can adjust to play with saturation) cmap = cm.bwr #norm = mpl.colors.Normalize(vmin=-absmax, vmax=absmax) norm = mpl.colors.Normalize(vmin=-0.5, vmax=0.5) print(-absmax, absmax) # # Get list of station lat/lons # lats = [] lons = [] codes = []
# # Normalize traces and find absolute max value (for plotting) # stalist_obtained = [] absmax = -999999 st.normalize() for tr in st: stalist_obtained.append(tr.stats.station) tr_abs_max = np.max(np.abs(tr.data)) if tr_abs_max > absmax: absmax = tr_abs_max # Get station inventory so we have lats/lons stalist_obtained_string = str.join(",",stalist_obtained) inventory = client.get_stations(network=netlist, station=stalist_obtained_string, location=loclist, channel=chanlist, starttime=starttime, endtime=endtime) # Normalize the color map (can adjust to play with saturation) cmap = cm.bwr #norm = mpl.colors.Normalize(vmin=-absmax, vmax=absmax) norm = mpl.colors.Normalize(vmin=-0.5, vmax=0.5) print(-absmax, absmax) # # Get list of station lat/lons # lats = [] lons = [] codes = [] nets = [] for net in inventory:
inv = read_inventory("./all_stations.xml") def print_error(msg): print colorama.Fore.RED + msg + colorama.Fore.RESET def print_ok(msg): print colorama.Fore.GREEN + msg + colorama.Fore.RESET for network in inv.networks: for station in network.stations: output_filename = os.path.join( output_dir, "%s.%s.xml" % (network.code, station.code)) if os.path.exists(output_filename): continue try: out = c.get_stations(network=network.code, station=station.code, level="response") except: print_error("Failed to download %s.%s." % (network.code, station.code)) continue with open(output_filename, "w") as fh: fh.write(out) print_ok("Downloaded %s.%s." % (network.code, station.code))
output_dir = './output/' # read catalogue.txt (see plan-phase1.txt for format details) cat = np.loadtxt(cat_file) cat = [cat[8]] for line in cat: eve_lat = line[1] eve_lon = line[2] eve_depth = line[3] eve_ot = UTCDateTime(int(line[4]), int(line[5]), int(line[6]), int(line[7]), int(line[8]), float(line[9])) print("Downloading station inventory for eve_" + str(int(line[0]))) inv = client.get_stations(network="AU", station="*", channel="BH*", starttime=eve_ot, endtime=eve_ot + 3600, latitude=eve_lat, longitude=eve_lon, maxradius=radius) print("completed for eve_" + str(int(line[0]))) # bulk request from IRIS bulk_req = [] S = [] for sta in inv[0].stations: dist = locations2degrees(eve_lat, eve_lon, sta.latitude, sta.longitude) arrival = model.get_travel_times(source_depth_in_km=eve_depth, distance_in_degree=dist, phase_list=['P']) p_arrival = eve_ot + arrival[0].time t1 = p_arrival - wl_10deg t2 = p_arrival + wl_10deg
def test_download_urls_for_custom_mapping(self, download_url_mock): """ Tests the downloading of data with custom mappings. """ base_url = "http://example.com" # More extensive mock setup simulation service discovery. def custom_side_effects(*args, **kwargs): if "version" in args[0]: return 200, "1.0.200" elif "event" in args[0]: with open( os.path.join(self.datapath, "2014-01-07_iris_event.wadl")) as fh: return 200, fh.read() elif "station" in args[0]: with open( os.path.join(self.datapath, "2014-01-07_iris_station.wadl")) as fh: return 200, fh.read() elif "dataselect" in args[0]: with open( os.path.join(self.datapath, "2014-01-07_iris_dataselect.wadl")) as fh: return 200, fh.read() return 404, None download_url_mock.side_effect = custom_side_effects # Some custom urls base_url_event = "http://other_url.com/beta/event_service/11" base_url_station = "http://some_url.com/beta2/station/7" base_url_ds = "http://new.com/beta3/dataselect/8" # An exception will be raised if not actual WADLs are returned. c = Client(base_url=base_url, service_mappings={ "event": base_url_event, "station": base_url_station, "dataselect": base_url_ds, }) # Test the dataselect downloading. download_url_mock.reset_mock() download_url_mock.side_effect = None download_url_mock.return_value = 404, None try: c.get_waveforms("A", "B", "C", "D", UTCDateTime() - 100, UTCDateTime()) except: pass self.assertTrue( base_url_ds in download_url_mock.call_args_list[0][0][0]) # Test the station downloading. download_url_mock.reset_mock() download_url_mock.side_effect = None download_url_mock.return_value = 404, None try: c.get_stations() except: pass self.assertTrue( base_url_station in download_url_mock.call_args_list[0][0][0]) # Test the event downloading. download_url_mock.reset_mock() download_url_mock.side_effect = None download_url_mock.return_value = 404, None try: c.get_events() except: pass self.assertTrue( base_url_event in download_url_mock.call_args_list[0][0][0])
from obspy import UTCDateTime from obspy.fdsn import Client client = Client("IRIS") from obspy.fdsn.header import URL_MAPPINGS for key in sorted(URL_MAPPINGS.keys()): print("{0:<7} {1}".format(key, URL_MAPPINGS[key])) # Inventory (from link above) -------------------------------------------------- starttime = UTCDateTime("2002-01-01") endtime = UTCDateTime("2002-01-02") inventory = client.get_stations(network="IU", station="A*", starttime=starttime, endtime=endtime) print(inventory) # NCEDC channels request ------------------------------------------------------- client = Client("NCEDC") starttime = UTCDateTime("2015-07-07") endtime = UTCDateTime("2015-07-08") # NOTE: Good documentation on STATIONXML and the ObsPy "Inventory" object is here: # NOTE: # NOTE: http://docs.obspy.org/packages/obspy.station.html
outfolder = '/storage/ANT/NZ Station Responses' # create list of all possible FDSN clients that work under obspy. client_list = (u'BGR', u'ETH', u'GEONET', u'GFZ', u'INGV', u'IPGP', u'IRIS', u'KOERI', u'LMU', u'NCEDC', u'NEIP', u'NERIES', u'ODC', u'ORFEUS', u'RESIF', u'SCEDC', u'USGS', u'USP') client = Client("GEONET") starttime = UTCDateTime("2014-01-01") endtime = UTCDateTime("2015-01-01") inventory = client.get_stations(network="*", station="*", loc='*', channel="*Z", starttime=starttime, endtime=endtime, level="response") for net in inventory: print net for sta in net: print sta quit() # save all response plots #inventory[0].plot_response(min_freq=1E-4, # channel="BHZ", # location="10",
class MetaFetch: def __init__(self, network=None, station=None, starttime=None, endtime=None, level='channel', channel='BH?', minlongitude=None, maxlongitude=None, minlatitude=None, maxlatitude=None, savedirectory=None): #initiase all the necessary variables self.network = network self.station = station self.starttime = starttime self.endtime = endtime self.minlongitude = minlongitude self.minlatitude = minlatitude self.maxlongitude = maxlongitude self.maxlatitude = maxlatitude self.level = level self.channel = channel self.savedirectory = savedirectory error = self.raiseinputerrors() if error is not None: print 'ERROR on input' def fetchinventory(self): '''Get an obspy inventory containing all the station information''' self.client = Client( "IRIS") #eventuually change so that we can get data from elsewhere self.Iclient = iclient() if self.station != 'None': self.inventory = self.client.get_stations( network=self.network, station=self.station, level='channel', channel=self.channel, starttime=self.starttime, endtime=self.endtime, minlongitude=self.minlongitude, minlatitude=self.minlatitude, maxlongitude=self.maxlongitude, maxlatitude=self.maxlatitude) else: self.inventory = self.client.get_stations( network=self.network, station=None, level='channel', channel=self.channel, starttime=self.starttime, endtime=self.endtime, minlongitude=self.minlongitude, minlatitude=self.minlatitude, maxlongitude=self.maxlongitude, maxlatitude=self.maxlatitude) def printinventory(self): '''Print out useful metadata about the requested networks and/or channel''' print '########################################' print 'The following information was requested:' for network in self.inventory.networks: for station in network.stations: print '########################################' print station def raiseinputerrors(self): '''If incorrect input is entered, raise errors''' if self.network == None: error = 'A valid network code is needed' if self.starttime == None: error = 'A valid starttime in UTCDateTime format is needed' if self.endtime == None: error = 'A valid endtime in UTCDateTime format is needed' else: error = None def extract_stations(self, writefile=True): '''Make a dictionary containing station,latitude and longitude for that network. Also output a file containing station lon lat depth In its rows ''' networks = {} for network in self.inventory.networks: #should only be one network information = str(network).split('\n')[0] networks[information] = {} for station in network.stations: stationname = str(station).split('\n')[0].split(' ')[1].strip() stationlat = station.latitude stationlon = station.longitude stationelev = station.elevation channels = [] azimuths = [] dips = [] for channel in station.channels: azimuths.append(channel.azimuth) dips.append(channel.dip) code = str(channel.code) #sometimes the code will appear more than once, in which case the data will be downloaded multiple times. Stop this. if code not in channels: channels.append(code) networks[information][stationname] = [ stationlat, stationlon, stationelev, channels, azimuths, dips ] #write the information to a file if writefile: outfilename = information.replace(" ", '_') + '.dat' outfilename = outfilename.replace(')', '') outfilename = outfilename.replace('(', '_') outfilename = outfilename.replace('-', '_') outfilename = outfilename.replace('/', '_') outfile = open(outfilename, 'w') for station in networks[information]: outfile.write('%s %s %s %s\n' % (station, networks[information][station][1], networks[information][station][0], networks[information][station][2])) outfile.close() #should probably move this file to the save location - contains useful information if one wants to make a plot of the station distribution if (self.savedirectory) and ( os.getcwd() != self.savedirectory ): #moves the metadata file to the location speficied by the user os.system('mv %s %s' % (outfilename, self.savedirectory)) return networks
def getepidata(event_lat, event_lon, event_time, tstart=-5., tend=200., minradiuskm=0., maxradiuskm=20., channels='*', location='*', source='IRIS'): """ Automatically pull existing data within a certain distance of the epicenter (or any lat/lon coordinates) and attach station coordinates to data USAGE st = getepidata(event_lat, event_lon, event_time, tstart=-5., tend=200., minradiuskm=0., maxradiuskm=20., channels='*', location='*', source='IRIS') INPUTS event_lat = latitude of event in decimal degrees event_lon = longitude of event in decimal degrees event_time = Event time in UTC in any format obspy's UTCDateTime can parse - e.g. '2016-02-05T19:57:26' tstart = number of seconds to add to event time for start time of data (use negative number to start before event_time) tend = number of seconds to add to event time for end time of data radiuskm = radius to search for data channels = 'strong motion' to get all strong motion channels (excluding low sample rate ones), 'broadband' to get all broadband instruments, 'short period' for all short period channels, otherwise a single line of comma separated channel codes, * wildcards are okay, e.g. channels = '*N*,*L*' location = comma separated list of location codes allowed, or '*' for all location codes source = FDSN source, 'IRIS', 'NCEDC', 'GEONET' etc., see list here http://docs.obspy.org/archive/0.10.2/packages/obspy.fdsn.html OUTPUTS st = obspy stream containing data from within requested area """ event_time = UTCDateTime(event_time) client = FDSN_Client(source) if channels.lower() == 'strong motion': channels = 'EN*,HN*,BN*,EL*,HL*,BL*' elif channels.lower() == 'broadband': channels = 'BH*,HH*' elif channels.lower() == 'short period': channels = 'EH*' else: channels = channels.replace(' ', '') # Get rid of spaces t1 = UTCDateTime(event_time) + tstart t2 = UTCDateTime(event_time) + tend inventory = client.get_stations(latitude=event_lat, longitude=event_lon, minradius=minradiuskm / 111.32, maxradius=maxradiuskm / 111.32, channel=channels, level='channel', startbefore=t1, endafter=t2) temp = inventory.get_contents() netnames = temp['networks'] stas = temp['stations'] stanames = [n.split('.')[1].split()[0] for n in stas] st = getdata(','.join(unique_list(netnames)), ','.join(unique_list(stanames)), location, channels, t1, t2, attach_response=True, clientname=source) if st is None: print('No data returned') return for trace in st: try: coord = inventory.get_coordinates(trace.id) trace.stats.coordinates = AttribDict({ 'latitude': coord['latitude'], 'longitude': coord['longitude'], 'elevation': coord['elevation'] }) except: print('Could not attach coordinates for %s' % trace.id) return st
def FDSN_available(input_dics, event, target_path, event_number): """ Check the availablity of FDSN stations :param input_dics: :param event: :param target_path: :param event_number: :return: """ print "Check the availablity of FDSN stations: %s" \ % input_dics['fdsn_base_url'] client_fdsn = Client_fdsn(base_url=input_dics['fdsn_base_url'], user=input_dics['fdsn_user'], password=input_dics['fdsn_pass']) Sta_fdsn = [] try: if input_dics['fdsn_base_url'].lower() in ['resif']: msg = 'WARNING\n' \ 'You have selected RESIF as fdsn_base_url!\n\n' \ 'I have detected some problems in sending request to ' \ 'this data provider:\n' \ 'It seems that the start and end times in channel level ' \ 'are not set correctly.\n\n' \ 'It is better to use --list_stas options other ' \ 'than sending the availability\nrequest directly.' print '\n\n' + 80*'=' print msg print 80*'=' + '\n\n' # start_time = None # end_time = None start_time = event['t1'] end_time = event['t2'] else: start_time = event['t1'] end_time = event['t2'] available = client_fdsn.get_stations( network=input_dics['net'], station=input_dics['sta'], location=input_dics['loc'], channel=input_dics['cha'], starttime=start_time, endtime=end_time, latitude=input_dics['lat_cba'], longitude=input_dics['lon_cba'], minradius=input_dics['mr_cba'], maxradius=input_dics['Mr_cba'], minlatitude=input_dics['mlat_rbb'], maxlatitude=input_dics['Mlat_rbb'], minlongitude=input_dics['mlon_rbb'], maxlongitude=input_dics['Mlon_rbb'], level='channel') for network in available.networks: for station in network: for channel in station: Sta_fdsn.append([network.code, station.code, channel.location_code, channel.code, channel.latitude, channel.longitude, channel.elevation, channel.depth]) if input_dics['fdsn_bulk'] == 'Y': if input_dics['fdsn_update'] != 'N': if os.path.exists(os.path.join(target_path, 'info', 'bulkdata.txt')): os.remove(os.path.join(target_path, 'info', 'bulkdata.txt')) if os.path.exists(os.path.join(target_path, 'info', 'bulkdata.txt')): print 'bulkdata.txt exists in the directory!' else: print 'Start creating a list for bulk request' bulk_list = [] for bulk_sta in Sta_fdsn: if input_dics['cut_time_phase']: t_start, t_end = calculate_time_phase(event, bulk_sta) else: t_start = event['t1'] t_end = event['t2'] bulk_list.append((bulk_sta[0], bulk_sta[1], bulk_sta[2], bulk_sta[3], t_start, t_end)) bulk_list_fio = open(os.path.join(target_path, 'info', 'bulkdata_list'), 'a+') pickle.dump(bulk_list, bulk_list_fio) bulk_list_fio.close() except Exception as e: exc_file = open(os.path.join(target_path, 'info', 'exception'), 'a+') ee = 'fdsn -- Event: %s --- %s\n' % (str(event_number+1), e) exc_file.writelines(ee) exc_file.close() print 'ERROR: %s' % ee if len(Sta_fdsn) == 0: Sta_fdsn.append([]) Sta_fdsn.sort() return Sta_fdsn
endtime = None network = "YV" station = "*" location = '*' channel = '*H*' file_name = 'list_stas_created.txt' # ########################## END INPUT client = Client(req_client) if starttime: starttime = UTCDateTime(starttime) if endtime: endtime = UTCDateTime(endtime) inv = client.get_stations(network=network, station=station, location=location, channel=channel, starttime=starttime, endtime=endtime, level='channel') content = inv.get_contents() chans = list(set(content['channels'])) chans.sort() net_inv = inv.networks[0] fio = open(file_name, 'w') for _i in range(len(chans)): net, sta, loc, cha = chans[_i].split('.') try: coord_chan = get_coordinates(net_inv, chans[_i], None) fio.writelines('%s %s %s %s %s %s %s %s\n' % (sta, net, loc, cha, coord_chan['latitude'], coord_chan['longitude'], coord_chan['elevation'],
from obspy.fdsn import Client from obspy import UTCDateTime import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.basemap import Basemap import pickle client = Client("GEONET") starttime = UTCDateTime("2014-01-01") endtime = UTCDateTime("2015-01-01") inventory = client.get_stations(network="*", station="*", loc='*', channel="*Z", starttime=starttime, endtime=endtime, level="response") # save all response plots #inventory[0].plot_response(min_freq=1E-4, # channel="BHZ", # location="10", # outfile=None) #help(inventory[0][0]) # goal: to populate a list of stations with appropriate seismic noise frequency # response ranges.