|
1 | 1 | import os
|
2 | 2 |
|
3 | 3 | import matplotlib
|
| 4 | + |
4 | 5 | # To avoid possible display issues when Matplotlib uses a non-GUI backend
|
5 | 6 | if "DISPLAY" not in os.environ:
|
6 | 7 | matplotlib.use("agg")
|
|
17 | 18 | entry = imas.training.get_training_db_entry()
|
18 | 19 | cp = entry.get("core_profiles")
|
19 | 20 |
|
| 21 | +####################################################################################### |
| 22 | +# Steps 2, 3 and 4, using imas.util.to_xarray |
| 23 | +# Create an xarray Dataset containing t_i_average and its coordinates |
| 24 | +xrds = imas.util.to_xarray(cp, "profiles_1d/t_i_average") |
| 25 | +# Note that profiles_1d.grid.rho_tor_norm is a 2D coordinate: its values may be |
| 26 | +# different at different times. |
| 27 | +# |
| 28 | +# Since the values at different time slices differ only minutely in this example, we'll |
| 29 | +# rename the `profiles_1d.grid.rho_tor_norm:i` dimension to `rho_tor_norm` and set the |
| 30 | +# values to the values of rho_tor_norm of the first time slice: |
| 31 | +xrds = xrds.rename({"profiles_1d.grid.rho_tor_norm:i": "rho_tor_norm"}).assign_coords( |
| 32 | + {"rho_tor_norm": xrds["profiles_1d.grid.rho_tor_norm"].isel(time=0).data} |
| 33 | +) |
| 34 | + |
| 35 | +# Extract temperatures as an xarray DataArray |
| 36 | +temperature = xrds["profiles_1d.t_i_average"] |
| 37 | + |
| 38 | +# 5a. Select subset of temperature where 0.4 <= rho_tor_norm < 0.6: |
| 39 | +print(temperature.sel(rho_tor_norm=slice(0.4, 0.6))) |
| 40 | + |
| 41 | +# 5b. Interpolate temperature on a new grid: [0, 0.1, 0.2, ..., 0.9, 1.0] |
| 42 | +print(temperature.interp(rho_tor_norm=numpy.linspace(0, 1, 11))) |
| 43 | + |
| 44 | +# 5c. Interpolate temperature on a new time base: [10, 20] |
| 45 | +print(temperature.interp(time=[10, 20])) |
| 46 | + |
| 47 | +# 5d. Plot |
| 48 | +temperature.plot(x="time", norm=matplotlib.colors.LogNorm()) |
| 49 | +plt.show() |
| 50 | + |
| 51 | +####################################################################################### |
| 52 | +# We can also manually build an xarray DataArray, this is shown below: |
| 53 | + |
20 | 54 | # 2. Store the temperature of the first time slice
|
21 | 55 | temperature = cp.profiles_1d[0].t_i_average
|
22 | 56 |
|
|
0 commit comments