Skip to content

Commit

Permalink
GetMenu API
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci committed Oct 11, 2023
1 parent 5cee589 commit 05cfdaa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion xpublish_wms/wms/get_feature_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def get_feature_info(ds: xr.Dataset, query: dict) -> Response:
else:
elevation = []
has_vertical_axis = [
"vertical" in ds[parameter].cf.coordinates for parameter in parameters
"vertical" in ds.grid.has_elevation(ds[parameter]) for parameter in parameters
]
any_has_vertical_axis = True in has_vertical_axis

Expand All @@ -157,6 +157,8 @@ def get_feature_info(ds: xr.Dataset, query: dict) -> Response:
else:
selected_ds = selected_ds.cf.isel(time=0)

# TODO: This is really difficult when we have multiple parameters
# with different vertical dimensions, and even some without indexes
if any_has_vertical_axis:
if elevation == "all":
# Dont select an elevation, just keep all elevation coords
Expand Down
29 changes: 26 additions & 3 deletions xpublish_wms/wms/get_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ def get_metadata(ds: xr.Dataset, cache: cachey.Cache, params: dict) -> Response:
layer_name = params.get("layername", None)
metadata_type = params.get("item", "layerdetails")

if not layer_name and metadata_type != "minmax":
if not layer_name and metadata_type != "minmax" and metadata_type != "menu":
raise HTTPException(
status_code=400,
detail="layerName must be specified",
)
elif layer_name not in ds and metadata_type != "minmax":
elif layer_name not in ds and metadata_type != "minmax" and metadata_type != "menu":
raise HTTPException(
status_code=400,
detail=f"layerName {layer_name} not found in dataset",
)

if metadata_type == "layerdetails":
if metadata_type == "menu":
payload = get_menu(ds)
elif metadata_type == "layerdetails":
payload = get_layer_details(ds, layer_name)
elif metadata_type == "timesteps":
da = ds[layer_name]
Expand Down Expand Up @@ -120,3 +122,24 @@ def get_layer_details(ds: xr.Dataset, layer_name: str) -> dict:
"elevation_units": elevation_units,
"timesteps": timesteps,
}

def get_menu(ds: xr.Dataset):
"""
Returns the dataset menu items for the xreds viewer
TODO - support grouped layers?
"""
results = {
"children": [],
"label": ds.attrs.get("title", "")
}

for var in ds.data_vars:
da = ds[var]

results["children"].append({
"plottable": "longitude" in da.cf.coords,
"id": var,
"label": da.attrs.get("long_name", da.attrs.get("name", var))
})

return results

0 comments on commit 05cfdaa

Please sign in to comment.