API reference

xemc3 Dataset

All of the functions that are part of the EMC3DatasetAccessor can be accessed via the emc3 dataset accessor. For a given dataset ds they can be reached as ds.emc3.*, e.g. ds.emc3.plot_rz(...).

Additional functions for working with EMC3 data.

xemc3.EMC3DatasetAccessor.evaluate_at_diagnostic(self, diag, key=None, num_points=100)
xemc3.EMC3DatasetAccessor.evaluate_at_rpz(self, r, phi, z, key=None, periodicity: int = 5, updownsym: bool = True, delta_phi: float | None = None, fill_value=None, lazy=False, progress=False)

Evaluate the field key in the dataset at the positions given by the array r, phi, z. If key is None, return the indices to access the 3D field and get the appropriate values.

Parameters:
  • r (array-like) – The (major) radial coordinates to evaluate

  • phi (array-like) – The toroidal coordinate

  • z (array-like) – The z component

  • key (None or str or sequence of str) – If None return the index-coordinates otherwise evaluate the specified field in the dataset

  • periodicity (int) – The rotational symmetry in toroidal direction

  • updownsym (bool) – Whether the data is additionally up-down symmetric with half the periodicity.

  • delta_phi (None or float) – If not None, delta_phi gives the accuracy of the precision at which phi is evaluated. Giving a float enables caching of the phi slices, and can speed up computation. Note that it should be smaller then the toroidal resolution. For a grid with 1° resolution, delta_phi=2 * np.pi / 360 would be the upper bound. None disables caching.

  • fill_value (None or any) – If fill_value is None, missing data is initialised as np.nan, or as -1 for non-float datatypes. Otherwise fill_value is used to set missing data.

  • lazy (bool) – Force the loading of the data for key. Defaults to False. Can significantly decrease performance, but can decrease memory usage.

  • progress (bool) – Show the progress of the mapping. Defaults to False.

xemc3.EMC3DatasetAccessor.evaluate_at_xyz(self, x, y, z, *args, **kwargs)

See evaluate_at_rpz for options. Unlike evaluate_at_rpz the coordinates are given here in cartesian coordinates.

xemc3.EMC3DatasetAccessor.from_fort(self, fn, skip_first=0, ignore_broken=False, kinetic=False)

Read from an EMC3 file, using the mappings for plasma or kinetic data.

Parameters:
  • fn (str) – The name of the file to be read

  • skip_first (int (optional)) – For each datablock the first skip_first lines are ignored

  • ignore_broken (boolean (optional)) – Incomplete datasets are ignored. This indicates that something is wrong, but is the default for EMC3

  • kinetic (boolean (optional)) – If kinetic then the data is read using the mapping for neutal quantities, otherwise the mapping for plasma quantities is assumed

Returns:

The list of the read quantities

Return type:

list of xr.DataArray

xemc3.EMC3DatasetAccessor.get(self, *args)

Get one or more variables of the dataset.

The shapes are cropped to include only valid data if data for the divertor modules is returened.

The code also transforms to *_corner cases, if *_bounds is present. The bounds version is an array of shape \(n\times...\times m\times 2\times...\times 2\) while the corner version is a \(n+1\times...\times m+1\) dimensional array.

Parameters:

args (list of str) – The keys of the data to get

Returns:

The requested arrays from the dataset

Return type:

list of xr.DataArray

xemc3.EMC3DatasetAccessor.isel(self, indexers: Mapping[str, Any] | None = None, drop: bool = False, missing_dims: Literal['raise', 'warn', 'ignore'] = 'raise', **indexers_kwargs: Any) Dataset
xemc3.EMC3DatasetAccessor.iter_plates(self, *, symmetry=False, segments=1)

Iterate over all plates.

Repeat with stellerator symmetry if symmetry is given. If segments is given, repeat for all segments - assuming a 2*pi/n rotational transform symmetry.

Parameters:
  • symmetry (boolean (optional)) – Iterate over mirrored setup

  • segments (int (optional)) – Iterate over all segments, assuming a symmetry of n=segments.

Returns:

The plates of the divertor plates

Return type:

iterator of xr.Dataset

xemc3.EMC3DatasetAccessor.iter_zones(self)

Iterate over all zones

xemc3.EMC3DatasetAccessor.load(self, path)

Load EMC3 simulation data from path

Parameters:

path (str) – The directory of the EMC3 simulation data

Returns:

The xemc3 dataset with the simulation data

Return type:

xr.Dataset

xemc3.EMC3DatasetAccessor.mean_time(self) Dataset

Average in time.

Workaround for https://github.com/pydata/xarray/issues/4885

xemc3.EMC3DatasetAccessor.plot(self, key, *args, **kw)

Plot some data.

In case of data from data from the plates, a 3D plot of the divertor is returned.

In case of 3D data a volume plot is returend.

Otherwise xarray is used for plotting. Axes information might be missing.

See also: plot_rz

Parameters:

key (str) – The key used for plotting

Returns:

Return value depends on the specific plotting routine used.

Return type:

any

xemc3.EMC3DatasetAccessor.plot_Rz(self, key, phi, **kwargs)
xemc3.EMC3DatasetAccessor.plot_div(self, index, **kwargs)

Plot divertor data.

xemc3.EMC3DatasetAccessor.plot_rz(self, key, phi, **kwargs)

Plot a R-z slice in lab coordinates.

Parameters:
  • key (str or None) – Index of the data to plot. Select None to plot the mesh instead.

  • phi (float) – Angle at which to plot. As always in radian.

  • ax (Axis object (optional)) – Axis object to be used for plotting

  • Rmin (float (optional)) – left bound for plot

  • Rmax (float (option)) – right bound for plot

  • zmin (float (optional)) – lower bound for plot

  • zmax (float (optional)) – upper bound for plot

  • kwargs (Other arguments to be passed to matplotlib)

Returns:

The return value from matplotlib.pyplot.pcolormesh is returned.

Return type:

matplotlib.collections.QuadMesh

xemc3.EMC3DatasetAccessor.sel(self, indexers: Mapping[str, Any] | None = None, drop: bool = False, missing_dims: str = 'raise', **indexers_kwargs: Any) Dataset
xemc3.EMC3DatasetAccessor.to_fort(self, keys, fn, kinetic=False)

Write to text file, using the mappings for plasma or kinetic data.

xemc3.EMC3DatasetAccessor.to_netcdf(self, fn, complevel=1)

Write to netcdf file.

Enables fast compression by default, which helps a lot with the sparse data.

fn: string

The path of the netcdf file to be written.

complevel: integer (optional)

The compression level in the range 1 to 9

xemc3.EMC3DatasetAccessor.tracer()
xemc3.EMC3DatasetAccessor.unit(self, var, value=None)

Get or set the units of a quantity

xemc3.load module

Note

Please ensure that files are linked to their EMC3 names, if you use alternative file names.

Routines for reading EMC3 files.

xemc3.load.all(path: str, ignore_missing: bool | None = None, ignore_broken: bool | None = None) Dataset

Load all data from a path and return as dataset

Parameters:
  • path (str) – Directory that contains files to be read

  • ignore_missing (None or bool) – True: ignore missing files. False: raise exceptions if a file is not found. None: use default option for that file.

  • ignore_broken (None or bool) – True: ignore if files are incomplete. False: raise exceptions if a file is incomplete. None: use default option for that file.

Returns:

A dataset with all the simulation data. Unless ignore_missing=False has been set, some data might be missing because it was not found. However ignore_missing=False has the disadvantage that files added in later versions will cause errors if the files are not present.

Return type:

xr.Dataset

xemc3.load.any(path: str, *args, **kwargs) Dataset

Read a file or directory. For possible kwargs see the respective functions that are called:

For a directory xemc3.load.all is called.

xemc3.load.plates for the “TARGET_PROFILES” file.

xemc3.load.file otherwise.

xemc3.load.file(fn: str, ds: None | Dataset | DataArray = None, **opts) Dataset

Read a EMC3 simulation file. The expected content is derived from the filename.

Parameters:
  • fn (str) – The location of the file to read

  • ds (xr.DataArray or xr.Dataset or None) – The mapping or a dataset containing a mapping or None. If one is needed and none is provided, it is read from the folder.

  • opts (dict) – Additional options depending on the type of file that is read.

Returns:

The read variable is set. If a Dataset was given, the newly read entries from the file are added. Otherwise a new Dataset is returned with the read variables added.

Return type:

xr.Dataset

xemc3.load.mapped_raw(fn: str, mapping: Dataset | DataArray, skip_first: int = 0, ignore_broken: bool = False, kinetic: bool = False, unmapped: bool = False, dtype: DTypeLike = <class 'float'>, squeeze: bool = True) Sequence[DataArray]

Read a file with the EMC3 mapping. Note that this function does not add meta-data or does normalisation of the data, thus xemc3.load.file is generally preferred.

Parameters:
  • fn (str) – The full path of the file to read

  • mapping (xr.DataArray or xr.Dataset) – The dataarray of the mesh mapping or a dataset containing the mapping

  • skip_first (int (optional)) – Ignore the first n lines. Default 0

  • ignore_broken (bool (optional)) – if incomplete datasets at the end should be ignored. Default: False

  • kinetic (bool (optional)) – The file contains also data for cells that are only evolved by EIRENE, rather then EMC3. Default: False

  • unmapped (bool (optional)) – The file contains unmapped data, i.e. on value for each cell. Default: False

  • dtype (datatype (optional)) – The type of the data, e.g. float or int. Is passed to numpy. Default: float

  • squeeze (bool) – If True return a DataArray if only one field is read.

Returns:

The data that has been read from the file. If squeeze is True and only one field is read only a single DataArray is returned.

Return type:

xr.DataArray or list of xr.DataArray

xemc3.load.plates(dir: str, cache: bool = True) Dataset

Read the target fluxes from the EMC3_post processing routine

Parameters:
  • dir (str) – The directory in which the file is

  • cache (bool (optional)) – Whether to check whether a netcdf file is present, and if not create one. This can result in faster reading the next time.

Returns:

The target profiles

Return type:

xr.Dataset

xemc3.load.plates_geom(filename: str | Sequence[str]) Dataset

Read Target structures from a file that is in the Kisslinger format as used by EMC3.

Parameters:

filename (str or sequence of str) – The location of the file to read

Returns:

The coordinates

Return type:

xr.Dataset

xemc3.load.var(dir: str, var: str, ds: None | Dataset | DataArray = None) Dataset

Read the variable var from the simulation in directory dir.

Parameters:
  • dir (str) – The path of the simulation directory

  • var (str) – The name of the variable to be read

  • ds (xr.DataArray or xr.Dataset or None (optional)) – An xemc3 Dataset or an DataArray containing the mapping. If not given and a mapping is required to read the file, it is read from the directory.

Returns:

A dataset in which at least var is set. If there are other variables in the read file, it is also added. Finally, mapping and other variables that are required to read are also added.

Return type:

xr.Dataset

xemc3.write module

Routines for writing EMC3 files.

xemc3.write.fortran.all(ds, dir)

Write all files to directory dir

Parameters:
  • ds (xr.Dataset) – The xemc3 dataset

  • dir (str) – The directory to write the files to

xemc3.write.fortran.coordinates(ds: Dataset, fn: str) None

Write spatial positions of grid points in EMC3 format.

Parameters:
  • ds (xr.Dataset) – The dataset with the EMC3 simulation data

  • fn (str) – The filename to write to

xemc3.write.fortran.magnetic_field(path: str, ds: Dataset) None

Write the magnetic field to a file

Parameters:
  • path (str) – The filename to write to

  • ds (xr.Dataset) – The xemc3 dataset with the magnetic field

xemc3.write.fortran.mapped(ds: Dataset, dir: str, fn: str | None = None, **args) None

Write a file for EMC3 using the mapped format.

Parameters:
  • ds (xr.Dataset) – A xemc3 dataset

  • dir (str) – The directory to which to write the file

  • fn (str or None (optional)) – In case of None all mapped files are written. In the case of a str only that file is written. Any missing data is ignored. Thus if a file is specified, but the data hasn’t been loaded this is ignored.

  • args (dict (optional)) – Can be used to overwrite options for writing. Defaults to the options used for that file.

xemc3.write.fortran.plates_mag(fn: str, ds: Dataset) None

Write the PLATES_MAG info to a file in the EMC3 format.

Parameters:
  • fn (str) – The file to write to

  • ds (xr.Dataset or xr.DataArray) – either the PLATES_MAG dataarray or a dataset containing PLATES_MAG