{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creation of a new calibrant\n",
    "\n",
    "In this tutorial we will see how to create a new calibrant. For this example we will use one of the calibrant sold by the NIST: Chromium oxide.\n",
    "\n",
    "The cell parameter are definied in this document:\n",
    "http://www.cristallografia.org/uploaded/614.pdf\n",
    "\n",
    "The first step is to record the cell parameters and provide them to pyFAI to define the cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.18.0-dev1\n"
     ]
    }
   ],
   "source": [
    "import pyFAI\n",
    "print(pyFAI.version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyFAI.calibrant import Cell\n",
    "crox = Cell.hexagonal(4.958979, 13.59592)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Chromium oxide has a crystal structure de Corrundom which is R-3c (space group 167). \n",
    "The selection rules are rather complicated and are available in:\n",
    "http://img.chem.ucl.ac.uk/sgp/large/167bz2.gif\n",
    "\n",
    "We will setup a function corresponding to the selection rules. It returns True if the reflection is active and False  otherwise.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reflection_condition_167(h,k,l):\n",
    "    \"\"\"from http://img.chem.ucl.ac.uk/sgp/large/167bz2.htm\"\"\"\n",
    "    if h == 0 and k == 0:\n",
    "        # 00l: 6n\n",
    "        return l%6 == 0\n",
    "    elif h == 0 and l == 0:\n",
    "        # 0k0: k=3n\n",
    "        return k%3 == 0\n",
    "    elif k == 0 and l == 0:\n",
    "        # h00: h=3n\n",
    "        return h%3 == 0\n",
    "    elif h == k:\n",
    "        # hhl: l=3n\n",
    "        return l%3 == 0\n",
    "    elif l == 0:\n",
    "        # hk0: h-k = 3n\n",
    "        return (h-3)%3 == 0\n",
    "    elif k == 0:\n",
    "        # h0l: l=2n h-l = 3n\n",
    "        return (l%2 == 0) and ((h - l)%3 == 0)\n",
    "    elif h == 0:\n",
    "        # 0kl: l=2n h+l = 3n\n",
    "        return (l%2 == 0) and ((k + l)%3 == 0)\n",
    "    else:\n",
    "        # -h + k + l = 3n\n",
    "        return (-h + k + l) % 3 == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use the actual selection rule, not the short version:\n",
    "#cro.selection_rules.append(lambda h, k, l: ((-h + k + l) % 3 == 0))\n",
    "crox.selection_rules.append(reflection_condition_167)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0237895129603751 (-4, 0, -4)\n",
      "1.06050521932356 (-4, 0, 2)\n",
      "1.091026544381222 (-4, 1, -5)\n",
      "1.1732349299369376 (-4, 1, -2)\n",
      "1.1865634227509676 (-4, 1, 1)\n",
      "1.1240862807166163 (-4, 1, 4)\n",
      "1.0153814717019545 (-4, 1, 7)\n",
      "1.0876084918690192 (-4, 2, -6)\n",
      "1.1958092169288077 (-4, 2, -3)\n",
      "1.07031836243183 (-4, 4, 1)\n",
      "1.191108228042296 (-3, -1, 0)\n",
      "1.210253337060561 (-3, 0, -6)\n",
      "1.431533930277851 (-3, 0, 0)\n",
      "1.042279063895563 (-3, 1, -10)\n",
      "1.245515918797666 (-3, 1, -7)\n",
      "1.464751514565614 (-3, 1, -4)\n",
      "1.6117606917001979 (-3, 1, -1)\n",
      "1.623206902656887 (-3, 1, 0)\n",
      "1.5788223557618772 (-3, 1, 2)\n",
      "1.3937626066300757 (-3, 1, 5)\n",
      "1.173822262746344 (-3, 1, 8)\n",
      "1.0390938597733972 (-3, 3, -9)\n",
      "1.3650526839471662 (-3, 3, -3)\n",
      "1.2397447500000003 (-2, -2, 0)\n",
      "1.3326159122281918 (-2, 0, -8)\n",
      "2.0475790259207503 (-2, 0, -2)\n",
      "1.8153800055908411 (-2, 0, 4)\n",
      "1.1486975649382964 (-2, 0, 10)\n",
      "1.0305051285304707 (-2, 1, -12)\n",
      "1.2900773006845312 (-2, 1, -9)\n",
      "1.6726910204294987 (-2, 1, -6)\n",
      "2.1752169837380384 (-2, 1, -3)\n",
      "1.4404399928990794 (-2, 2, -7)\n",
      "2.1210104386471196 (-2, 2, -1)\n",
      "1.6852056306951424 (-2, 2, 5)\n",
      "1.0712106709595173 (-2, 2, 11)\n",
      "2.4794895000000006 (-1, -1, 0)\n",
      "1.2961881850329564 (-1, 0, -10)\n",
      "2.6652318244563835 (-1, 0, -4)\n",
      "3.6307600111816822 (-1, 0, 2)\n",
      "1.5802545229021043 (-1, 0, 8)\n",
      "1.187779534309575 (-1, 1, -11)\n",
      "2.297395129876593 (-1, 1, -5)\n",
      "4.0951580518415 (-1, 1, 1)\n",
      "1.7697022220402947 (-1, 1, 7)\n",
      "1.0161432164799655 (-1, 1, 13)\n",
      "1.1329933333333335 (0, 0, -12)\n",
      "2.265986666666667 (0, 0, -6)\n"
     ]
    }
   ],
   "source": [
    "for reflex in crox.d_spacing(1).values():\n",
    "    print(reflex[0], reflex[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "crox.save(\"Cr2O3\", \"Eskolaite (R-3c)\", dmin=0.1, doi=\"NIST reference compound SRM 674b\")"
   ]
  },
  {
   "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.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
