Skip to content

Commit ce313de

Browse files
maarten-icolivhoenen
authored andcommitted
Additional documentation for imas.util.to_xarray
1 parent a56a5e7 commit ce313de

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

docs/source/courses/advanced/xarray.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Create ``xarray.DataArray`` from an IDS
88
recommend to use :py:func:`imas.util.to_xarray` instead of manually creating
99
xarray ``DataArray``\ s.
1010

11+
See also: :ref:`Convert IMAS-Python IDSs directly to Xarray Datasets`.
12+
1113
Let's start with an introduction of Xarray. According to `their website
1214
<https://docs.xarray.dev/en/stable/getting-started-guide/why-xarray.html>`_ (where you
1315
can also find an excellent summary of why that is useful):

docs/source/netcdf.rst

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _`IMAS netCDF files`:
22

3-
IMAS netCDF files
4-
=================
3+
IMAS netCDF files \& Xarray
4+
===========================
55

66
.. toctree::
77
:hidden:
@@ -69,6 +69,7 @@ features that are supported by DBEntries using ``imas_core`` respectively
6969
- Yes (requires ``imas_core >= 5.4.0``)
7070
- Not implemented
7171

72+
.. _`Using IMAS netCDF files with 3rd-party tools`:
7273

7374
Using IMAS netCDF files with 3rd-party tools
7475
--------------------------------------------
@@ -138,3 +139,47 @@ Validating an IMAS netCDF file
138139
IMAS netCDF files can be validated with IMAS-Python through the command line ``imas
139140
validate_nc <filename>``. See also :ref:`IMAS-Python Command Line tool` or type
140141
``imas validate_nc --help`` in a command line.
142+
143+
144+
.. _`Convert IMAS-Python IDSs directly to Xarray Datasets`:
145+
146+
Convert IMAS-Python IDSs directly to Xarray Datasets
147+
----------------------------------------------------
148+
149+
In the section :ref:`Using IMAS netCDF files with 3rd-party tools`, we showed
150+
how to open an IMAS netCDF file with Xarray. However, IMAS-Python IDSs can also
151+
be converted directly to Xarray ``Dataset``\ s with
152+
:py:func:`imas.util.to_xarray`.
153+
154+
This method can be used to convert a full IDS to an Xarray ``Dataset``, or only
155+
specific paths inside the IDS. The latter variant can also be combined with
156+
:ref:`lazy loading`. We'll show a small example below:
157+
158+
.. code-block:: python
159+
:caption: Converting a lazy loaded IDS to Xarray
160+
161+
import imas.training
162+
163+
# Open the training entry
164+
with imas.training.get_training_db_entry() as training_entry:
165+
# Lazy load the core_profiles IDS
166+
core_profiles = training_entry.get("core_profiles", lazy=True)
167+
# Load the average ion temperature and all coordinate data
168+
xrds = imas.util.to_xarray(core_profiles, "profiles_1d.t_i_average")
169+
# All relevant data is now loaded from the data entry into the xarray
170+
# Dataset. We close the data entry by exiting the with-statement.
171+
172+
# Inspect what's inside the dataset
173+
print(xrds.data_vars)
174+
# Data variables:
175+
# profiles_1d.t_i_average
176+
177+
# Included coordinates depends on the used Data Dictionary version
178+
print(xrds.coords)
179+
# Coordinates: (with DD 4.0.0)
180+
# * time
181+
# profiles_1d.grid.area
182+
# profiles_1d.grid.volume
183+
# profiles_1d.grid.rho_tor
184+
# profiles_1d.grid.rho_tor_norm
185+
# profiles_1d.grid.psi

0 commit comments

Comments
 (0)