{
"cells": [
{
"cell_type": "markdown",
"id": "defae8d5978518cb",
"metadata": {},
"source": [
"\n",
"\n",
"# UXarray Dataset & DataArray\n",
"\n",
"### In this section, you'll learn:\n",
"\n",
"* What are `UxDataset` and `UxDataArray`?\n",
"* How do they relate to their Xarray counterparts?\n",
"\n",
"## Related Documentation\n",
"\n",
"* [UXarray Data Structures Documentation](https://uxarray.readthedocs.io/en/latest/user-guide/data-structures.html)\n",
"\n",
"### Prerequisites\n",
"\n",
"| Concepts | Importance | Notes |\n",
"| --- | --- | --- |\n",
"| [Introduction to Xarray](https://foundations.projectpythia.org/core/xarray/xarray-intro) | Necessary | |\n",
"\n",
"**Time to learn**: 15 minutes\n",
"\n",
"-----\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "8b0f60ea-73c4-4f6f-849f-fb0642fcb78c",
"metadata": {},
"source": [
"## Overview"
]
},
{
"cell_type": "markdown",
"id": "e6539b10-cca3-4c93-8368-528b157b26a1",
"metadata": {},
"source": [
"As stated before in this chapter, ``UxDataset`` and ``UxDataArray`` are the UXarray classes that are inherited from Xarray counterparts and enable to perform analysis and visualization on unstructured mesh datasets. \n",
"\n",
"An instance of ``UxDataset`` can be thought of as a collection of data variables, i.e. ``UxDataArray`` objects, that reside on a particular unstructured grid as defined in the class property (``UxDataset.uxgrid`` or ``UxDataArray.uxgrid``).\n",
"\n",
"In other words, each of these data structures is linked to its own ``Grid`` object through ``uxgrid`` to enable grid-awareness, and for ``UxDataArray``s that belong to a specific ``UxDataset``, ``uxgrid`` of all of them points to the same ``Grid`` object. "
]
},
{
"cell_type": "markdown",
"id": "3a6e4836-6c49-4598-98c0-9750923e84c0",
"metadata": {},
"source": [
"## Unstructured Mesh Datasets"
]
},
{
"cell_type": "markdown",
"id": "20746384-c123-4282-8db2-d75e47f6cb25",
"metadata": {},
"source": [
"Since most of the unstructured grid datasets contain a single grid definition file along with one or more data files, UXarray's core API allows to open such datasets using those files. \n",
"\n",
"Let the following dataset have a single data file along with the grid definition: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "409a3239-e51b-4353-8418-29dc1693a4dd",
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-18T19:26:50.031729Z",
"start_time": "2024-12-18T19:26:50.029554Z"
}
},
"outputs": [],
"source": [
"grid_path = \"../../meshfiles/outCSne30.grid.ug\"\n",
"data_path = \"../../meshfiles/outCSne30.data.nc\""
]
},
{
"cell_type": "markdown",
"id": "0cbfc5ef-d38e-42dd-a582-4c4259fe64ac",
"metadata": {},
"source": [
"## Loading a `UxDataset`"
]
},
{
"cell_type": "markdown",
"id": "29e59517-526f-434e-8647-bb8301118950",
"metadata": {},
"source": [
"To open a grid file with a **single** data file, the `uxarray.open_dataset()` method can be used. UXarray also provides `uxarray.open_mfdataset()` for opening multiple data files at once (refer to the [UXarray Opening Multipled Data Files Documentation](https://uxarray.readthedocs.io/en/latest/user-guide/data-structures.html#opening-multiple-data-files) for that), but this notebook will cover only the former."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d5560f80-09f1-4c5f-b542-0c7c52ba729b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
<xarray.UxDataset> Size: 43kB\n",
"Dimensions: (n_face: 5400)\n",
"Dimensions without coordinates: n_face\n",
"Data variables:\n",
" psi (n_face) float64 43kB ...<uxarray.Grid>\n",
"Original Grid Type: UGRID\n",
"Grid Dimensions:\n",
" * n_node: 5402\n",
" * n_face: 5400\n",
" * n_max_face_nodes: 4\n",
" * n_nodes_per_face: (5400,)\n",
"Grid Coordinates (Spherical):\n",
" * node_lon: (5402,)\n",
" * node_lat: (5402,)\n",
"Grid Coordinates (Cartesian):\n",
"Grid Connectivity Variables:\n",
" * face_node_connectivity: (5400, 4)\n",
"Grid Descriptor Variables:\n",
" * n_nodes_per_face: (5400,)\n",
"<xarray.UxDataArray 'psi' (n_face: 5400)> Size: 43kB\n",
"[5400 values with dtype=float64]\n",
"Dimensions without coordinates: n_face<uxarray.Grid>\n",
"Original Grid Type: UGRID\n",
"Grid Dimensions:\n",
" * n_node: 5402\n",
" * n_face: 5400\n",
" * n_max_face_nodes: 4\n",
" * n_nodes_per_face: (5400,)\n",
"Grid Coordinates (Spherical):\n",
" * node_lon: (5402,)\n",
" * node_lat: (5402,)\n",
"Grid Coordinates (Cartesian):\n",
"Grid Connectivity Variables:\n",
" * face_node_connectivity: (5400, 4)\n",
"Grid Descriptor Variables:\n",
" * n_nodes_per_face: (5400,)\n",
"<xarray.Dataset> Size: 30kB\n",
"Dimensions: (lat: 45, lon: 80)\n",
"Coordinates:\n",
" * lat (lat) int64 360B -90 -86 -82 -78 -74 -70 -66 ... 66 70 74 78 82 86\n",
" * lon (lon) float64 640B -180.0 -175.5 -171.0 ... 166.5 171.0 175.5\n",
"Data variables:\n",
" psi (lat, lon) float64 29kB ...<xarray.DataArray 'psi' (lat: 45, lon: 80)> Size: 29kB\n",
"[3600 values with dtype=float64]\n",
"Coordinates:\n",
" * lat (lat) int64 360B -90 -86 -82 -78 -74 -70 -66 ... 66 70 74 78 82 86\n",
" * lon (lon) float64 640B -180.0 -175.5 -171.0 ... 166.5 171.0 175.5\n",
"Attributes:\n",
" regrid_method: nearest_s2dSee also:
\n", " For further information, please visit the \n", " UXarray Inheritance from Xarray documentation\n", "