From c3c06518170a624b2d73645d5fa598cbd4a5951a Mon Sep 17 00:00:00 2001
From: amsnyder <asnyder@usgs.gov>
Date: Thu, 11 Jan 2024 14:18:49 -0600
Subject: [PATCH] updates to fix variable pull and auto-fill x,y,t dims in dims
 dict

---
 catalog/conus404-daily/collection.json         |  2 +-
 .../conus404-daily/conus404-daily.json         |  2 +-
 ...e_collection_from_zarr_conus404-daily.ipynb | 18 +++++++++++-------
 .../create_item_from_zarr_conus404-daily.ipynb | 18 +++++++++++-------
 4 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/catalog/conus404-daily/collection.json b/catalog/conus404-daily/collection.json
index 7e99fdd5..c1243093 100644
--- a/catalog/conus404-daily/collection.json
+++ b/catalog/conus404-daily/collection.json
@@ -32,8 +32,8 @@
     },
     "x": {
       "type": "spatial",
-      "description": "x coordinate of projection",
       "axis": "x",
+      "description": "x coordinate of projection",
       "extent": [
         -2732097.901153542,
         2731902.098846458
diff --git a/catalog_items/conus404-daily/conus404-daily.json b/catalog_items/conus404-daily/conus404-daily.json
index fcbb6847..417a2ab3 100644
--- a/catalog_items/conus404-daily/conus404-daily.json
+++ b/catalog_items/conus404-daily/conus404-daily.json
@@ -16,8 +16,8 @@
       },
       "x": {
         "type": "spatial",
-        "description": "x coordinate of projection",
         "axis": "x",
+        "description": "x coordinate of projection",
         "extent": [
           -2732097.901153542,
           2731902.098846458
diff --git a/workflows/examples/create_collection_from_zarr_conus404-daily.ipynb b/workflows/examples/create_collection_from_zarr_conus404-daily.ipynb
index 2fcdba41..602f929c 100644
--- a/workflows/examples/create_collection_from_zarr_conus404-daily.ipynb
+++ b/workflows/examples/create_collection_from_zarr_conus404-daily.ipynb
@@ -567,12 +567,16 @@
    "outputs": [],
    "source": [
     "# create a dictionary of datacube dimensions you would like to assign to this dataset\n",
-    "# dimension name should come from the coordinates printed above\n",
+    "# dimension name should come from the dims printed in above cell\n",
+    "\n",
+    "# x, y, t dimension info is pulled out automatically using the dim dict we created above\n",
+    "# all other dims listed in above cell need to be manually written in\n",
+    "\n",
     "# we do not recommend including redundant dimensions (do not include x,y if you have lon,lat)\n",
     "# note that the extent of each dimension should be pulled from the dataset\n",
-    "dims_dict = {'time': pystac.extensions.datacube.Dimension({'type': 'temporal', 'description': get_long_name(ds, 'time'), 'extent': [temporal_extent_lower.strftime('%Y-%m-%dT%XZ'), temporal_extent_upper.strftime('%Y-%m-%dT%XZ')], 'step': pd.Timedelta(get_step(ds, 'time')).isoformat()}),\n",
-    "             'x': pystac.extensions.datacube.Dimension({'type': 'spatial', 'description': get_long_name(ds, 'x'), 'axis': 'x', 'extent': [xy_bounds[0], xy_bounds[2]], 'step': get_step(ds, 'x'), 'reference_system': projjson}),\n",
-    "             'y': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'y', 'description': get_long_name(ds, 'y'), 'extent': [xy_bounds[1], xy_bounds[3]], 'step': get_step(ds, 'y'), 'reference_system': projjson}),\n",
+    "dims_dict = {dim_names_dict['T']: pystac.extensions.datacube.Dimension({'type': 'temporal', 'description': get_long_name(ds, dim_names_dict['T']), 'extent': [temporal_extent_lower.strftime('%Y-%m-%dT%XZ'), temporal_extent_upper.strftime('%Y-%m-%dT%XZ')], 'step': pd.Timedelta(get_step(ds, dim_names_dict['T'])).isoformat()}),\n",
+    "             dim_names_dict['X']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'x', 'description': get_long_name(ds, dim_names_dict['X']), 'extent': [xy_bounds[0], xy_bounds[2]], 'step': get_step(ds, dim_names_dict['X']), 'reference_system': projjson}),\n",
+    "             dim_names_dict['Y']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'y', 'description': get_long_name(ds, dim_names_dict['Y']), 'extent': [xy_bounds[1], xy_bounds[3]], 'step': get_step(ds, dim_names_dict['Y']), 'reference_system': projjson}),\n",
     "             'bottom_top_stag': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'z', 'description': get_long_name(ds, 'bottom_top_stag')}),\n",
     "             'bottom_top': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'z', 'description': get_long_name(ds, 'bottom_top')}),\n",
     "             'soil_layers_stag': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'z', 'description': get_long_name(ds, 'soil_layers_stag')}),\n",
@@ -635,13 +639,13 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "# pull list of vars from dataset\n",
-    "vars = list(ds.variables)\n",
-    "\n",
     "# drop metpy_crs coordinate we have added\n",
     "if 'metpy_crs' in ds.coords:\n",
     "    ds = ds.drop_vars('metpy_crs')\n",
     "\n",
+    "# pull list of vars from dataset\n",
+    "vars = list(ds.variables)\n",
+    "\n",
     "# spec says that the keys of cube:dimensions and cube:variables should be unique together; a key like lat should not be both a dimension and a variable.\n",
     "# we will drop all values in dims from vars\n",
     "vars = [v for v in vars if v not in dims]\n",
diff --git a/workflows/examples/create_item_from_zarr_conus404-daily.ipynb b/workflows/examples/create_item_from_zarr_conus404-daily.ipynb
index 473e8b1d..eeee7798 100644
--- a/workflows/examples/create_item_from_zarr_conus404-daily.ipynb
+++ b/workflows/examples/create_item_from_zarr_conus404-daily.ipynb
@@ -577,12 +577,16 @@
    "outputs": [],
    "source": [
     "# create a dictionary of datacube dimensions you would like to assign to this dataset\n",
-    "# dimension name should come from the coordinates printed above\n",
+    "# dimension name should come from the dims printed in above cell\n",
+    "\n",
+    "# x, y, t dimension info is pulled out automatically using the dim dict we created above\n",
+    "# all other dims listed in above cell need to be manually written in\n",
+    "\n",
     "# we do not recommend including redundant dimensions (do not include x,y if you have lon,lat)\n",
     "# note that the extent of each dimension should be pulled from the dataset\n",
-    "dims_dict = {'time': pystac.extensions.datacube.Dimension({'type': 'temporal', 'description': get_long_name(ds, 'time'), 'extent': [temporal_extent_lower.strftime('%Y-%m-%dT%XZ'), temporal_extent_upper.strftime('%Y-%m-%dT%XZ')], 'step': pd.Timedelta(get_step(ds, 'time')).isoformat()}),\n",
-    "             'x': pystac.extensions.datacube.Dimension({'type': 'spatial', 'description': get_long_name(ds, 'x'), 'axis': 'x', 'extent': [xy_bounds[0], xy_bounds[2]], 'step': get_step(ds, 'x'), 'reference_system': projjson}),\n",
-    "             'y': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'y', 'description': get_long_name(ds, 'y'), 'extent': [xy_bounds[1], xy_bounds[3]], 'step': get_step(ds, 'y'), 'reference_system': projjson}),\n",
+    "dims_dict = {dim_names_dict['T']: pystac.extensions.datacube.Dimension({'type': 'temporal', 'description': get_long_name(ds, dim_names_dict['T']), 'extent': [temporal_extent_lower.strftime('%Y-%m-%dT%XZ'), temporal_extent_upper.strftime('%Y-%m-%dT%XZ')], 'step': pd.Timedelta(get_step(ds, dim_names_dict['T'])).isoformat()}),\n",
+    "             dim_names_dict['X']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'x', 'description': get_long_name(ds, dim_names_dict['X']), 'extent': [xy_bounds[0], xy_bounds[2]], 'step': get_step(ds, dim_names_dict['X']), 'reference_system': projjson}),\n",
+    "             dim_names_dict['Y']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'y', 'description': get_long_name(ds, dim_names_dict['Y']), 'extent': [xy_bounds[1], xy_bounds[3]], 'step': get_step(ds, dim_names_dict['Y']), 'reference_system': projjson}),\n",
     "             'bottom_top_stag': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'z', 'description': get_long_name(ds, 'bottom_top_stag')}),\n",
     "             'bottom_top': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'z', 'description': get_long_name(ds, 'bottom_top')}),\n",
     "             'soil_layers_stag': pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'z', 'description': get_long_name(ds, 'soil_layers_stag')}),\n",
@@ -645,12 +649,12 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "# pull list of vars from dataset\n",
-    "vars = list(ds.variables)\n",
-    "\n",
     "# drop metpy_crs coordinate we have added\n",
     "if 'metpy_crs' in ds.coords:\n",
     "    ds = ds.drop_vars('metpy_crs')\n",
+    "  \n",
+    "# pull list of vars from dataset\n",
+    "vars = list(ds.variables)\n",
     "\n",
     "# spec says that the keys of cube:dimensions and cube:variables should be unique together; a key like lat should not be both a dimension and a variable.\n",
     "# we will drop all values in dims from vars\n",
-- 
GitLab