{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75c40c3b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import h5py\n",
    "import json\n",
    "import matplotlib.gridspec as gridspec\n",
    "\n",
    "\n",
    "NoSpectra = json.load(open(\"Setting.json\"))[0][\"Spectra\"];\n",
    "for spectr in [\"Halpha_0.h5\", \"IR_0.h5\", \"VIS_0.h5\", \"UV_0.h5\", \"IRVISUV_0.h5\"]:\n",
    "\n",
    "    Spectra = h5py.File(spectr, \"r\")\n",
    "    ShotNo=open(\"ShotNo\",\"r\").read()\n",
    "    \n",
    "    fig = plt.figure()\n",
    "    gs = fig.add_gridspec(NoSpectra, hspace=0)\n",
    "    axs = gs.subplots(sharex=True, sharey=True)\n",
    "    fig.set_figheight(10);fig.set_figwidth(20)\n",
    "    plt.suptitle(\"Golem spectum #\" + str(ShotNo) +str(spectr[0:-5]))\n",
    "    \n",
    "    for i in range(0, NoSpectra):\n",
    "          axs[i].plot(Spectra['Wavelengths'][0:], Spectra['Spectra'][i:][0], label=i)\n",
    "    plt.savefig('ScreenShotAll.png')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2386836",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "NoSpectra = json.load(open(\"Setting.json\"))[0][\"Spectra\"]\n",
    "ShotNo = open(\"ShotNo\", \"r\").read()\n",
    "\n",
    "spectr_list = [\"Halpha_0.h5\", \"IR_0.h5\", \"VIS_0.h5\", \"UV_0.h5\", \"IRVISUV_0.h5\"]\n",
    "num_files = len(spectr_list)\n",
    "\n",
    "fig, axs = plt.subplots(NoSpectra, num_files, sharex=True, figsize=(20, 10))\n",
    "fig.subplots_adjust(hspace=0, wspace=0)\n",
    "\n",
    "for col_idx, spectr in enumerate(spectr_list):\n",
    "    Spectra = h5py.File(spectr, \"r\")\n",
    "    for row_idx in range(NoSpectra):\n",
    "        if NoSpectra > 1:\n",
    "            ax = axs[row_idx, col_idx]\n",
    "        else:\n",
    "            ax = axs[col_idx]\n",
    "        ax.plot(Spectra['Wavelengths'][:], Spectra['Spectra'][row_idx], label=f\"Spectrum {row_idx}\")\n",
    "        \n",
    "        if row_idx != NoSpectra - 1:\n",
    "            ax.tick_params(labelbottom=False)\n",
    "        else:\n",
    "            ax.set_xlabel('Wavelength')\n",
    "        \n",
    "        if col_idx != 0:\n",
    "            ax.tick_params(labelleft=False)\n",
    "\n",
    "        \n",
    "        if row_idx == 0:\n",
    "            ax.set_title(spectr[:-5])\n",
    "\n",
    "fig.suptitle(f\"Golem Spectrum #{ShotNo}\", fontsize=16)\n",
    "\n",
    "plt.savefig('ScreenShotAll_horizontal_stack.png')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "822a03b4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "NoSpectra = json.load(open(\"Setting.json\"))[0][\"Spectra\"]\n",
    "spectr_list = [\"Halpha_0.h5\", \"IR_0.h5\", \"VIS_0.h5\", \"UV_0.h5\", \"IRVISUV_0.h5\"]\n",
    "number_of_files = len(spectr_list)\n",
    "ShotNo = open(\"ShotNo\", \"r\").read()\n",
    "\n",
    "rows_per_file = NoSpectra + 1\n",
    "total_rows = number_of_files * rows_per_file\n",
    "\n",
    "fig = plt.figure(figsize=(20, total_rows * 0.75))\n",
    "gs = gridspec.GridSpec(total_rows, 1, hspace=0)\n",
    "\n",
    "current_row = 0 \n",
    "for spectr in spectr_list:\n",
    "    ax_title = fig.add_subplot(gs[current_row, 0])\n",
    "    ax_title.text(0.5, 0.5, f\"{spectr[:-5]}\", ha='center', va='center', fontsize=16)\n",
    "    ax_title.axis('off')  # Hide the axis\n",
    "    current_row += 1  # Move to the next row\n",
    "\n",
    "    with h5py.File(spectr, \"r\") as Spectra:\n",
    "        wavelengths = Spectra['Wavelengths'][:]\n",
    "        spectra_data = Spectra['Spectra']\n",
    "\n",
    "        for i in range(NoSpectra):\n",
    "            # Create a subplot for each spectrum\n",
    "            ax = fig.add_subplot(gs[current_row, 0], sharex=ax if current_row > 1 else None)\n",
    "            ax.plot(wavelengths, spectra_data[i], label=f\"Spectrum {i}\")\n",
    "\n",
    "\n",
    "            current_row += 1  # Move to the next row\n",
    "\n",
    "fig.suptitle(f\"Golem Spectrum #{ShotNo}\", fontsize=20)\n",
    "\n",
    "plt.tight_layout(rect=[0, 0, 1, 0.98])  # Leave space for the super title\n",
    "plt.savefig('ScreenShotAll_vertical_stack.png', bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a14e7126",
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.graph_objects as go\n",
    "from plotly.subplots import make_subplots\n",
    "import h5py\n",
    "import json\n",
    "\n",
    "# load basic data\n",
    "NoSpectra = json.load(open(\"Setting.json\"))[0][\"Spectra\"]\n",
    "ShotNo = open(\"ShotNo\", \"r\").read()\n",
    "\n",
    "# store names of spectroscopes\n",
    "spectr_list = [\"Halpha_0.h5\", \"IR_0.h5\", \"VIS_0.h5\", \"UV_0.h5\", \"IRVISUV_0.h5\"]\n",
    "\n",
    "# define colors for each spectra file\n",
    "color_list = ['red', 'green', 'blue', 'orange', 'purple']\n",
    "spectr_colors = dict(zip(spectr_list, color_list))\n",
    "\n",
    "# subplot with shared axes\n",
    "fig = make_subplots(rows=NoSpectra, cols=1, shared_xaxes=False, vertical_spacing=0.02)\n",
    "\n",
    "# initialize annotations list\n",
    "annotations = []\n",
    "\n",
    "# loop over each spectrum index (corresponding to different times)\n",
    "for ti in range(NoSpectra):\n",
    "    # loop over each spectroscopes\n",
    "    for spectr in spectr_list:\n",
    "        with h5py.File(spectr, \"r\") as Spectra:\n",
    "            wavelengths = Spectra['Wavelengths'][:]\n",
    "            spectrum_data = Spectra['Spectra'][ti]\n",
    "            # create a trace with labels\n",
    "            fig.add_trace(\n",
    "                go.Scatter(\n",
    "                    x=wavelengths,\n",
    "                    y=spectrum_data,\n",
    "                    mode='lines',\n",
    "                    name=f'{spectr[:-5]}',\n",
    "                    showlegend=False,  # Disable the global legend\n",
    "                    line=dict(color=spectr_colors[spectr]),\n",
    "                    hovertemplate=f'{spectr[:-5]}<br>Wavelength: %{{x}}<br>Intensity: %{{y}}<extra></extra>'\n",
    "                ),\n",
    "                row=ti + 1,\n",
    "                col=1,\n",
    "            )\n",
    "    # update y-axis label for each subplot\n",
    "    fig.update_yaxes(title_text=f'Spectrum {ti}', row=ti + 1, col=1)\n",
    "\n",
    "    # determine yref for annotations\n",
    "    yref = 'y' + (str(ti+1) if ti > 0 else '') + ' domain'\n",
    "\n",
    "    # ddd legend-like annotations to each subplot (showing both legend and datapoint descriptions)\n",
    "    for idx, spectr in enumerate(spectr_list):\n",
    "        annotations.append(\n",
    "            dict(\n",
    "                x=1.01,  # Slightly outside the plot area to the right\n",
    "                y=1 - (idx * 0.08),  # Adjust y position for each legend entry\n",
    "                xref='x domain',\n",
    "                yref=yref,\n",
    "                xanchor='left',\n",
    "                yanchor='middle',\n",
    "                text=f'<span style=\"color:{spectr_colors[spectr]};\">{spectr[:-5]}</span>',\n",
    "                showarrow=False,\n",
    "                font=dict(size=10),\n",
    "            )\n",
    "        )\n",
    "\n",
    "# update the figure layout to include annotations from above\n",
    "fig.update_layout(\n",
    "    annotations=annotations,\n",
    "    height=300 * NoSpectra,\n",
    "    width=1000,\n",
    "    title_text=f\"Golem Spectrum #{ShotNo}\",\n",
    "    margin=dict(r=150),  # Increase right margin to make space for annotations\n",
    ")\n",
    "\n",
    "# Update x-axis label\n",
    "fig.update_xaxes(showticklabels=True, title_text='Wavelength', row=NoSpectra, col=1)\n",
    "\n",
    "# Save to HTML\n",
    "fig.write_html('interactive_plot.html', auto_open=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b053bb11",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
