{ "cells": [ { "cell_type": "markdown", "id": "8d04a175", "metadata": {}, "source": [ "# Importing/Exporting field from/to `xarray.DataArray`\n", "\n", "[`xarray`](https://docs.xarray.dev/en/stable/) provides a convenient method to handle *labeled* multi-dimensional arrays. It integrates well with `numpy` and `pandas` for fast array manipulation, as well as `matplotlib` and `holoviews` for visualization. This makes it a good candidate to carry `discretisedfield.Field` data and additional metadata such as field name and unit (which appear in the rendered plots).\n", "\n", "## Exporting `discretisedfield.Field` to `xarray.DataArray`\n", "\n", "`to_xarray` method of the `discretisedfield.Field` object is utilised to export the field data to a DataArray. As an example, we use a three-dimensional field." ] }, { "cell_type": "code", "execution_count": 1, "id": "756dbabb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Field\n", "
<xarray.DataArray 'field' (x: 2, y: 2, z: 2, vdims: 3)>\n",
"array([[[[0., 0., 1.],\n",
" [0., 0., 1.]],\n",
"\n",
" [[0., 0., 1.],\n",
" [0., 0., 1.]]],\n",
"\n",
"\n",
" [[[0., 0., 1.],\n",
" [0., 0., 1.]],\n",
"\n",
" [[0., 0., 1.],\n",
" [0., 0., 1.]]]])\n",
"Coordinates:\n",
" * x (x) float64 2.5e-09 7.5e-09\n",
" * y (y) float64 2.5e-09 7.5e-09\n",
" * z (z) float64 2.5e-09 7.5e-09\n",
" * vdims (vdims) <U1 'x' 'y' 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: [5.e-09 5.e-09 5.e-09]\n",
" pmin: [0. 0. 0.]\n",
" pmax: [1.e-08 1.e-08 1.e-08]\n",
" nvdim: 3\n",
" tolerance_factor: 1e-12<xarray.DataArray 'field' (y: 2, z: 2, vdims: 3)>\n",
"array([[[0., 0., 1.],\n",
" [0., 0., 1.]],\n",
"\n",
" [[0., 0., 1.],\n",
" [0., 0., 1.]]])\n",
"Coordinates:\n",
" x float64 2.5e-09\n",
" * y (y) float64 2.5e-09 7.5e-09\n",
" * z (z) float64 2.5e-09 7.5e-09\n",
" * vdims (vdims) <U1 'x' 'y' 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: [5.e-09 5.e-09 5.e-09]\n",
" pmin: [0. 0. 0.]\n",
" pmax: [1.e-08 1.e-08 1.e-08]\n",
" nvdim: 3\n",
" tolerance_factor: 1e-12"
],
"text/plain": [
"<xarray.DataArray 'field' (x: 2, y: 2, z: 2)>\n",
"array([[[1., 1.],\n",
" [1., 1.]],\n",
"\n",
" [[1., 1.],\n",
" [1., 1.]]])\n",
"Coordinates:\n",
" * x (x) float64 2.5e-09 7.5e-09\n",
" * y (y) float64 2.5e-09 7.5e-09\n",
" * z (z) float64 2.5e-09 7.5e-09\n",
" vdims <U1 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: [5.e-09 5.e-09 5.e-09]\n",
" pmin: [0. 0. 0.]\n",
" pmax: [1.e-08 1.e-08 1.e-08]\n",
" nvdim: 3\n",
" tolerance_factor: 1e-12"
],
"text/plain": [
"<xarray.DataArray 'field' (y: 2, z: 2)>\n",
"array([[1., 1.],\n",
" [1., 1.]])\n",
"Coordinates:\n",
" x float64 2.5e-09\n",
" * y (y) float64 2.5e-09 7.5e-09\n",
" * z (z) float64 2.5e-09 7.5e-09\n",
" vdims <U1 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: [5.e-09 5.e-09 5.e-09]\n",
" pmin: [0. 0. 0.]\n",
" pmax: [1.e-08 1.e-08 1.e-08]\n",
" nvdim: 3\n",
" tolerance_factor: 1e-12"
],
"text/plain": [
"<xarray.DataArray 'mag' (x: 2, y: 2, z: 2, vdims: 3)>\n",
"array([[[[1., 1., 1.],\n",
" [1., 1., 1.]],\n",
"\n",
" [[1., 1., 1.],\n",
" [1., 1., 1.]]],\n",
"\n",
"\n",
" [[[1., 1., 1.],\n",
" [1., 1., 1.]],\n",
"\n",
" [[1., 1., 1.],\n",
" [1., 1., 1.]]]])\n",
"Coordinates:\n",
" * x (x) float64 2.5e-09 7.5e-09\n",
" * y (y) float64 2.5e-09 7.5e-09\n",
" * z (z) float64 2.5e-09 7.5e-09\n",
" * vdims (vdims) <U1 'x' 'y' 'z'\n",
"Attributes:\n",
" nvdim: 3\n",
" cell: [5e-09, 5e-09, 5e-09]\n",
" pmin: [0.0, 0.0, 0.0]\n",
" pmax: [1e-08, 1e-08, 1e-08]