{ "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` 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 plots rendered by `matplotlib`).\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." ] }, { "cell_type": "code", "execution_count": 1, "id": "756dbabb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Field\n", "
<xarray.DataArray 'field' (x: 2, y: 2, z: 2, comp: 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",
" * comp (comp) <U1 'x' 'y' 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: (5e-09, 5e-09, 5e-09)\n",
" p1: (0, 0, 0)\n",
" p2: (1e-08, 1e-08, 1e-08)<xarray.DataArray 'field' (y: 2, z: 2, comp: 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",
" * comp (comp) <U1 'x' 'y' 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: (5e-09, 5e-09, 5e-09)\n",
" p1: (0, 0, 0)\n",
" p2: (1e-08, 1e-08, 1e-08)"
],
"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",
" comp <U1 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: (5e-09, 5e-09, 5e-09)\n",
" p1: (0, 0, 0)\n",
" p2: (1e-08, 1e-08, 1e-08)"
],
"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",
" comp <U1 'z'\n",
"Attributes:\n",
" units: None\n",
" cell: (5e-09, 5e-09, 5e-09)\n",
" p1: (0, 0, 0)\n",
" p2: (1e-08, 1e-08, 1e-08)"
],
"text/plain": [
"<xarray.DataArray 'mag' (x: 2, y: 2, z: 2, comp: 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",
" * comp (comp) <U1 'x' 'y' 'z'\n",
"Attributes:\n",
" cell: [5e-09, 5e-09, 5e-09]\n",
" p1: [0.0, 0.0, 0.0]\n",
" p2: [1e-08, 1e-08, 1e-08]