You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromfastapiimportAPIRouter, Depends, HTTPExceptionfromxpublish.dependenciesimportget_datasetmyrouter=APIRouter()
@myrouter.get("/{var_name}/mean")defget_mean(var_name: str, dataset: xr.Dataset=Depends(get_dataset)):
ifvar_namenotindataset.variables:
raiseHTTPException(
status_code=404, detail=f"Variable '{var_name}' not found in dataset"
)
returnfloat(dataset[var_name].mean())
ds.rest(routers=[myrouter])
ds.rest.serve()
The example above returns a float. What I'd like to do is to implement API endpoints for a derived dataset (e.g. spatial subset) served as zarr, let's say:
Do you need to expose any aggregation parameter as API endpoint parameter?
If you don't need to, the simplest way is to compute the aggregated datasets (maybe lazily) before serving them with xpublish.
Otherwise, I think you could do something like this, although this feels a bit hacky and it applies to all endpoints of the application (not just zarr endpoints):
importxarrayasxrimportxpublishfromxpublish.dependenciesimportget_datasetdatasets_to_serve= {...}
rest=xpublish.Rest(datasets_to_serve)
get_actual_dataset=rest.app.dependency_overrides[get_dataset]
defget_aggregated_dataset(dataset_id: str, dim: str='time'):
ds=get_actual_dataset(id)
# call aggregate function using `dim`# (maybe use xpublish's cache to avoid computing the aggregated dataset each time)aggregated= ...
returnaggregatedrest.app.dependency_overrides[get_dataset] =get_aggregated_datasetrest.serve()
@benbovy, I do need to expose the aggregation parameter as an API endpoint parameter so I'll look at what you propose and come back with feeedback. Thanks for your time and code snippet!
I really like the approach you implemented with:
The example above returns a float. What I'd like to do is to implement API endpoints for a derived dataset (e.g. spatial subset) served as zarr, let's say:
/datasets/{dataset_id}/{variable}/processes/position:aggregate-time/.zmetadata
/datasets/{dataset_id}/{variable}/processes/position:aggregate-time/zgroups
/datasets/{dataset_id}/{variable}/processes/position:aggregate-time/zattrs
/datasets/{dataset_id}/{variable}/processes/position:aggregate-time/{var}/{chunk}
The client would then do something like
or
What would be the best approach to implement this with xpublish? Any suggestion would be appreciated
The text was updated successfully, but these errors were encountered: