Ejemplo n.º 1
0
    def test_geolocation(self):
        """Station GE.*.*.* with latitude between -10 and 10"""

        expURL = 'http://geofon.gfz-potsdam.de/fdsnws/station/1/query'
        result = self.rc.getRoute(Stream('GE', '*', '*', '*'),
                                  TW(None, None),
                                  'station',
                                  geoLoc=geoRectangle(-10, 10, -180, 180))
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')
        self.assertEqual(len(result), 1,
                         'Wrong number of data centers for GE.*.*.*!')
        self.assertEqual(result[0]['url'], expURL, 'Wrong URL for GE.*.*.*')
        self.assertEqual(result[0]['name'], 'station', 'Wrong service name!')

        queryparams = '?net={net}&sta={sta}&loc={loc}&cha={cha}&minlat=-10&maxlat=10&format=text'
        for st in result[0]['params']:
            req = ul.Request(expURL + queryparams.format_map(st))
            try:
                u = ul.urlopen(req)
                buffer = u.read().decode('utf-8')
            except:
                raise Exception(
                    'Error retrieving GE stations with latitude between -10 and 10'
                )

            for line in buffer.splitlines():
                if line.startswith('#'):
                    continue

                self.assertGreaterEqual(float(line.split('|')[2]), -10.0,
                                        'Latitude smaller than -10.0!')
                self.assertLessEqual(float(line.split('|')[2]), 10.0,
                                     'Latitude bigger than 10.0!')
                break
Ejemplo n.º 2
0
    def test2services_GE_DS_ST_WF(self):
        """Dataselect AND Station AND WFCatalog GE.*.*.*"""

        expURL_WF = 'http://geofon.gfz-potsdam.de/eidaws/wfcatalog/1/query'
        expURL_DS = 'http://geofon.gfz-potsdam.de/fdsnws/dataselect/1/query'
        expURL_ST = 'http://geofon.gfz-potsdam.de/fdsnws/station/1/query'
        result = self.rc.getRoute(Stream('GE', '*', '*', '*'),
                                  TW(None, None),
                                  service='dataselect,station,wfcatalog')
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')
        self.assertEqual(len(result), 3,
                         'Wrong number of data centers for GE.*.*.*!')
        self.assertIn(expURL_DS,
                      [result[0]['url'], result[1]['url'], result[2]['url']],
                      'Wrong URL for GE.*.*.*')
        self.assertIn(expURL_ST,
                      [result[0]['url'], result[1]['url'], result[2]['url']],
                      'Wrong URL for GE.*.*.*')
        self.assertIn(expURL_WF,
                      [result[0]['url'], result[1]['url'], result[2]['url']],
                      'Wrong URL for GE.*.*.*')
        self.assertIn(
            'dataselect',
            [result[0]['name'], result[1]['name'], result[2]['name']],
            'Wrong service name!')
        self.assertIn(
            'station',
            [result[0]['name'], result[1]['name'], result[2]['name']],
            'Wrong service name!')
        self.assertIn(
            'wfcatalog',
            [result[0]['name'], result[1]['name'], result[2]['name']],
            'Wrong service name!')
Ejemplo n.º 3
0
    def testDS_XXX(self):
        """Non-existing network XXX"""

        try:
            result = self.rc.getRoute(Stream('XXX', '*', '*', '*'),
                                      TW(None, None))
        except RoutingException:
            return

        self.assertTrue(False, 'A RoutingException was expected!')
Ejemplo n.º 4
0
    def testDS_GE_RO(self):
        """Dataselect GE,RO.*.*.*"""

        expURL_GE = 'http://geofon.gfz-potsdam.de/fdsnws/dataselect/1/query'
        expURL_RO = 'http://eida-sc3.infp.ro/fdsnws/dataselect/1/query'

        result = self.rc.getRoute(Stream('GE', '*', '*', '*'), TW(None, None))
        result.extend(
            self.rc.getRoute(Stream('RO', '*', '*', '*'), TW(None, None)))
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')
        self.assertEqual(len(result), 2,
                         'Wrong number of data centers for GE,RO.*.*.*!')
        self.assertEqual(result[0]['url'], expURL_GE, 'Wrong URL for GE.*.*.*')
        self.assertEqual(result[0]['name'], 'dataselect',
                         'Wrong service name!')
        self.assertEqual(result[1]['url'], expURL_RO, 'Wrong URL for RO.*.*.*')
        self.assertEqual(result[1]['name'], 'dataselect',
                         'Wrong service name!')
Ejemplo n.º 5
0
    def test_wrong_datetime(self):
        """Swap start and end time."""

        d1 = datetime.datetime(2004, 1, 1)
        d2 = d1 - datetime.timedelta(days=1)

        try:
            result = self.rc.getRoute(Stream('GE', '*', '*', '*'), TW(d1, d2))
        except RoutingException:
            return

        self.assertTrue(False, 'A RoutingException was expected!')
Ejemplo n.º 6
0
    def testDS_GE(self):
        """Dataselect GE.*.*.*"""

        expURL = 'http://geofon.gfz-potsdam.de/fdsnws/dataselect/1/query'
        result = self.rc.getRoute(Stream('GE', '*', '*', '*'), TW(None, None))
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')
        self.assertEqual(len(result), 1,
                         'Wrong number of data centers for GE.*.*.*!')
        self.assertEqual(result[0]['url'], expURL, 'Wrong URL for GE.*.*.*')
        self.assertEqual(result[0]['name'], 'dataselect',
                         'Wrong service name!')
Ejemplo n.º 7
0
    def testDS_ZE(self):
        """Dataselect ZE.*.*.*"""

        expURL = 'http://geofon.gfz-potsdam.de/fdsnws/dataselect/1/query'
        result = self.rc.getRoute(Stream('ZE', '*', '*', '*'), TW(None, None))
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')
        self.assertEqual(len(result), 1,
                         'Wrong number of data centers for GE.*.*.*!')
        self.assertEqual(len(result[0]['params']), 4,
                         '4 epochs are expected from the ZE network')
        self.assertEqual(result[0]['name'], 'dataselect',
                         'Wrong service name!')
Ejemplo n.º 8
0
    def testDS_RO_BZS_BHZ(self):
        """Dataselect RO.BZS.*.BHZ"""

        expURL = 'http://eida-sc3.infp.ro/fdsnws/dataselect/1/query'
        result = self.rc.getRoute(Stream('RO', 'BZS', '*', 'BHZ'),
                                  TW(None, None))
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')
        self.assertEqual(len(result), 1,
                         'Wrong number of data centers for RO.BZS.*.BHZ!')
        self.assertEqual(result[0]['url'], expURL,
                         'Wrong URL for RO.BZS.*.BHZ!')
        self.assertEqual(result[0]['name'], 'dataselect',
                         'Wrong service name!')
Ejemplo n.º 9
0
    def testDS_CH_LIENZ_BHZ(self):
        """Dataselect CH.LIENZ.*.BHZ"""

        expURL = 'http://eida.ethz.ch/fdsnws/dataselect/1/query'
        result = self.rc.getRoute(Stream('CH', 'LIENZ', '*', 'BHZ'),
                                  TW(None, None))
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')
        self.assertEqual(len(result), 1,
                         'Wrong number of data centers for CH.LIENZ.*.BHZ!')
        self.assertEqual(result[0]['url'], expURL,
                         'Wrong URL for CH.LIENZ.*.BHZ!')
        self.assertEqual(result[0]['name'], 'dataselect',
                         'Wrong service name!')
Ejemplo n.º 10
0
    def test2services_GE_DS_ST_WF_FDSN(self):
        """Dataselect AND Station AND WFCatalog GE.*.*.* in FDSN format"""

        result = self.rc.getRoute(Stream('GE', '*', '*', '*'),
                                  TW(None, None),
                                  service='dataselect,wfcatalog,station')
        fdsnresult = FDSNRules(result)
        self.assertIsInstance(fdsnresult, FDSNRules,
                              'A FDSNRules object was expected!')
        self.assertEqual(
            len(fdsnresult['datacenters'][0]['repositories'][0]
                ['timeseriesRouting'][0]['services']), 0,
            'Wrong number of services for GE.*.*.*! An empty list is expected.'
        )
Ejemplo n.º 11
0
    def testDS_GE_FDSN_output(self):
        """Dataselect GE.*.*.* start=2010 format=fdsn"""

        expURL = 'http://geofon.gfz-potsdam.de/fdsnws/dataselect/1/'
        startD = datetime.datetime(2010, 1, 1)
        result = self.rc.getRoute(Stream('GE', '*', '*', '*'),
                                  TW(startD, None))
        self.assertIsInstance(result, RequestMerge,
                              'A RequestMerge object was expected!')

        fdsnResult = FDSNRules(result)
        self.assertEqual(len(fdsnResult['datacenters']), 1,
                         'Wrong number of data centers for GE.*.*.*!')
        tsr = fdsnResult['datacenters'][0]['repositories'][0][
            'timeseriesRouting']
        self.assertEqual(len(tsr), 1, 'Wrong number of rules for GE.*.*.*!')
        self.assertEqual(tsr[0]['network'], 'GE', 'Wrong network code')
        self.assertEqual(tsr[0]['services'][0]['url'], expURL,
                         'Wrong URL for GE.*.*.*')
        self.assertEqual(tsr[0]['services'][0]['name'], 'fdsnws-dataselect',
                         'Wrong service name!')
Ejemplo n.º 12
0
    def test2services_GE_ST_WF_FDSN(self):
        """Station AND WFCatalog GE.*.*.* in FDSN format"""

        expURL_WF = 'http://geofon.gfz-potsdam.de/eidaws/wfcatalog/1/'
        expURL_ST = 'http://geofon.gfz-potsdam.de/fdsnws/station/1/'
        result = self.rc.getRoute(Stream('GE', '*', '*', '*'),
                                  TW(None, None),
                                  service='wfcatalog,station')
        fdsnresult = FDSNRules(result)
        self.assertIsInstance(fdsnresult, FDSNRules,
                              'A FDSNRules object was expected!')
        self.assertEqual(
            len(fdsnresult['datacenters'][0]['repositories'][0]
                ['timeseriesRouting'][0]['services']), 2,
            'Wrong number of services for GE.*.*.*!')
        self.assertIn(expURL_WF, [
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][0]['url'],
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][1]['url']
        ], 'WFCatalog URL not found for GE.*.*.*')
        self.assertIn(expURL_ST, [
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][0]['url'],
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][1]['url']
        ], 'StationWS URL not found for GE.*.*.*')
        self.assertIn('eidaws-wfcatalog', [
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][0]['name'],
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][1]['name']
        ], 'WFCatalog name not found for GE.*.*.*')
        self.assertIn('fdsnws-station', [
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][0]['name'],
            fdsnresult['datacenters'][0]['repositories'][0]
            ['timeseriesRouting'][0]['services'][1]['name']
        ], 'StationWS name not found for GE.*.*.*')
Ejemplo n.º 13
0
def makeQueryGET(parameters):
    """Process a request made via a GET method."""
    global routes

    # List all the accepted parameters
    allowedParams = ['net', 'network',
                     'sta', 'station',
                     'loc', 'location',
                     'cha', 'channel',
                     'start', 'starttime',
                     'end', 'endtime',
                     'minlat', 'minlatitude',
                     'maxlat', 'maxlatitude',
                     'minlon', 'minlongitude',
                     'maxlon', 'maxlongitude',
                     'service', 'format',
                     'alternative']

    for param in parameters:
        if param not in allowedParams:
            msg = 'Unknown parameter: %s' % param
            raise WIClientError(msg)

    try:
        net = getParam(parameters, ['net', 'network'], '*', csv=True)
        sta = getParam(parameters, ['sta', 'station'], '*', csv=True)
        loc = getParam(parameters, ['loc', 'location'], '*', csv=True)
        cha = getParam(parameters, ['cha', 'channel'], '*', csv=True)
        start = getParam(parameters, ['start', 'starttime'], None)
    except Exception as e:
        raise WIClientError(str(e))

    try:
        if start is not None:
            start = str2date(start)
    except:
        msg = 'Error while converting starttime parameter.'
        raise WIClientError(msg)

    endt = getParam(parameters, ['end', 'endtime'], None)
    try:
        if endt is not None:
            endt = str2date(endt)
    except:
        msg = 'Error while converting endtime parameter.'
        raise WIClientError(msg)

    try:
        minlat = float(getParam(parameters, ['minlat', 'minlatitude'],
                                '-90.0'))
    except:
        msg = 'Error while converting the minlatitude parameter.'
        raise WIClientError(msg)

    try:
        maxlat = float(getParam(parameters, ['maxlat', 'maxlatitude'],
                                '90.0'))
    except:
        msg = 'Error while converting the maxlatitude parameter.'
        raise WIClientError(msg)

    try:
        minlon = float(getParam(parameters, ['minlon', 'minlongitude'],
                                '-180.0'))
    except:
        msg = 'Error while converting the minlongitude parameter.'
        raise WIClientError(msg)

    try:
        maxlon = float(getParam(parameters, ['maxlon', 'maxlongitude'],
                                '180.0'))
    except:
        msg = 'Error while converting the maxlongitude parameter.'
        raise WIClientError(msg)

    ser = getParam(parameters, ['service'], 'dataselect').lower()
    aux = getParam(parameters, ['alternative'], 'false').lower()
    if aux == 'true':
        alt = True
    elif aux == 'false':
        alt = False
    else:
        msg = 'Wrong value passed in parameter "alternative"'
        raise WIClientError(msg)

    form = getParam(parameters, ['format'], 'xml').lower()

    if ((alt) and (form == 'get')):
        msg = 'alternative=true and format=get are incompatible parameters'
        raise WIClientError(msg)

    # print start, type(start), endt, type(endt), (start > endt)
    if ((start is not None) and (endt is not None) and (start > endt)):
        msg = 'Start datetime cannot be greater than end datetime'
        raise WIClientError(msg)

    if ((minlat == -90.0) and (maxlat == 90.0) and (minlon == -180.0) and
            (maxlon == 180.0)):
        geoLoc = None
    else:
        geoLoc = geoRectangle(minlat, maxlat, minlon, maxlon)

    result = RequestMerge()
    # Expand lists in parameters (f.i., cha=BHZ,HHN) and yield all possible
    # values
    for (n, s, l, c) in lsNSLC(net, sta, loc, cha):
        try:
            st = Stream(n, s, l, c)
            tw = TW(start, endt)
            result.extend(routes.getRoute(st, tw, ser, geoLoc, alt))
        except RoutingException:
            pass

    if len(result) == 0:
        raise WIContentError()
    return result
Ejemplo n.º 14
0
def makeQueryPOST(postText):
    """Process a request made via a POST method."""
    global routes

    # These are the parameters accepted appart from N.S.L.C
    extraParams = ['format', 'service', 'alternative',
                   'minlat', 'minlatitude',
                   'maxlat', 'maxlatitude',
                   'minlon', 'minlongitude',
                   'maxlon', 'maxlongitude']

    # Default values
    ser = 'dataselect'
    alt = False

    result = RequestMerge()
    # Check if we are still processing the header of the POST body. This has a
    # format like key=value, one per line.
    inHeader = True

    minlat = -90.0
    maxlat = 90.0
    minlon = -180.0
    maxlon = 180.0

    filterdefined = False
    for line in postText.splitlines():
        if not len(line):
            continue

        if (inHeader and ('=' not in line)):
            inHeader = False

        if inHeader:
            try:
                key, value = line.split('=')
                key = key.strip()
                value = value.strip()
            except:
                msg = 'Wrong format detected while processing: %s' % line
                raise WIClientError(msg)

            if key not in extraParams:
                msg = 'Unknown parameter "%s"' % key
                raise WIClientError(msg)

            if key == 'service':
                ser = value
            elif key == 'alternative':
                alt = True if value.lower() == 'true' else False
            elif key == 'minlat':
                minlat = float(value.lower())
            elif key == 'maxlat':
                maxlat = float(value.lower())
            elif key == 'minlon':
                minlon = float(value.lower())
            elif key == 'maxlon':
                maxlon = float(value.lower())

            continue

        # I'm already in the main part of the POST body, where the streams are
        # specified
        filterdefined = True

        net, sta, loc, cha, start, endt = line.split()
        net = net.upper()
        sta = sta.upper()
        loc = loc.upper()
        try:
            if start.strip() == '*':
                start = None
            else:
                start = str2date(start)
        except Exception:
            msg = 'Error while converting %s to datetime' % start
            raise WIClientError(msg)

        try:
            if endt.strip() == '*':
                endt = None
            else:
                endt = str2date(endt)
        except Exception:
            msg = 'Error while converting %s to datetime' % endt
            raise WIClientError(msg)

        if ((minlat == -90.0) and (maxlat == 90.0) and (minlon == -180.0) and
                (maxlon == 180.0)):
            geoLoc = None
        else:
            geoLoc = geoRectangle(minlat, maxlat, minlon, maxlon)

        try:
            st = Stream(net, sta, loc, cha)
            tw = TW(start, endt)
            result.extend(routes.getRoute(st, tw, ser, geoLoc, alt))
        except RoutingException:
            pass

    if not filterdefined:
        st = Stream('*', '*', '*', '*')
        tw = TW(None, None)
        geoLoc = None
        result.extend(routes.getRoute(st, tw, ser, geoLoc, alt))

    if len(result) == 0:
        raise WIContentError()
    return result