|
1 | 1 | .. _`IMAS netCDF files`:
|
2 | 2 |
|
3 |
| -IMAS netCDF files |
4 |
| -================= |
| 3 | +IMAS netCDF files \& Xarray |
| 4 | +=========================== |
5 | 5 |
|
6 | 6 | .. toctree::
|
7 | 7 | :hidden:
|
@@ -69,6 +69,7 @@ features that are supported by DBEntries using ``imas_core`` respectively
|
69 | 69 | - Yes (requires ``imas_core >= 5.4.0``)
|
70 | 70 | - Not implemented
|
71 | 71 |
|
| 72 | +.. _`Using IMAS netCDF files with 3rd-party tools`: |
72 | 73 |
|
73 | 74 | Using IMAS netCDF files with 3rd-party tools
|
74 | 75 | --------------------------------------------
|
@@ -138,3 +139,47 @@ Validating an IMAS netCDF file
|
138 | 139 | IMAS netCDF files can be validated with IMAS-Python through the command line ``imas
|
139 | 140 | validate_nc <filename>``. See also :ref:`IMAS-Python Command Line tool` or type
|
140 | 141 | ``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