Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes for OPeNDAP attributes #48

Merged
merged 18 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ build-backend = "setuptools.build_meta"

[project]
name = "xpublish_opendap"
description = ""
description = "OpenDAP plugin for Xpublish"
readme = "README.md"
requires-python = ">=3.9"
keywords = []
keywords = ["xarray", "xpublish", "opendap"]
license = { file = "LICENSE.txt" }

classifiers = [
Expand Down
4 changes: 4 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
black
check-manifest
doctr
h5netcdf
h5pyd
httpx
nbsphinx
netCDF4
nox
pooch
pre-commit
pydap
pylint
pytest
pytest-cov
pytest-flake8
pytest-github-actions-annotate-failures
pytest-xdist
pytest-xprocess
recommonmark
ruff
setuptools_scm
Expand Down
45 changes: 45 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Py.test configuration and shared fixtures."""
from pathlib import Path

import pytest
from xprocess import ProcessStarter

server_path = Path(__file__).parent / "server.py"


@pytest.fixture
def xpublish_server(xprocess):
"""Launch an Xpublish server in the background.

Server has the air_temperature tutorial dataset
at `air` and has the OpenDAP plugin running with
defaults.
"""

class Starter(ProcessStarter):
# Wait till the pattern is printed before
# considering things started
pattern = "Uvicorn running on"

# server startup args
args = ["python", str(server_path)]

# seconds before timing out on server startup
timeout = 30

# Try to cleanup if inturrupted
terminate_on_interrupt = True

xprocess.ensure("xpublish", Starter)
yield "http://0.0.0.0:9000"
xprocess.getinfo("xpublish").terminate()


@pytest.fixture(scope="session")
def dataset():
"""Xarray air temperature tutorial dataset."""
from xarray.tutorial import open_dataset

ds = open_dataset("air_temperature")

return ds
20 changes: 20 additions & 0 deletions tests/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Test OpenDAP server with air temperature dataset."""
import numpy as np
import xarray.tutorial
import xpublish

from xpublish_opendap import OpenDapPlugin

ds = xarray.tutorial.open_dataset("air_temperature")

ds_attrs_quote = xarray.tutorial.open_dataset("air_temperature")
ds_attrs_quote.attrs["quotes"] = 'This attribute uses "quotes"'
ds_attrs_quote.attrs["npint"] = np.int16(16)
ds_attrs_quote.attrs["npintthirtytwo"] = np.int32(32)

rest = xpublish.Rest(
{"air": ds, "attrs_quote_types": ds_attrs_quote},
noaaroland marked this conversation as resolved.
Show resolved Hide resolved
plugins={"opendap": OpenDapPlugin()},
)

rest.serve()
9 changes: 0 additions & 9 deletions tests/test_opendap_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
from xpublish_opendap import OpenDapPlugin


@pytest.fixture(scope="session")
def dataset():
from xarray.tutorial import open_dataset

ds = open_dataset("air_temperature")

return ds


@pytest.fixture(scope="session")
def dap_xpublish(dataset):
rest = xpublish.Rest({"air": dataset}, plugins={"opendap": OpenDapPlugin()})
Expand Down
44 changes: 44 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Test OpenDAP clients against Xpublish OpenDAP plugin."""
import netCDF4
import pytest
import xarray as xr


def test_netcdf4(xpublish_server):
"""Test opening OpenDAP air dataset directly with NetCDF4 library."""
url = f"{xpublish_server}/datasets/air/opendap"
netCDF4.Dataset(url)


def test_default_xarray_engine(xpublish_server, dataset):
"""Test opening OpenDAP air dataset with default Xarray engine."""
url = f"{xpublish_server}/datasets/air/opendap"
ds = xr.open_dataset(url)
assert ds == dataset


@pytest.mark.parametrize(
"engine",
[
"netcdf4",
# "h5netcdf", # fails with 404 not found
# "pydap" # fails with incomplete read
],
)
def test_xarray_engines(xpublish_server, engine, dataset):
"""Test opening OpenDAP dataset with specified engines."""
url = f"{xpublish_server}/datasets/air/opendap"
ds = xr.open_dataset(url, engine=engine)
assert ds == dataset


def test_attrs_types(xpublish_server):
"""Test that we are formatting OpenDAP attributes that contain '"' properly."""
url = f"{xpublish_server}/datasets/attrs_quote_types/opendap"
ds = xr.open_dataset(url)

print(ds.attrs)

assert ds.attrs["quotes"] == 'This attribute uses "quotes"'
assert ds.attrs["npint"] == 16
assert ds.attrs["npintthirtytwo"] == 32
11 changes: 11 additions & 0 deletions xpublish_opendap/dap_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def dap_attribute(key: str, value: Any) -> dap.Attribute:
dtype = dap.Int32
elif isinstance(value, float):
dtype = dap.Float64
elif isinstance(value, np.float32):
dtype = dap.Float32
elif isinstance(value, np.int16):
dtype = dap.Int16
elif isinstance(value, np.int32):
dtype = dap.Int32
elif isinstance(value, str):
dtype = dap.String
# Escape a double quote in the attribute value.
# Other servers like TDS do this. Without this clients fail.
value = value.replace('"', '\\"')
else:
dtype = dap.String

Expand Down
Loading