{
"cells": [
{
"cell_type": "markdown",
"id": "a4dc683b-ce0e-446b-8430-8c13755c91ae",
"metadata": {},
"source": [
"## Coordinate aliasing\n",
"\n",
"When loading volumetric data into yt from xarray, there are a number of situations that may need some extra handling. \n",
"\n",
"1. The first situation is when the data contain a time dimensions (as is common in many cf-compliant netcdfs). In this case, we'll need to select the time to load into yt. \n",
"2. It is also possible that the coordinates for a variable may not be recognized by yt, in which case we need to tell yt how to interpret those coordinates. \n",
"\n",
"\n",
"We'll demonstrate this once again using random data:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a4f7d36d-f30b-463a-a3d5-a247d9fb733c",
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import yt_xarray\n",
"import yt\n",
"from yt_xarray.sample_data import load_random_xr_data\n",
"\n",
"fields = {'temp': ('h1', 'b2', 'c1'), \n",
" 'precip': ('h1', 'b2'),\n",
" 'precip_t': ('h1', 'b2', 'time')}\n",
"dims = {'h1': (0,1,15), 'b2': (0, 1, 10), 'c1': (0, 1, 15), 'time': (0, 1, 5)}\n",
"ds = load_random_xr_data(fields, dims, length_unit='m')"
]
},
{
"cell_type": "markdown",
"id": "f22286a2-9088-4864-9189-ab18f2ab0d85",
"metadata": {},
"source": [
"because these coordinates do not match up with those used by yt, simply trying\n",
"\n",
"```python\n",
"yt_ds = ds.yt.load_uniform_grid(fields=['temp'])\n",
"```\n",
"\n",
"will result in the following error:\n",
"\n",
"```\n",
"ValueError: h1 is not a known coordinate. To load in yt, you must supply an alias via the yt_xarray.known_coord_aliases dictionary.\n",
"```\n",
"This is telling us that to load data into yt, we need to explain how to map our dimension names to those expected by yt. \n",
"\n",
"If our dataset is cartesian, we can do so by adding some mappings to the `yt_xarray.known_coord_aliases` dictionary. The keys here are your native dataset names, while the values are the expected yt name:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "87c38846-3243-4990-bfda-fa680260e8f1",
"metadata": {},
"outputs": [],
"source": [
"yt_xarray.known_coord_aliases['h1'] = 'y'\n",
"yt_xarray.known_coord_aliases['b2'] = 'x'\n",
"yt_xarray.known_coord_aliases['c1'] = 'z'"
]
},
{
"cell_type": "markdown",
"id": "11b417b5-8371-433f-aeee-dc8f3550cb7c",
"metadata": {},
"source": [
"now we can load up the data:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c1b1d82c-78ca-40bd-9fbe-69feef221c44",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"yt_xarray : [INFO ] 2024-04-03 16:33:53,933: Inferred geometry type is cartesian. To override, use ds.yt.set_geometry\n",
"yt_xarray : [INFO ] 2024-04-03 16:33:53,933: Attempting to detect if yt_xarray will require field interpolation:\n",
"yt_xarray : [INFO ] 2024-04-03 16:33:53,933: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n",
"yt : [INFO ] 2024-04-03 16:33:53,959 Parameters: current_time = 0.0\n",
"yt : [INFO ] 2024-04-03 16:33:53,960 Parameters: domain_dimensions = [15 10 15]\n",
"yt : [INFO ] 2024-04-03 16:33:53,960 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.03571429]\n",
"yt : [INFO ] 2024-04-03 16:33:53,960 Parameters: domain_right_edge = [1.03571429 1.05555556 1.03571429]\n",
"yt : [INFO ] 2024-04-03 16:33:53,961 Parameters: cosmological_simulation = 0\n"
]
}
],
"source": [
"yt_ds = ds.yt.load_grid(fields=['temp'])"
]
},
{
"cell_type": "markdown",
"id": "0d71c6f9-aceb-4da6-bd51-460855fea954",
"metadata": {},
"source": [
"Note that from here, you'll have to use the yt name within yt commands:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ef0ee925-ce8d-4064-b796-59791ebf7584",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"yt : [INFO ] 2024-04-03 16:33:54,007 xlim = -0.055556 1.055556\n",
"yt : [INFO ] 2024-04-03 16:33:54,007 ylim = -0.035714 1.035714\n",
"yt : [INFO ] 2024-04-03 16:33:54,008 xlim = -0.055556 1.055556\n",
"yt : [INFO ] 2024-04-03 16:33:54,008 ylim = -0.035714 1.035714\n",
"yt : [INFO ] 2024-04-03 16:33:54,012 Making a fixed resolution buffer of (('stream', 'temp')) 800 by 800\n"
]
},
{
"data": {
"text/html": [
" "
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"slc = yt.SlicePlot(yt_ds, 'z', ('stream', 'temp'))\n",
"slc.set_log(('stream', 'temp'), False)"
]
},
{
"cell_type": "markdown",
"id": "318cedc8-87a3-4de3-9790-de7525dc06cb",
"metadata": {},
"source": [
"## cf-compliant coordinate names\n",
"\n",
"Additionally, the conversion of xarray coordinate name to the expected yt name can use [cf_xarray](https://cf-xarray.readthedocs.io) in the process of disambiguation. Note that this will only work if cf_xarray is installed (`pip install cf_xarray`). \n",
"\n",
"\n",
"First, let's import a cf_xarray sample dataset and checkout the `air` variable:\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ca0ad965-4e8c-497a-9562-2348f9f7567b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"