diff --git a/.gitignore b/.gitignore index b0849d6..49a2d9c 100644 --- a/.gitignore +++ b/.gitignore @@ -136,3 +136,10 @@ dmypy.json # Pyre type checker .pyre/ + +# data +*.h5 +*.hdf5 +notebooks/1 +notebooks/2 +notebooks/3 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9b1ccd4..d81569e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,14 +25,14 @@ repos: hooks: - id: docformatter - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: v0.1.6 + rev: v0.1.13 hooks: - id: ruff args: [--fix] - id: ruff-format types_or: [python, pyi, jupyter] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.7.1 + rev: v1.8.0 hooks: - id: mypy additional_dependencies: diff --git a/notebooks/prepare.py b/notebooks/prepare.py new file mode 100644 index 0000000..94f32c0 --- /dev/null +++ b/notebooks/prepare.py @@ -0,0 +1,37 @@ +# Artificially modify data for the notebooks +from __future__ import annotations + +from imas2xarray import Variable, to_imas, to_xarray + +variables = ( + Variable( + name='ion_temperature', + ids='core_profiles', + path='profiles_1d/*/ion/*/temperature', + dims=['time', 'ion', '$rho_tor_norm'], + ), + 't_i_ave', +) + +ids = 'core_profiles' +path = '.' + +for subdir, k in ( + ('2', 1.2), + ('3', 1.4), +): + dataset = to_xarray( + f'{path}/1/data', + ids=ids, + variables=variables, + ) + print(subdir, k) + dataset['t_i_ave'] *= k + dataset['ion_temperature'] *= k + + to_imas( + f'{path}/{subdir}/data', + dataset=dataset, + ids=ids, + variables=variables, + ) diff --git a/notebooks/prepare.sh b/notebooks/prepare.sh new file mode 100644 index 0000000..4052c88 --- /dev/null +++ b/notebooks/prepare.sh @@ -0,0 +1,8 @@ +# Small script to initialize a few datasets for the notebooks using the `hdf5_testdata`: +# https://github.com/duqtools/hdf5_testdata/tree/main + +git clone ~/python/hdf5_testdata 1 +git clone ~/python/hdf5_testdata 2 +git clone ~/python/hdf5_testdata 3 + +python prepare.py diff --git a/notebooks/xarray-2D.ipynb b/notebooks/xarray-2D.ipynb index 099fdc2..a1e8be5 100644 --- a/notebooks/xarray-2D.ipynb +++ b/notebooks/xarray-2D.ipynb @@ -5,26 +5,9 @@ "execution_count": 1, "id": "ceeb08fb", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.12.0 (main, Oct 2 2023, 00:00:00) [GCC 13.2.1 20230918 (Red Hat 13.2.1-3)]'" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "from __future__ import annotations\n", - "\n", - "%load_ext autoreload\n", - "%autoreload 2\n", - "import sys\n", - "\n", - "sys.version" + "from __future__ import annotations" ] }, { @@ -65,21 +48,20 @@ " name='dim1',\n", " ids='equilibrium',\n", " path='time_slice/*/profiles_2d/0/grid/dim1',\n", - " dims=['time', '$dim1'],\n", + " dims=['time_eq', '$dim1'],\n", " ),\n", " Variable(\n", " name='dim2',\n", " ids='equilibrium',\n", " path='time_slice/*/profiles_2d/0/grid/dim2',\n", - " dims=['time', '$dim2'],\n", + " dims=['time_eq', '$dim2'],\n", " ),\n", " Variable(\n", " name='psi',\n", " ids='equilibrium',\n", " path='time_slice/*/profiles_2d/0/psi',\n", - " dims=['time', '$dim1', '$dim2'],\n", + " dims=['time_eq', '$dim1', '$dim2'],\n", " ),\n", - " Variable(name='time', ids='equilibrium', path='time', dims=['time']),\n", ")" ] }, @@ -115,9 +97,8 @@ " name='psi',\n", " ids='equilibrium',\n", " path='time_slice/*/profiles_2d/0/psi',\n", - " dims=['time', 'dim1', 'dim2'],\n", + " dims=['time_eq', 'dim1', 'dim2'],\n", " ),\n", - " Variable(name='time', ids='equilibrium', path='time', dims=['time']),\n", ")" ] }, @@ -504,20 +485,13 @@ " fill: currentColor;\n", "}\n", "
<xarray.Dataset>\n",
- "Dimensions: (time: 1, dim1: 65, dim2: 65)\n",
+ "Dimensions: (time_eq: 1, dim1: 65, dim2: 65)\n",
"Coordinates:\n",
- " * time (time) float64 50.04\n",
- " * dim1 (dim1) float64 1.65 1.687 1.725 1.762 ... 3.938 3.975 4.013 4.05\n",
+ " * time_eq (time_eq) float64 50.04\n",
" * dim2 (dim2) float64 -1.9 -1.837 -1.773 -1.71 ... 1.96 2.023 2.087 2.15\n",
+ " * dim1 (dim1) float64 1.65 1.687 1.725 1.762 ... 3.938 3.975 4.013 4.05\n",
"Data variables:\n",
- " psi (time, dim1, dim2) float64 -6.212 -6.112 -6.012 ... -10.8 -11.03<xarray.Dataset>\n",
- "Dimensions: (run: 3, time: 1, dim1: 65, dim2: 65)\n",
+ "Dimensions: (run: 3, time_eq: 1, dim1: 65, dim2: 65)\n",
"Coordinates:\n",
- " * time (time) float64 0.1\n",
+ " * time_eq (time_eq) float64 0.1\n",
" * dim1 (dim1) float64 1.65 1.687 1.725 1.762 ... 3.938 3.975 4.013 4.05\n",
" * dim2 (dim2) float64 -1.9 -1.837 -1.773 -1.71 ... 1.96 2.023 2.087 2.15\n",
" * run (run) <U5 'run_0' 'run_1' 'run_2'\n",
"Data variables:\n",
- " psi (run, time, dim1, dim2) float64 -6.834 -6.724 ... -9.717 -9.929<xarray.Dataset>\n",
- "Dimensions: (time: 1, ion: 4, rho_tor_norm: 101)\n",
+ "Dimensions: (time: 1, rho_tor_norm: 101, ion: 4)\n",
"Coordinates:\n",
" * time (time) float64 50.04\n",
" * rho_tor_norm (rho_tor_norm) float64 0.0 0.01 0.02 0.03 ... 0.98 0.99 1.0\n",
"Dimensions without coordinates: ion\n",
"Data variables:\n",
- " ion_temperature (time, ion, rho_tor_norm) float64 1.036e+04 ... 508.4\n",
- " t_i_ave (time, rho_tor_norm) float64 1.036e+04 1.036e+04 ... 508.4<xarray.Dataset>\n",
- "Dimensions: (run: 3, time: 1, ion: 4, rho_tor_norm: 101)\n",
+ "Dimensions: (time: 1, run: 3, rho_tor_norm: 101, ion: 4)\n",
"Coordinates:\n",
" * time (time) float64 0.1\n",
" * rho_tor_norm (rho_tor_norm) float64 0.0 0.01 0.02 0.03 ... 0.98 0.99 1.0\n",
" * run (run) <U5 'run_0' 'run_1' 'run_2'\n",
"Dimensions without coordinates: ion\n",
"Data variables:\n",
- " ion_temperature (run, time, ion, rho_tor_norm) float64 1.139e+04 ... 457.5\n",
- " t_i_ave (run, time, rho_tor_norm) float64 1.036e+04 ... 508.4<xarray.Dataset>\n",
- "Dimensions: (time: 1, rho_tor_norm: 101, ion: 4)\n",
+ "Dimensions: (time: 1, ion: 4, rho_tor_norm: 101)\n",
"Coordinates:\n",
" * time (time) float64 50.04\n",
" * rho_tor_norm (rho_tor_norm) float64 0.0 0.01 0.02 0.03 ... 0.98 0.99 1.0\n",
"Dimensions without coordinates: ion\n",
"Data variables: (12/14)\n",
- " j_tot (time, rho_tor_norm) float64 -2.019e+06 ... -1.537e+05\n",
- " j_ohm (time, rho_tor_norm) float64 -2.019e+06 ... -1.537e+05\n",
- " p_i (time, ion, rho_tor_norm) float64 1.242e+05 ... 0.2948\n",
- " t_i_ave (time, rho_tor_norm) float64 1.036e+04 1.036e+04 ... 508.4\n",
+ " n_i_tot (time, ion, rho_tor_norm) float64 7.486e+19 ... 3.619e+15\n",
+ " t_e (time, rho_tor_norm) float64 7.566e+03 7.564e+03 ... 240.3\n",
" n_e_tot (time, rho_tor_norm) float64 7.976e+19 ... 1.742e+19\n",
+ " p_i (time, ion, rho_tor_norm) float64 1.242e+05 ... 0.2948\n",
" n_e (time, rho_tor_norm) float64 7.976e+19 ... 1.742e+19\n",
+ " j_tot (time, rho_tor_norm) float64 -2.019e+06 ... -1.537e+05\n",
" ... ...\n",
- " omega_tor (time, rho_tor_norm) float64 1.169e+05 ... 1.353e+04\n",
+ " p_e (time, rho_tor_norm) float64 9.669e+04 9.665e+04 ... 670.6\n",
" n_i (time, ion, rho_tor_norm) float64 7.486e+19 ... 3.619e+15\n",
- " q (time, rho_tor_norm) float64 0.7887 0.7888 ... 4.262 4.845\n",
- " n_i_tot (time, ion, rho_tor_norm) float64 7.486e+19 ... 3.619e+15\n",
+ " zeff (time, rho_tor_norm) float64 1.761 1.761 ... 1.761 1.761\n",
" collisionality (time, rho_tor_norm) float64 23.23 7.554 ... 7.31 10.19\n",
- " zeff (time, rho_tor_norm) float64 1.761 1.761 ... 1.761 1.761