{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Convert a HDF5 file into a multiframe TIFF\n",
    "\n",
    "This simple tutorial explains how to convert a mutiframe HDF5 into a Tiff.\n",
    "FabIO does not support mutiframe TIFF file writing (reading is OK) and provides TiffIO which supports it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import fabio\n",
    "from fabio.test.utilstest import UtilsTest\n",
    "from fabio.TiffIO import TiffIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Just downloaded /tmp/fabio_testdata_kieffer/sample_water0000.h5\n"
     ]
    }
   ],
   "source": [
    "filename = UtilsTest.getimage(\"sample_water0000.h5\")\n",
    "print(f\"Just downloaded {filename}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This file contains 10 frames of shape (1679, 1475) and weights 99.101MB\n"
     ]
    }
   ],
   "source": [
    "fimg = fabio.open(filename)\n",
    "print(f\"This file contains {fimg.nframes} frames of shape {fimg.shape} and weights {os.stat(filename).st_size/1e6:.3f}MB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Initialization of the TiffIO file with the first frame, note the mode \"w\"\n",
    "dest = filename.replace(\".h5\", \".tiff\")\n",
    "tif = TiffIO(dest, mode='w')\n",
    "tif.writeImage(fimg.data)\n",
    "del  tif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The destination file /tmp/fabio_testdata_kieffer/sample_water0000.tiff contains 10 frames  and weights 99.062MB\n"
     ]
    }
   ],
   "source": [
    "# Complete the Tiff file with all other frames, note the mode \"r+\"\n",
    "tif = TiffIO(dest, mode='r+')\n",
    "for frame_id in range(1, fimg.nframes):\n",
    "    tif.writeImage(fimg.get_frame(frame_id).data)\n",
    "print(f\"The destination file {dest} contains {tif.getNumberOfImages()} frames  and weights {os.stat(dest).st_size/1e6:.3f}MB\")\n",
    "del tif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frame id  |min hdf5  |min tiff  |max hdf5  |max tiff  |mean hdf5 |mean tiff |std hdf5  |std tiff  |hdf5==tiff\n",
      "         0|        -2|        -2|    284956|    284956|  6.854841|  6.854841|181.234427|181.234427|True\n",
      "         1|        -2|        -2|    284956|    284956|  6.853417|  6.853417|181.238234|181.238234|True\n",
      "         2|        -2|        -2|    284956|    284956|  6.854140|  6.854140|181.265561|181.265561|True\n",
      "         3|        -2|        -2|    284956|    284956|  6.844210|  6.844210|181.167266|181.167266|True\n",
      "         4|        -2|        -2|    284956|    284956|  6.847996|  6.847996|181.168757|181.168757|True\n",
      "         5|        -2|        -2|    284956|    284956|  6.853962|  6.853962|181.265936|181.265936|True\n",
      "         6|        -2|        -2|    284956|    284956|  6.855220|  6.855220|181.327199|181.327199|True\n",
      "         7|        -2|        -2|    284956|    284956|  6.851232|  6.851232|181.309509|181.309509|True\n",
      "         8|        -2|        -2|    284956|    284956|  6.856564|  6.856564|181.376959|181.376959|True\n",
      "         9|        -2|        -2|    284956|    284956|  6.849990|  6.849990|181.285734|181.285734|True\n"
     ]
    }
   ],
   "source": [
    "timg = fabio.open(dest)\n",
    "print(f\"{'frame id':10s}|{'min hdf5':10s}|{'min tiff':10s}|{'max hdf5':10s}|{'max tiff':10s}|{'mean hdf5':10s}|{'mean tiff':10s}|{'std hdf5':10s}|{'std tiff':10s}|{'hdf5==tiff':10s}\")\n",
    "for frame_id in range(fimg.nframes):\n",
    "    hdata = fimg.get_frame(frame_id).data\n",
    "    tdata = timg.get_frame(frame_id).data\n",
    "    print(f\"{frame_id:10d}|{hdata.min():10d}|{tdata.min():10d}|{hdata.max():10d}|{tdata.max():10d}|{hdata.mean():10f}|{tdata.mean():10f}|{hdata.std():10f}|{tdata.std():10f}|{(hdata==tdata).all()}\")\n",
    "# The two files have actually the same content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "This simple tutorial explains how to perform a file conversion towards multiframe TIFF. \n",
    "\n",
    "Note the importance of the **opening mode** in TiffIO!\n"
   ]
  }
 ],
 "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
