From c6ab0d4c8e3c255ac4e8e614232c00155a704b70 Mon Sep 17 00:00:00 2001
From: amsnyder <asnyder@usgs.gov>
Date: Mon, 8 Apr 2024 12:41:29 -0500
Subject: [PATCH] fix crs var detection

---
 .../Red_River_MPI-ESM-LR_future/collection.json  |  2 +-
 .../collection.json                              |  2 +-
 catalog/Red_River_historical/collection.json     |  2 +-
 ...M-LR_future_create_collection_from_zarr.ipynb | 16 ++++++++++++++--
 ..._historical_create_collection_from_zarr.ipynb | 16 ++++++++++++++--
 ..._historical_create_collection_from_zarr.ipynb | 16 ++++++++++++++--
 workflows/stac_helpers.py                        |  4 ++--
 7 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/catalog/Red_River_MPI-ESM-LR_future/collection.json b/catalog/Red_River_MPI-ESM-LR_future/collection.json
index b70203ae..32fa3288 100644
--- a/catalog/Red_River_MPI-ESM-LR_future/collection.json
+++ b/catalog/Red_River_MPI-ESM-LR_future/collection.json
@@ -62,7 +62,7 @@
   "cube:variables": {
     "crs": {
       "dimensions": [],
-      "type": "data",
+      "type": "auxiliary",
       "description": null,
       "unit": null
     },
diff --git a/catalog/Red_River_MPI-ESM-LR_historical/collection.json b/catalog/Red_River_MPI-ESM-LR_historical/collection.json
index d62d21f0..6f8471be 100644
--- a/catalog/Red_River_MPI-ESM-LR_historical/collection.json
+++ b/catalog/Red_River_MPI-ESM-LR_historical/collection.json
@@ -62,7 +62,7 @@
   "cube:variables": {
     "crs": {
       "dimensions": [],
-      "type": "data",
+      "type": "auxiliary",
       "description": null,
       "unit": null
     },
diff --git a/catalog/Red_River_historical/collection.json b/catalog/Red_River_historical/collection.json
index f02ff0de..e2bd2b98 100644
--- a/catalog/Red_River_historical/collection.json
+++ b/catalog/Red_River_historical/collection.json
@@ -62,7 +62,7 @@
   "cube:variables": {
     "crs": {
       "dimensions": [],
-      "type": "data",
+      "type": "auxiliary",
       "description": null,
       "unit": null
     },
diff --git a/workflows/archive/Red_River_MPI-ESM-LR_future_create_collection_from_zarr.ipynb b/workflows/archive/Red_River_MPI-ESM-LR_future_create_collection_from_zarr.ipynb
index e8080acf..e3555e16 100644
--- a/workflows/archive/Red_River_MPI-ESM-LR_future_create_collection_from_zarr.ipynb
+++ b/workflows/archive/Red_River_MPI-ESM-LR_future_create_collection_from_zarr.ipynb
@@ -207,6 +207,18 @@
     "## Get crs info"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "1a5e0bed-68f9-4b99-8d96-0831e9348a4e",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "crs_var = 'crs'"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -215,7 +227,7 @@
    "outputs": [],
    "source": [
     "# use pyproj to automatically extract crs info\n",
-    "crs = pyproj.CRS.from_cf(ds.crs.attrs)\n",
+    "crs = pyproj.CRS.from_cf(ds[crs_var].attrs)\n",
     "\n",
     "# alternatively, create the appropriate cartopy projection\n",
     "# crs = ccrs.LambertConformal(central_longitude=crs_info.longitude_of_central_meridian, \n",
@@ -771,7 +783,7 @@
     "vars_dict={}\n",
     "for v in vars:\n",
     "    unit = stac_helpers.get_unit(ds, v)\n",
-    "    var_type = stac_helpers.get_var_type(ds, v)\n",
+    "    var_type = stac_helpers.get_var_type(ds, v, crs_var)\n",
     "    long_name = stac_helpers.get_long_name(ds, v)\n",
     "    vars_dict[v] = pystac.extensions.datacube.Variable({'dimensions':list(ds[v].dims), 'type': var_type, 'description': long_name, 'unit': unit})"
    ]
diff --git a/workflows/archive/Red_River_MPI-ESM-LR_historical_create_collection_from_zarr.ipynb b/workflows/archive/Red_River_MPI-ESM-LR_historical_create_collection_from_zarr.ipynb
index 81de48c3..1ed66e41 100644
--- a/workflows/archive/Red_River_MPI-ESM-LR_historical_create_collection_from_zarr.ipynb
+++ b/workflows/archive/Red_River_MPI-ESM-LR_historical_create_collection_from_zarr.ipynb
@@ -207,6 +207,18 @@
     "## Get crs info"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "0206f01c-02bb-42d8-885d-6b1644fba9cc",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "crs_var = 'crs'"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -215,7 +227,7 @@
    "outputs": [],
    "source": [
     "# use pyproj to automatically extract crs info\n",
-    "crs = pyproj.CRS.from_cf(ds.crs.attrs)\n",
+    "crs = pyproj.CRS.from_cf(ds[crs_var].attrs)\n",
     "\n",
     "# alternatively, create the appropriate cartopy projection\n",
     "# crs = ccrs.LambertConformal(central_longitude=crs_info.longitude_of_central_meridian, \n",
@@ -771,7 +783,7 @@
     "vars_dict={}\n",
     "for v in vars:\n",
     "    unit = stac_helpers.get_unit(ds, v)\n",
-    "    var_type = stac_helpers.get_var_type(ds, v)\n",
+    "    var_type = stac_helpers.get_var_type(ds, v, crs_var)\n",
     "    long_name = stac_helpers.get_long_name(ds, v)\n",
     "    vars_dict[v] = pystac.extensions.datacube.Variable({'dimensions':list(ds[v].dims), 'type': var_type, 'description': long_name, 'unit': unit})"
    ]
diff --git a/workflows/archive/Red_River_historical_create_collection_from_zarr.ipynb b/workflows/archive/Red_River_historical_create_collection_from_zarr.ipynb
index 7558fc13..2d992eaf 100644
--- a/workflows/archive/Red_River_historical_create_collection_from_zarr.ipynb
+++ b/workflows/archive/Red_River_historical_create_collection_from_zarr.ipynb
@@ -207,6 +207,18 @@
     "## Get crs info"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "840c5128-1928-4011-a52b-e9b35ab313d5",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "crs_var = 'crs'"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -215,7 +227,7 @@
    "outputs": [],
    "source": [
     "# use pyproj to automatically extract crs info\n",
-    "crs = pyproj.CRS.from_cf(ds.crs.attrs)\n",
+    "crs = pyproj.CRS.from_cf(ds[crs_var].attrs)\n",
     "\n",
     "# alternatively, create the appropriate cartopy projection\n",
     "# crs = ccrs.LambertConformal(central_longitude=crs_info.longitude_of_central_meridian, \n",
@@ -771,7 +783,7 @@
     "vars_dict={}\n",
     "for v in vars:\n",
     "    unit = stac_helpers.get_unit(ds, v)\n",
-    "    var_type = stac_helpers.get_var_type(ds, v)\n",
+    "    var_type = stac_helpers.get_var_type(ds, v, crs_var)\n",
     "    long_name = stac_helpers.get_long_name(ds, v)\n",
     "    vars_dict[v] = pystac.extensions.datacube.Variable({'dimensions':list(ds[v].dims), 'type': var_type, 'description': long_name, 'unit': unit})"
    ]
diff --git a/workflows/stac_helpers.py b/workflows/stac_helpers.py
index a2466305..55801bc9 100644
--- a/workflows/stac_helpers.py
+++ b/workflows/stac_helpers.py
@@ -99,8 +99,8 @@ def get_unit(ds, v):
         assert cfunits.Units(unit).isvalid
     return unit
 
-def get_var_type(ds, v):
-    if v in ds.coords:
+def get_var_type(ds, v, crs_var):
+    if v in ds.coords or v==crs_var:
         # type = auxiliary for a variable that contains coordinate data, but isn't a dimension in cube:dimensions.
         # For example, the values of the datacube might be provided in the projected coordinate reference system, 
         # but the datacube could have a variable lon with dimensions (y, x), giving the longitude at each point.
-- 
GitLab