@@ -183,3 +183,39 @@ specific paths inside the IDS. The latter variant can also be combined with
183183 # profiles_1d.grid.rho_tor
184184 # profiles_1d.grid.rho_tor_norm
185185 # profiles_1d.grid.psi
186+
187+
188+ Store Xarray Datasets in IMAS-compatible netCDF file
189+ ''''''''''''''''''''''''''''''''''''''''''''''''''''
190+
191+ .. versionadded :: 2.3.0 :py:meth:`~imas.util.to_xarray` now includes the required
192+ metadata to load the IDS from a netCDF file.
193+
194+ The following snippet shows how to store an IMAS Xarray dataset in an IMAS-compatible
195+ netCDF file. The group name in the netCDF file must correspond to ``<IDS
196+ Name>/<occurrence> `` (``core_profiles/0 `` in the snippet).
197+
198+ .. code-block :: python
199+ :caption: Store IMAS Xarray dataset in an IMAS - compatible netCDF file
200+
201+ import imas.training
202+ import netCDF4
203+
204+ with imas.training.get_training_db_entry() as training_entry:
205+ core_profiles = training_entry.get(" core_profiles" )
206+ xrds = imas.util.to_xarray(core_profiles)
207+
208+ # Store the xarray dataset in an IMAS-compatible netCDF dataset
209+ filename = " data.nc"
210+ xrds.to_netcdf(
211+ filename,
212+ group = " core_profiles/0" , # Update to the correct IDS name and occurrence
213+ # auto_complex=True, # Uncomment if the dataset contains complex data
214+ )
215+ # Set global DD version metadata
216+ with netCDF4.Dataset(filename, " a" ) as ds:
217+ ds.data_dictionary_version = imas.util.get_data_dictionary_version(ids)
218+
219+ # Test that we can get the IDS from the netCDF file
220+ with imas.DBEntry(filename, " r" ) as entry:
221+ ids2 = entry.get(" core_profiles" )
0 commit comments