def buildStationList(net, sta, cha, t, resp):
    '''This will build a list of stations for a given network.
       ex: staList = buildStationList("IU","*","BH*",eventTime,True) for resp info
       ex: staList = buildStationList("IU","*","BH*",eventTime,False) w/o resp info

    '''

    from obspy.clients.fdsn import Client
    from obspy import UTCDateTime

    client = Client("IRIS")

    # set some event info
    # this is a deep earthquake in bolivia
    #t = UTCDateTime("2017-02-21T14:09:04")

    if resp:
        inventory = client.get_stations(network="IU",
                                        station="*",
                                        starttime=t,
                                        channel="BH*",
                                        level="response")
    else:
        inventory = client.get_stations(network="IU", station="*", starttime=t)

# to plot up all the stations...
#inventory.plot()

# to get the station coordinates
#station_coordinates = []
#for network in inventory:
#    for station in network:
#        station_coordinates.append((network.code, station.code, station.latitude, station.longitude, station.elevation))

    return inventory
Exemplo n.º 2
0
    def download(self, s):

        client = Client("IRIS")
        starttime = obspy.UTCDateTime("2010-01-01")
        endtime = obspy.UTCDateTime("2015-01-02")

        # for s in stations:
        # Try and download, don't worry if no data is available.
        try:
            stream = client.get_stations(network=s.net,
                                         station=s.sta,
                                         starttime=starttime,
                                         endtime=endtime,
                                         level="channel")
            fname = os.path.join("STATION_XML_META",
                                 "station.{}_{}.meta.xml".format(s.net, s.sta))
            stream.write(fname, format='STATIONXML')
        except Exception as e:
            print e
            pass

        try:
            stream = client.get_stations(network=s.net,
                                         station=s.sta,
                                         starttime=starttime,
                                         endtime=endtime,
                                         level="response")
            fname = os.path.join(
                "STATION_XML_META",
                "station.{}_{}.response.xml".format(s.net, s.sta))
            stream.write(fname, format='STATIONXML')
            print 'Finished downloading {}.{}'.format(s.net, s.sta)
        except Exception as e:
            print e
            pass
Exemplo n.º 3
0
    def test_level_argument(self):
        client = FDSNClient(self.live_server_url)

        inv = client.get_stations(level="channel")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)

        inv = client.get_stations(level="station")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 0)

        inv = client.get_stations(level="network")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 0)
        self.assertEqual(len(c["channels"]), 0)

        inv = client.get_stations(level="response")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)

        for channel in inv[0][0]:
            self.assertIsNotNone(channel.response)
Exemplo n.º 4
0
    def test_level_argument(self):
        client = FDSNClient(self.live_server_url)

        inv = client.get_stations(level="channel")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)

        inv = client.get_stations(level="station")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 0)

        inv = client.get_stations(level="network")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 0)
        self.assertEqual(len(c["channels"]), 0)

        inv = client.get_stations(level="response")
        c = inv.get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)

        for channel in inv[0][0]:
            self.assertIsNotNone(channel.response)
Exemplo n.º 5
0
    def download(self, s):

        client = Client("IRIS")
        starttime = obspy.UTCDateTime("2010-01-01")
        endtime = obspy.UTCDateTime("2015-01-02")

        # for s in stations:
            # Try and download, don't worry if no data is available.
        try:
            stream = client.get_stations(network=s.net, station=s.sta, starttime=starttime, endtime=endtime,
                                         level="channel")
            fname = os.path.join("STATION_XML_META", "station.{}_{}.meta.xml".format(s.net, s.sta))
            stream.write(fname, format='STATIONXML')
        except Exception as e:
            print e
            pass

        try:
            stream = client.get_stations(network=s.net, station=s.sta, starttime=starttime, endtime=endtime,
                                         level="response")
            fname = os.path.join("STATION_XML_META", "station.{}_{}.response.xml".format(s.net, s.sta))
            stream.write(fname, format='STATIONXML')
            print 'Finished downloading {}.{}'.format(s.net, s.sta)
        except Exception as e:
            print e
            pass
Exemplo n.º 6
0
def check_stations_network(net, Year, Month, Day, Hour, Minute, Second):
    # find stations with data
    start_1 = (str(Year) + '-' + str(Month) + '-' + str(Day) + '-' +
               str(Hour) + '-' + str(Minute) + '-' + str(Second))
    t0 = UTCDateTime(start_1)

    client = Client('IRIS')

    inv = client.get_stations(network=net, level="station")

    network = inv[0]
    netstat = []
    for i in range(len(network)):
        netstat.extend([net + "." + network[i].code])

    print(color.GREEN + 'data is available for these stations:' +
          str(netstat) + color.END)

    # now get metadata
    endtime = t0 + 30
    inv = client.get_stations(network=net,
                              station="*",
                              location="*",
                              channel="HHZ",
                              starttime=t0,
                              endtime=endtime,
                              level="response")

    return netstat, t0, inv
Exemplo n.º 7
0
def geonet_north_island(level="station"):
    """
    Return geonet broadband stations in an obspy network

    :type level: str
    :param level: level to propogate network creation
    :rtype: obspy.core.inventory.network.Network
    """
    c = Client("GEONET")

    # Extended North Island coverage, Kaikoura to East Cape
    lat_min = -42.8
    lat_max = -37.1
    lon_min = 173
    lon_max = 179.4

    starttime = UTCDateTime("2000-01-01")

    # GeoNet Broadband instruments with standard station code Z
    inv_geonet = c.get_stations(network="NZ",
                                station="*Z",
                                channel="HH?",
                                minlatitude=lat_min,
                                maxlatitude=lat_max,
                                minlongitude=lon_min,
                                maxlongitude=lon_max,
                                level=level,
                                starttime=starttime,
                                endtime=UTCDateTime())

    inv_hold = c.get_stations(network="NZ",
                              station="*Z",
                              channel="BH?",
                              minlatitude=lat_min,
                              maxlatitude=lat_max,
                              minlongitude=lon_min,
                              maxlongitude=lon_max,
                              level=level,
                              starttime=starttime,
                              endtime=UTCDateTime())
    inv_geonet = merge_inventories(inv_geonet, inv_hold)
    # Stations with unique names (Wellington and Baring Head)
    for station in ["WEL", "BHW"]:
        inv_hold = c.get_stations(network="NZ",
                                  station=station,
                                  channel="HH?",
                                  minlatitude=lat_min,
                                  maxlatitude=lat_max,
                                  minlongitude=lon_min,
                                  maxlongitude=lon_max,
                                  level=level,
                                  starttime=starttime,
                                  endtime=UTCDateTime())
        inv_geonet = merge_inventories(inv_geonet, inv_hold)

    return inv_geonet[0]
Exemplo n.º 8
0
    def test_rectangular_geo_queries(self):
        client = FDSNClient(self.live_server_url)
        # lat = 48.995167
        # lon = 11.519922

        # This works.
        self.assertEqual(
            len(client.get_stations(
                minlatitude=48, maxlatitude=49,
                minlongitude=11, maxlongitude=12).get_contents()["stations"]),
            1)

        # Make sure one border does not include the point at a time.
        with self.assertRaises(FDSNException):
            client.get_stations(minlatitude=48.996, maxlatitude=49,
                                minlongitude=11, maxlongitude=12)

        with self.assertRaises(FDSNException):
            client.get_stations(minlatitude=48, maxlatitude=48.5,
                                minlongitude=11, maxlongitude=12)

        with self.assertRaises(FDSNException):
            client.get_stations(minlatitude=48, maxlatitude=49,
                                minlongitude=11.6, maxlongitude=12)

        with self.assertRaises(FDSNException):
            client.get_stations(minlatitude=48, maxlatitude=49,
                                minlongitude=11, maxlongitude=11.4)
Exemplo n.º 9
0
    def get_stnxml(self, network='*', station="*", channel="BHZ,BHE,BHN"):
        print("\n")
        self.logger.info('Retrieving station information')
        ninvt = 0
        while ninvt < len(self.client):
            try:
                client = Client(self.client[ninvt])
            except Exception as e:
                self.logger.error(
                    f"No FDSN services could be discovered for {self.client[ninvt]}! Try again after some time."
                )
                sys.exit()
            self.logger.info(f'from {self.client[ninvt]}')
            try:
                invt = client.get_stations(network=network,
                                           station=station,
                                           channel=self.channel,
                                           level='channel',
                                           minlongitude=self.minlongitude,
                                           maxlongitude=self.maxlongitude,
                                           minlatitude=self.minlatitude,
                                           maxlatitude=self.maxlatitude)
                inventory = invt
                break
            except Exception as exception:
                self.logger.error(
                    f"No stations found for the given parameters for {self.client[ninvt]}",
                    exc_info=True)
            ninvt += 1
            # sys.exit()
        if len(self.client) > 1:
            for cl in self.client[ninvt + 1:]:
                self.logger.info(f'from {cl}')
                try:
                    client = Client(cl)
                    invt = client.get_stations(network=network,
                                               station=station,
                                               channel=self.channel,
                                               level='channel',
                                               minlongitude=self.minlongitude,
                                               maxlongitude=self.maxlongitude,
                                               minlatitude=self.minlatitude,
                                               maxlatitude=self.maxlatitude)
                    inventory += invt
                except Exception as exception:
                    self.logger.warning(f"FDSNNoDataException for {cl}")
        # self.logger.info(inventory)

        inventory.write(self.inventoryfile, 'STATIONXML')
        self.inv = inventory
        inventory.write(self.inventorytxtfile, 'STATIONTXT', level='station')
        organize_inventory(self.inventorytxtfile)
Exemplo n.º 10
0
    def get_station_coordinates(self, client_base_url='IRIS'):
        """
        get_station_coordinates(self, client_base_url='IRIS')

        Use an obspy FDSN client to retrieve station locations for all traces
        in the stream. The results are written into a sac header-like
        dictionary.

        Parameters
        ----------
        self : stfinv.Stream()
            Stream with signals.

        client_base_url : str
            String with valid URL for FDSN client.


        """
        client = Client(client_base_url)
        # bulk = []
        network = ['II']
        inv = client.get_stations(network='II')
        for tr in self:
            stats = tr.stats

            # Get all networks of which stations are contained in self.
            # This is more stable than requesting a bulk of the stations
            # in self.
            if stats.network not in network:
                network.append(stats.network)
                inv += client.get_stations(network=stats.network)

            # Correct Instaseis Streams
            if stats.location in ['', 'SE']:
                stats.location = u''
                stats.channel = u'BH%s' % (stats.channel[2])

        for tr in self:
            stats = tr.stats
            if not hasattr(stats, 'sac'):
                stat = inv.select(network=stats.network,
                                  station=stats.station,
                                  location=stats.location,
                                  channel=stats.channel)
                if (len(stat) > 0):
                    stats.sac = dict(stla=stat[0][0].latitude,
                                     stlo=stat[0][0].longitude)
                else:
                    print('Could not find station %s.%s.%s.%s' %
                          (stats.network, stats.station, stats.location,
                           stats.channel))
                    raise IndexError
Exemplo n.º 11
0
def get_and_remove_response(station, channel, location, output, t1, duration):
    client = Client("http://service.geonet.org.nz")
    st = client.get_waveforms(network="NZ",
                              station=station,
                              location=location,
                              channel=channel,
                              starttime=t1,
                              endtime=t1 + duration)
    tr = Stream()
    st.merge()
    print(st)
    for n in range(len(st)):

        inv = client.get_stations(network=st[n].stats.network,
                                  station=st[n].stats.station,
                                  location=st[n].stats.location,
                                  channel=st[n].stats.channel,
                                  level="response",
                                  startbefore=t1,
                                  endafter=t1 + duration)
        # st[n].detrend('linear')
        # st[n].taper(max_percentage=0.05, type='cosine')
        st[n].filter('bandpass', freqmin=0.1, freqmax=0.2, zerophase=True)
        st[n].remove_response(output=output,
                              pre_filt=False,
                              plot=False,
                              water_level=60,
                              inventory=inv)
        tr += st[n]

    return tr
Exemplo n.º 12
0
def rm_response_(trace_path, comps, locs, outdir, client):
    tr = read(trace_path)[0]
    network = tr.stats.network
    station = tr.stats.station
    channel = tr.stats.channel
    location = tr.stats.location
    start = tr.stats.starttime
    end = tr.stats.endtime

    try:
        tr.remove_response()
        print 'response was attached'
    except Exception, message:
        print message
        inv = None
        fdsn_client = Client(client)
        for comp, loc in product(comps[channel], locs[location]):
            try:
                inv = fdsn_client.get_stations(network=network,
                                               station=station,
                                               location=loc,
                                               channel=comp,
                                               level='response',
                                               starttime=start,
                                               endtime=end)
                break
            except Exception, message:
                print message
                pass
Exemplo n.º 13
0
def geonet_south_island(level="station"):
    """
    Return geonet broadband stations in an obspy network

    :type level: str
    :param level: level to propogate network creation
    :rtype: obspy.core.inventory.network.Network
    """
    c = Client("GEONET")

    # Extended North Island coverage, Kaikoura to East Cape
    lat_min = -48.
    lat_max = -40.
    lon_min = 165.
    lon_max = 175.

    starttime = UTCDateTime("2000-01-01")

    # GeoNet Broadband instruments with standard station code Z
    inv_geonet = c.get_stations(network="NZ",
                                station="*Z",
                                channel="HH?",
                                minlatitude=lat_min,
                                maxlatitude=lat_max,
                                minlongitude=lon_min,
                                maxlongitude=lon_max,
                                level=level,
                                starttime=starttime,
                                endtime=UTCDateTime())

    return inv_geonet[0]
def main(clientid: str, eventid: str, stationid: str, location_code: str,
         duration: float):
    client = Client(clientid)
    try:
        event = client.get_events(eventid=eventid)[0]
    except:
        raise FileNotFoundError(f"Event id not recognised {eventid}")
    origin = event.preferred_origin() or event.origins[0]
    station = client.get_stations(station=stationid,
                                  location=location_code,
                                  endafter=origin.time,
                                  startbefore=origin.time)[0][0]
    # Predict arrival times using TauP
    degrees_dist = locations2degrees(station.latitude, station.longitude,
                                     origin.latitude, origin.longitude)
    p_arrival = origin.time + MODEL.get_travel_times(
        source_depth_in_km=origin.depth / 1000.0,
        distance_in_degree=degrees_dist,
        phase_list=["P"])[0].time
    # Get waveforms around that time
    st = client.get_waveforms(station=stationid,
                              location=location_code,
                              starttime=p_arrival - 30,
                              network="*",
                              channel="BH?",
                              endtime=p_arrival + duration)
    return define_plot(origin=origin, station=station, st=st)
Exemplo n.º 15
0
def get_and_remove_response(station,
                            channel,
                            location,
                            output,
                            t1,
                            duration=100):
    client = Client("http://service.iris.edu")
    st = client.get_waveforms(network="IU",
                              station=station,
                              location=location,
                              channel=channel,
                              starttime=t1,
                              endtime=t1 + duration)
    tr = Stream()
    for n in range(len(st)):
        st.merge()[n]
        inv = client.get_stations(network=st[n].stats.network,
                                  station=st[n].stats.station,
                                  location=st[n].stats.location,
                                  channel=st[n].stats.channel,
                                  level="response",
                                  startbefore=t1,
                                  endafter=t1 + duration)
        # pre_filt = (0.005, 0.006, 30.0, 35.0)
        st[n].remove_response(output=output,
                              pre_filt=False,
                              plot=False,
                              water_level=60,
                              inventory=inv)
        tr += st[n]

    return tr
Exemplo n.º 16
0
class Query():
    def __init__(self):
        self.client = Client("IRIS")

    def get_events(self, starttime=UTCDateTime(2000, 1, 1),
                   endtime=UTCDateTime.now(), **kwargs):

        chunk_length = 365 * 86400  # Query length in seconds
        events = Catalog()
        while starttime <= endtime:
            print(starttime)
            events += self.client.get_events(starttime=starttime,
                                             endtime=starttime + chunk_length,
                                             **kwargs)
            if starttime + chunk_length > endtime:
                chunk = endtime - starttime
                if chunk <= 1:
                    break
            starttime += chunk_length
            
        self.events = _cat2df(events)

    def get_stations(self, includerestricted=False, **kwargs):
        self.stations = self.client.get_stations(includerestricted=includerestricted, **kwargs)

    def get_conti(self, starttime, endtime, hours=24):
        self.conti_time = []
        nowtime = starttime
        while nowtime < endtime:
            nexttime = nowtime + timedelta(hours=hours)
            self.conti_time.append([nowtime, nexttime])
            nowtime = nexttime
Exemplo n.º 17
0
def sitemap(mseedid, data_provider='ETH', ax=None, self=None):

    if self is None:
        self = PSDs()
    if not hasattr(self, 'resps'):
        self.resps = {}
    c = Client(data_provider)
    if mseedid not in self.resps:
        msid = mseedid.split('.')
        self.resps[mseedid] = c.get_stations(network=msid[0],
                                             station=msid[1],
                                             location=msid[2],
                                             channel=msid[3],
                                             level="response")
    if ax is None:
        ax = plt.figure(figsize=(7, 9)).add_subplot(111)
    longitude = self.resps[mseedid][-1][-1][-1].longitude
    latitude = self.resps[mseedid][-1][-1][-1].latitude
    print(longitude, latitude)
    if False:
        map = Basemap(llcrnrlon=longitude - 0.001,
                      llcrnrlat=latitude - 0.001,
                      urcrnrlon=longitude + 0.001,
                      urcrnrlat=latitude - 0.001,
                      epsg=4326,
                      projection='merc',
                      ax=ax)
        map.arcgisimage(service='ESRI_Imagery_World_2D',
                        xpixels=1500,
                        verbose=True)
    return ax
Exemplo n.º 18
0
    def create_nodes_from_fdsn(self, base_url, network_code, station_codes,
                               channel_codes, location_codes, country_code="",
                               node_prefix=""):
        '''
        Creates WebObs Nodes architecture and configuration files from FDSN
        Web Service (fdsnws)

        :type base_url: str
        :param base_url: A string representing the FDSN provider URL.
        :type network_code: str
        :param network_code: A string representing the FDSN network code.
        :type station_codes: str
        :param station_codes: A string representing the FDSN station(s) 
            (wildcard accepted).
        :type channel_codes: str
        :param channel_codes: A string representing the FDSN channel(s)
            (wildcard accepted).
        :type location_codes: str
        :param location_codes: A string representing the FDSN location 
            code(s) (wildcard accepted).
        :type country_code: str, optional
        :param country_code: A string representing the country code. 
        :type node_prefix: str, optional
        :param node_prefix: A string representing the node prefix.
        '''
        # Connect to FDSN web service and retrieve inventory
        fdsn_client = Client(base_url=base_url)
        inventory = fdsn_client.get_stations(network=network_code,
                                             station=station_codes,
                                             channel=channel_codes,
                                             location=location_codes,
                                             level="response")
        for network in inventory.networks:
            print("Processing %s network" % (network.code))
            for station in network.stations:
                print("|--Processing %s station" % (station.code))
                # Create node name and path
                node_name = "%s%s%s" % (country_code, node_prefix,
                                          station.code)
                node_path = "%s/%s" % (self.output_dir, node_name)
                # Create file tree and files
                if not os.path.exists(node_path):
                    os.makedirs(node_path)
                    os.makedirs("%s/DOCUMENTS/THUMBNAILS" % (node_path))
                    os.makedirs("%s/FEATURES" % (node_path))
                    os.makedirs("%s/INTERVENTIONS" % (node_path))
                    os.makedirs("%s/PHOTOS/THUMBNAILS" % (node_path))
                    os.makedirs("%s/SCHEMAS/THUMBNAILS" % (node_path))
                    open("%s/acces.txt" % (node_path), 'a').close()
                    open("%s/info.txt" % (node_path), 'a').close()
                    open("%s/installation.txt" % (node_path), 'a').close()
                    open("%s/%s.kml" % (node_path, node_name), 'a').close()
                    open("%s/FEATURES/sensor.txt" % (node_path), 'a').close()
                    open("%s/INTERVENTIONS/%s_Projet.txt" %
                         (node_path, node_name), 'a').close()
                    self.create_clb_file(node_path=node_path,
                                         node_name=node_name,
                                         network_code=network.code,
                                         station=station)
Exemplo n.º 19
0
    def test_redirection(self):
        """
        Tests the redirection of GET and POST requests. We redirect
        everything if not authentication is used.

        IRIS runs three services to test it:
            http://ds.iris.edu/files/redirect/307/station/1
            http://ds.iris.edu/files/redirect/307/dataselect/1
            http://ds.iris.edu/files/redirect/307/event/1
        """
        c = Client("IRIS", service_mappings={
            "station":
                "http://ds.iris.edu/files/redirect/307/station/1",
            "dataselect":
                "http://ds.iris.edu/files/redirect/307/dataselect/1",
            "event":
                "http://ds.iris.edu/files/redirect/307/event/1"},
            user_agent=USER_AGENT)

        st = c.get_waveforms(
            network="IU", station="ANMO", location="00", channel="BHZ",
            starttime=UTCDateTime("2010-02-27T06:30:00.000"),
            endtime=UTCDateTime("2010-02-27T06:30:01.000"))
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(st)))

        inv = c.get_stations(
            starttime=UTCDateTime("2000-01-01"),
            endtime=UTCDateTime("2001-01-01"),
            network="IU", station="ANMO", level="network")
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(inv.networks)))

        cat = c.get_events(starttime=UTCDateTime("2001-01-07T01:00:00"),
                           endtime=UTCDateTime("2001-01-07T01:05:00"),
                           catalog="ISC")
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(cat)))

        # Also test the bulk requests which are done using POST requests.
        bulk = (("TA", "A25A", "", "BHZ",
                 UTCDateTime("2010-03-25T00:00:00"),
                 UTCDateTime("2010-03-25T00:00:01")),
                ("TA", "A25A", "", "BHE",
                 UTCDateTime("2010-03-25T00:00:00"),
                 UTCDateTime("2010-03-25T00:00:01")))
        st = c.get_waveforms_bulk(bulk, quality="B", longestonly=False)
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(st)))

        starttime = UTCDateTime(1990, 1, 1)
        endtime = UTCDateTime(1990, 1, 1) + 10
        bulk = [
            ["IU", "ANMO", "", "BHE", starttime, endtime],
            ["IU", "CCM", "", "BHZ", starttime, endtime],
        ]
        inv = c.get_stations_bulk(bulk, level="network")
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(inv.networks)))
Exemplo n.º 20
0
def inv4stream(stream, network, client_name):

	start 	= stream[0].stats.starttime
	end 	= stream[0].stats.endtime
	client 	= Client(client_name)
	inv 	= client.get_stations(network=network, starttime=start, endtime=end)

	return inv
def plot_stations(network):
    """
    Draws a map of stations in an IRIS network.
    """
    client = Client('IRIS')
    network = network
    inventory = client.get_stations(network=network)
    net = inventory[0]
    lons = []
    lats = []
    names = []

    # extract the values we want from the inventory
    for sta in net:
        lons.append(sta.longitude)
        lats.append(sta.latitude)
        names.append(sta.code)

    #pad out the region we're going to draw
    region = [np.min(lons), np.max(lons), np.min(lats), np.max(lats)]
    x_pad = (region[1] - region[0]) * 0.1
    y_pad = (region[3] - region[2]) * 0.1
    region = [
        region[0] - x_pad, region[1] + x_pad, region[2] - y_pad,
        region[3] + y_pad
    ]

    # Set up the stereographic projection
    lon_0 = np.mean(region[:2])
    lat_0 = np.mean(region[2:])
    if lat_0 > 0:
        ref_lat = 90
    else:
        ref_lat = -90

    # GMT strings
    projection = f'S{lon_0}/{ref_lat}/6i'
    map_scale = f'JTL+c{lon_0}+w100k+f'
    rose = 'JML+w1i+f3+l'

    # Create figure
    fig = pygmt.Figure()
    fig.grdimage(
        grid='@earth_relief_15s',
        region=region,
        projection=projection,
        frame=['a', f'+t"{network} Seismograph Network"'],
        shading=True,
    )
    fig.coast(shorelines=True)
    fig.basemap(map_scale=map_scale, rose=rose)
    fig.plot(lons, lats, style='i0.2i', color='red', pen=True)
    fig.text(x=lons, y=lats, text=names, justify='CT', offset='j0/0.5')

    return fig
Exemplo n.º 22
0
def geonet_waveforms(station_code, start, end):
    """
    Get waveforms from geonet for comparison against BEACON
    """
    print(f"GeoNet waveform {station_code}", end="... ")
    net, sta, loc, cha = station_code.split('.')

    path_to = "./data"
    fid = os.path.join(path_to,
                       f"{station_code}.D.{start.year}.{start.julday:0>3}")
    inv_fid = os.path.join(path_to, f"RESP.{net}.{sta}.{loc}.{cha}")

    # Get Data
    if os.path.exists(fid) and os.path.exists(inv_fid):
        print("internal")
        st = read(fid)
        inv = read_inventory(inv_fid)
    else:
        print("external", end="... ")

        c = Client("GEONET")
        try:
            st = c.get_waveforms(network=net,
                                 station=sta,
                                 location=loc,
                                 channel=cha,
                                 starttime=start,
                                 endtime=end,
                                 attach_response=False)
        except FDSNException:
            raise FileNotFoundError(f"GeoNet waveforms could not be fetched")
        try:
            inv = c.get_stations(network=net,
                                 station=sta,
                                 location=loc,
                                 channel=cha,
                                 starttime=start,
                                 endtime=end,
                                 level="response")
        except FDSNException:
            raise FileNotFoundError(f"GeoNet inventory could not be fetched")

        print("writing")
        st.write(fid, format="mseed")
        inv.write(inv_fid, format="stationxml")

    if not st:
        raise FileNotFoundError(f"GeoNet data not found for {station_code}")
    if len(st) > 1:
        raise Exception("Non continuous data")

    st.trim(start, end)

    return st, inv
Exemplo n.º 23
0
def GeoNetFDSNrequest(date1, date2, net, sta, loc, cmp):
    """
    Request waveform and meta data from GeoNet's FDSN webservices.
    """
    #GeoNet's FDSN web servers
    arc_client = Client('http://service.geonet.org.nz')
    nrt_client = Client('http://beta-service-nrt.geonet.org.nz')
    time1 = UTCDateTime(date1)
    time2 = UTCDateTime(date2)
    try:
        st = nrt_client.get_waveforms(net, sta, loc, cmp, time1, time2,
                                           attach_response=True)
        inv = nrt_client.get_stations(network=net, station=sta, 
                                           starttime=time1, endtime=time2)
    except FDSNNoDataException:
        st = arc_client.get_waveforms(net, sta, loc, cmp, time1, time2,
                                           attach_response=True)
        inv = arc_client.get_stations(network=net, station=sta, 
                                           starttime=time1, endtime=time2)
    return (st, inv)
Exemplo n.º 24
0
def get_channel_orientation(t0, net, st0, loc, duration, channel):
    """
    Get the station channel orientation.

    Returns azimuth and dip angle.
    """
    client = Client('IRIS')
    st0 = client.get_stations(starttime=t0, endtime=t0+duration*60,
                              network=net, station=st0, channel=channel,
                              level='channel')
    return st0[0][0].channels[0].azimuth, st0[0][0].channels[0].dip
Exemplo n.º 25
0
 def setUpClass(cls):
     from obspy.clients.fdsn import Client
     client = Client('IRIS')
     t1 = UTCDateTime('2012-03-26T00:00:00')
     t2 = t1 + (3 * 86400)
     cls.catalog = client.get_events(
         starttime=t1, endtime=t2, latitude=0,
         longitude=170, maxradius=40)
     cls.inventory = client.get_stations(
         starttime=t1, endtime=t2, latitude=0, longitude=170,
         maxradius=40)
Exemplo n.º 26
0
    def test_radial_queries(self):
        client = FDSNClient(self.live_server_url)
        lat = 48.995167 + 1.0
        lon = 11.519922

        self.assertEqual(
            len(
                client.get_stations(latitude=lat, longitude=lon,
                                    maxradius=2).get_contents()["stations"]),
            1)

        self.assertEqual(
            len(
                client.get_stations(latitude=lat, longitude=lon,
                                    maxradius=1.1).get_contents()["stations"]),
            1)

        with self.assertRaises(FDSNException):
            client.get_stations(latitude=lat,
                                longitude=lon,
                                minradius=1.1,
                                maxradius=10)

        with self.assertRaises(FDSNException):
            client.get_stations(latitude=lat,
                                longitude=lon,
                                minradius=0.1,
                                maxradius=0.5)
Exemplo n.º 27
0
def get_inventory():
    try:
        return read_inventory(invname)
    except Exception:
        pass
    client = Client('GFZ')
    net, sta, loc, cha = seedid.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
Exemplo n.º 28
0
 def test_sim_WA(self):
     """ Test simulating a Wood Anderson instrument. """
     t1 = UTCDateTime("2010-09-3T16:30:00.000")
     t2 = UTCDateTime("2010-09-3T17:00:00.000")
     fdsn_client = Client('IRIS')
     st = fdsn_client.get_waveforms(
         network='NZ', station='BFZ', location='10', channel='HHZ',
         starttime=t1, endtime=t2, attach_response=True)
     inventory = fdsn_client.get_stations(
         network='NZ', station='BFZ', location='10', channel='HHZ',
         starttime=t1, endtime=t2, level="response")
     tr = st[0]
     # Test with inventory
     _sim_WA(trace=tr, inventory=inventory, water_level=10)
Exemplo n.º 29
0
def redownload_statxml(st, network, station, statfile):
    """Errorhandler: Redownload station xml in case that it is not found."""
    for c in tmp.re_client:
        try:
            client = Client(c)
            station_inv = client.get_stations(level="response",
                                              network=network,
                                              station=station)
            # write the new, working stationxml file
            station_inv.write(statfile, format="STATIONXML")
            break
        except (header.FDSNNoDataException, header.FDSNException):
            pass  # wrong client
    return station_inv
Exemplo n.º 30
0
def inv4stream(stream, client_name):

    stat = ""
    for i, trace in enumerate(stream):
        if i > 0:
            if trace.stats.station == stream[i - 1].stats.station:
                continue
        stat = stat + trace.stats.station + ','

    stat = str(stat)
    client = Client(client_name)
    inv = client.get_stations(station=stat)

    return inv
Exemplo n.º 31
0
def download_stationxml(stations,
                        starttime,
                        endtime,
                        outputdir=None,
                        client=None,
                        level="response"):

    if client is None:
        client = Client("IRIS")

    if starttime > endtime:
        raise ValueError("Starttime(%s) is larger than endtime(%s)" %
                         (starttime, endtime))

    if not os.path.exists(outputdir):
        raise ValueError("Outputdir not exists: %s" % outputdir)

    _status = {}
    for station_id in stations:
        error_code = "None"
        network, station, location, channel = _parse_station_id(station_id)

        if outputdir is not None:
            filename = os.path.join(outputdir, "%s.xml" % station_id)
            if os.path.exists(filename):
                os.remove(filename)
        else:
            filename = None

        try:
            inv = client.get_stations(network=network,
                                      station=station,
                                      location=location,
                                      channel=channel,
                                      starttime=starttime,
                                      endtime=endtime,
                                      level=level)
            if len(inv) == 0:
                error_code = "Inventory Empty"
            if filename is not None and len(inv) > 0:
                inv.write(filename, format="STATIONXML")
        except Exception as e:
            error_code = "Failed to download StationXML '%s' due to: %s" \
                % (station_id, str(e))
            print(error_code)

        _status[station_id] = error_code

    return {"inventory": inv, "status": _status}
Exemplo n.º 32
0
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 get_dataless(net, stime, etime, client):
    if os.path.exists(path + net + '_metadata.pickle'):
        with open(path + net + '_metadata.pickle', 'rb') as fhand:
            inv = pickle.load(fhand)
    else:
        client = Client()
        inv = client.get_stations(starttime=stime,
                                  endtime=etime,
                                  station="*",
                                  channel=chan1,
                                  network=net,
                                  level="response")
        with open(path + net + '_metadata.pickle', 'wb') as fhand:
            pickle.dump(inv, fhand)
    return inv
Exemplo n.º 34
0
def hobitss(level="station"):
    """
    Return hobitss broadband OBS sensors (trillium compacts)

    :type level: str
    :param level: level to propogate network creation
    :rtype: obspy.core.inventory.network.Network
    """
    c = Client("IRIS")
    inv_hobitss = c.get_stations(network='YH',
                                 station="LOBS*",
                                 location='',
                                 channel="HH?",
                                 level=level)
    return inv_hobitss[0]
Exemplo n.º 35
0
    def test_total_and_selected_number_of_sta_and_cha(self):
        client = FDSNClient(self.live_server_url)

        inv = client.get_stations(level="network")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 0)

        inv = client.get_stations(level="station")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 1)
        self.assertEqual(inv[0][0].total_number_of_channels, 3)
        self.assertEqual(inv[0][0].selected_number_of_channels, 0)

        inv = client.get_stations(level="channel")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 1)
        self.assertEqual(inv[0][0].total_number_of_channels, 3)
        self.assertEqual(inv[0][0].selected_number_of_channels, 3)

        inv = client.get_stations(level="response")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 1)
        self.assertEqual(inv[0][0].total_number_of_channels, 3)
        self.assertEqual(inv[0][0].selected_number_of_channels, 3)
Exemplo n.º 36
0
    def test_total_and_selected_number_of_sta_and_cha(self):
        client = FDSNClient(self.live_server_url)

        inv = client.get_stations(level="network")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 0)

        inv = client.get_stations(level="station")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 1)
        self.assertEqual(inv[0][0].total_number_of_channels, 3)
        self.assertEqual(inv[0][0].selected_number_of_channels, 0)

        inv = client.get_stations(level="channel")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 1)
        self.assertEqual(inv[0][0].total_number_of_channels, 3)
        self.assertEqual(inv[0][0].selected_number_of_channels, 3)

        inv = client.get_stations(level="response")
        self.assertEqual(inv[0].total_number_of_stations, 1)
        self.assertEqual(inv[0].selected_number_of_stations, 1)
        self.assertEqual(inv[0][0].total_number_of_channels, 3)
        self.assertEqual(inv[0][0].selected_number_of_channels, 3)
Exemplo n.º 37
0
def get_station_location(t0, net, st0, loc):
    """
    Get the station latitude, longitude, and elevation.

    Given a time, duration, loc code, and station network/name, get
    station information from IRIS.  Return a list containing the
    lat, lon, and elevation.
    """
    client = Client('IRIS')
    st0 = client.get_stations(starttime=t0, endtime=t0+timedelta(seconds=60),
                              network=net, station=st0, level='station')
    slat = st0[0][0].latitude
    slon = st0[0][0].longitude
    selev = st0[0][0].elevation
    return [slat, slon, selev]
Exemplo n.º 38
0
def get_from_iris(level="station"):
    """
    Return available broadband stations from IRIS

    :type level: str
    :param level: level to propogate network creation
    :rtype: obspy.core.inventory.network.Network
    """
    c = Client("IRIS")

    # A whole-Alaska bounding box including western Canada and Aleutians
    lat_min = 52.17  # South
    lat_max = 72.23  # North
    lon_min = -178.04  # West
    lon_max = -121.04  # East

    starttime = UTCDateTime("2000-01-01")
    networks = [
        "AK",  # Alaska Regional Network
        "AT",  # National Tsunami Warning Center (Alaska Seismic Network)
        "AV",  # Alaska Volcano Observatory
        "CN",  # Canadian National Seismograph Network
        "II",  # Global Seismograph Network (GSN)
        "IU",  # Global Seismograph Network (GSN)
        "NY",  # Yukon-Northwest Seismic Network
        "TA",  # USArray Transportable Array
        "US",  # United States National Seismic Network
        "YO",  # Yukon Observatory
    ]
    channels = ["BH?", "BL?", "HH?", "HL?"]

    # ObsPy doesnt accept lists or bracketed wildcards, must be comma separated
    networks = ",".join(networks)
    channels = ",".join(channels)

    inv_alaska = c.get_stations(network=networks,
                                station="*",
                                channel=channels,
                                minlatitude=lat_min,
                                maxlatitude=lat_max,
                                minlongitude=lon_min,
                                maxlongitude=lon_max,
                                level=level,
                                starttime=starttime,
                                endtime=UTCDateTime())

    return inv_alaska
Exemplo n.º 39
0
def Doit():
    from sys import argv

    nstas = [argv[1]]
    #    nstas=['FS05B']
    print("doing nstas", nstas)

    client = Client("IRIS")
    t1 = UTCDateTime(year=2012, julday=1)
    t2 = UTCDateTime(year=2012, julday=365)

    inventory = client.get_stations(network='7D', starttime=t1, endtime=t2)

    allstas = {}
    for sta in inventory[0].stations:
        nsta = sta.code
        xlat = sta.latitude
        xlon = sta.longitude
        xdep = sta.elevation
        allstas[nsta] = (xlat, xlon, np.abs(xdep))

    cat = read_events("cmt2012.ndk")
    cat = cat.filter("magnitude >= 5.0")
    cat_large = cat.filter("magnitude >= 5.5")
    t1_all = []
    for item in cat_large:
        t1_all.append(item.origins[0].time)
    print("number of evenst:", len(t1_all))
    # t1_all.append(cat_large[100].origins[0].time)
    # print(t1_all)

    for nsta in nstas:
        outfile = './' + nsta + '_result_new.txt'
        #        for t1 in t1_all[100:101]:
        for t1 in t1_all:
            fobj = open(outfile, 'a')
            t2 = t1 + 86400
            print("doing", nsta, t1)
            ierr, angle, coh, adm, phs, coh2, adm2, phs2 = DoDay(nsta,
                                                                 t1,
                                                                 t2,
                                                                 cat,
                                                                 allstas,
                                                                 iopt=2)
            fobj.write("%3d %3d %10.3f %10.3f %12.5g %12.5g %10.3f %12.5g %12.5g\n" \
                       % (ierr,t1.julday,np.rad2deg(angle),coh,adm,phs,coh2,adm2,phs2))
            fobj.close()
Exemplo n.º 40
0
def download_stationxml(stations, starttime, endtime, outputdir=None,
                        client=None, level="response"):

    if client is None:
        client = Client("IRIS")

    if starttime > endtime:
        raise ValueError("Starttime(%s) is larger than endtime(%s)"
                         % (starttime, endtime))

    if not os.path.exists(outputdir):
        raise ValueError("Outputdir not exists: %s" % outputdir)

    _status = {}
    for station_id in stations:
        error_code = "None"
        network, station, location, channel = _parse_station_id(station_id)

        if outputdir is not None:
            filename = os.path.join(outputdir, "%s.xml" % station_id)
            if os.path.exists(filename):
                os.remove(filename)
        else:
            filename = None

        try:
            inv = client.get_stations(
                network=network, station=station, location=location,
                channel=channel, starttime=starttime, endtime=endtime,
                level=level)
            if len(inv) == 0:
                error_code = "Inventory Empty"
            if filename is not None and len(inv) > 0:
                inv.write(filename, format="STATIONXML")
        except Exception as e:
            error_code = "Failed to download StationXML '%s' due to: %s" \
                % (station_id, str(e))
            print(error_code)

        _status[station_id] = error_code

    return {"inventory": inv, "status": _status}
Exemplo n.º 41
0
def download_stationxml(stations, starttime, endtime, outputdir=".",
                        client=None, level="response"):

    if client is None:
        client = Client("IRIS")

    if starttime > endtime:
        raise ValueError("Starttime(%s) is larger than endtime(%s)"
                         % (starttime, endtime))

    if not os.path.exists(outputdir):
        raise ValueError("Outputdir not exists: %s" % outputdir)

    _status = {}
    for station_id in stations:
        network, station, location, channel = _parse_station_id(station_id)

        filename = os.path.join(outputdir, "%s.xml" % station_id)
        if os.path.exists(filename):
            os.remove(filename)

        try:
            inv = client.get_stations(
                network=network, station=station, location=location,
                channel=channel, starttime=starttime, endtime=endtime,
                level=level)
            if len(inv) > 0:
                inv.write(filename, format="STATIONXML")
                error_code = 0
            else:
                error_code = 1
        except Exception as e:
            print("Failed to download StationXML '%s' due to: %s"
                  % (station_id, str(e)))
            error_code = 2
        _status[station_id] = error_code

    return _status
Exemplo n.º 42
0
    def get_inventory(self):
        starttime = UTCDateTime()
        endtime = UTCDateTime()
        client = Client(self.fdsn_server)
        for s in self.streams:
            net, sta, chan, loc = s
            inv = client.get_stations(starttime=starttime,
                                      endtime=endtime,
                                      network=net,
                                      station=sta,
                                      location=loc,
                                      channel=chan,
                                      level="response")
            channels = set(inv.get_contents()['channels'])

            for c in channels:
                try:
                    coords = inv.get_coordinates(c, datetime=starttime)
                except:
                    try:
                        coords = inv.get_coordinates(c)
                    except:
                        print c, "No matching coordinates found"
                        continue

                latitude = coords['latitude']
                longitude = coords['longitude']
                elevation = coords['elevation']
                self.station_coordinfo[c] = \
                    {"latitude": latitude,
                     "longitude": longitude,
                     "elevation": elevation,
                     "geohash": Geohash.encode(latitude,
                                               longitude,
                                               precision=7)
                     }
Exemplo n.º 43
0
    def test_radial_queries(self):
        client = FDSNClient(self.live_server_url)
        lat = 48.995167 + 1.0
        lon = 11.519922

        self.assertEqual(
            len(client.get_stations(
                latitude=lat, longitude=lon,
                maxradius=2).get_contents()["stations"]),
            1)

        self.assertEqual(
            len(client.get_stations(
                latitude=lat, longitude=lon,
                maxradius=1.1).get_contents()["stations"]),
            1)

        with self.assertRaises(FDSNException):
            client.get_stations(latitude=lat, longitude=lon,
                                minradius=1.1, maxradius=10)

        with self.assertRaises(FDSNException):
            client.get_stations(latitude=lat, longitude=lon,
                                minradius=0.1, maxradius=0.5)
Exemplo n.º 44
0
def data_request(client_name, cat_client_name, start, end, minmag, net=None, scode="*", channels="*", minlat=None,
                 maxlat=None,minlon=None,maxlon=None, station_minlat=None,
                 station_maxlat=None, station_minlon=None, station_maxlon=None, mindepth=None, maxdepth=None, 
                 radialcenterlat=None, radialcenterlon=None, minrad=None, maxrad=None,
                 station_radialcenterlat=None, station_radialcenterlon=None, station_minrad=None, station_maxrad=None,
                 azimuth=None, baz=False, t_before_first_arrival=1, t_after_first_arrival=9, savefile=False, file_format='SAC'):
	"""
	Searches in a given Database for seismic data. Restrictions in terms of starttime, endtime, network etc can be made.
	If data is found it returns a stream variable, with the waveforms, an inventory with all station and network information
	and a catalog with the event information.

	:param client_name: Name of desired fdsn client, for a list of all clients see: 
		                https://docs.obspy.org/tutorial/code_snippets/retrieving_data_from_datacenters.html
	:type  client_name:  string

	:param cat_client_name: Name of Event catalog

	:type  cat_client_name: string

	:param start, end: starttime, endtime
	:type : UTCDateTime

	:param minmag: Minimum magnitude of event
	:type  minmag: float

	:param net: Network code for which to search data for
	:type  net: string

	:param scode: Station code for which to search data for
	:type  scode: string

	:param channels: Used channels of stations 
	:type  channels: string

	:param minlat, maxlat, minlon, maxlon: Coordinate-window of interest
	:type : float

	:param mindepth, maxdepth: depth information of event in km
	:type : float

	:param radialcenterlat, radialcenterlon: Centercoordinates of a radialsearch, if radialsearch=True
	:type : float

	:param minrad, maxrad: Minimum and maximum radii for radialsearch
	:type : float

	:param azimuth: Desired range of azimuths of event, station couples in deg as a list [minimum azimuth, maximum azimuth]
	:type  azimuth: list

	:param baz: Desired range of back-azimuths of event, station couples in deg as a list [minimum back azimuth, maximum back azimuth]
	:type  baz: list

	:param t_before_first_arrival, t_before_after_arrival: Length of the seismograms, startingpoint, minutes before 1st arrival and
															minutes after 1st arrival.
	:type  t_before_first_arrival, t_before_after_arrival: float, int
	
	:param savefile: if True, Stream, Inventory and Catalog will be saved local, in the current directory.
	:type  savefile: bool

	:param format: File-format of the data, for supported formats see: https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.write.html#obspy.core.stream.Stream.write
	:type  format: string
	
	returns

	:param: list_of_stream, Inventory, Catalog
	:type: list, obspy, obspy 



	### Example 1 ###

	from obspy import UTCDateTime
	from sipy.util.data_request import data_request

	start = UTCDateTime(2010,1,1,0,0)
	end = UTCDateTime(2010,12,31,0,0)
	minmag = 8
	station = '034A'
	list_of_stream, inventory, cat = data_request('IRIS', start, end, minmag, net='TA', scode=station)
	
	st = list_of_stream[0]
	st = st.select(channel='BHZ')
	st.normalize()
	inv = inventory[0]

	st.plot()
	inv.plot()
	cat.plot()

	### Example 2 ###

	from obspy import UTCDateTime
	from sipy.util.data_request import data_request

	start = UTCDateTime(2010,1,1,0,0)
	end = UTCDateTime(2010,12,31,0,0)
	minmag = 8
	station = '034A'
	client = 'IRIS'
	cat_client = 'globalcmt'
	list_of_stream, inventory, cat = data_request(client, cat_client, start, end, minmag, net='TA', scode=station)
	
	st = list_of_stream[0]
	st = st.select(channel='BHZ')
	st.normalize()
	inv = inventory[0]

	st.plot()
	inv.plot()
	cat.plot()

	"""

	data =[]
	stream = Stream()
	streamall = []
	

	#build in different approach for catalog search, using urllib

	if cat_client_name == 'globalcmt':
		catalog = request_gcmt(starttime=start, endtime=end, minmagnitude=minmag, mindepth=mindepth, maxdepth=maxdepth, minlatitude=minlat, maxlatitude=maxlat, minlongitude=minlon, maxlongitude=maxlon)
		client = Client(client_name)
	else:	
		client = Client(client_name)
		try:
			catalog = client.get_events(starttime=start, endtime=end, minmagnitude=minmag, mindepth=mindepth, maxdepth=maxdepth, latitude=radialcenterlat, longitude=radialcenterlon, minradius=minrad, maxradius=maxrad,minlatitude=minlat, maxlatitude=maxlat, minlongitude=minlon, maxlongitude=maxlon)

		except:
			print("No events found for given parameters.")
			return

	print("Following events found: \n")
	print(catalog)
	m = TauPyModel(model="ak135")
	Plist = ["P", "Pdiff", "p"]
	for event in catalog:
		print("\n")
		print("########################################")
		print("Looking for available data for event: \n")
		print(event.short_str())
		print("\n")

		origin_t = event.origins[0].time
		station_stime = UTCDateTime(origin_t - 3600*24)
		station_etime = UTCDateTime(origin_t + 3600*24)

		try:
			inventory = client.get_stations(network=net, station=scode, level="station", starttime=station_stime, endtime=station_etime,
			 								minlatitude=station_minlat, maxlatitude=station_maxlat, minlongitude=station_minlon, maxlongitude=station_maxlon,
			 								latitude=station_radialcenterlat, longitude=station_radialcenterlon, minradius=station_minrad, maxradius=station_maxrad)
			print("Inventory found.")
		except:
			print("No Inventory found for given parameters")
			return
		
		for network in inventory:

			elat = event.origins[0].latitude
			elon = event.origins[0].longitude
			depth = event.origins[0].depth/1000.

			array_fits = True
			if azimuth or baz:
				cog=center_of_gravity(network)
				slat = cog['latitude']
				slon = cog['longitude']			
				epidist = locations2degrees(slat,slon,elat,elon)
				arrivaltime = m.get_travel_times(source_depth_in_km=depth, distance_in_degree=epidist,
							                        phase_list=Plist)

				P_arrival_time = arrivaltime[0]

				Ptime = P_arrival_time.time
				tstart = UTCDateTime(event.origins[0].time + Ptime - t_before_first_arrival * 60)
				tend = UTCDateTime(event.origins[0].time + Ptime + t_after_first_arrival * 60)


				center = geometrical_center(inv)
				clat = center['latitude']
				clon = center['longitude']
				if azimuth:
					print("Looking for events in the azimuth range of %f to %f" % (azimuth[0], azimuth[1]) )
					center_az = gps2dist_azimuth(clat, clon, elat, elon)[1]
					if center_az > azimuth[1] and center_az < azimuth[0]: 
						print("Geometrical center of Array out of azimuth bounds, \ncheking if single stations fit")
						array_fits = False

				elif baz:
					print("Looking for events in the back azimuth range of %f to %f" %(baz[0], baz[1]))
					center_baz = gps2dist_azimuth(clat, clon, elat, elon)[2]
					if center_baz > baz[1] and center_baz < baz[0]: 
						print("Geometrical center of Array out of back azimuth bounds, \ncheking if single stations fit")
						array_fits = False

			# If array fits to azimuth/back azimuth or no azimuth/back azimuth is given
			no_of_stations = 0
			if array_fits:

				for station in network:

					epidist = locations2degrees(station.latitude,station.longitude,elat,elon)
					arrivaltime = m.get_travel_times(source_depth_in_km=depth, distance_in_degree=epidist,
								                        phase_list=Plist)

					P_arrival_time = arrivaltime[0]

					Ptime = P_arrival_time.time
					tstart = UTCDateTime(event.origins[0].time + Ptime - t_before_first_arrival * 60)
					tend = UTCDateTime(event.origins[0].time + Ptime + t_after_first_arrival * 60)

					try:
						streamreq = client.get_waveforms(network=network.code, station=station.code, location='*', channel=channels, starttime=tstart, endtime=tend, attach_response=True)
						no_of_stations += 1
						print("Downloaded data for %i of %i available stations!" % (no_of_stations, network.selected_number_of_stations), end='\r' )
						sys.stdout.flush()
						stream 		   += streamreq
						try:
							if inventory_used:
								inventory_used 	+= client.get_stations(network=net, station=scode, level="station", starttime=station_stime, endtime=station_etime,
			 								minlatitude=station_minlat, maxlatitude=station_maxlat, minlongitude=station_minlon, maxlongitude=station_maxlon,
			 								latitude=station_radialcenterlat, longitude=station_radialcenterlon, minradius=station_minrad, maxradius=station_maxrad)
									
						except:
								inventory_used 	 = client.get_stations(network=net, station=scode, level="station", starttime=station_stime, endtime=station_etime,
			 								minlatitude=station_minlat, maxlatitude=station_maxlat, minlongitude=station_minlon, maxlongitude=station_maxlon,
			 								latitude=station_radialcenterlat, longitude=station_radialcenterlon, minradius=station_minrad, maxradius=station_maxrad)
					except:
						continue


			# If not checking each station individually.
			else:
				for station in network:
					epidist = locations2degrees(station.latitude,station.longitude,elat,elon)
					arrivaltime = m.get_travel_times(source_depth_in_km=depth, distance_in_degree=epidist,
								                        phase_list=Plist)


					P_arrival_time = arrivaltime[0]

					Ptime = P_arrival_time.time
					tstart = UTCDateTime(event.origins[0].time + Ptime - t_before_first_arrival * 60)
					tend = UTCDateTime(event.origins[0].time + Ptime + t_after_first_arrival * 60)

					fit = False
					if azimuth:
						stat_az = gps2dist_azimuth(station.latitude, station.longitude, elat, elon)[1]
						if stat_az > azimuth[1] and stat_az < azimuth[0]: fit = True
					elif baz:
						stat_baz = gps2dist_azimuth(station.latitude, station.longitude, elat, elon)[2]
						if stat_baz > baz[1] and stat_baz < baz[0]: fit = True
					if fit:
						try:
							streamreq = client.get_waveforms(network = network.code, station = station.code, location='*', channel = channels, startime = tstart, endtime = tend, attach_response = True)
							no_of_stations += 1
							print("Downloaded data for %i of %i available stations!" % (no_of_stations, network.selected_number_of_stations), end='\r' )
							sys.stdout.flush()
							stream 		+= streamreq
							try:
								if inventory_used:
									inventory_used 	+= client.get_stations(network=net, station=scode, level="station", starttime=station_stime, endtime=station_etime,
			 								minlatitude=station_minlat, maxlatitude=station_maxlat, minlongitude=station_minlon, maxlongitude=station_maxlon,
			 								latitude=station_radialcenterlat, longitude=station_radialcenterlon, minradius=station_minrad, maxradius=station_maxrad)
							except:
									inventory_used 	 = client.get_stations(network=net, station=scode, level="station", starttime=station_stime, endtime=station_etime,
			 								minlatitude=station_minlat, maxlatitude=station_maxlat, minlongitude=station_minlon, maxlongitude=station_maxlon,
			 								latitude=station_radialcenterlat, longitude=station_radialcenterlon, minradius=station_minrad, maxradius=station_maxrad)
						except:

							continue

		try:
			if invall:
				invall += inventory
		except:
			invall 		= inventory

		attach_network_to_traces(stream, inventory)
		attach_coordinates_to_traces(stream, inventory, event)
		streamall.append(stream)
		stream = Stream()

	if savefile:
		stname = str(origin_t).split('.')[0] + ".MSEED"
		invname = stname + "_inv.xml"
		catname = stname + "_cat.xml"
		stream.write(stname, format=file_format)
		inventory.write(invname, format="STATIONXML")
		catalog.write(catname, format="QUAKEML")

	plt.ion()
	#invall.plot()
	#catalog.plot()
	plt.ioff()
	inventory = invall
	list_of_stream = streamall
	return(list_of_stream, inventory, catalog)
Exemplo n.º 45
0

#If we arent doing manual station lists we need to get one for the network
if parserval.sta:
    manstalist = True
    if debug: 
        print("We are using a manual station list")
    stalist = parserval.sta.split(",")
    stations = []
    for sta in stalist:
        stations.append(parserval.network + " " + sta)
    if debug:
        print(stations) 
else:
    manstalist = False
    stations = client.get_stations(network=parserval.network, starttime=eventtime, endtime=eventtime)
    stations = [ sta.code for sta in stations[0]]

if debug:
    print("Here are the stations we found") 
    for sta in stations:
        print("Here is a station:" + sta)


#Lets start by using a station list and then move to a different approach
for sta in stations:
    
    net = parserval.network
    cursta = sta

#Now we get the data for the event
Exemplo n.º 46
0
def fdsnws(base_url="http://arclink.ethz.ch:8080",
                      endafter=40.,
                      maxradius=.6,
                      location='*',
                      channel='HNZ,HNE,HNN,HGZ,HGE,HGN,HHZ,HHE,HHN,EHZ,EHE,EHN,SHZ,SHE,SHN',
                      stations_base_url=None,
                      waveforms_base_url=None,
                      quality=None,
                      minimumlength=None,
                      longestonly=None,
           correction_method = remove_sensitivity,
         eventid=None,
                      **get_events_options):
    
    
    # First import :
    from obspy.clients.fdsn import Client
    fdsnclient = Client(base_url)
    
    # eventid in URL case
    if eventid is  None:
        eventid = 'smi:ch.ethz.sed/sc3a/2017epaqsp'
        print('Picks default eventid:',eventid)
    elif '#' in eventid :
        eventid = eventid.split('#')[-1]
        print('Picks eventid in URL format:',eventid)
    
    # Special clients systems
    stationsclient = fdsnclient
    waveformsclient = fdsnclient
    if stations_base_url:
        stationsclient = Client(stations_base_url)
        if not waveforms_base_url:
            waveformsclient = Client(stations_base_url)
    if waveforms_base_url:
        waveformsclient = Client(waveforms_base_url)
        if not stations_base_url:
            stationsclient = Client(waveforms_base_url)
    


    # Load event
    fdsnclient.get_events(eventid=eventid,format='sc3ml',filename='events.xml', **get_events_options)
    eventstreams = {'catalog': obspy.read_events('events.xml',format='sc3ml'),
                    'inventory': obspy.core.inventory.Inventory([],None),
                    'raw' : obspy.core.Stream()}
    if eventstreams['catalog'] is None:
        print('catalog is',eventstreams['catalog'])
    for output in ['catalog','inventory','raw']:
        eventstreams[output].output=output
    
    for event in eventstreams['catalog'].events :
        
        # Load stations
        t=event.preferred_origin().time
        try:
            inventory = stationsclient.get_stations(level='station',
                                                startbefore=t,
                                                endafter=t+endafter,
                                                latitude=event.preferred_origin().latitude,
                                                longitude=event.preferred_origin().longitude,
                                                maxradius=maxradius,
                                                location=location,
                                                channel=channel)
        except:
            print('No station found for event:')
            print(event)
            print('Using client:')
            print(stationsclient)
            continue
        # Load waveforms
        addons = [location, channel] + [t,t+endafter]
        bulk = [tuple(station.split()[0].split('.')[:2]+addons) for station in inventory.get_contents()['stations']]
        try:
            waveforms = waveformsclient.get_waveforms_bulk(bulk,
                                                      attach_response=True,
                                                      quality=quality,
                                                      minimumlength=minimumlength,
                                                      longestonly=longestonly)
        except:
            print('No waveform found for request:')
            print(bulk)
            print('Using client:')
            print(waveformsclient)
            continue
        # Improve waveforms attributes
        for trace in waveforms:
            station = inventory.select(network=trace.stats.network,
                                       station=trace.stats.station).networks[0].stations[0]
            trace.stats.coordinates = {'latitude':station.latitude,
                                       'longitude':station.longitude,
                                       'elevation':station.elevation}
            distance = obspy.geodetics.base.gps2dist_azimuth(station.latitude,
                                                             station.longitude,
                                                             event.preferred_origin().latitude,
                                                             event.preferred_origin().longitude)[0]
            distance = ((distance**2+(trace.stats.coordinates['elevation']*-1)**2.)**.5)
            distance = distance/len(eventstreams['catalog'].events)
            if not hasattr(trace.stats, 'distance'):
                trace.stats.distance = 0.
            trace.stats.distance += distance

        eventstreams['inventory'] += inventory
        eventstreams['raw'] += waveforms

    eventstreams['raw'].sort(keys=['distance'])

    if correction_method:
        eventstreams = correction_method(eventstreams)

    return eventstreams
Exemplo n.º 47
0
    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://example.com/beta/event_service/11"
        base_url_station = "http://example.org/beta2/station/7"
        base_url_ds = "http://example.edu/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])
Exemplo n.º 48
0
    def test_redirection_auth(self):
        """
        Tests the redirection of GET and POST requests using authentication.

        By default these should not redirect and an exception is raised.
        """
        # Clear the cache.
        Client._Client__service_discovery_cache.clear()

        # The error will already be raised during the initialization in most
        # cases.
        self.assertRaises(
            FDSNRedirectException,
            Client, "IRIS", service_mappings={
                "station": "http://ds.iris.edu/files/redirect/307/station/1",
                "dataselect":
                    "http://ds.iris.edu/files/redirect/307/dataselect/1",
                "event": "http://ds.iris.edu/files/redirect/307/event/1"},
            user="******", password="******",
            user_agent=USER_AGENT)

        # The force_redirect flag overwrites that behaviour.
        c_auth = Client("IRIS", service_mappings={
            "station":
                "http://ds.iris.edu/files/redirect/307/station/1",
            "dataselect":
                "http://ds.iris.edu/files/redirect/307/dataselect/1",
            "event":
                "http://ds.iris.edu/files/redirect/307/event/1"},
            user="******", password="******",
            user_agent=USER_AGENT, force_redirect=True)

        st = c_auth.get_waveforms(
            network="IU", station="ANMO", location="00", channel="BHZ",
            starttime=UTCDateTime("2010-02-27T06:30:00.000"),
            endtime=UTCDateTime("2010-02-27T06:30:01.000"))
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(st)))

        inv = c_auth.get_stations(
            starttime=UTCDateTime("2000-01-01"),
            endtime=UTCDateTime("2001-01-01"),
            network="IU", station="ANMO", level="network")
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(inv.networks)))

        cat = c_auth.get_events(starttime=UTCDateTime("2001-01-07T01:00:00"),
                                endtime=UTCDateTime("2001-01-07T01:05:00"),
                                catalog="ISC")
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(cat)))

        # Also test the bulk requests which are done using POST requests.
        bulk = (("TA", "A25A", "", "BHZ",
                 UTCDateTime("2010-03-25T00:00:00"),
                 UTCDateTime("2010-03-25T00:00:01")),
                ("TA", "A25A", "", "BHE",
                 UTCDateTime("2010-03-25T00:00:00"),
                 UTCDateTime("2010-03-25T00:00:01")))
        st = c_auth.get_waveforms_bulk(bulk, quality="B", longestonly=False)
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(st)))

        starttime = UTCDateTime(1990, 1, 1)
        endtime = UTCDateTime(1990, 1, 1) + 10
        bulk = [
            ["IU", "ANMO", "", "BHE", starttime, endtime],
            ["IU", "CCM", "", "BHZ", starttime, endtime],
        ]
        inv = c_auth.get_stations_bulk(bulk, level="network")
        # Just make sure something is being downloaded.
        self.assertTrue(bool(len(inv.networks)))
Exemplo n.º 49
0
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"]:
            # 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
Exemplo n.º 50
0
    def test_temporal_queries(self):
        """
        Test the various temporal parameters.
        """
        # All 3 channels start at the same time, two are open ended,
        # one ends a bit earlier.
        client = FDSNClient(self.live_server_url)

        start = obspy.UTCDateTime("2010-04-29T00:00:00.000000Z")
        end = obspy.UTCDateTime("2011-01-01T00:00:00.000000Z")

        inv = client.get_stations(starttime=obspy.UTCDateTime(2000, 1, 1),
                                  level="channel")
        c = inv.get_contents()
        self.assertEqual(c["channels"],
                         ['BW.ALTM..EHE', 'BW.ALTM..EHN', 'BW.ALTM..EHZ'])
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)

        # Same thing.
        c = client.get_stations(starttime=start - 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)

        # Go before and after the endtime of the one channel.
        c = client.get_stations(starttime=end - 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)
        c = client.get_stations(starttime=end + 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 2)

        # Test the endtime parameter.
        inv = client.get_stations(endtime=obspy.UTCDateTime(2016, 1, 1),
                                  level="channel")
        c = inv.get_contents()
        self.assertEqual(c["channels"],
                         ['BW.ALTM..EHE', 'BW.ALTM..EHN', 'BW.ALTM..EHZ'])
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)
        c = client.get_stations(endtime=start + 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)
        with self.assertRaises(FDSNException):
            client.get_stations(endtime=start - 10, level="channel")

        # startbefore
        c = client.get_stations(startbefore=start + 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)
        with self.assertRaises(FDSNException):
            client.get_stations(startbefore=start - 10, level="channel")

        # startafter
        c = client.get_stations(startafter=start - 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)
        with self.assertRaises(FDSNException):
            client.get_stations(startafter=start + 10, level="channel")

        # endbefore
        c = client.get_stations(endbefore=end + 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 1)
        with self.assertRaises(FDSNException):
            client.get_stations(endbefore=end - 10, level="channel")

        # endafter
        c = client.get_stations(endafter=end - 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 3)
        c = client.get_stations(endafter=end + 10,
                                level="channel").get_contents()
        self.assertEqual(len(c["networks"]), 1)
        self.assertEqual(len(c["stations"]), 1)
        self.assertEqual(len(c["channels"]), 2)
Exemplo n.º 51
0
def fdsn_available(input_dics, cl, event, target_path):
    """
    check the availablity of FDSN stations
    :param input_dics:
    :param cl:
    :param event:
    :param target_path:
    :return:
    """
    print("check the availability: %s" % input_dics['data_source'][cl])

    if input_dics['username_fdsn']:
        include_restricted = True
    else:
        include_restricted = None

    sta_fdsn = []
    try:
        client_fdsn = Client_fdsn(
            base_url=input_dics['data_source'][cl].upper(),
            user=input_dics['username_fdsn'],
            password=input_dics['password_fdsn'])

        available = client_fdsn.get_stations(
            network=input_dics['net'],
            station=input_dics['sta'],
            location=input_dics['loc'],
            channel=input_dics['cha'],
            starttime=event['t1'],
            endtime=event['t2'],
            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'],
            includerestricted=include_restricted,
            level='channel')

        for network in available.networks:
            for station in network:
                for channel in station:
                    st_id = '%s_%s_%s_%s' % (network.code,
                                             station.code,
                                             channel.location_code,
                                             channel.code)
                    sta_fdsn.append([network.code, station.code,
                                     channel.location_code, channel.code,
                                     channel.latitude, channel.longitude,
                                     channel.elevation, channel.depth,
                                     input_dics['data_source'][cl], st_id,
                                     channel.azimuth, channel.dip])

        if input_dics['bulk']:
            print('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_%s' % input_dics['data_source'][cl]), 'ab+')
            pickle.dump(bulk_list, bulk_list_fio, protocol=2)
            bulk_list_fio.close()

    except Exception as error:
        exc_file = open(os.path.join(target_path, 'info', 'exception'), 'at+')
        ee = 'availability -- %s -- %s\n' % (input_dics['data_source'][cl],
                                             error)
        exc_file.writelines(ee)
        exc_file.close()
        print('ERROR: %s' % ee)
        return []

    if len(sta_fdsn) == 0:
        sta_fdsn = []
    sta_fdsn.sort()
    return sta_fdsn
Exemplo n.º 52
0
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']:
            # 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
Exemplo n.º 53
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from obspy.clients.fdsn import Client
from obspy import UTCDateTime

client = Client("IRIS")

t = UTCDateTime("2010-02-27T06:45:00.000")

inventory = client.get_stations(network="IC", station="BJT", location="00",
                                channel="BH*", level="response", starttime=t)
st = client.get_waveforms("IC", "BJT", "00", "BH*", t, t + 60 * 60)
st.detrend(type="demean")  # rmean
st.detrend(type="linear")  # rtrend
st.taper(max_percentage=0.05)  # taper
# transfer to vel freq 0.005 0.01 10 25
st.remove_response(inventory=inventory, output="VEL",
                   pre_filt=[0.005, 0.01, 10, 25])
# bandpass c 1 10 n 2 p 2
st.filter('bandpass', freqmin=0.01, freqmax=0.1, corners=2, zerophase=True)
st.plot()
Exemplo n.º 54
0
    def test_seed_code_queries(self):
        client = FDSNClient(self.live_server_url)

        # First test some very specific queries.
        inv = client.get_stations(level="channel", network="BW",
                                  station="ALTM", location="--", channel="EH?")
        c = inv.get_contents()
        self.assertEqual(c["channels"],
                         ['BW.ALTM..EHE', 'BW.ALTM..EHN', 'BW.ALTM..EHZ'])

        client = FDSNClient(self.live_server_url)
        inv = client.get_stations(level="channel", network="BW",
                                  station="ALTM", location="--", channel="EH*")
        c = inv.get_contents()
        self.assertEqual(c["channels"],
                         ['BW.ALTM..EHE', 'BW.ALTM..EHN', 'BW.ALTM..EHZ'])

        client = FDSNClient(self.live_server_url)
        inv = client.get_stations(level="channel", network="BW",
                                  station="ALTM", location="", channel="EH*")
        c = inv.get_contents()
        self.assertEqual(c["channels"],
                         ['BW.ALTM..EHE', 'BW.ALTM..EHN', 'BW.ALTM..EHZ'])

        client = FDSNClient(self.live_server_url)
        inv = client.get_stations(level="channel", network="B*",
                                  station="AL?M", location="*", channel="EH*")
        c = inv.get_contents()
        self.assertEqual(c["channels"],
                         ['BW.ALTM..EHE', 'BW.ALTM..EHN', 'BW.ALTM..EHZ'])

        # Test exclusions. - First exclude things that don't exist in the
        # test database - should naturally still return everything.
        inv = client.get_stations(level="channel", network="-XX",
                                  station="-YY", location="-ZZ",
                                  channel="-BHE")
        c = inv.get_contents()
        self.assertEqual(c["channels"],
                         ['BW.ALTM..EHE', 'BW.ALTM..EHN', 'BW.ALTM..EHZ'])

        inv = client.get_stations(level="channel", channel="-EHE")
        c = inv.get_contents()
        self.assertEqual(c["channels"], ['BW.ALTM..EHN', 'BW.ALTM..EHZ'])

        inv = client.get_stations(level="channel", channel="-EHE,-EHN")
        c = inv.get_contents()
        self.assertEqual(c["channels"], ['BW.ALTM..EHZ'])

        # A couple of no-datas
        with self.assertRaises(FDSNException):
            client.get_stations(network="TA", station="ALTM",
                                location="--", channel="EH?")

        with self.assertRaises(FDSNException):
            client.get_stations(network="BW", station="FURT",
                                location="--", channel="EH?")

        with self.assertRaises(FDSNException):
            client.get_stations(network="BW", station="ALTM",
                                location="00", channel="EH?")

        with self.assertRaises(FDSNException):
            client.get_stations(network="BW", station="ALTM",
                                location="--", channel="BHZ?")
starttime = UTCDateTime("2016-05-20")
endtime = UTCDateTime("2016-05-22")
cat = client.get_events(starttime=starttime, endtime=endtime, minmagnitude=2, limit=5, mindepth=5)
print(type(cat)) #Catalog
print(cat)
cat.plot(outfile='output/py_eq_utc20160521_ml3.0_event.png') #add also resouces like: projection="local"

#get stations with the event
evt = cat[1]
print(type(evt))
print(evt)
origin = evt.origins[0]
otime = origin.time
print(type(origin))
t = origin.time
inv = client.get_stations(longitude=origin.longitude, latitude=origin.latitude, maxradius=0.2, starttime=t, endtime =t+100, channel="HH?", network="CH", level="station")
print(type(inv))
print(inv)
inv.plot(projection="local", outfile='output/py_eq_utc20160521_ml3.0_station.png')

#get the waveforms
st = Stream()
for network in inv:
    for station in network:
        try:
            st += client.get_waveforms(network.code, station.code, "*", "HH?", t - 5 * 60, t + 30 * 60, attach_response=True)
        except:
            pass
#print(type(st))
#print(st)
#st.select(component="Z").plot(bgcolor="#FF5733")
        return ('mo')
    elif depth < 6.0:
        return ('yo')
    elif depth < 8.0:
        return ('go')
    elif depth < 10.0:
        return ('co')
    else:
        return ('bo')

#---DATA_FROM_FDSN---#
#fetch station:SIOM information
client = Client(base_url = "http://arclink.ethz.ch", user='******', password='******')
starttime = UTCDateTime("2014-01-01")
endtime = UTCDateTime()
inv = client.get_stations(network="CH", station="SIOM", starttime=starttime, endtime=endtime, level="station") #choose between other level, like: "network" "channel" "response"
print(type(inv)) #Inventory
print(inv)
network = inv[0]
print(network)
station = network[0]
print(station)
#inv.plot(projection="local")
centerlat = station.latitude
centerlong = station.longitude

#get specified event
cat = client.get_events(starttime=starttime, endtime=endtime, latitude=centerlat, longitude=centerlong, maxradius=1, minmagnitude=1)#, filename="sion_events.xml")
#print len(cat)
evtnum = cat.count()
print(type(cat)) #Catalog
Exemplo n.º 57
0
class Concierge(object):
    """
    ISPAQ Data Access Expediter.

    :type user_request: :class:`~ispaq.concierge.user_request`
    :param user_request: User request containing the combination of command-line
        arguments and information from the parsed user preferences file.

    :rtype: :class:`~ispaq.concierge` or ``None``
    :return: ISPAQ Concierge.

    .. rubric:: Example

    TODO:  include doctest examples
    """
    def __init__(self, user_request=None, logger=None):
        """
        Initializes the ISPAQ data access expediter.

        See :mod:`ispaq.concierge` for all parameters.
        """
        # Keep the entire UserRequest and logger
        self.user_request = user_request
        self.logger = logger
        
        # Copy important UserRequest properties to the Concierge for smpler access
        self.requested_starttime = user_request.requested_starttime
        self.requested_endtime = user_request.requested_endtime
        self.metric_names = user_request.metrics
        self.sncl_patterns = user_request.sncls
        self.function_by_logic = user_request.function_by_logic
        self.logic_types = user_request.function_by_logic.keys()
        
        # Individual elements from the Preferences: section of the preferences file
        self.csv_output_dir = user_request.csv_output_dir
        self.plot_output_dir = user_request.plot_output_dir
        self.sigfigs = user_request.sigfigs
        
        # Output information
        file_base = '%s_%s_%s' % (self.user_request.requested_metric_set,
                                  self.user_request.requested_sncl_set, 
                                  self.requested_starttime.date)
        self.output_file_base = self.csv_output_dir + '/' + file_base
        
        # Availability dataframe is stored if it is read from a local file
        self.availability = None
        
        # Filtered availability dataframe is stored for potential reuse
        self.filtered_availability = None
        
        # Add dataselect clients and URLs or reference a local file
        if user_request.dataselect_url in URL_MAPPINGS.keys():
            # Get data from FDSN dataselect service
            self.dataselect_url = URL_MAPPINGS[user_request.dataselect_url]
            self.dataselect_client = Client(user_request.dataselect_url)
        else:
            if os.path.exists(os.path.abspath(user_request.dataselect_url)):
                # Get data from local miniseed files
                self.dataselect_url = os.path.abspath(user_request.dataselect_url)
                self.dataselect_client = None
            else:
                err_msg = "Cannot find preference file dataselect_url: '%s'" % user_request.dataselect_url
                self.logger.error(err_msg)
                raise ValueError(err_msg)

        # Add event clients and URLs or reference a local file
        if user_request.event_url in URL_MAPPINGS.keys():
            self.event_url = URL_MAPPINGS[user_request.event_url]
            self.event_client = Client(user_request.event_url)
        else:
            if os.path.exists(os.path.abspath(user_request.event_url)):
                # Get data from local QUAKEML files
                self.event_url = os.path.abspath(user_request.event_url)
                self.event_client = None
            else:
                err_msg = "Cannot find preference file event_url: '%s'" % user_request.event_url
                self.logger.error(err_msg)
                raise ValueError(err_msg)

        # Add station clients and URLs or reference a local file
        if user_request.station_url in URL_MAPPINGS.keys():
            self.station_url = URL_MAPPINGS[user_request.station_url]
            self.station_client = Client(user_request.station_url)
        else:
            if os.path.exists(os.path.abspath(user_request.station_url)):
                # Get data from local StationXML files
                self.station_url = os.path.abspath(user_request.station_url)
                self.station_client = None
            else:
                err_msg = "Cannot find preference file station_url: '%s'" % user_request.station_url
                self.logger.error(err_msg)
                raise ValueError(err_msg)



    def get_availability(self,
                         network=None, station=None, location=None, channel=None,
                         starttime=None, endtime=None, includerestricted=None,
                         latitude=None, longitude=None, minradius=None, maxradius=None):
        """
        ################################################################################
        # getAvailability method returns a dataframe with information from the output
        # of the fdsn station web service with "format=text&level=channel".
        # With additional parameters, this webservice returns information on all
        # matching SNCLs that have available data.
        #
        # The fdsnws/station/availability web service will return space characters for location
        # codes that are SPACE SPACE.
        #
        #   http://service.iris.edu/fdsnws/station/1/
        #
        # #Network | Station | Location | Channel | Latitude | Longitude | Elevation | Depth | Azimuth | Dip | Instrument | Scale | ScaleFreq | ScaleUnits | SampleRate | StartTime | EndTime
        # CU|ANWB|00|LHZ|17.66853|-61.78557|39.0|0.0|0.0|-90.0|Streckeisen STS-2 Standard-gain|2.43609E9|0.05|M/S|1.0|2010-02-10T18:35:00|2599-12-31T23:59:59
        #
        ################################################################################
        
        if (!isGeneric("getAvailability")) {
          setGeneric("getAvailability", function(obj, network, station, location, channel,
                                                 starttime, endtime, includerestricted,
                                                 latitude, longitude, minradius, maxradius) {
            standardGeneric("getAvailability")
          })
        }
        
        # END of R documentation


        Returns a dataframe of SNCLs available from the `station_url` source
        specified in the `user_request` object used to initialize the
        `Concierge`.

        By default, information in the `user_request` is used to generate
        a FDSN webservices request for station data. Where arguments are
        provided, these are used to override the information found in
        `user_request.

        :type network: str
        :param network: Select one or more network codes. Can be SEED network
            codes or data center defined codes. Multiple codes are
            comma-separated.
        :type station: str
        :param station: Select one or more SEED station codes. Multiple codes
            are comma-separated.
        :type location: str
        :param location: Select one or more SEED location identifiers. Multiple
            identifiers are comma-separated. As a special case ``"--"`` (two
            dashes) will be translated to a string of two space characters to
            match blank location IDs.
        :type channel: str
        :param channel: Select one or more SEED channel codes. Multiple codes
            are comma-separated.
        :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param starttime: Limit to metadata epochs starting on or after the
            specified start time.
        :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param endtime: Limit to metadata epochs ending on or before the
            specified end time.
        :type includerestricted: bool
        :param includerestricted: Specify if results should include information
            for restricted stations.
        :type latitude: float
        :param latitude: Specify the latitude to be used for a radius search.
        :type longitude: float
        :param longitude: Specify the longitude to the used for a radius
            search.
        :type minradius: float
        :param minradius: Limit results to stations within the specified
            minimum number of degrees from the geographic point defined by the
            latitude and longitude parameters.
        :type maxradius: float
        :param maxradius: Limit results to stations within the specified
            maximum number of degrees from the geographic point defined by the
            latitude and longitude parameters.

        #.. rubric:: Example

        #>>> my_request =  UserRequest(dummy=True)
        #>>> concierge = Concierge(my_request)
        #>>> concierge.get_availability() #doctest: +ELLIPSIS
        #[u'US.OXF..BHE', u'US.OXF..BHN', u'US.OXF..BHZ']
        """

        # NOTE:  Building the availability dataframe from a large StationXML is time consuming.
        # NOTE:  If we are using local station data then we should only do this once.
        
        # Special case when using all defaults helps speed up any metrics making mutiple calls to get_availability
        if (network is None and
            station is None and
            location is None and
            channel is None and
            starttime is None and
            endtime is None and
            self.filtered_availability is not None):
            return(self.filtered_availability)
        
        # Read from a local StationXML file one time only
        if self.station_client is None:
            
            # Only read/parse if we haven't already done so
            if self.availability is None:
                try:
                    self.logger.info("Reading StationXML file %s" % self.station_url)
                    sncl_inventory = obspy.read_inventory(self.station_url)
                except Exception as e:
                    err_msg = "The StationXML file: '%s' is not valid" % self.station_url
                    self.logger.debug(e)
                    self.logger.error(err_msg)   
                    raise ValueError(err_msg)
                
                self.logger.debug('Building availability dataframe...')
                
                # Set up empty dataframe
                df = pd.DataFrame(columns=("network", "station", "location", "channel",
                                           "latitude", "longitude", "elevation", "depth" ,
                                           "azimuth", "dip", "instrument",
                                           "scale", "scalefreq", "scaleunits", "samplerate",
                                           "starttime", "endtime", "snclId"))

                # Walk through the Inventory object
                for n in sncl_inventory.networks:
                    for s in n.stations:
                        for c in s.channels:
                            snclId = n.code + "." + s.code + "." + c.location_code + "." + c.code
                            df.loc[len(df)] = [n.code, s.code, c.location_code, c.code,
                                               c.latitude, c.longitude, c.elevation, c.depth,
                                               c.azimuth, c.dip, c.sensor.description,
                                               None,     # TODO:  Figure out how to get instrument 'scale'
                                               None,     # TODO:  Figure out how to get instrument 'scalefreq'
                                               None,     # TODO:  Figure out how to get instrument 'scaleunits'
                                               c.sample_rate,
                                               c.start_date, c.end_date, snclId]

                # Save this dataframe internally
                self.logger.debug('Finished creating availability dataframe')
                self.availability = df
            

        # Container for all of the individual sncl_pattern dataframes generated
        sncl_pattern_dataframes = []
        
        # Loop through all sncl_patterns ---------------------------------------
        
        for sncl_pattern in self.sncl_patterns:
            
            # Get "User Reqeust" parameters
            (UR_network, UR_station, UR_location, UR_channel) = sncl_pattern.split('.')

            # Allow arguments to override UserRequest parameters
            if starttime is None:
                _starttime = self.requested_starttime
            else:
                _starttime = starttime
            if endtime is None:
                _endtime = self.requested_endtime
            else:
                _endtime = endtime
            if network is None:
                _network = UR_network
            else:
                _network = network
            if station is None:
                _station = UR_station
            else:
                _station = station
            if location is None:
                _location = UR_location
            else:
                _location = location
            if channel is None:
                _channel = UR_channel
            else:
                _channel = channel
                
            _sncl_pattern = "%s.%s.%s.%s" % (_network,_station,_location,_channel)

            # Get availability dataframe ---------------------------------------
            
            if self.station_client is None:
                # Use internal dataframe
                df = self.availability
                
            else:
                # Read from FDSN web services
                try:
                    sncl_inventory = self.station_client.get_stations(starttime=_starttime, endtime=_endtime,
                                                                      network=_network, station=_station,
                                                                      location=_location, channel=_channel,
                                                                      includerestricted=None,
                                                                      latitude=latitude, longitude=longitude,
                                                                      minradius=minradius, maxradius=maxradius,                                                                
                                                                      level="channel")
                except Exception as e:
                    err_msg = "No sncls matching %s found at %s" % (_sncl_pattern, self.station_url)
                    self.logger.debug(e)
                    self.logger.warning(err_msg)
                    continue


                self.logger.debug('Building availability dataframe...')

                # Set up empty dataframe
                df = pd.DataFrame(columns=("network", "station", "location", "channel",
                                           "latitude", "longitude", "elevation", "depth" ,
                                           "azimuth", "dip", "instrument",
                                           "scale", "scalefreq", "scaleunits", "samplerate",
                                           "starttime", "endtime", "snclId"))
    
                # Walk through the Inventory object
                for n in sncl_inventory.networks:
                    for s in n.stations:
                        for c in s.channels:
                            snclId = n.code + "." + s.code + "." + c.location_code + "." + c.code
                            df.loc[len(df)] = [n.code, s.code, c.location_code, c.code,
                                               c.latitude, c.longitude, c.elevation, c.depth,
                                               c.azimuth, c.dip, c.sensor.description,
                                               None,     # TODO:  Figure out how to get instrument 'scale'
                                               None,     # TODO:  Figure out how to get instrument 'scalefreq'
                                               None,     # TODO:  Figure out how to get instrument 'scaleunits'
                                               c.sample_rate,
                                               c.start_date, c.end_date, snclId]
            
            
            # Subset availability dataframe based on _sncl_pattern -------------
            
            # NOTE:  This shouldn't be necessary for dataframes obtained from FDSN
            # NOTE:  but it's quick so we always do it
            
            # Create python regex from _sncl_pattern
            # NOTE:  Replace '.' first before introducing '.*' or '.'!
            py_pattern = _sncl_pattern.replace('.','\\.').replace('*','.*').replace('?','.')
            
            # Filter dataframe
            df = df[df.snclId.str.contains(py_pattern)]
            
            
            # Subset based on locally available data ---------------------------
            
            if self.dataselect_client is None:
                filename = '%s.%s.%s.%s.%s' % (_network, _station, _location, _channel, _starttime.strftime('%Y.%j'))
                filepattern = self.dataselect_url + '/' + filename + '*' # Allow for possible quality codes
                matching_files = glob.glob(filepattern)
                if (len(matching_files) == 0):
                    err_msg = "No local waveforms matching %s" % filepattern
                    self.logger.debug(err_msg)
                    continue
                else:
                    # Create a mask based on available file names
                    mask = df.snclId.str.contains("MASK WITH ALL FALSE")
                    for i in range(len(matching_files)):
                        basename = os.path.basename(matching_files[i])
                        match = re.match('[^\\.]*\\.[^\\.]*\\.[^\\.]*\\.[^\\.]*',basename)
                        sncl = match.group(0)
                        py_pattern = sncl.replace('.','\\.')
                        mask = mask | df.snclId.str.contains(py_pattern)
                        
                # Subset based on the mask
                df = df[mask]
                                

            # Append this dataframe
            if df.shape[0] == 0:
                self.logger.debug("No SNCLS found matching '%s'" % _sncl_pattern)
            else:
                sncl_pattern_dataframes.append(df)
                            
        # END of sncl_patterns loop --------------------------------------------
        
        if len(sncl_pattern_dataframes) == 0:
            err_msg = "No available waveforms matching" + str(self.sncl_patterns)
            self.logger.info(err_msg)
            raise NoAvailableDataError(err_msg)
        
        else:
            availability = pd.concat(sncl_pattern_dataframes, ignore_index=True)
            
            # TODO: remove duplicates       
               
            if availability.shape[0] == 0:              
                err_msg = "No available waveforms matching" + str(self.sncl_patterns)
                self.logger.info(err_msg)
                raise NoAvailableDataError(err_msg)
            else:
                # The concierge should remember this dataframe for metrics that
                # make multiple calls to get_availability with all defaults.
                self.filtered_availability = availability
                return availability
    

    def get_dataselect(self,
                       network=None, station=None, location=None, channel=None,
                       starttime=None, endtime=None, quality="B",
                       inclusiveEnd=True, ignoreEpoch=False):
        """
        Returns an R Stream that can be passed to metrics calculation methods.

        All arguments are required except for starttime and endtime. These arguments
        may be specified but will default to the time information found in the
        `user_request` used to generate a FDSN webservices request for MINIseed data.

        :type network: str
        :param network: Select one or more network codes. Can be SEED network
            codes or data center defined codes. Multiple codes are
            comma-separated.
        :type station: str
        :param station: Select one or more SEED station codes. Multiple codes
            are comma-separated.
        :type location: str
        :param location: Select one or more SEED location identifiers. Multiple
            identifiers are comma-separated. As a special case ``"--"`` (two
            dashes) will be translated to a string of two space characters to
            match blank location IDs.
        :type channel: str
        :param channel: Select one or more SEED channel codes. Multiple codes
            are comma-separated.
        :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param starttime: Limit to metadata epochs starting on or after the
            specified start time.
        :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param endtime: Limit to metadata epochs ending on or before the
            specified end time.
        """

        # Allow arguments to override UserRequest parameters
        if starttime is None:
            _starttime = self.requested_starttime
        else:
            _starttime = starttime
        if endtime is None:
            _endtime = self.requested_endtime
        else:
            _endtime = endtime

        if self.dataselect_client is None:
            # Read local MINIseed file and convert to R_Stream
            filename = '%s.%s.%s.%s.%s' % (network, station, location, channel, _starttime.strftime('%Y.%j'))
            filepattern = self.dataselect_url + '/' + filename + '*' # Allow for possible quality codes
            matching_files = glob.glob(filepattern)
            
            if (len(matching_files) == 0):
                self.logger.info("No files found matching '%s'" % (filepattern))
                
            else:
                filepath = matching_files[0]
                if (len(matching_files) > 1):
                    self.logger.warning("Multiple files found matching" '%s -- using %s' % (filepattern, filepath))
                try:
                    # Get the ObsPy version of the stream
                    py_stream = obspy.read(filepath)
                    py_stream = py_stream.slice(_starttime, _endtime)
                    # NOTE:  ObsPy does not store state-of-health flags with each stream.
                    # NOTE:  We need to read them in separately from the miniseed file.
                    flag_dict = obspy.io.mseed.util.get_timing_and_data_quality(filepath)
                    act_flags = [0,0,0,0,0,0,0,0] # TODO:  Find a way to read act_flags
                    io_flags = [0,0,0,0,0,0,0,0] # TODO:  Find a way to read io_flags
                    dq_flags = flag_dict['data_quality_flags']
                    # NOTE:  ObsPy does not store station metadata with each trace.
                    # NOTE:  We need to read them in separately from station metadata.
                    availability = self.get_availability(network, station, location, channel, _starttime, _endtime)
                    sensor = availability.instrument[0]
                    scale = availability.scale[0]
                    scalefreq = availability.scalefreq[0]
                    scaleunits = availability.scaleunits[0]
                    if sensor is None: sensor = ""           # default from IRISSeismic Trace class prototype
                    if scale is None: scale = 1.0            # default from IRISSeismic Trace class prototype
                    if scalefreq is None: scalefreq = 1.0    # default from IRISSeismic Trace class prototype
                    if scaleunits is None: scaleunits = ""   # default from IRISSeismic Trace class prototype
                    latitude = availability.latitude[0]
                    longitude = availability.longitude[0]
                    elevation = availability.elevation[0]
                    depth = availability.depth[0]
                    azimuth = availability.azimuth[0]
                    dip = availability.dip[0]
                    # Create the IRISSeismic version of the stream
                    r_stream = irisseismic.R_Stream(py_stream, _starttime, _endtime, act_flags, io_flags, dq_flags,
                                                    sensor, scale, scalefreq, scaleunits, latitude, longitude, elevation, depth, azimuth, dip)
                except Exception as e:
                    err_msg = "Error reading in local waveform from %s" % filepath
                    self.logger.debug(e)
                    self.logger.error(err_msg)
                    raise
        else:
            # Read from FDSN web services
            try:
                r_stream = irisseismic.R_getDataselect(self.dataselect_url, network, station, location, channel, _starttime, _endtime, quality, inclusiveEnd, ignoreEpoch)
            except Exception as e:
                err_msg = "Error reading in waveform from %s webservice" % self.dataselect_client
                self.logger.debug(e)
                self.logger.error(err_msg)
                raise

           
        # TODO:  Do we need to test for valid R_Stream.
        if False:              
            return None # TODO:  raise an exception
        else:
            return r_stream


    def get_event(self,
                  starttime=None, endtime=None,
                  minmag=5.5, maxmag=None, magtype=None,
                  mindepth=None, maxdepth=None):
        """
        ################################################################################
        # getEvent method returns seismic event data from the event webservice:
        #
        #   http://service.iris.edu/fdsnws/event/1/
        #
        # TODO:  The getEvent method could be fleshed out with a more complete list
        # TODO:  of arguments to be used as ws-event parameters.
        ################################################################################
        
        # http://service.iris.edu/fdsnws/event/1/query?starttime=2013-02-01T00:00:00&endtime=2013-02-02T00:00:00&minmag=5&format=text
        #
        # #EventID | Time | Latitude | Longitude | Depth | Author | Catalog | Contributor | ContributorID | MagType | Magnitude | MagAuthor | EventLocationName
        # 4075900|2013-02-01T22:18:33|-11.12|165.378|10.0|NEIC|NEIC PDE|NEIC PDE-Q||MW|6.4|GCMT|SANTA CRUZ ISLANDS

        if (!isGeneric("getEvent")) {
          setGeneric("getEvent", function(obj, starttime, endtime, minmag, maxmag, magtype,
                                          mindepth, maxdepth) {
            standardGeneric("getEvent")
          })
        }

        # END of R documentation


        Returns a dataframe of events returned by the `event_url` source
        specified in the `user_request` object used to initialize the
        `Concierge`.

        By default, information in the `user_request` is used to generate
        a FDSN webservices request for event data. Where arguments are
        provided, these are used to override the information found in
        `user_request.

        :type starttime: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param starttime: Limit to metadata epochs starting on or after the
            specified start time.
        :type endtime: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param endtime: Limit to metadata epochs ending on or before the
            specified end time.
        :type minmagnitude: float, optional
        :param minmagnitude: Limit to events with a magnitude larger than the
            specified minimum.
        :type maxmagnitude: float, optional
        :param maxmagnitude: Limit to events with a magnitude smaller than the
            specified maximum.
        :type magnitudetype: str, optional
        :param magnitudetype: Specify a magnitude type to use for testing the
            minimum and maximum limits.
        :type mindepth: float, optional
        :param mindepth: Limit to events with depth, in kilometers, larger than
            the specified minimum.
        :type maxdepth: float, optional
        :param maxdepth: Limit to events with depth, in kilometers, smaller
            than the specified maximum.

        #.. rubric:: Example

        #>>> my_request =  UserRequest(dummy=True)
        #>>> concierge = Concierge(my_request)
        #>>> concierge.get_event() #doctest: +ELLIPSIS
        '
         eventId                         time  latitude  longitude  depth author...'
        """

        # Allow arguments to override UserRequest parameters
        if starttime is None:
            _starttime = self.requested_starttime
        else:
            _starttime = starttime
        if endtime is None:
            _endtime = self.requested_endtime
        else:
            _endtime = endtime


        if self.event_client is None:
            # Read local QuakeML file
            try:
                event_catalog = obspy.read_events(self.event_url)
            except Exception as e:
                err_msg = "The StationXML file: '%s' is not valid." % self.station_url
                self.logger.debug(e)
                self.logger.error(err_msg)
                raise ValueError(err_msg)
            
            # events.columns
            # Index([u'eventId', u'time', u'latitude', u'longitude', u'depth', u'author',
            #        u'cCatalog', u'contributor', u'contributorId', u'magType', u'magnitude',
            #        u'magAuthor', u'eventLocationName'],
            #        dtype='object')
            #
            dataframes = []
            
            for event in event_catalog:
                origin = event.preferred_origin()
                magnitude = event.preferred_magnitude()
                df = pd.DataFrame({'eventId': re.sub('.*eventid=','',event.resource_id.id),
                                   'time': origin.time,
                                   'latitude': origin.latitude,
                                   'longitude': origin.longitude,
                                   'depth': origin.depth/1000, # IRIS event webservice returns depth in km # TODO:  check this
                                   'author': origin.creation_info.author,
                                   'cCatalog': None,
                                   'contributor': None,
                                   'contributorId': None,
                                   'magType': magnitude.magnitude_type,
                                   'magnitude': magnitude.mag,
                                   'magAuthor': magnitude.creation_info.author,
                                   'eventLocationName': event.event_descriptions[0].text},
                                  index=[0])
                dataframes.append(df)
                
            # Concatenate into the events dataframe
            events = pd.concat(dataframes, ignore_index=True)    

        else:
            # Read from FDSN web services
            # TODO:  Need to make sure irisseismic.getEvent uses any FDSN site
            try:
                events = irisseismic.getEvent(starttime=_starttime,
                                              endtime=_endtime,
                                              minmag=minmag,
                                              maxmag=maxmag,
                                              magtype=magtype,
                                              mindepth=mindepth,
                                              maxdepth=maxdepth)

            except Exception as e:
                err_msg = "The event_url: '%s' returns an error" % (self.event_url)
                self.logger.debug(e)
                self.logger.error(err_msg)
                raise


        if events.shape[0] == 0:
            return None # TODO:  raise an exception
        else:
            return events