Skip to content

Examples

Modify data

Below is an example of how to use imas2xarray to data in-place.

Note that Imas2xarray can only update data in-place, i.e. the new data must have the same shape as the existing data.

from imas2xarray import to_imas, to_xarray

variables = ('rho_tor_norm', 'time', 't_e')
ids = 'core_profiles'

dataset = to_xarray(
    '/pfs/work/g2aho/public/imasdb/test/3/92436/1/',
    ids=ids,
    variables=variables,
)

print(dataset['t_e'])
dataset['t_e'] += 1

to_imas(
    '/pfs/work/g2aho/public/imasdb/test/3/92436/1/',
    dataset=dataset,
    ids=ids,
    variables=variables,
)

Source code

Plotting single dataset

Below is an example of how to use imas2xarray to plot data with matplotlib.

import matplotlib.pyplot as plt

from imas2xarray import to_xarray

x_var = 'rho_tor_norm'
y_var = 't_e'
time_var = 'time'

dataset = to_xarray('/pfs/work/g2aho/public/imasdb/test/3/92436/1/', ids='core_profiles')

fig = dataset.plot.scatter(x=x_var, y=y_var, hue=time_var, marker='.')

plt.show()

Source code

Plotting multiple datasets

The code below shows how to make a plot with matplotlib for multiple datasets.

For a more advanced example of how to concatenate data, check out the example notebooks.

import matplotlib.pyplot as plt
import pandas as pd
import xarray as xr

from imas2xarray import to_xarray, standardize_grid_and_time

runs = 1, 2, 3

ids = 'core_profiles'

x_var = 'rho_tor_norm'
y_var = 't_i_ave'
time_var = 'time'

ds_list = []

for run in runs:
    path = f'/pfs/work/g2aho/public/imasdb/test/3/92436/{run}/'

    ds = to_xarray(path, ids=ids, variables=(x_var, y_var, time_var))
    ds_list.append(ds)

ds_list = standardize_grid_and_time(ds_list, grid_var=x_var, time_var=time_var)

dataset = xr.concat(ds_list, pd.Index(runs, name='run'))

fig = dataset.plot.scatter(x=x_var, y=y_var, hue='time', col='run', marker='.')

plt.show()

Source code