xpublish-community / xpublish-opendap

OpenDAP router for Xpublish
BSD 3-Clause "New" or "Revised" License
7 stars 4 forks source link

compatibility with SingleDatasetRest #69

Open jhamman opened 4 months ago

jhamman commented 4 months ago

I expected the following to work:

import xarray as xr
import xpublish
from xpublish_opendap import OpenDapPlugin

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

rest = xpublish.SingleDatasetRest(
    ds,
    plugins={
        "opendap": OpenDapPlugin(),
    },
)

rest.serve(host="localhost", port=9999)

This does successfully start up an Xpublish server, however, accessing the openap endpoint fails with the following error.

> ncdump -h "http://localhost:9999/opendap"
syntax error, unexpected '{', expecting SCAN_ATTR or SCAN_DATASET or SCAN_ERROR
context: {^"detail":[{"type":"missing","loc":["query","dataset_id"],"msg":"Field required","input":null}]}
ncdump: http://localhost:9999/opendap: NetCDF: Access failure

The missing dataset_id seems important.

BTW, changing the above example to use the standard Rest class does work:

rest = xpublish.SingleDatasetRest(
    {"foo": ds},
    plugins={
        "opendap": OpenDapPlugin(),
    },
)
abkfenris commented 4 months ago

I think that's because we're trying to see if we have a cached version of the OpenDAP-ed dataset kicking around, and that function takes the dataset_id.

https://github.com/xpublish-community/xpublish-opendap/blob/adf376ff133fef5d2168e23abb4054ad760c7b3d/xpublish_opendap/plugin.py#L45-L61

We could probably convert that signature to

        def get_dap_dataset(
            dataset_id: str = "default",
            ds: xr.Dataset = Depends(deps.dataset),
            cache: cachey.Cache = Depends(deps.cache),
        ) -> dap.Dataset:

To support SingleDatasetRest usage.