Example #1
0
def makeQueryGET(parameters):
    global routes

    # List all the accepted parameters
    allowedParams = ['net', 'network',
                     'sta', 'station',
                     'loc', 'location',
                     'cha', 'channel',
                     'start', 'starttime',
                     'end', 'endtime',
                     'service', 'format',
                     'alternative']

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

    try:
        if 'network' in parameters:
            net = parameters['network'].value.upper()
        elif 'net' in parameters:
            net = parameters['net'].value.upper()
        else:
            net = '*'

        net = net.split(',')
    except:
        net = ['*']

    try:
        if 'station' in parameters:
            sta = parameters['station'].value.upper()
        elif 'sta' in parameters:
            sta = parameters['sta'].value.upper()
        else:
            sta = '*'

        sta = sta.split(',')
    except:
        sta = ['*']

    try:
        if 'location' in parameters:
            loc = parameters['location'].value.upper()
        elif 'loc' in parameters:
            loc = parameters['loc'].value.upper()
        else:
            loc = '*'

        loc = loc.split(',')
    except:
        loc = ['*']

    try:
        if 'channel' in parameters:
            cha = parameters['channel'].value.upper()
        elif 'cha' in parameters:
            cha = parameters['cha'].value.upper()
        else:
            cha = '*'

        cha = cha.split(',')
    except:
        cha = ['*']

    try:
        if 'starttime' in parameters:
            start = parameters['starttime'].value.upper()
        elif 'start' in parameters:
            start = parameters['start'].value.upper()
        else:
            raise Exception
        startParts = start.replace('-', ' ').replace('T', ' ')
        startParts = startParts.replace(':', ' ').replace('.', ' ')
        startParts = startParts.replace('Z', '').split()
        start = datetime.datetime(*map(int, startParts))
        # if 'starttime' in parameters:
        #     start = datetime.datetime.strptime(
        #         parameters['starttime'].value[:19].upper(),
        #         '%Y-%m-%dT%H:%M:%S')
        # elif 'start' in parameters:
        #     start = datetime.datetime.strptime(
        #         parameters['start'].value[:19].upper(),
        #         '%Y-%m-%dT%H:%M:%S')
        # else:
        #     start = None
    except:
        msg = 'Error while converting starttime parameter.'
        raise WIClientError(msg)

    try:
        if 'endtime' in parameters:
            endt = parameters['endtime'].value.upper()
        elif 'end' in parameters:
            endt = parameters['end'].value.upper()
        else:
            raise Exception
        endParts = endt.replace('-', ' ').replace('T', ' ')
        endParts = endParts.replace(':', ' ').replace('.', ' ')
        endParts = endParts.replace('Z', '').split()
        endt = datetime.datetime(*map(int, endParts))
        # if 'endtime' in parameters:
        #     endt = datetime.datetime.strptime(
        #         parameters['endtime'].value[:19].upper(),
        #         '%Y-%m-%dT%H:%M:%S')
        # elif 'end' in parameters:
        #     endt = datetime.datetime.strptime(
        #         parameters['end'].value[:19].upper(),
        #         '%Y-%m-%dT%H:%M:%S')
        # else:
        #     endt = None
    except:
        msg = 'Error while converting endtime parameter.'
        raise WIClientError(msg)

    try:
        if 'service' in parameters:
            ser = parameters['service'].value.lower()
        else:
            ser = 'dataselect'
    except:
        ser = 'dataselect'

    try:
        if 'alternative' in parameters:
            if parameters['alternative'].value.lower() == 'true':
                alt = True
            elif parameters['alternative'].value.lower() == 'false':
                alt = False
            else:
                msg = 'Wrong value passed in parameter "alternative"'
                raise WIClientError(msg)
        else:
            alt = False
    except WIClientError:
        raise
    except:
        alt = False

    try:
        if 'format' in parameters:
            form = parameters['format'].value.lower()
        else:
            form = 'xml'
    except:
        form = 'xml'

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

    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)

    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:
            result.extend(routes.getRoute(n, s, l, c, start, endt, ser, alt))
        except RoutingException:
            pass

    if len(result) == 0:
        raise WIContentError()
    return result
Example #2
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
Example #3
0
def makeQueryPOST(postText):
    global routes

    # This are the parameters accepted appart from N.S.L.C
    extraParams = ['format', 'service', 'alternative']

    # Defualt 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

    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

            continue

        # I'm already in the main part of the POST body, where the streams are
        # specified
        net, sta, loc, cha, start, endt = line.split()
        net = net.upper()
        sta = sta.upper()
        loc = loc.upper()
        try:
            startParts = start.replace('-', ' ').replace('T', ' ')
            startParts = startParts.replace(':', ' ').replace('.', ' ')
            startParts = startParts.replace('Z', '').split()
            start = datetime.datetime(*map(int, startParts))
            # start = None if start in ("''", '""') else \
            #     datetime.datetime.strptime(start[:19].upper(),
            #                                '%Y-%m-%dT%H:%M:%S')
        except:
            msg = 'Error while converting %s to datetime' % start
            raise WIClientError(msg)

        try:
            endParts = endt.replace('-', ' ').replace('T', ' ')
            endParts = endParts.replace(':', ' ').replace('.', ' ')
            endParts = endParts.replace('Z', '').split()
            endt = datetime.datetime(*map(int, endParts))
            # endt = None if endt in ("''", '""') else \
            #     datetime.datetime.strptime(endt[:19].upper(),
            #                                '%Y-%m-%dT%H:%M:%S')
        except:
            msg = 'Error while converting %s to datetime' % endt
            raise WIError(msg)

        try:
            result.extend(routes.getRoute(net, sta, loc, cha,
                                          start, endt, ser, alt))
        except RoutingException:
            pass

    if len(result) == 0:
        raise WIContentError()
    return result
Example #4
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