{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7c600ac0",
   "metadata": {},
   "source": [
    "# Using RawFile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "38912c9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import numpy as np \n",
    "import matplotlib.pyplot as plt\n",
    "import boost_histogram as bh\n",
    "import time\n",
    "\n",
    "#Python cluster reader\n",
    "from creader import RawFile\n",
    "path = Path('/mnt/sls_det_storage/moench_data/cluster_reader_test/')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f06378a1",
   "metadata": {},
   "source": [
    "## Moench03 data\n",
    "\n",
    "Fetches info from the master file\n",
    "\n",
    "### Frames only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "218e2897",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 531244 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "with RawFile(path/'Moench03_master_0.json') as f:\n",
    "    frames = f.read(100)\n",
    "    \n",
    "fig, ax = plt.subplots(1,3, figsize = (10,5))\n",
    "im0 = ax[0].imshow(frames[0])\n",
    "im1 = ax[1].imshow(frames[9])\n",
    "im1 = ax[2].imshow(frames[0]-frames[9])\n",
    "fig.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3f92db90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fc4ccea5d50>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 58020 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(frames.sum(axis =1).sum(axis = 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d25663bd",
   "metadata": {},
   "source": [
    "### Frames and header"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "befafb31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Frame Number,   Timestamp \n",
      "201             1107824650\n",
      "202             1108024651\n",
      "203             1108224652\n",
      "204             1108424653\n",
      "205             1108624654\n",
      "206             1108824655\n",
      "207             1109024656\n",
      "208             1109224657\n",
      "209             1109424658\n",
      "210             1109624659\n"
     ]
    }
   ],
   "source": [
    "with RawFile(path/'Moench03_master_0.json', header = True) as f:\n",
    "    frames, header = f.read(10)\n",
    "    \n",
    "    \n",
    "print(f'{\"Frame Number,\":<15} {\"Timestamp\":<10}')\n",
    "for h in header:\n",
    "    print(f\"{h['Frame Number']:<15} {h['Timestamp']:<10}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "991c2729",
   "metadata": {},
   "source": [
    "## Moench 04 data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "15fcde89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1088940 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#if the file has analog and digital data it's automatically read\n",
    "\n",
    "with RawFile(path/'Moench04_digital_master_0.raw', header = True) as f:\n",
    "    analog, digital, header = f.read(20)\n",
    "    \n",
    "fig, ax = plt.subplots(2,2, figsize = (10,10))\n",
    "im0 = ax[0][0].imshow(analog[0], origin = 'lower')\n",
    "im1 = ax[0][1].imshow(digital[0], origin = 'lower')\n",
    "im2 = ax[1][0].imshow(analog[9], origin = 'lower')\n",
    "im3 = ax[1][1].imshow(digital[9], origin = 'lower')\n",
    "\n",
    "fig.tight_layout()"
   ]
  }
 ],
 "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.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
