Exemple #1
0
def plot_v1_0():
    """
    API Format: /api/v1.0/plot/?query='...'&format

    query = {
        dataset   : Dataset to extract data
        names     :
        plottitle : Title of Plot (Default if blank)
        quantum   : (year, month, day, hour)
        showmap   : Include a map of the plots location on the map
        station   : Coordinates of the point/line/area/etc
        time      : Time retrieved data was gathered/modeled
        type      : File / Plot Type (Check Navigator for Possible options)
        variable  : Type of data to plot - Options found using /api/variables/?dataset='...'
    }
    **Query must be written in JSON and converted to encodedURI**
    **Not all components of query are required
    """

    if request.method == 'GET':
        args = request.args
    else:
        args = request.form

    if "query" not in args:
        raise APIError("Please provide a query.")

    query = json.loads(args.get('query'))

    fmt = args.get('format')
    if fmt == 'json':

        def make_response(data, mime):
            b64 = base64.encodebytes(data).decode()

            return Response(json.dumps("data:%s;base64,%s" % (mime, b64)),
                            status=200,
                            mimetype="application/json")
    else:

        def make_response(data, mime):
            return Response(data, status=200, mimetype=mime)

    dataset = query.get('dataset')
    plottype = query.get('type')

    options = {
        'format': fmt,
        'size': args.get('size', '15x9'),
        'dpi': args.get('dpi', 72)
    }

    # Determine which plotter we need.
    if plottype == 'map':
        plotter = MapPlotter(dataset, query, **options)
    elif plottype == 'transect':
        plotter = TransectPlotter(dataset, query, **options)
    elif plottype == 'timeseries':
        plotter = TimeseriesPlotter(dataset, query, **options)
    elif plottype == 'ts':
        plotter = TemperatureSalinityPlotter(dataset, query, **options)
    elif plottype == 'sound':
        plotter = SoundSpeedPlotter(dataset, query, **options)
    elif plottype == 'profile':
        plotter = ProfilePlotter(dataset, query, **options)
    elif plottype == 'hovmoller':
        plotter = HovmollerPlotter(dataset, query, **options)
    elif plottype == 'observation':
        plotter = ObservationPlotter(dataset, query, **options)
    elif plottype == 'track':
        plotter = TrackPlotter(dataset, query, **options)
    elif plottype == 'class4':
        plotter = Class4Plotter(dataset, query, **options)
    elif plottype == 'stick':
        plotter = StickPlotter(dataset, query, **options)
    else:
        raise APIError(
            "You Have Not Selected a Plot Type - Please Review your Query")

    if 'data' in request.args:
        data = plotter.prepare_plot()
        return data

    img, mime, filename = plotter.run()

    if img:
        response = make_response(img, mime)
    else:
        raise FAILURE

    if 'save' in args:
        response.headers[
            'Content-Disposition'] = "attachment; filename=\"%s\"" % filename

    response.cache_control.max_age = 300

    if 'data' in args:
        plotData = {
            'data': str(resp),
            'shape': resp.shape,
            'mask': str(resp.mask)
        }
        plotData = json.dumps(plotData)
        return Response(plotData, status=200, mimetype='application/json')

    return response
Exemple #2
0
def plot():
    if request.method == "GET":
        if 'query' not in request.args:
            raise FAILURE

        query = json.loads(request.args.get('query'))
    else:
        if 'query' not in request.form:
            raise FAILURE

        query = json.loads(request.form.get('query'))

    if ("format" in request.args and request.args.get("format") == "json") or \
       ("format" in request.form and request.form.get("format") == "json"):

        # Generates a Base64 encoded string
        def make_response(data, mime):
            b64 = base64.b64encode(data)

            return Response(json.dumps("data:%s;base64,%s" % (mime, b64)),
                            status=200,
                            mimetype="application/json")
    else:

        def make_response(data, mime):
            return Response(data, status=200, mimetype=mime)

    dataset = query.get('dataset')

    opts = {
        'dpi': 72,
        'query': query,
    }
    plottype = query.get('type')

    size = None
    if 'save' in request.args:
        if 'size' in request.args:
            size = request.args.get('size')
        if 'dpi' in request.args:
            opts['dpi'] = request.args.get('dpi')

    if 'format' in request.args:
        opts['format'] = request.args.get('format')

    if size is None:
        opts['size'] = '11x9'
    else:
        opts['size'] = size

    filename = 'png'
    img = ""

    # Determine which plotter we need.
    if plottype == 'map':
        plotter = MapPlotter(dataset, query, request.args.get('format'))
    elif plottype == 'transect':
        plotter = TransectPlotter(dataset, query, request.args.get('format'))
    elif plottype == 'timeseries':
        plotter = TimeseriesPlotter(dataset, query, request.args.get('format'))
    elif plottype == 'ts':
        plotter = TemperatureSalinityPlotter(dataset, query,
                                             request.args.get('format'))
    elif plottype == 'sound':
        plotter = SoundSpeedPlotter(dataset, query, request.args.get('format'))
    elif plottype == 'profile':
        plotter = ProfilePlotter(dataset, query, request.args.get('format'))
    elif plottype == 'hovmoller':
        plotter = HovmollerPlotter(dataset, query, request.args.get('format'))
    elif plottype == 'observation':
        plotter = ObservationPlotter(dataset, query,
                                     request.args.get('format'))
    elif plottype == 'drifter':
        plotter = DrifterPlotter(dataset, query, request.args.get('format'))
    elif plottype == 'class4':
        plotter = Class4Plotter(dataset, query, request.args.get('format'))
    elif plottype == 'stick':
        plotter = StickPlotter(dataset, query, request.args.get('format'))
    else:
        raise FAILURE

    # Get the data from the selected plotter.
    img, mime, filename = plotter.run(size=size, dpi=request.args.get('dpi'))

    if img != "":
        response = make_response(img, mime)
    else:
        raise FAILURE

    if 'save' in request.args:
        response.headers[
            'Content-Disposition'] = "attachment; filename=\"%s\"" % filename

    response.cache_control.max_age = 300

    return response
def plot_impl(args, query=None):
    """
    API Format: /plot/?query='...'&format

    query = {
        dataset   : Dataset to extract data
        names     :
        plottitle : Title of Plot (Default if blank)
        quantum   : (year, month, day, hour)
        showmap   : Include a map of the plots location on the map
        station   : Coordinates of the point/line/area/etc
        time      : Time retrieved data was gathered/modeled
        type      : File / Plot Type (Check Navigator for Possible options)
        variable  : Type of data to plot - Options found using /api/variables/?dataset='...'
    }
    **Query must be written in JSON and converted to encodedURI**
    **Not all components of query are required
    """

    #Checks if query has already been extracted from args
    if query == None:
        if 'query' not in args:
            raise APIError(
                "Please Specify a Query - This should be written in JSON and converted to an encodedURI"
            )
        query = json.loads(args.get('query'))

    if ("format" in args and args.get("format") == "json"):
        # Generates a Base64 encoded string
        def make_response(data, mime):
            b64 = base64.encodebytes(data).decode()

            return Response(json.dumps("data:%s;base64,%s" % (mime, b64)),
                            status=200,
                            mimetype="application/json")
    else:

        def make_response(data, mime):
            return Response(data, status=200, mimetype=mime)

    dataset = query.get('dataset')

    opts = {
        'dpi': 72,
        'query': query,
    }
    plottype = query.get('type')

    size = None
    if 'save' in args:
        if 'size' in args:
            size = args.get('size')
        if 'dpi' in args:
            opts['dpi'] = args.get('dpi')

    if 'format' in args:
        opts['format'] = args.get('format')

    if size is None:
        opts['size'] = '11x9'
    else:
        opts['size'] = size

    filename = 'png'
    img = ""

    # Determine which plotter we need.
    if plottype == 'map':
        plotter = MapPlotter(dataset, query, args.get('format'))
    elif plottype == 'transect':
        plotter = TransectPlotter(dataset, query, args.get('format'))
    elif plottype == 'timeseries':
        plotter = TimeseriesPlotter(dataset, query, args.get('format'))
    elif plottype == 'ts':
        plotter = TemperatureSalinityPlotter(dataset, query,
                                             args.get('format'))
    elif plottype == 'sound':
        plotter = SoundSpeedPlotter(dataset, query, args.get('format'))
    elif plottype == 'profile':
        plotter = ProfilePlotter(dataset, query, args.get('format'))
    elif plottype == 'hovmoller':
        plotter = HovmollerPlotter(dataset, query, args.get('format'))
    elif plottype == 'observation':
        plotter = ObservationPlotter(dataset, query, args.get('format'))
    elif plottype == 'drifter':
        plotter = DrifterPlotter(dataset, query, args.get('format'))
    elif plottype == 'class4':
        plotter = Class4Plotter(dataset, query, args.get('format'))
    elif plottype == 'stick':
        plotter = StickPlotter(dataset, query, args.get('format'))
    else:
        raise APIError(
            "You Have Not Selected a Plot Type - Please Review your Query")

    # Get the data from the selected plotter.
    if 'data' in request.args:
        data = plotter.prepare_plot(size=size, dpi=args.get('dpi'))
        return data

    img, mime, filename = plotter.run(size=size, dpi=args.get('dpi'))

    if img != "":
        response = make_response(img, mime)
    else:
        raise FAILURE

    if 'save' in args:
        response.headers[
            'Content-Disposition'] = "attachment; filename=\"%s\"" % filename

    response.cache_control.max_age = 300

    return response
Exemple #4
0
def plot_v1_0():
    """
    API Format: /api/v1.0/plot/?query='...'&format

    query = {
        dataset   : Dataset to extract data
        names     :
        plottitle : Title of Plot (Default if blank)
        showmap   : Include a map of the plots location on the map
        station   : Coordinates of the point/line/area/etc
        time      : Time retrieved data was gathered/modeled
        type      : File / Plot Type (Check Navigator for Possible options)
        variable  : Variable key (e.g. votemper)
    }
    **Query must be written in JSON and converted to encodedURI**
    **Not all components of query are required
    """

    if request.method == "GET":
        args = request.args
    else:
        args = request.form

    if "query" not in args:
        raise APIError("Please provide a query.")

    query = json.loads(args.get("query"))

    fmt = args.get("format")
    if fmt == "json":

        def make_response(data, mime):
            b64 = base64.encodebytes(data).decode()

            return Response(
                json.dumps("data:%s;base64,%s" % (mime, b64)),
                status=200,
                mimetype="application/json",
            )

    else:

        def make_response(data, mime):
            return Response(data, status=200, mimetype=mime)

    dataset = query.get("dataset")
    plottype = query.get("type")

    options = {
        "format": fmt,
        "size": args.get("size", "15x9"),
        "dpi": args.get("dpi", 72),
    }

    # Determine which plotter we need.
    if plottype == "map":
        plotter = MapPlotter(dataset, query, **options)
    elif plottype == "transect":
        plotter = TransectPlotter(dataset, query, **options)
    elif plottype == "timeseries":
        plotter = TimeseriesPlotter(dataset, query, **options)
    elif plottype == "ts":
        plotter = TemperatureSalinityPlotter(dataset, query, **options)
    elif plottype == "sound":
        plotter = SoundSpeedPlotter(dataset, query, **options)
    elif plottype == "profile":
        plotter = ProfilePlotter(dataset, query, **options)
    elif plottype == "hovmoller":
        plotter = HovmollerPlotter(dataset, query, **options)
    elif plottype == "observation":
        plotter = ObservationPlotter(dataset, query, **options)
    elif plottype == "track":
        plotter = TrackPlotter(dataset, query, **options)
    elif plottype == "class4":
        plotter = Class4Plotter(dataset, query, **options)
    elif plottype == "stick":
        plotter = StickPlotter(dataset, query, **options)
    else:
        raise APIError("You Have Not Selected a Plot Type - Please Review your Query")

    if "data" in request.args:
        data = plotter.prepare_plot()
        return data

    img, mime, filename = plotter.run()

    if img:
        response = make_response(img, mime)
    else:
        raise FAILURE

    if "save" in args:
        response.headers["Content-Disposition"] = 'attachment; filename="%s"' % filename

    response.cache_control.max_age = 300

    if "data" in args:
        plotData = {
            "data": str(resp),  # noqa: F821
            "shape": resp.shape,  # noqa: F821
            "mask": str(resp.mask),  # noqa: F821
        }
        plotData = json.dumps(plotData)
        return Response(plotData, status=200, mimetype="application/json")

    return response