Skip to content

Commit a56a5e7

Browse files
maarten-icolivhoenen
authored andcommitted
Update xarray advanced course to mention imas.util.to_xarray
1 parent 78b89f6 commit a56a5e7

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

docs/source/courses/advanced/imas_snippets/tensorized_ids_to_xarray.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import matplotlib
4+
45
# To avoid possible display issues when Matplotlib uses a non-GUI backend
56
if "DISPLAY" not in os.environ:
67
matplotlib.use("agg")
@@ -17,6 +18,39 @@
1718
entry = imas.training.get_training_db_entry()
1819
cp = entry.get("core_profiles")
1920

21+
#######################################################################################
22+
# Steps 2, 3 and 4, using imas.util.to_xarray
23+
# Create an xarray Dataset containing t_i_average and its coordinates
24+
xrds = imas.util.to_xarray(cp, "profiles_1d/t_i_average")
25+
# Note that profiles_1d.grid.rho_tor_norm is a 2D coordinate: its values may be
26+
# different at different times.
27+
#
28+
# Since the values at different time slices differ only minutely in this example, we'll
29+
# rename the `profiles_1d.grid.rho_tor_norm:i` dimension to `rho_tor_norm` and set the
30+
# values to the values of rho_tor_norm of the first time slice:
31+
xrds = xrds.rename({"profiles_1d.grid.rho_tor_norm:i": "rho_tor_norm"}).assign_coords(
32+
{"rho_tor_norm": xrds["profiles_1d.grid.rho_tor_norm"].isel(time=0).data}
33+
)
34+
35+
# Extract temperatures as an xarray DataArray
36+
temperature = xrds["profiles_1d.t_i_average"]
37+
38+
# 5a. Select subset of temperature where 0.4 <= rho_tor_norm < 0.6:
39+
print(temperature.sel(rho_tor_norm=slice(0.4, 0.6)))
40+
41+
# 5b. Interpolate temperature on a new grid: [0, 0.1, 0.2, ..., 0.9, 1.0]
42+
print(temperature.interp(rho_tor_norm=numpy.linspace(0, 1, 11)))
43+
44+
# 5c. Interpolate temperature on a new time base: [10, 20]
45+
print(temperature.interp(time=[10, 20]))
46+
47+
# 5d. Plot
48+
temperature.plot(x="time", norm=matplotlib.colors.LogNorm())
49+
plt.show()
50+
51+
#######################################################################################
52+
# We can also manually build an xarray DataArray, this is shown below:
53+
2054
# 2. Store the temperature of the first time slice
2155
temperature = cp.profiles_1d[0].t_i_average
2256

docs/source/courses/advanced/xarray.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ Create ``xarray.DataArray`` from an IDS
33

44
.. info::
55

6-
In this lesson you will create a ``DataArray`` manually. In a future version of
7-
IMAS-Python we plan to include functionality that will automatically do this for you.
8-
That should further simplify working with data inside IDSs.
6+
This lesson was written before :py:func:`imas.util.to_xarray` was
7+
implemented. This lesson is retained for educational purposes, however we
8+
recommend to use :py:func:`imas.util.to_xarray` instead of manually creating
9+
xarray ``DataArray``\ s.
910

1011
Let's start with an introduction of Xarray. According to `their website
1112
<https://docs.xarray.dev/en/stable/getting-started-guide/why-xarray.html>`_ (where you
@@ -61,6 +62,10 @@ Exercise 1: create a ``DataArray`` for ``profiles_1d/temperature``
6162

6263
.. md-tab-item:: Solution
6364

65+
This exercise was created before the implementation of
66+
:py:func:`imas.util.to_xarray`. The original approach is available below
67+
for educational purposes.
68+
6469
.. literalinclude:: imas_snippets/ids_to_xarray.py
6570

6671

@@ -96,4 +101,9 @@ the ``profiles_1d`` array of structures. When the grid is not changing in the ID
96101

97102
.. md-tab-item:: Solution
98103

104+
This exercise was created before the implementation of
105+
:py:func:`imas.util.to_xarray`. Below code sample is updated to provide
106+
two alternatives: the first is based on :py:func:`imas.util.to_xarray`,
107+
the second is the original, manual approach.
108+
99109
.. literalinclude:: imas_snippets/tensorized_ids_to_xarray.py

0 commit comments

Comments
 (0)