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

compatibility with SingleDatasetRest #69

Open
jhamman opened this issue Jul 23, 2024 · 1 comment · May be fixed by #70
Open

compatibility with SingleDatasetRest #69

jhamman opened this issue Jul 23, 2024 · 1 comment · May be fixed by #70

Comments

@jhamman
Copy link
Contributor

jhamman commented Jul 23, 2024

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(),
    },
)
@jhamman jhamman changed the title compatability with compatibility with SingleDatasetRest Jul 23, 2024
@abkfenris
Copy link
Member

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.

def get_dap_dataset(
dataset_id: str,
ds: xr.Dataset = Depends(deps.dataset),
cache: cachey.Cache = Depends(deps.cache),
) -> dap.Dataset:
"""Get a dataset that has been translated to opendap."""
# get cached dataset if it exists
cache_key = f"opendap_dataset_{dataset_id}"
dataset = cache.get(cache_key)
# if not, convert the xarray dataset to opendap
if dataset is None:
dataset = dap_xarray.dap_dataset(ds, dataset_id)
cache.put(cache_key, dataset, 99999)
return dataset

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.

abkfenris added a commit that referenced this issue Jul 24, 2024
Adds support for the `SingleDatasetRest` pattern by making the `dataset_id` arugment to the cache have a default value (of 'default'), in case there is not a `dataset_id` in the path.

Closes  #69
@abkfenris abkfenris linked a pull request Jul 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants