{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from py4xs.local import ExpPara\n",
    "from py4xs.data2d import Data2d,Axes2dPlot,DataType\n",
    "import numpy as np\n",
    "import pylab as plt\n",
    "import matplotlib as mpl\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br>\n",
    "## Setting up ExpPara and Mask\n",
    "This is how the ExpPara structure for a detector is defined:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ep = ExpPara(619, 487)    # \n",
    "\n",
    "ene = 10790.6\n",
    "wl = 2.*np.pi*1973/ene\n",
    "\n",
    "ep.wavelength = wl\n",
    "ep.det_orient = 0.\n",
    "ep.det_tilt = -26.\n",
    "ep.det_phi = 0.\n",
    "ep.bm_ctr_x = -141.\n",
    "ep.bm_ctr_y = 328.3\n",
    "ep.ratioDw = 2.86\n",
    "ep.grazing_incident = False\n",
    "ep.flip = 1\n",
    "ep.incident_angle = 2.0\n",
    "ep.sample_normal = 0\n",
    "\n",
    "ep.calc_rot_matrix()\n",
    "ep.init_coordinates()\n",
    "\n",
    "ep.mask.read_file(\"data/Sol-mask.WAXS1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Associating the ExpPara to a scattering pattern saved in a CFB file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data from Pilatus 300K\n",
    "dw1 = Data2d(\"data/Sol-b_000018_WAXS1.cbf\")\n",
    "dw1.set_exp_para(ep)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Displaying the scattering patten using the Axes2dPlot class. Note that the mask is used to block of some sharp feature that may be due to the mica vacuum windows or hot detector pixels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 110040 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "ax = plt.gca()\n",
    "paxr = Axes2dPlot(ax, dw1.data, exp=ep)\n",
    "# plotting the data twice with and without mask to show how well the mask works\n",
    "paxr.plot(showMask=True)\n",
    "paxr.coordinate_translation=\"xy2qphi\"\n",
    "# the following operation only affect the most recent plot??\n",
    "paxr.img.set_clim(0,14000)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br>\n",
    "## Azimuthal average\n",
    "Calculating the azimuthal average of the scattering pattern. The *q*-grid does not need to have a constant spacing. The resulting intensity is adjusted based on the local bin width."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "qgrid = np.hstack((np.arange(0.005, 0.0499, 0.001),\n",
    "                   np.arange(0.05, 0.0999, 0.002),\n",
    "                   np.arange(0.1, 0.4999, 0.005),\n",
    "                   np.arange(0.5, 0.9999, 0.01),\n",
    "                   np.arange(1.0, 3.2,0.03)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "(Iq,dI) = dw1.conv_Iq(qgrid, mask=ep.mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14056 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "plt.errorbar(qgrid, Iq, dI)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br>\n",
    "## Coordinates conversion\n",
    "Now convert the scattering data into a *q-phi* intensity map, display the map, and annotate the map by a point markerand some line markers at constant coordinates:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "dw1.conv_Iqphi(500, 240, mask=ep.mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39980 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6,6))\n",
    "ax = plt.gca()\n",
    "paxr = Axes2dPlot(ax,dw1.qphi_data)\n",
    "paxr.plot(aspect=np.radians(1.))\n",
    "#paxr.set_color_scale(mpl.cm.gist_yarg)\n",
    "paxr.img.set_clim(8000,14000)\n",
    "paxr.mark_points([2.0], [0], \"k+\")\n",
    "paxr.mark_coords([0.5, 2.0], [20], \"r--\")\n",
    "paxr.draw_dec()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br>\n",
    "## Line cuts\n",
    "Extract a region-of-interest (ROI) and squash the data into a line profile:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14016 characters]\n",
      "text/plain": [
       "<Figure size 576x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ret = dw1.qphi_data.roi(1.95, 2.05, -50, 50)\n",
    "\n",
    "plt.figure(figsize=(8,4))\n",
    "plt.subplot(121)\n",
    "ax = plt.gca()\n",
    "paxr = Axes2dPlot(ax,ret)\n",
    "paxr.plot(aspect=np.radians(1.))\n",
    "paxr.img.set_clim(8000,14000)\n",
    "plt.subplot(122)\n",
    "plt.plot(ret.yc, ret.flatten(axis=1), \".\")\n",
    "plt.xlim([ret.yc[0], ret.yc[-1]])\n",
    "plt.ylim([0, 14000])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##<br>\n",
    "ROI and line cut in the horizontal direction:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23472 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ret = dw1.qphi_data.roi(1.0, 2.5, -5, 5)\n",
    "\n",
    "plt.figure(figsize=(6,6))\n",
    "plt.subplot(211)\n",
    "ax = plt.gca()\n",
    "paxr = Axes2dPlot(ax,ret)\n",
    "paxr.plot(aspect=np.radians(1.))\n",
    "paxr.img.set_clim(8000,14000)\n",
    "plt.subplot(212)\n",
    "plt.plot(ret.xc, ret.flatten(axis=0))\n",
    "plt.xlim([ret.xc[0], ret.xc[-1]])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
