Skip to content

Commit

Permalink
Fixed whitespace and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmjoyce committed May 21, 2024
1 parent ec146c1 commit 19cd1f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 15 additions & 4 deletions examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@

class TutorialDataset(Plugin):
"""Demonstrates how to create a plugin to load a dataset for demo purposes.
This uses the default xarray tutorial datasets.
"""

name: str = 'xarray-tutorial-datasets'

@hookimpl
def get_datasets(self):
"""Returns a list of available datasets"""
"""Returns a list of available datasets.
This function returns a list of the available datasets that can be loaded using the xarray.tutorial.file_formats module.
"""
return list(xr.tutorial.file_formats)

@hookimpl
def get_dataset(self, dataset_id: str):
"""Returns a dataset specified by dataset_id"""
"""Retrieves a dataset from the xarray tutorial dataset by the given dataset ID.
Args:
dataset_id (str): The ID of the dataset to retrieve.
Returns:
xarray.Dataset: The retrieved dataset, or None if the dataset could not be loaded.
"""
try:
ds = xr.tutorial.open_dataset(dataset_id)
if ds.cf.coords['longitude'].dims[0] == 'longitude':
if ds.cf.coords["longitude"].dims[0] == "longitude":
ds = ds.assign_coords(longitude=(((ds.longitude + 180) % 360) - 180)).sortby(
'longitude'
"longitude"
)
# TODO: Yeah this should not be assumed... but for regular grids we will viz with rioxarray so for now we will assume
ds = ds.rio.write_crs(4326)
Expand Down
13 changes: 11 additions & 2 deletions examples/mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ class MeanPlugin(Plugin):

@hookimpl
def dataset_router(self, deps: Dependencies):
"""Provides a route to retrieve the mean value of a variable in a dataset.
Args:
var_name (str): The name of the variable to retrieve the mean for.
dataset (Dataset): The dataset containing the variable.
Returns:
float: The mean value of the variable, or 'NaN' if the mean is null.
"""
router = APIRouter(prefix=self.dataset_router_prefix, tags=list(self.dataset_router_tags))

@router.get('/{var_name}/mean')
@router.get("/{var_name}/mean")
def get_mean(var_name: str, dataset=Depends(deps.dataset)):
if var_name not in dataset.variables:
raise HTTPException(
Expand All @@ -35,7 +44,7 @@ def get_mean(var_name: str, dataset=Depends(deps.dataset)):

mean = dataset[var_name].mean()
if mean.isnull():
return 'NaN'
return "NaN"
return float(mean)

return router

0 comments on commit 19cd1f8

Please sign in to comment.