Skip to content

Commit

Permalink
update to intake v2
Browse files Browse the repository at this point in the history
Updating to intake v2. Working with `xpublish-dev` this worked for the `air` example with
a change to the intake library. However, it still does not work with an opendap case served
through zarr.
  • Loading branch information
kthyng committed May 24, 2024
1 parent 1332d0b commit 8bc1175
Showing 1 changed file with 36 additions and 54 deletions.
90 changes: 36 additions & 54 deletions xpublish_intake/plugins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
from typing import Sequence

import intake
import xarray as xr
import tempfile
import yaml
from fastapi import APIRouter, Depends, Request, Response
from starlette.routing import NoMatchFound
Expand Down Expand Up @@ -30,7 +33,7 @@ def get_dataset_id(ds):
return next(x for x in dataset_id_options if x)


def get_zarr_source(xpublish_id, dataset, request):
def get_zarr_source(xpublish_id, request):
url = ''
try:
from xpublish.plugins.included.zarr import ZarrPlugin # noqa
Expand All @@ -48,21 +51,14 @@ def get_zarr_source(xpublish_id, dataset, request):
if not url:
return {}

return {
'driver': 'zarr',
'description': dataset.attrs.get('summary', ''),
'args': {
'consolidated': True,
'urlpath': url
}
}
return url


class IntakePlugin(Plugin):
"""Adds an Intake catalog endpoint"""

name = 'intake_catalog'
dataset_metadata = dict()
name: str = 'intake_catalog'
dataset_metadata: dict = dict()

app_router_prefix: str = '/intake'
app_router_tags: Sequence[str] = ['intake']
Expand All @@ -84,38 +80,23 @@ def get_root_catalog(
dataset_ids = Depends(deps.dataset_ids)
):

data = {
'metadata': {
'source': 'Served via `xpublish-intake`',
'access_url': str(request.url),
}
# ADD METADATA IN
metadata = {
'source': 'Served via `xpublish-intake`',
'access_url': str(request.url),
}

if dataset_ids:
data['sources'] = {
d: {
'description': self.dataset_metadata.get(d, {}).get('description', ''),
'driver': 'intake.catalog.local.YAMLFileCatalog',
'metadata': self.dataset_metadata.get(d, {}),
'args': {
'path': str(request.url_for('get_dataset_catalog', dataset_id=d))
}
}
for d in dataset_ids
}
else:
data['sources'] = {
'dataset': {
'description': self.dataset_metadata.get('default', {}).get('description', ''),
'driver': 'intake.catalog.local.YAMLFileCatalog',
'metadata': self.dataset_metadata.get('default', {}),
'args': {
'path': str(request.url_for('get_dataset_catalog'))
}
}
}

return Response(yaml.dump(data), media_type="text/yaml")
cat = intake.entry.Catalog(metadata=metadata)

for dataset_id in dataset_ids:
url = get_zarr_source(dataset_id, request)
if not url:
continue
data = intake.datatypes.Zarr(url, metadata={})
reader = data.to_reader("xarray", consolidated=True)
cat[f'{dataset_id}'] = reader

return Response(yaml.dump(cat.to_dict()), media_type="text/yaml")

return router

Expand All @@ -132,21 +113,22 @@ def get_dataset_catalog(
dataset=Depends(deps.dataset),
):
xpublish_id = get_dataset_id(dataset)
sources = {
'zarr': get_zarr_source(xpublish_id, dataset, request)
}

data = {
'name': xpublish_id,
'metadata': {
'source': 'Served via `xpublish-intake`',
'access_url': str(request.url),
},
'sources': {
f'{xpublish_id}-{k}': v for k, v in sources.items() if v
}
# ADD METADATA IN

cat = intake.entry.Catalog()
urls = {
'zarr': get_zarr_source(xpublish_id, request)
}

return Response(yaml.dump(data), media_type="text/yaml")
for k, url in urls.items():
if not url:
continue

data = intake.datatypes.Zarr(url, metadata={})
reader = data.to_reader("xarray", consolidated=True)
cat[f'{xpublish_id}'] = reader

return Response(yaml.dump(cat.to_dict()), media_type="text/yaml")

return router

0 comments on commit 8bc1175

Please sign in to comment.