def test_basic_plot(self):

        print("\nBASIC PLOT - TESTING")
        test_data = self.test_data

        query = {
            "colormap":
            "default",
            "dataset":
            "giops_day",
            "depth_limit":
            False,
            "linearthresh":
            200,
            "area": [{
                'innerrings': [],
                'name':
                '',
                'polygons': [[[70.61599666750213, -132.48690912244984],
                              [76.33225862899116, -162.6089605518461],
                              [84.74968635370459, -121.23323854769576],
                              [75.15953587228074, -100.95402112796245],
                              [70.61599666750213, -132.48690912244984]]]
            }],
            "plotTitle":
            "",
            "quantum":
            "day",
            "scale":
            "-5,30,auto",
            "selectedPlots":
            "0,1,1",
            "showmap":
            True,
            "surfacevariable":
            "none",
            "time":
            '820',
            "type":
            "map",
            "variable":
            "votemper",
        }
        for dataset in test_data['datasets']:
            if dataset != 'gem':

                query['dataset'] = dataset
                print("\n dataset: " + dataset)
                for variable in test_data['variables'][dataset]:
                    print(".", end="", flush=True)
                    query['variable'] = variable
                    with self.app:
                        if variable != 'aice' and variable != 'vice' and variable != 'u-component_of_wind_height_above_ground' and variable != 'v-component_of_wind_height_above_ground':
                            resp = self.app.get(geturl(query))
                            self.assertEqual(resp.status_code, 200)
                            print("Request OK")
                            print("Creating Plot")
                            plotter = MapPlotter(dataset, query, 'json')
                            print("Plot Created")
                            plot = plotter.prepare_plot(size=None, dpi=None)

                            print(plot)
                            print(plot.shape)

                            self.assertEqual(plot.shape, ((500, 444)))
                            print(type(plot))
                            self.assertEqual(type(plot),
                                             'numpy.ma.core.MaskedArray')
Exemple #2
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 #3
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 #5
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