{ "cells": [ { "cell_type": "markdown", "id": "2e35e4f7", "metadata": {}, "source": [ "(xarray-intro)=\n", "# Xarray - brief introduction\n", "\n", "```{seealso}\n", "The complete source code of this tutorial can be found in\n", "\n", "{nb-download}`Xarray introduction.ipynb`\n", "```\n", "\n", "The Quantify dataset is based on {doc}`Xarray `.\n", "This subsection is a very brief overview of some concepts and functionalities of xarray.\n", "Here we use only pure xarray concepts and terminology.\n", "\n", "This is not intended as an extensive introduction to xarray.\n", "Please consult the {doc}`xarray documentation ` if you never used it\n", "before (it has very neat features!)." ] }, { "cell_type": "code", "execution_count": 1, "id": "fbeea621", "metadata": { "tags": [ "hide-cell" ] }, "outputs": [], "source": [ "import numpy as np\n", "import xarray as xr\n", "from rich import pretty\n", "\n", "pretty.install()" ] }, { "cell_type": "markdown", "id": "c8bd036f", "metadata": {}, "source": [ "There are different ways to create a new xarray dataset.\n", "Below we exemplify a few of them to showcase specific functionalities.\n", "\n", "An xarray dataset has **Dimensions** and **Variables**. Variables \"lie\" along at least\n", "one dimension:" ] }, { "cell_type": "code", "execution_count": 2, "id": "63e688c4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.Dataset>\n",
"Dimensions:   (position_x: 5, velocity_x: 5)\n",
"Dimensions without coordinates: position_x, velocity_x\n",
"Data variables:\n",
"    position  (position_x) float64 -5.0 -2.5 0.0 2.5 5.0\n",
"    velocity  (velocity_x) float64 0.0 2.5 5.0 7.5 10.0\n",
"Attributes:\n",
"    my_attribute_name:  some meta information```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.Dataset\u001b[0m\u001b[1m>\u001b[0m\n", "Dimensions: \u001b[1m(\u001b[0mposition_x: \u001b[1;36m5\u001b[0m, velocity_x: \u001b[1;36m5\u001b[0m\u001b[1m)\u001b[0m\n", "Dimensions without coordinates: position_x, velocity_x\n", "Data variables:\n", " position \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m-5.0\u001b[0m \u001b[1;36m-2.5\u001b[0m \u001b[1;36m0.0\u001b[0m \u001b[1;36m2.5\u001b[0m \u001b[1;36m5.0\u001b[0m\n", " velocity \u001b[1m(\u001b[0mvelocity_x\u001b[1m)\u001b[0m float64 \u001b[1;36m0.0\u001b[0m \u001b[1;36m2.5\u001b[0m \u001b[1;36m5.0\u001b[0m \u001b[1;36m7.5\u001b[0m \u001b[1;36m10.0\u001b[0m\n", "Attributes:\n", " my_attribute_name: some meta information" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 5\n", "\n", "values_pos = np.linspace(-5, 5, n)\n", "dimensions_pos = (\"position_x\",)\n", "# the \"unit\" and \"long_name\" are a convention for automatic plotting\n", "attrs_pos = dict(unit=\"m\", long_name=\"Position\") # attributes of this data variable\n", "\n", "values_vel = np.linspace(0, 10, n)\n", "dimensions_vel = (\"velocity_x\",)\n", "attrs_vel = dict(unit=\"m/s\", long_name=\"Velocity\")\n", "\n", "data_vars = dict(\n", " position=(dimensions_pos, values_pos, attrs_pos),\n", " velocity=(dimensions_vel, values_vel, attrs_vel),\n", ")\n", "\n", "dataset_attrs = dict(my_attribute_name=\"some meta information\")\n", "\n", "dataset = xr.Dataset(\n", " data_vars=data_vars,\n", " attrs=dataset_attrs,\n", ") # dataset attributes\n", "dataset" ] }, { "cell_type": "code", "execution_count": 3, "id": "dc96be76", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\u001b[1;35mFrozenMappingWarningOnValuesAccess\u001b[0m\u001b[1m(\u001b[0m\u001b[1m{\u001b[0m\u001b[32m'position_x'\u001b[0m: \u001b[1;36m5\u001b[0m, \u001b[32m'velocity_x'\u001b[0m: \u001b[1;36m5\u001b[0m\u001b[1m}\u001b[0m\u001b[1m)\u001b[0m"
]
},
"execution_count": 3,
"output_type": "execute_result"
}
],
"source": [
"dataset.dims"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f4b62946",
"outputs": [
{
"data": {
"text/html": [
"
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\n",
"\u001b[1;35mFrozen\u001b[0m\u001b[1m(\u001b[0m\u001b[1m{\u001b[0m\u001b[32m'position'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mxarray.Variable\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mposition_x: \u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m>\u001b[0m\n",
"\u001b[1;35marray\u001b[0m\u001b[1;39m(\u001b[0m\u001b[1;39m[\u001b[0m\u001b[1;36m-5\u001b[0m\u001b[39m. , \u001b[0m\u001b[1;36m-2.5\u001b[0m\u001b[39m,  \u001b[0m\u001b[1;36m0\u001b[0m\u001b[39m. ,  \u001b[0m\u001b[1;36m2.5\u001b[0m\u001b[39m,  \u001b[0m\u001b[1;36m5\u001b[0m\u001b[39m. \u001b[0m\u001b[1;39m]\u001b[0m\u001b[1;39m)\u001b[0m\n",
"\u001b[39mAttributes:\u001b[0m\n",
"\u001b[39m    unit:       m\u001b[0m\n",
"\u001b[39m    long_name:  Position, \u001b[0m\u001b[32m'velocity'\u001b[0m\u001b[39m: \u001b[0m\n",
"\u001b[1;35marray\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m. ,  \u001b[1;36m2.5\u001b[0m,  \u001b[1;36m5\u001b[0m. ,  \u001b[1;36m7.5\u001b[0m, \u001b[1;36m10\u001b[0m. \u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n",
"Attributes:\n",
"    unit:       m/s\n",
"    long_name:  Velocity\u001b[1m}\u001b[0m\u001b[1m)\u001b[0m"
]
},
"execution_count": 4,
"output_type": "execute_result"
}
],
"source": [
"dataset.variables"
]
},
{
"cell_type": "markdown",
"id": "4898cb49",
"source": [
"A variable can be \"promoted\" to (or defined as) a **Coordinate** for its dimension(s):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a180b23c",
"outputs": [
{
"data": {
"text/html": [
"
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.Dataset>\n",
"Dimensions:   (position_x: 5)\n",
"Coordinates:\n",
"    position  (position_x) float64 -5.0 -2.5 0.0 2.5 5.0\n",
"Dimensions without coordinates: position_x\n",
"Data variables:\n",
"    velocity  (position_x) float64 26.0 7.25 1.0 7.25 26.0\n",
"Attributes:\n",
"    my_attribute_name:  some meta information```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.Dataset\u001b[0m\u001b[1m>\u001b[0m\n", "Dimensions: \u001b[1m(\u001b[0mposition_x: \u001b[1;36m5\u001b[0m\u001b[1m)\u001b[0m\n", "Coordinates:\n", " position \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m-5.0\u001b[0m \u001b[1;36m-2.5\u001b[0m \u001b[1;36m0.0\u001b[0m \u001b[1;36m2.5\u001b[0m \u001b[1;36m5.0\u001b[0m\n", "Dimensions without coordinates: position_x\n", "Data variables:\n", " velocity \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m26.0\u001b[0m \u001b[1;36m7.25\u001b[0m \u001b[1;36m1.0\u001b[0m \u001b[1;36m7.25\u001b[0m \u001b[1;36m26.0\u001b[0m\n", "Attributes:\n", " my_attribute_name: some meta information" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values_vel = 1 + values_pos**2\n", "data_vars = dict(\n", " position=(dimensions_pos, values_pos, attrs_pos),\n", " # now the velocity array \"lies\" along the same dimension as the position array\n", " velocity=(dimensions_pos, values_vel, attrs_vel),\n", ")\n", "dataset = xr.Dataset(\n", " data_vars=data_vars,\n", " # NB We could set \"position\" as a coordinate directly when creating the dataset:\n", " # coords=dict(position=(dimensions_pos, values_pos, attrs_pos)),\n", " attrs=dataset_attrs,\n", ")\n", "\n", "# Promote the \"position\" variable to a coordinate:\n", "# In general, most of the functions that modify the structure of the xarray dataset will\n", "# return a new object, hence the assignment\n", "dataset = dataset.set_coords([\"position\"])\n", "dataset" ] }, { "cell_type": "code", "execution_count": 6, "id": "8e5f2caa", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.DataArray 'position' (position_x: 5)>\n",
"array([-5. , -2.5,  0. ,  2.5,  5. ])\n",
"Coordinates:\n",
"    position  (position_x) float64 -5.0 -2.5 0.0 2.5 5.0\n",
"Dimensions without coordinates: position_x\n",
"Attributes:\n",
"    unit:       m\n",
"    long_name:  Position```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.DataArray\u001b[0m\u001b[39m \u001b[0m\u001b[32m'position'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mposition_x: \u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1;35marray\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m-5\u001b[0m. , \u001b[1;36m-2.5\u001b[0m, \u001b[1;36m0\u001b[0m. , \u001b[1;36m2.5\u001b[0m, \u001b[1;36m5\u001b[0m. \u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n", "Coordinates:\n", " position \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m-5.0\u001b[0m \u001b[1;36m-2.5\u001b[0m \u001b[1;36m0.0\u001b[0m \u001b[1;36m2.5\u001b[0m \u001b[1;36m5.0\u001b[0m\n", "Dimensions without coordinates: position_x\n", "Attributes:\n", " unit: m\n", " long_name: Position" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.coords[\"position\"]" ] }, { "cell_type": "markdown", "id": "1804e77a", "metadata": {}, "source": [ "Note that the xarray coordinates are available as variables as well:" ] }, { "cell_type": "code", "execution_count": 7, "id": "a4a2851c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.Variable (position_x: 5)>\n",
"array([-5. , -2.5,  0. ,  2.5,  5. ])\n",
"Attributes:\n",
"    unit:       m\n",
"    long_name:  Position```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.Variable\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mposition_x: \u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1;35marray\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m-5\u001b[0m. , \u001b[1;36m-2.5\u001b[0m, \u001b[1;36m0\u001b[0m. , \u001b[1;36m2.5\u001b[0m, \u001b[1;36m5\u001b[0m. \u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n", "Attributes:\n", " unit: m\n", " long_name: Position" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.variables[\"position\"]" ] }, { "cell_type": "markdown", "id": "8b12f767", "metadata": {}, "source": [ "Which, on its own, might not be very useful yet, however, xarray coordinates can be set\n", "to **index** other variables ({func}`~quantify_core.data.handling.to_gridded_dataset`\n", "does this for the Quantify dataset), as shown below (note the bold font in the output!):" ] }, { "cell_type": "code", "execution_count": 8, "id": "f58a7fa2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.Dataset>\n",
"Dimensions:     (position_x: 5)\n",
"Coordinates:\n",
"  * position_x  (position_x) float64 -5.0 -2.5 0.0 2.5 5.0\n",
"Data variables:\n",
"    velocity    (position_x) float64 26.0 7.25 1.0 7.25 26.0\n",
"Attributes:\n",
"    my_attribute_name:  some meta information```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.Dataset\u001b[0m\u001b[1m>\u001b[0m\n", "Dimensions: \u001b[1m(\u001b[0mposition_x: \u001b[1;36m5\u001b[0m\u001b[1m)\u001b[0m\n", "Coordinates:\n", " * position_x \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m-5.0\u001b[0m \u001b[1;36m-2.5\u001b[0m \u001b[1;36m0.0\u001b[0m \u001b[1;36m2.5\u001b[0m \u001b[1;36m5.0\u001b[0m\n", "Data variables:\n", " velocity \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m26.0\u001b[0m \u001b[1;36m7.25\u001b[0m \u001b[1;36m1.0\u001b[0m \u001b[1;36m7.25\u001b[0m \u001b[1;36m26.0\u001b[0m\n", "Attributes:\n", " my_attribute_name: some meta information" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset = dataset.set_index({\"position_x\": \"position\"})\n", "dataset.position_x.attrs[\"unit\"] = \"m\"\n", "dataset.position_x.attrs[\"long_name\"] = \"Position x\"\n", "dataset" ] }, { "cell_type": "markdown", "id": "b74e4c79", "metadata": {}, "source": [ "At this point the reader might get very confused. In an attempt to clarify, we now have\n", "a dimension, a coordinate and a variable with the same name `\"position_x\"`." ] }, { "cell_type": "code", "execution_count": 9, "id": "e8dcb228", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\u001b[1m(\u001b[0m\u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m\u001b[1m)\u001b[0m"
]
},
"execution_count": 9,
"output_type": "execute_result"
}
],
"source": [
"(\n",
"    \"position_x\" in dataset.dims,\n",
"    \"position_x\" in dataset.coords,\n",
"    \"position_x\" in dataset.variables,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8c96c7c1",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.9/site-packages/xarray/core/utils.py:494: FutureWarning: The return type of `Dataset.dims` will be changed to return a set of dimension names in future, in order to be more consistent with `DataArray.dims`. To access a mapping from dimension names to lengths, please use `Dataset.sizes`.\n",
"  warnings.warn(\n"
]
},
{
"data": {
"text/html": [
"
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\u001b[1;36m5\u001b[0m"
]
},
"execution_count": 10,
"output_type": "execute_result"
}
],
"source": [
"dataset.dims[\"position_x\"]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "1b38f2df",
"outputs": [
{
"data": {
"text/html": [
"
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.DataArray 'position_x' (position_x: 5)>\n",
"array([-5. , -2.5,  0. ,  2.5,  5. ])\n",
"Coordinates:\n",
"  * position_x  (position_x) float64 -5.0 -2.5 0.0 2.5 5.0\n",
"Attributes:\n",
"    unit:       m\n",
"    long_name:  Position x```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.DataArray\u001b[0m\u001b[39m \u001b[0m\u001b[32m'position_x'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mposition_x: \u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1;35marray\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m-5\u001b[0m. , \u001b[1;36m-2.5\u001b[0m, \u001b[1;36m0\u001b[0m. , \u001b[1;36m2.5\u001b[0m, \u001b[1;36m5\u001b[0m. \u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n", "Coordinates:\n", " * position_x \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m-5.0\u001b[0m \u001b[1;36m-2.5\u001b[0m \u001b[1;36m0.0\u001b[0m \u001b[1;36m2.5\u001b[0m \u001b[1;36m5.0\u001b[0m\n", "Attributes:\n", " unit: m\n", " long_name: Position x" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.coords[\"position_x\"]" ] }, { "cell_type": "code", "execution_count": 12, "id": "bdc518fc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.IndexVariable 'position_x' (position_x: 5)>\n",
"array([-5. , -2.5,  0. ,  2.5,  5. ])\n",
"Attributes:\n",
"    unit:       m\n",
"    long_name:  Position x```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.IndexVariable\u001b[0m\u001b[39m \u001b[0m\u001b[32m'position_x'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mposition_x: \u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1;35marray\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m-5\u001b[0m. , \u001b[1;36m-2.5\u001b[0m, \u001b[1;36m0\u001b[0m. , \u001b[1;36m2.5\u001b[0m, \u001b[1;36m5\u001b[0m. \u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n", "Attributes:\n", " unit: m\n", " long_name: Position x" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.variables[\"position_x\"]" ] }, { "cell_type": "markdown", "id": "d6b85f15", "metadata": {}, "source": [ "Here the intention is to make the reader aware of this peculiar behavior.\n", "Please consult the {doc}`xarray documentation ` for more details.\n", "\n", "An example of how this can be useful is to retrieve data from an xarray variable using\n", "one of its coordinates to select the desired entries:" ] }, { "cell_type": "code", "execution_count": 13, "id": "e19a7914", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.DataArray 'velocity' (position_x: 5)>\n",
"array([26.  ,  7.25,  1.  ,  7.25, 26.  ])\n",
"Coordinates:\n",
"  * position_x  (position_x) float64 -5.0 -2.5 0.0 2.5 5.0\n",
"Attributes:\n",
"    unit:       m/s\n",
"    long_name:  Velocity```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.DataArray\u001b[0m\u001b[39m \u001b[0m\u001b[32m'velocity'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mposition_x: \u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1;35marray\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m26\u001b[0m. , \u001b[1;36m7.25\u001b[0m, \u001b[1;36m1\u001b[0m. , \u001b[1;36m7.25\u001b[0m, \u001b[1;36m26\u001b[0m. \u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n", "Coordinates:\n", " * position_x \u001b[1m(\u001b[0mposition_x\u001b[1m)\u001b[0m float64 \u001b[1;36m-5.0\u001b[0m \u001b[1;36m-2.5\u001b[0m \u001b[1;36m0.0\u001b[0m \u001b[1;36m2.5\u001b[0m \u001b[1;36m5.0\u001b[0m\n", "Attributes:\n", " unit: m/s\n", " long_name: Velocity" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.velocity" ] }, { "cell_type": "code", "execution_count": 14, "id": "50f7acd8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n", "
```<xarray.DataArray 'velocity' ()>\n",
"array(7.25)\n",
"Coordinates:\n",
"    position_x  float64 2.5\n",
"Attributes:\n",
"    unit:       m/s\n",
"    long_name:  Velocity```
" ], "text/plain": [ "\n", "\u001b[1m<\u001b[0m\u001b[1;95mxarray.DataArray\u001b[0m\u001b[39m \u001b[0m\u001b[32m'velocity'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m\n", "\u001b[1;35marray\u001b[0m\u001b[1m(\u001b[0m\u001b[1;36m7.25\u001b[0m\u001b[1m)\u001b[0m\n", "Coordinates:\n", " position_x float64 \u001b[1;36m2.5\u001b[0m\n", "Attributes:\n", " unit: m/s\n", " long_name: Velocity" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "retrieved_value = dataset.velocity.sel(position_x=2.5)\n", "retrieved_value" ] }, { "cell_type": "markdown", "id": "550fee7f", "metadata": {}, "source": [ "Note that without this feature we would have to keep track of numpy integer indexes to\n", "retrieve the desired data:" ] }, { "cell_type": "code", "execution_count": 15, "id": "afa78109", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\u001b[1m(\u001b[0m\u001b[1;36m7.25\u001b[0m, \u001b[3;92mTrue\u001b[0m\u001b[1m)\u001b[0m"
]
},
"execution_count": 15,
"output_type": "execute_result"
}
],
"source": [
"dataset.velocity.values[3], retrieved_value.values == dataset.velocity.values[3]"
]
},
{
"cell_type": "markdown",
"id": "ca6a51d7",
"source": [
"One of the great features of xarray is automatic plotting (explore the xarray\n",
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5d37260f",
"outputs": [
{
"data": {
"text/html": [
"
\n"
],
"text/plain": []
},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\u001b[1m<\u001b[0m\u001b[1;95mFigure\u001b[0m\u001b[39m size 64\u001b[0m\u001b[1;36m0x480\u001b[0m\u001b[39m with \u001b[0m\u001b[1;36m1\u001b[0m\u001b[39m Axes\u001b[0m\u001b[1m>\u001b[0m"
]
},
"output_type": "display_data"
}
],
"source": [
"_ = dataset.velocity.plot(marker=\"o\")"
]
},
{
"cell_type": "markdown",
"id": "23aa150d",
"source": [
"Note the automatic labels and unit."
]
}
],
"file_format": "mystnb",
"jupytext": {
"text_representation": {
"extension": ".md",
"format_name": "myst"
}
},
"kernelspec": {
"display_name": "python3",
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}