{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "17b0da73",
   "metadata": {},
   "source": [
    "# Using H5Web in the notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bf4ebd9",
   "metadata": {},
   "source": [
    "## Display a simple HDF5 file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0085f19f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import h5py\n",
    "\n",
    "with h5py.File(\"simple.h5\", \"w\") as h5file:\n",
    "    X = np.arange(-5, 5, 0.25)\n",
    "    Y = np.arange(-5, 5, 0.25)\n",
    "    Xg, Yg = np.meshgrid(X, Y)\n",
    "    h5file['threeD'] = [np.sin(2*np.pi*f*np.sqrt(Xg**2 + Yg**2)) for f in np.arange(0.1, 1.1, 0.1)]\n",
    "    h5file['twoD'] = np.sin(np.sqrt(Xg**2 + Yg**2))\n",
    "    h5file['oneD'] = X\n",
    "    h5file['scalar'] = 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a80e6e4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from jupyterlab_h5web import H5Web\n",
    "\n",
    "H5Web('simple.h5')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb1acb7e",
   "metadata": {},
   "source": [
    "## Display a NeXus file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38b74d00",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import h5py\n",
    "\n",
    "with h5py.File(\"nexus.nx\", \"w\") as h5file:\n",
    "    root_group = h5file\n",
    "    root_group.attrs[\"NX_class\"] = \"NXroot\"\n",
    "    root_group.attrs[\"default\"] = \"entry\"\n",
    "\n",
    "    entry = root_group.create_group(\"entry\")\n",
    "    entry.attrs[\"NX_class\"] = \"NXentry\"\n",
    "    entry.attrs[\"default\"] = \"process/spectrum\"\n",
    "\n",
    "    process = entry.create_group(\"process\")\n",
    "    process.attrs[\"NX_class\"] = \"NXprocess\"\n",
    "    process.attrs[\"default\"] = \"spectrum\"\n",
    "\n",
    "    spectrum = process.create_group(\"spectrum\")\n",
    "    spectrum.attrs[\"NX_class\"] = \"NXdata\"\n",
    "    spectrum.attrs[\"signal\"] = \"data\"\n",
    "    spectrum.attrs[\"auxiliary_signals\"] = [\"aux1\", \"aux2\"]\n",
    "    data = np.array([np.linspace(-x, x, 10) for x in range(1, 6)])\n",
    "    spectrum[\"data\"] = data ** 2\n",
    "    spectrum[\"aux1\"] = -(data ** 2)\n",
    "    spectrum[\"aux2\"] = -data\n",
    "    spectrum[\"data\"].attrs[\"interpretation\"] = \"spectrum\"\n",
    "    \n",
    "    image = process.create_group(\"image\")\n",
    "    image.attrs[\"NX_class\"] = \"NXdata\"\n",
    "    image.attrs[\"signal\"] = \"data\"\n",
    "    x = np.linspace(-5, 5, 50)\n",
    "    x0 = np.linspace(10, 100, 10)\n",
    "    image[\"data\"] = [a*x**2 for a in x0]\n",
    "    image[\"X\"] = np.linspace(-2, 2, 50, endpoint=False)\n",
    "    image[\"X\"].attrs[\"units\"] = u\"µm\"\n",
    "    image[\"Y\"] = np.linspace(0, 0.1, 10, endpoint=False)\n",
    "    image[\"Y\"].attrs[\"units\"] = \"s\"\n",
    "    image.attrs[\"axes\"] = [\"X\"]\n",
    "    image.attrs[\"axes\"] = [\"Y\", \"X\"]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d754aa6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from jupyterlab_h5web import H5Web\n",
    "\n",
    "H5Web('nexus.nx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9de9e3a8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}