{ "cells": [ { "cell_type": "markdown", "id": "27fede2a-cd34-46b9-9b06-776112f2e680", "metadata": { "tags": [] }, "source": [ "# Processing with HDF5 datasets\n", "\n", "Unlike the `h5py` package, which returns `numpy.ndarray` when accessing the values of datasets, the `h5rdmtoolbox` returns [`xarray.DataArray` objects](https://xarray.pydata.org/). The `xarray.DataArray` object allows to carry attributes with the numpy-like multi-dimensional array. It also supports the concept of dimensions and coordinates, allowing to assign the array axis with meaning ful (meta) data.\n", "\n", "Let's dive into it and explore the practical implications of retrieving `xarray.DatArray`:" ] }, { "cell_type": "code", "execution_count": 1, "id": "9fc2b312-16d5-4aa7-8f10-9107a2e53c08", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "using(\"h5py\")" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import h5rdmtoolbox as h5tbx\n", "import numpy as np\n", "\n", "h5tbx.use(None)" ] }, { "cell_type": "markdown", "id": "8bc428f1-f2a6-4f4d-bbbf-9880358cff06", "metadata": {}, "source": [ "Let's create an example file. Note, that we pass `make_scale` and `attach_scale` as arguments to setup the coordinates and their association to the HDF5 dataset \"data\". The useful implications will be visible when we access the dataset values in the next steps." ] }, { "cell_type": "code", "execution_count": 2, "id": "bf629a23-0162-4d0b-a910-691b4ec97941", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "<xarray.DataArray 'vel' (y: 11, x: 5)>\n",
"0.5832 0.7347 0.3899 0.7013 0.07984 ... 0.5462 0.07077 0.3864 0.2998 0.2473\n",
"Coordinates:\n",
" * y (y) float64 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0\n",
" * x (x) float64 0.0 2.5 5.0 7.5 10.0\n",
"Attributes:\n",
" long_name: velocity\n",
" units: m/s<xarray.DataArray 'vel' (x: 5)>\n",
"0.3443 0.3647 0.09872 0.8726 0.3015\n",
"Coordinates:\n",
" y float64 2.0\n",
" * x (x) float64 0.0 2.5 5.0 7.5 10.0\n",
"Attributes:\n",
" long_name: velocity\n",
" units: m/s<xarray.DataArray 'vel' (x: 5)>\n",
"0.3443 0.3647 0.09872 0.8726 0.3015\n",
"Coordinates:\n",
" y float64 2.0\n",
" * x (x) float64 0.0 2.5 5.0 7.5 10.0\n",
"Attributes:\n",
" long_name: velocity\n",
" units: m/s<xarray.DataArray 'vel' (y: 11, x: 5)>\n",
"583.2 734.7 389.9 701.3 79.84 317.7 ... 484.6 546.2 70.77 386.4 299.8 247.3\n",
"Coordinates:\n",
" * y (y) float64 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0\n",
" * x (x) float64 0.0 0.0025 0.005 0.0075 0.01\n",
" uncertainty (y, x) float64 0.02454 0.02421 0.02566 ... 0.02507 0.0234\n",
"Attributes:\n",
" ANCILLARY_DATASETS: ['uncertainty']\n",
" long_name: velocity\n",
" units: mm/s