{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assign CF Compliant Topology\n", "\n", "This example assigns the missing CF Compliant topology information to allow for subsetting with the same workflow as the [fvcom](./fvcom.ipynb) example, but with a netcdf file that does not contain the necessary metadata." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/asascience-open/xarray-subset-grid/blob/main/docs/examples/sscofs.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install required libraries\n", "# !pip install -q xarray_subset_grid@git+https://github.com/asascience-open/xarray-subset-grid.git\n", "# !pip install -q s3fs cftime xarray cf-xarray fsspec dask h5netcdf" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 199MB\n",
       "Dimensions:             (nele: 433410, node: 239734, siglay: 10, siglev: 11,\n",
       "                         three: 3, time: 1, maxnode: 11, maxelem: 9, four: 4)\n",
       "Coordinates:\n",
       "    lon                 (node) float32 959kB dask.array<chunksize=(239734,), meta=np.ndarray>\n",
       "    lat                 (node) float32 959kB dask.array<chunksize=(239734,), meta=np.ndarray>\n",
       "    lonc                (nele) float32 2MB dask.array<chunksize=(433410,), meta=np.ndarray>\n",
       "    latc                (nele) float32 2MB dask.array<chunksize=(433410,), meta=np.ndarray>\n",
       "    siglay              (siglay, node) float32 10MB dask.array<chunksize=(10, 239734), meta=np.ndarray>\n",
       "    siglev              (siglev, node) float32 11MB dask.array<chunksize=(11, 239734), meta=np.ndarray>\n",
       "  * time                (time) datetime64[ns] 8B 2024-03-23T13:00:07.031249984\n",
       "Dimensions without coordinates: nele, node, three, maxnode, maxelem, four\n",
       "Data variables: (12/45)\n",
       "    nprocs              int32 4B ...\n",
       "    partition           (nele) int32 2MB dask.array<chunksize=(433410,), meta=np.ndarray>\n",
       "    x                   (node) float32 959kB dask.array<chunksize=(239734,), meta=np.ndarray>\n",
       "    y                   (node) float32 959kB dask.array<chunksize=(239734,), meta=np.ndarray>\n",
       "    xc                  (nele) float32 2MB dask.array<chunksize=(433410,), meta=np.ndarray>\n",
       "    yc                  (nele) float32 2MB dask.array<chunksize=(433410,), meta=np.ndarray>\n",
       "    ...                  ...\n",
       "    wet_nodes           (time, node) int32 959kB dask.array<chunksize=(1, 239734), meta=np.ndarray>\n",
       "    wet_cells           (time, nele) int32 2MB dask.array<chunksize=(1, 433410), meta=np.ndarray>\n",
       "    wet_nodes_prev_int  (time, node) int32 959kB dask.array<chunksize=(1, 239734), meta=np.ndarray>\n",
       "    wet_cells_prev_int  (time, nele) int32 2MB dask.array<chunksize=(1, 433410), meta=np.ndarray>\n",
       "    wet_cells_prev_ext  (time, nele) int32 2MB dask.array<chunksize=(1, 433410), meta=np.ndarray>\n",
       "    inundation_cells    (time, nele) int32 2MB dask.array<chunksize=(1, 433410), meta=np.ndarray>\n",
       "Attributes: (12/14)\n",
       "    title:                       SSCOFS\n",
       "    institution:                 School for Marine Science and Technology\n",
       "    source:                      FVCOM_4.4.5\n",
       "    history:                     model started at: 20/03/2024   21:46\n",
       "    references:                  http://fvcom.smast.umassd.edu, http://codfis...\n",
       "    Conventions:                 CF-1.0\n",
       "    ...                          ...\n",
       "    Tidal_Forcing:               Tidal Forcing Time Series Title: sscofs late...\n",
       "    River_Forcing:               THERE ARE 38 RIVERS IN THIS MODEL.\\nRIVER IN...\n",
       "    GroundWater_Forcing:         GROUND WATER FORCING IS OFF!\n",
       "    Surface_Heat_Forcing:        FVCOM variable surface heat forcing file:\\nF...\n",
       "    Surface_Wind_Forcing:        FVCOM variable surface Wind forcing:\\nFILE N...\n",
       "    Surface_PrecipEvap_Forcing:  SURFACE PRECIPITATION FORCING IS OFF
" ], "text/plain": [ " Size: 199MB\n", "Dimensions: (nele: 433410, node: 239734, siglay: 10, siglev: 11,\n", " three: 3, time: 1, maxnode: 11, maxelem: 9, four: 4)\n", "Coordinates:\n", " lon (node) float32 959kB dask.array\n", " lat (node) float32 959kB dask.array\n", " lonc (nele) float32 2MB dask.array\n", " latc (nele) float32 2MB dask.array\n", " siglay (siglay, node) float32 10MB dask.array\n", " siglev (siglev, node) float32 11MB dask.array\n", " * time (time) datetime64[ns] 8B 2024-03-23T13:00:07.031249984\n", "Dimensions without coordinates: nele, node, three, maxnode, maxelem, four\n", "Data variables: (12/45)\n", " nprocs int32 4B ...\n", " partition (nele) int32 2MB dask.array\n", " x (node) float32 959kB dask.array\n", " y (node) float32 959kB dask.array\n", " xc (nele) float32 2MB dask.array\n", " yc (nele) float32 2MB dask.array\n", " ... ...\n", " wet_nodes (time, node) int32 959kB dask.array\n", " wet_cells (time, nele) int32 2MB dask.array\n", " wet_nodes_prev_int (time, node) int32 959kB dask.array\n", " wet_cells_prev_int (time, nele) int32 2MB dask.array\n", " wet_cells_prev_ext (time, nele) int32 2MB dask.array\n", " inundation_cells (time, nele) int32 2MB dask.array\n", "Attributes: (12/14)\n", " title: SSCOFS\n", " institution: School for Marine Science and Technology\n", " source: FVCOM_4.4.5\n", " history: model started at: 20/03/2024 21:46\n", " references: http://fvcom.smast.umassd.edu, http://codfis...\n", " Conventions: CF-1.0\n", " ... ...\n", " Tidal_Forcing: Tidal Forcing Time Series Title: sscofs late...\n", " River_Forcing: THERE ARE 38 RIVERS IN THIS MODEL.\\nRIVER IN...\n", " GroundWater_Forcing: GROUND WATER FORCING IS OFF!\n", " Surface_Heat_Forcing: FVCOM variable surface heat forcing file:\\nF...\n", " Surface_Wind_Forcing: FVCOM variable surface Wind forcing:\\nFILE N...\n", " Surface_PrecipEvap_Forcing: SURFACE PRECIPITATION FORCING IS OFF" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import cf_xarray #noqa\n", "import fsspec\n", "import xarray as xr\n", "\n", "import xarray_subset_grid\n", "\n", "# Open the dataset from NODD s3 bucket directly\n", "fs = fsspec.filesystem(\"s3\", anon=True)\n", "ds = xr.open_dataset(\n", " fs.open(\n", " \"s3://noaa-nos-ofs-pds/sscofs/netcdf/202403/sscofs.t21z.20240320.fields.f064.nc\"\n", " ),\n", " chunks={},\n", " drop_variables=[\"Itime2\"],\n", ")\n", "ds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This step assigns a new variable with the `mesh_toplogy` cf role to add the mesh information needed for subsetting in space" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'mesh' ()> Size: 4B\n",
       "array(0, dtype=int32)\n",
       "Attributes:\n",
       "    cf_role:                 mesh_topology\n",
       "    topology_dimension:      2\n",
       "    node_coordinates:        lon lat\n",
       "    face_node_connectivity:  nv\n",
       "    face_coordinates:        lonc latc\n",
       "    face_face_connectivity:  nbe
" ], "text/plain": [ " Size: 4B\n", "array(0, dtype=int32)\n", "Attributes:\n", " cf_role: mesh_topology\n", " topology_dimension: 2\n", " node_coordinates: lon lat\n", " face_node_connectivity: nv\n", " face_coordinates: lonc latc\n", " face_face_connectivity: nbe" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = xarray_subset_grid.grids.ugrid.assign_ugrid_topology(ds,\n", " face_node_connectivity='nv',\n", " face_face_connectivity='nbe')\n", "ds.cf['mesh_topology']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The subset grid accessor now finds the ugrid topology correctly" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ugrid'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.xsg.grid.name" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 2 }