Files
pygeoapi/tests/test_xarray_zarr_provider.py
Leo Ghignone d240a8210e Improvements for xarray provider (#1800)
* Manage non-cf-compliant time dimension

* Manage datasets without a time dimension

* Allow reversed slices also for axes

* Convert also metadata to float64 for json output

* Use named temporary file to enable netcdf4 engine

* Make float64 conversion faster

* Add netcdf output to xarray provider

* Flake8 fixes

* Fix bug when no time axis in data

* Use new xarray interface

* Add test for zarr dataset without time dimension

* Avoid errors if missing long_name

* Manage zarr and netcdf output in the same way

* Revert "Manage zarr and netcdf output in the same way"

This reverts commit 0b09281b608da95221951d05004f213379da168d.

* Revert "Add netcdf output to xarray provider"

This reverts commit 9f72bf7614775b418f53f4808fcaeab567c7024a.
2024-09-30 05:40:31 -04:00

114 lines
3.1 KiB
Python

# =================================================================
#
# Authors: Gregory Petrochenkov <gpetrochenkov@usgs.gov>
#
# Copyright (c) 2021 Gregory Petrochenkov
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================
from numpy import float64, int64
import pytest
import xarray as xr
from pygeoapi.provider.xarray_ import XarrayProvider
from pygeoapi.util import json_serial
from .util import get_test_file_path
path = get_test_file_path(
'data/analysed_sst.zarr')
@pytest.fixture()
def config():
return {
'name': 'zarr',
'type': 'coverage',
'data': path,
'format': {
'name': 'zarr',
'mimetype': 'application/zip'
}
}
@pytest.fixture()
def config_no_time(tmp_path):
ds = xr.open_zarr(path)
ds = ds.sel(time=ds.time[0])
ds = ds.drop_vars('time')
ds.to_zarr(tmp_path / 'no_time.zarr')
return {
'name': 'zarr',
'type': 'coverage',
'data': str(tmp_path / 'no_time.zarr'),
'format': {'name': 'zarr', 'mimetype': 'application/zip'},
}
def test_provider(config):
p = XarrayProvider(config)
assert len(p.fields) == 4
assert len(p.axes) == 3
assert p.axes == ['lon', 'lat', 'time']
def test_schema(config):
p = XarrayProvider(config)
assert isinstance(p.fields, dict)
assert len(p.fields) == 4
assert p.fields['analysed_sst']['title'] == 'analysed sea surface temperature' # noqa
def test_query(config):
p = XarrayProvider(config)
data = p.query()
assert isinstance(data, dict)
data = p.query(format_='zarr')
assert isinstance(data, bytes)
def test_numpy_json_serial():
d = int64(500_000_000_000)
assert json_serial(d) == 500_000_000_000
d = float64(500.00000005)
assert json_serial(d) == 500.00000005
def test_no_time(config_no_time):
p = XarrayProvider(config_no_time)
assert len(p.fields) == 4
assert p.axes == ['lon', 'lat']
coverage = p.query(format='json')
assert sorted(coverage['domain']['axes'].keys()) == ['x', 'y']