Esempio n. 1
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
Esempio n. 2
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