{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial is for the calibration of an Xpad_flat on the Soleil Diffabs Diffractometer.\n",
    "\n",
    "The calibration data is a scan on the delta motor with a CeO2 calibrant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib nbagg\n",
    "\n",
    "# first the imports\n",
    "from typing import Iterator, List, NamedTuple, Text, Tuple\n",
    "\n",
    "import os\n",
    "import functools\n",
    "\n",
    "from collections import namedtuple\n",
    "from math import pi\n",
    "\n",
    "import numpy\n",
    "import pylab\n",
    "import pyFAI\n",
    "\n",
    "from h5py import Dataset, File\n",
    "\n",
    "from pyFAI.gui import jupyter\n",
    "\n",
    "# local import\n",
    "from soleil import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# temporary until the ruch is ON\n",
    "ROOT = os.path.join(\"/home\", \"experiences\", \"instrumentation\", \"picca\", \"data\", \"99160066\", \"2016\", \"Run2\")\n",
    "PUBLISHED = os.path.join(\"/home\", \"experiences\", \"instrumentation\", \"picca\", \"data\", \"99160066\", \"published-data\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# do a calibration with a well centered sample.\n",
    "\n",
    "# TODO recuperer le point de départ à partir des ponis de référence.\n",
    "# TODO ajouter le diagramme complet à la fin.\n",
    "# TODO ajout du mask pour la calibration\n",
    "\n",
    "json = os.path.join(PUBLISHED, \"calibration\", \"calibration.json\")\n",
    "\n",
    "distance = Length(3.606331e-01)\n",
    "poni1 = Length(1.314012e-01)\n",
    "poni2 = Length(3.613907e-02)\n",
    "rot1 = Angle(0)\n",
    "rot2_scale = pi / 180.0  # convert degree to radian\n",
    "rot2_offset = Angle(0.328063469162)  # radian\n",
    "rot3 = Angle(7.19961198098e-07)\n",
    "\n",
    "params = Calibration(os.path.join(PUBLISHED, \"calibration\", \"XRD18keV_26.nxs\"),\n",
    "                     DatasetPathWithAttribute(\"interpretation\", b\"image\"),\n",
    "                     DatasetPathContains(\"scan_data/actuator_1_1\"),\n",
    "                     [0, 3, 6],\n",
    "                     Calibrant(\"CeO2\"),\n",
    "                     Detector(\"Xpad_flat\"),\n",
    "                     Wavelength(6.8877e-11),\n",
    "                     CalibrationParameters(Parameter[Length](\"dist\", distance, (distance, distance)),\n",
    "                                           Parameter[Length](\"poni1\", poni1, (poni1, poni1)),\n",
    "                                           Parameter[Length](\"poni2\", poni2, (poni2, poni2)),\n",
    "                                           Parameter[Angle](\"rot1\", rot1, (rot1, rot1)),\n",
    "                                           Parameter[float](\"rot2_scale\", rot2_scale, (rot2_scale, rot2_scale)),\n",
    "                                           Parameter[Angle](\"rot2_offset\", rot2_offset, (rot2_offset-0.5, rot2_offset+0.5)),\n",
    "                                           Parameter[Angle](\"rot3\", rot3, (rot3, rot3))\n",
    "                                          )\n",
    "                    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/experiences/instrumentation/picca/data/99160066/published-data/calibration/XRD18keV_26.nxs_00.edf\n",
      "/home/experiences/instrumentation/picca/data/99160066/published-data/calibration/XRD18keV_26.nxs_03.edf\n",
      "/home/experiences/instrumentation/picca/data/99160066/published-data/calibration/XRD18keV_26.nxs_06.edf\n"
     ]
    }
   ],
   "source": [
    "# save all the ref as images in order to do the calibration with\n",
    "# pyFAI-calib[2].\n",
    "# temporary until pyFAI-calib2 can read from NeXuS files.\n",
    "\n",
    "save_as_edf(params, os.path.join(PUBLISHED, \"calibration\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# execute the calibration\n",
    "\n",
    "#calibration(json, params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pylab\n",
    "pylab.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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
}
