{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Convert a bunch of CBF files into EDF format\n",
    "\n",
    "This simple tutorial explains how to convert a bunch of CBF files to EDF files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of files: 18\n"
     ]
    }
   ],
   "source": [
    "import glob\n",
    "files = glob.glob(\"*.cbf\")\n",
    "files.sort()\n",
    "print(\"Number of files: %s\" % len(files))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dest_format = \"edf\"\n",
    "dest_dir = \"edf_format\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fabio\n",
    "import os\n",
    "if not os.path.exists(dest_dir):\n",
    "    os.makedirs(dest_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 476 ms, sys: 576 ms, total: 1.05 s\n",
      "Wall time: 1.06 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "for onefile in files:\n",
    "    dst_name = os.path.join(dest_dir, os.path.splitext(onefile)[0] + \".\" + dest_format)\n",
    "    fabio.open(onefile).convert(dest_format).save(dst_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The overall speed is 3.2 frame/second\n"
     ]
    }
   ],
   "source": [
    "print(\"The overall speed is %.1f frame/second\"%(len(files)/5.64))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Conclusion\n\nThis simple tutorial explains how to perform simple file conversion. It is likely to be limited by the bandwidth available for the hard-drive of your computer or by the compression/decompression algorithm as it is the case here for CBF decompression.\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}