def get_data_v1_0():
    """
    Returns a geojson representation of requested model data.

    API Format: GET /api/v1.0/data?...

    Required params:
    * dataset: dataset key (e.g. giops_day)
    * variable: variable key (e.g. votemper)
    * time: time index (e.g. 0)
    * depth: depth index (e.g. 49)
    * geometry_type: the "shape" of the data being requested
    """

    try:
        result = GetDataSchema().load(request.args)
    except ValidationError as e:
        abort(400, str(e))

    config = DatasetConfig(result['dataset'])

    with open_dataset(config,
                      variable=result['variable'],
                      timestamp=result['time']) as ds:
        return jsonify(
            geojson.dumps(data_array_to_geojson(
                ds.nc_data.get_dataset_variable(
                    result['variable'])[result['time'], result['depth'], :, :],
                config.lat_var_key, config.lon_var_key),
                          allow_nan=True))
Ejemplo n.º 2
0
    def test_data_array_to_geojson_builds_correct_feature_collection(
            self) -> None:
        result = data_array_to_geojson(self.data_array.votemper[0, 0, :5, :5],
                                       None, self.data_array["latitude"][:5],
                                       self.data_array["longitude"][:5])

        self.assertIsInstance(result, FeatureCollection)
        self.assertEqual(25, len(result['features']))
Ejemplo n.º 3
0
def get_data_v1_0():
    """
    Returns a geojson representation of requested model data.

    API Format: GET /api/v1.0/data?...

    Required params:
    * dataset: dataset key (e.g. giops_day)
    * variable: variable key (e.g. votemper)
    * time: time index (e.g. 0)
    * depth: depth index (e.g. 49)
    * geometry_type: the "shape" of the data being requested
    """

    try:
        result = GetDataSchema().load(request.args)
    except ValidationError as e:
        abort(400, str(e))

    cached_file_name = os.path.join(
        current_app.config['CACHE_DIR'], "data",
        f"get_data_{result['dataset']}_{result['variable']}_{result['depth']}_{result['time']}_{result['geometry_type']}.geojson"
    )

    if os.path.isfile(cached_file_name):
        print(f"Using cached {cached_file_name}")
        with open(cached_file_name, 'r') as f:
            send_file(f, 'application/json')

    config = DatasetConfig(result['dataset'])

    with open_dataset(config,
                      variable=result['variable'],
                      timestamp=result['time']) as ds:

        lat_var, lon_var = ds.nc_data.latlon_variables

        lat_slice = slice(0, lat_var.size, 4)
        lon_slice = slice(0, lon_var.size, 4)

        time_index = ds.nc_data.timestamp_to_time_index(result['time'])

        data = ds \
                .nc_data \
                .get_dataset_variable(result['variable'])[time_index, result['depth'], lat_slice, lon_slice]

        bearings = None
        if 'mag' in result['variable']:
            with open_dataset(config,
                              variable='bearing',
                              timestamp=result['time']) as ds_bearing:
                bearings = ds_bearing \
                                .nc_data \
                                .get_dataset_variable('bearing')[time_index, result['depth'], lat_slice, lon_slice]

        d = data_array_to_geojson(
            data,
            bearings,  # this is a hack
            lat_var[lat_slice],
            lon_var[lon_slice])

        os.makedirs(os.path.dirname(cached_file_name), exist_ok=True)
        with open(cached_file_name, 'w', encoding='utf-8') as f:
            geojson.dump(d, f)

        return jsonify(d)
Ejemplo n.º 4
0
 def test_data_array_to_geojson_raises_when_data_not_2d(self) -> None:
     with self.assertRaises(ValueError):
         data_array_to_geojson(self.data_array.votemper[:, 0, :5, :5], None,
                               self.data_array["latitude"][:5],
                               self.data_array["longitude"][:5])
Ejemplo n.º 5
0
    def test_data_array_to_geojson_builds_correct_feature_collection(
            self) -> None:
        expected = {
            "features": [{
                "geometry": {
                    "coordinates": [56.799999, 310.399994],
                    "type": "Point"
                },
                "properties": {
                    "data": 3.0999999046325684,
                    "long_name": "Temperature",
                    "short_name": "votemper",
                    "units": "degrees_C"
                },
                "type": "Feature"
            }, {
                "geometry": {
                    "coordinates": [57.0, 310.600006],
                    "type": "Point"
                },
                "properties": {
                    "data": 3.0840001106262207,
                    "long_name": "Temperature",
                    "short_name": "votemper",
                    "units": "degrees_C"
                },
                "type": "Feature"
            }, {
                "geometry": {
                    "coordinates": [57.200001, 310.799988],
                    "type": "Point"
                },
                "properties": {
                    "data": 3.193000078201294,
                    "long_name": "Temperature",
                    "short_name": "votemper",
                    "units": "degrees_C"
                },
                "type": "Feature"
            }, {
                "geometry": {
                    "coordinates": [57.400002, 311.0],
                    "type": "Point"
                },
                "properties": {
                    "data": 3.3570001125335693,
                    "long_name": "Temperature",
                    "short_name": "votemper",
                    "units": "degrees_C"
                },
                "type": "Feature"
            }, {
                "geometry": {
                    "coordinates": [57.599998, 311.200012],
                    "type": "Point"
                },
                "properties": {
                    "data": 3.4590001106262207,
                    "long_name": "Temperature",
                    "short_name": "votemper",
                    "units": "degrees_C"
                },
                "type": "Feature"
            }],
            "type":
            "FeatureCollection"
        }

        result = data_array_to_geojson(self.data_array.votemper[0, 0, :5, :5],
                                       "latitude", "longitude")

        self.assertIsInstance(result, FeatureCollection)
        self.assertEqual(expected, result)
Ejemplo n.º 6
0
def get_data_v1_0():
    """
    Returns a geojson representation of requested model data.

    API Format: GET /api/v1.0/data?...

    Required params:
    * dataset: dataset key (e.g. giops_day)
    * variable: variable key (e.g. votemper)
    * time: time index (e.g. 0)
    * depth: depth index (e.g. 49)
    * geometry_type: the "shape" of the data being requested
    """

    try:
        result = GetDataSchema().load(request.args)
    except ValidationError as e:
        abort(400, str(e))

    cached_file_name = os.path.join(
        current_app.config["CACHE_DIR"],
        "data",
        f"get_data_{result['dataset']}_{result['variable']}_{result['depth']}_{result['time']}_{result['geometry_type']}.geojson",  # noqa: E501
    )

    if os.path.isfile(cached_file_name):
        print(f"Using cached {cached_file_name}")
        return send_file(cached_file_name, "application/json")

    config = DatasetConfig(result["dataset"])

    with open_dataset(
        config, variable=result["variable"], timestamp=result["time"]
    ) as ds:

        lat_var, lon_var = ds.nc_data.latlon_variables

        stride = config.vector_arrow_stride

        lat_slice = slice(0, lat_var.size, stride)
        lon_slice = slice(0, lon_var.size, stride)

        time_index = ds.nc_data.timestamp_to_time_index(result["time"])

        data = ds.nc_data.get_dataset_variable(result["variable"])

        if len(data.shape) == 3:
            data_slice = (time_index, lat_slice, lon_slice)
        else:
            data_slice = (time_index, result["depth"], lat_slice, lon_slice)

        data = data[data_slice]

        bearings = None
        if "mag" in result["variable"]:
            with open_dataset(
                config, variable="bearing", timestamp=result["time"]
            ) as ds_bearing:
                bearings = ds_bearing.nc_data.get_dataset_variable("bearing")[
                    data_slice
                ].squeeze(drop=True)

        d = data_array_to_geojson(
            data.squeeze(drop=True),
            bearings,  # this is a hack
            lat_var[lat_slice],
            lon_var[lon_slice],
        )

        os.makedirs(os.path.dirname(cached_file_name), exist_ok=True)
        with open(cached_file_name, "w", encoding="utf-8") as f:
            geojson.dump(d, f)

        return jsonify(d)