{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import supporting package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xarray as xr\n",
    "import numpy as np\n",
    "import copy\n",
    "\n",
    "from uncertainties import ufloat\n",
    "from uncertainties import unumpy as unp\n",
    "from uncertainties import umath\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['font.size'] = 12\n",
    "\n",
    "from DataContainer.ReadData import read_hdf5_file\n",
    "from Analyser.ImagingAnalyser import ImageAnalyser\n",
    "from Analyser.FitAnalyser import FitAnalyser\n",
    "from Analyser.FitAnalyser import NewFitModel, DensityProfileBEC2dModel\n",
    "from ToolFunction.ToolFunction import *\n",
    "\n",
    "from scipy.optimize import curve_fit\n",
    "\n",
    "from ToolFunction.HomeMadeXarrayFunction import errorbar, dataarray_plot_errorbar\n",
    "xr.plot.dataarray_plot.errorbar = errorbar\n",
    "xr.plot.accessor.DataArrayPlotAccessor.errorbar = dataarray_plot_errorbar\n",
    "\n",
    "imageAnalyser = ImageAnalyser()\n",
    "\n",
    "# %matplotlib notebook"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Start a client for parallel computing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "    <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\"> </div>\n",
       "    <div style=\"margin-left: 48px;\">\n",
       "        <h3 style=\"margin-bottom: 0px;\">Client</h3>\n",
       "        <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Client-23571205-fef2-11ed-8994-80e82ce2fa8e</p>\n",
       "        <table style=\"width: 100%; text-align: left;\">\n",
       "\n",
       "        <tr>\n",
       "        \n",
       "            <td style=\"text-align: left;\"><strong>Connection method:</strong> Cluster object</td>\n",
       "            <td style=\"text-align: left;\"><strong>Cluster type:</strong> distributed.LocalCluster</td>\n",
       "        \n",
       "        </tr>\n",
       "\n",
       "        \n",
       "            <tr>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:8787/status\" target=\"_blank\">http://127.0.0.1:8787/status</a>\n",
       "                </td>\n",
       "                <td style=\"text-align: left;\"></td>\n",
       "            </tr>\n",
       "        \n",
       "\n",
       "        </table>\n",
       "\n",
       "        \n",
       "\n",
       "        \n",
       "            <details>\n",
       "            <summary style=\"margin-bottom: 20px;\"><h3 style=\"display: inline;\">Cluster Info</h3></summary>\n",
       "            <div class=\"jp-RenderedHTMLCommon jp-RenderedHTML jp-mod-trusted jp-OutputArea-output\">\n",
       "    <div style=\"width: 24px; height: 24px; background-color: #e1e1e1; border: 3px solid #9D9D9D; border-radius: 5px; position: absolute;\">\n",
       "    </div>\n",
       "    <div style=\"margin-left: 48px;\">\n",
       "        <h3 style=\"margin-bottom: 0px; margin-top: 0px;\">LocalCluster</h3>\n",
       "        <p style=\"color: #9D9D9D; margin-bottom: 0px;\">3caf8456</p>\n",
       "        <table style=\"width: 100%; text-align: left;\">\n",
       "            <tr>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Dashboard:</strong> <a href=\"http://127.0.0.1:8787/status\" target=\"_blank\">http://127.0.0.1:8787/status</a>\n",
       "                </td>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Workers:</strong> 8\n",
       "                </td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Total threads:</strong> 128\n",
       "                </td>\n",
       "                <td style=\"text-align: left;\">\n",
       "                    <strong>Total memory:</strong> 149.01 GiB\n",
       "                </td>\n",
       "            </tr>\n",
       "            \n",
       "            <tr>\n",
       "    <td style=\"text-align: left;\"><strong>Status:</strong> running</td>\n",
       "    <td style=\"text-align: left;\"><strong>Using processes:</strong> True</td>\n",
       "</tr>\n",
       "\n",
       "            \n",
       "        </table>\n",
       "\n",
       "        <details>\n",
       "            <summary style=\"margin-bottom: 20px;\">\n",
       "                <h3 style=\"display: inline;\">Scheduler Info</h3>\n",
       "            </summary>\n",
       "\n",
       "            <div style=\"\">\n",
       "    <div>\n",
       "        <div style=\"width: 24px; height: 24px; background-color: #FFF7E5; border: 3px solid #FF6132; border-radius: 5px; position: absolute;\"> </div>\n",
       "        <div style=\"margin-left: 48px;\">\n",
       "            <h3 style=\"margin-bottom: 0px;\">Scheduler</h3>\n",
       "            <p style=\"color: #9D9D9D; margin-bottom: 0px;\">Scheduler-7358e8cb-f1c0-435a-b9a6-1ae89646703d</p>\n",
       "            <table style=\"width: 100%; text-align: left;\">\n",
       "                <tr>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Comm:</strong> tcp://127.0.0.1:52558\n",
       "                    </td>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Workers:</strong> 8\n",
       "                    </td>\n",
       "                </tr>\n",
       "                <tr>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Dashboard:</strong> <a href=\"http://127.0.0.1:8787/status\" target=\"_blank\">http://127.0.0.1:8787/status</a>\n",
       "                    </td>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Total threads:</strong> 128\n",
       "                    </td>\n",
       "                </tr>\n",
       "                <tr>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Started:</strong> Just now\n",
       "                    </td>\n",
       "                    <td style=\"text-align: left;\">\n",
       "                        <strong>Total memory:</strong> 149.01 GiB\n",
       "                    </td>\n",
       "                </tr>\n",
       "            </table>\n",
       "        </div>\n",
       "    </div>\n",
       "\n",
       "    <details style=\"margin-left: 48px;\">\n",
       "        <summary style=\"margin-bottom: 20px;\">\n",
       "            <h3 style=\"display: inline;\">Workers</h3>\n",
       "        </summary>\n",
       "\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 0</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52603\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52607/status\" target=\"_blank\">http://127.0.0.1:52607/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52561\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-_emdmb6_\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 1</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52604\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52606/status\" target=\"_blank\">http://127.0.0.1:52606/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52562\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-ztl68fct\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 2</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52590\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52595/status\" target=\"_blank\">http://127.0.0.1:52595/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52563\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-55i03qxs\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 3</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52597\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52599/status\" target=\"_blank\">http://127.0.0.1:52599/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52564\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-a9s2v84x\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 4</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52605\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52610/status\" target=\"_blank\">http://127.0.0.1:52610/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52565\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-jb353de7\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 5</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52617\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52618/status\" target=\"_blank\">http://127.0.0.1:52618/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52566\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-g3juqn2t\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 6</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52612\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52613/status\" target=\"_blank\">http://127.0.0.1:52613/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52567\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-xu67jkqc\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "        <div style=\"margin-bottom: 20px;\">\n",
       "            <div style=\"width: 24px; height: 24px; background-color: #DBF5FF; border: 3px solid #4CC9FF; border-radius: 5px; position: absolute;\"> </div>\n",
       "            <div style=\"margin-left: 48px;\">\n",
       "            <details>\n",
       "                <summary>\n",
       "                    <h4 style=\"margin-bottom: 0px; display: inline;\">Worker: 7</h4>\n",
       "                </summary>\n",
       "                <table style=\"width: 100%; text-align: left;\">\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Comm: </strong> tcp://127.0.0.1:52598\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Total threads: </strong> 16\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Dashboard: </strong> <a href=\"http://127.0.0.1:52601/status\" target=\"_blank\">http://127.0.0.1:52601/status</a>\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Memory: </strong> 18.63 GiB\n",
       "                        </td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td style=\"text-align: left;\">\n",
       "                            <strong>Nanny: </strong> tcp://127.0.0.1:52568\n",
       "                        </td>\n",
       "                        <td style=\"text-align: left;\"></td>\n",
       "                    </tr>\n",
       "                    <tr>\n",
       "                        <td colspan=\"2\" style=\"text-align: left;\">\n",
       "                            <strong>Local directory: </strong> C:\\Users\\data\\AppData\\Local\\Temp\\dask-worker-space\\worker-kdvp5zu9\n",
       "                        </td>\n",
       "                    </tr>\n",
       "\n",
       "                    \n",
       "\n",
       "                    \n",
       "\n",
       "                </table>\n",
       "            </details>\n",
       "            </div>\n",
       "        </div>\n",
       "        \n",
       "\n",
       "    </details>\n",
       "</div>\n",
       "\n",
       "        </details>\n",
       "    </div>\n",
       "</div>\n",
       "            </details>\n",
       "        \n",
       "\n",
       "    </div>\n",
       "</div>"
      ],
      "text/plain": [
       "<Client: 'tcp://127.0.0.1:52558' processes=8 threads=128, memory=149.01 GiB>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dask.distributed import Client\n",
    "client = Client(n_workers=8, threads_per_worker=16, processes=True, memory_limit='20GB')\n",
    "client"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set global path for experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "groupList = [\n",
    "    \"images/MOT_3D_Camera/in_situ_absorption\",\n",
    "    \"images/ODT_1_Axis_Camera/in_situ_absorption\",\n",
    "    \"images/ODT_2_Axis_Camera/in_situ_absorption\",\n",
    "]\n",
    "\n",
    "dskey = {\n",
    "    \"images/MOT_3D_Camera/in_situ_absorption\": \"camera_0\",\n",
    "    \"images/ODT_1_Axis_Camera/in_situ_absorption\": \"camera_1\",\n",
    "    \"images/ODT_2_Axis_Camera/in_situ_absorption\": \"camera_2\",\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_dir = '//DyLabNAS/Data/'\n",
    "SequenceName = \"Repetition_scan\" + \"/\"\n",
    "folderPath = img_dir + SequenceName + get_date()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Repetition Scans"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## scan MOT freq - Z Comp 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "shotNum = \"0000\"\n",
    "filePath = folderPath + \"/\" + shotNum + \"/*.h5\"\n",
    "\n",
    "dataSetDict = {\n",
    "    dskey[groupList[i]]: read_hdf5_file(filePath, groupList[i])\n",
    "    for i in range(len(groupList))\n",
    "}\n",
    "\n",
    "dataSet = dataSetDict[\"camera_1\"]\n",
    "\n",
    "print_scanAxis(dataSet)\n",
    "\n",
    "scanAxis = get_scanAxis(dataSet)\n",
    "\n",
    "dataSet = auto_rechunk(dataSet)\n",
    "\n",
    "dataSet = imageAnalyser.get_absorption_images(dataSet)\n",
    "\n",
    "imageAnalyser.center = (310, 825)\n",
    "imageAnalyser.span = (550, 1200)\n",
    "imageAnalyser.fraction = (0.1, 0.1)\n",
    "\n",
    "dataSet_cropOD = imageAnalyser.crop_image(dataSet.OD)\n",
    "dataSet_cropOD = imageAnalyser.substract_offset(dataSet_cropOD).load()\n",
    "\n",
    "Ncount = imageAnalyser.get_Ncount(dataSet_cropOD)\n",
    "Ncount_mean = Ncount.mean(dim='runs')\n",
    "Ncount_std = Ncount.std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "Ncount_mean.plot.errorbar(ax=ax, yerr = Ncount_std, fmt='ob')\n",
    "plt.xlabel('MOT AOM Freq (MHz)')\n",
    "plt.ylabel('NCount')\n",
    "plt.tight_layout()\n",
    "plt.grid(visible=1)\n",
    "plt.show()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## scan Push freq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shotNum = \"0001\"\n",
    "filePath = folderPath + \"/\" + shotNum + \"/*.h5\"\n",
    "\n",
    "dataSetDict = {\n",
    "    dskey[groupList[i]]: read_hdf5_file(filePath, groupList[i])\n",
    "    for i in range(len(groupList))\n",
    "}\n",
    "\n",
    "dataSet = dataSetDict[\"camera_1\"]\n",
    "\n",
    "print_scanAxis(dataSet)\n",
    "\n",
    "scanAxis = get_scanAxis(dataSet)\n",
    "\n",
    "dataSet = auto_rechunk(dataSet)\n",
    "\n",
    "dataSet = imageAnalyser.get_absorption_images(dataSet)\n",
    "\n",
    "imageAnalyser.center = (310, 825)\n",
    "imageAnalyser.span = (550, 1200)\n",
    "imageAnalyser.fraction = (0.1, 0.1)\n",
    "\n",
    "dataSet_cropOD = imageAnalyser.crop_image(dataSet.OD)\n",
    "dataSet_cropOD = imageAnalyser.substract_offset(dataSet_cropOD).load()\n",
    "\n",
    "Ncount = imageAnalyser.get_Ncount(dataSet_cropOD)\n",
    "Ncount_mean = Ncount.mean(dim='runs')\n",
    "Ncount_std = Ncount.std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "Ncount_mean.plot.errorbar(ax=ax, yerr = Ncount_std, fmt='ob')\n",
    "plt.xlabel('Push AOM Freq (MHz)')\n",
    "plt.ylabel('NCount')\n",
    "plt.tight_layout()\n",
    "plt.grid(visible=1)\n",
    "plt.show()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## scan cMOT final Amp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shotNum = \"0003\"\n",
    "filePath = folderPath + \"/\" + shotNum + \"/*.h5\"\n",
    "\n",
    "dataSetDict = {\n",
    "    dskey[groupList[i]]: read_hdf5_file(filePath, groupList[i])\n",
    "    for i in range(len(groupList))\n",
    "}\n",
    "\n",
    "dataSet = dataSetDict[\"camera_1\"]\n",
    "\n",
    "print_scanAxis(dataSet)\n",
    "\n",
    "scanAxis = get_scanAxis(dataSet)\n",
    "\n",
    "dataSet = auto_rechunk(dataSet)\n",
    "\n",
    "dataSet = imageAnalyser.get_absorption_images(dataSet)\n",
    "\n",
    "imageAnalyser.center = (306, 874)\n",
    "imageAnalyser.span = (550, 500)\n",
    "imageAnalyser.fraction = (0.1, 0.1)\n",
    "\n",
    "dataSet_cropOD = imageAnalyser.crop_image(dataSet.OD)\n",
    "dataSet_cropOD = imageAnalyser.substract_offset(dataSet_cropOD).load()\n",
    "\n",
    "Ncount = imageAnalyser.get_Ncount(dataSet_cropOD)\n",
    "Ncount_mean = Ncount.mean(dim='runs')\n",
    "Ncount_std = Ncount.std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "Ncount_mean.plot.errorbar(ax=ax, yerr = Ncount_std, fmt='ob')\n",
    "plt.xlabel('cMOT final Amp (%)')\n",
    "plt.ylabel('NCount')\n",
    "#plt.ylim([0, 25000])\n",
    "plt.tight_layout()\n",
    "plt.grid(visible=1)\n",
    "plt.show()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## scan Z comp current"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shotNum = \"0004\"\n",
    "filePath = folderPath + \"/\" + shotNum + \"/*.h5\"\n",
    "\n",
    "dataSetDict = {\n",
    "    dskey[groupList[i]]: read_hdf5_file(filePath, groupList[i])\n",
    "    for i in range(len(groupList))\n",
    "}\n",
    "\n",
    "dataSet = dataSetDict[\"camera_1\"]\n",
    "\n",
    "print_scanAxis(dataSet)\n",
    "\n",
    "scanAxis = get_scanAxis(dataSet)\n",
    "\n",
    "dataSet = auto_rechunk(dataSet)\n",
    "\n",
    "dataSet = imageAnalyser.get_absorption_images(dataSet)\n",
    "\n",
    "imageAnalyser.center = (310, 825)\n",
    "imageAnalyser.span = (550, 1200)\n",
    "imageAnalyser.fraction = (0.1, 0.1)\n",
    "\n",
    "dataSet_cropOD = imageAnalyser.crop_image(dataSet.OD)\n",
    "dataSet_cropOD = imageAnalyser.substract_offset(dataSet_cropOD).load()\n",
    "\n",
    "Ncount = imageAnalyser.get_Ncount(dataSet_cropOD)\n",
    "Ncount_mean = Ncount.mean(dim='runs')\n",
    "Ncount_std = Ncount.std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "Ncount_mean.plot.errorbar(ax=ax, yerr = Ncount_std, fmt='ob')\n",
    "plt.xlabel('comp Z current (A)')\n",
    "plt.ylabel('NCount')\n",
    "plt.tight_layout()\n",
    "plt.grid(visible=1)\n",
    "plt.show()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Evaporative Cooling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_dir = '//DyLabNAS/Data/'\n",
    "SequenceName = \"Evaporative_Cooling\" + \"/\"\n",
    "folderPath = img_dir + SequenceName + get_date()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check our BEC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "shotNum = \"0001\"\n",
    "filePath = folderPath + \"/\" + shotNum + \"/*.h5\"\n",
    "\n",
    "# datesetOfGlobal = xr.Dataset(\n",
    "#         data_vars={}\n",
    "# )\n",
    "\n",
    "# datesetOfGlobal.attrs['scanAxis'] = ['sin_mod_freq']\n",
    "# datesetOfGlobal.attrs['sin_mod_freq'] = [40.0, 40.5, 41.0, 41.5, 42.0, 42.5, 43.0, 43.5, 44.0, 44.5, 45.0, 45.5, 46.0, 46.5, 47.0, 47.5, 48.0, 48.5, 49.0, 49.5, 50.0, 50.5, 51.0, 51.5, 52.0, 52.5, 53.0, 53.5, 54.0, 54.5, 55.0, 55.5, 56.0, 56.5, 57.0, 57.5, 58.0, 58.5, 59.0, 59.5, 60.0]\n",
    "dataSetDict = {\n",
    "    dskey[groupList[i]]: read_hdf5_file(filePath, groupList[i], excludeAxis = ['sweep_start_freq', 'sweep_stop_freq'])\n",
    "    for i in [0]\n",
    "}\n",
    "\n",
    "dataSet = dataSetDict[\"camera_0\"]\n",
    "\n",
    "print_scanAxis(dataSet)\n",
    "\n",
    "scanAxis = get_scanAxis(dataSet)\n",
    "\n",
    "dataSet = auto_rechunk(dataSet)\n",
    "\n",
    "dataSet = imageAnalyser.get_absorption_images(dataSet)\n",
    "\n",
    "imageAnalyser.center = (884, 953)\n",
    "imageAnalyser.span = (100, 100)\n",
    "imageAnalyser.fraction = (0.1, 0.1)\n",
    "\n",
    "dataSet_cropOD = imageAnalyser.crop_image(dataSet.OD)\n",
    "dataSet_cropOD = imageAnalyser.substract_offset(dataSet_cropOD).load()\n",
    "\n",
    "Ncount = imageAnalyser.get_Ncount(dataSet_cropOD)\n",
    "Ncount_mean = Ncount# .mean(dim='runs')\n",
    "Ncount_std = Ncount# .std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "\n",
    "Ncount_mean.plot.errorbar(ax=ax, yerr = None, fmt='ob')\n",
    "\n",
    "plt.ylabel('NCount')\n",
    "plt.tight_layout()\n",
    "#plt.ylim([0, 500])\n",
    "plt.grid(visible=1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataSet_cropOD.plot.pcolormesh(cmap='jet', vmin=0, col=scanAxis[0], row=scanAxis[1])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fitModel = DensityProfileBEC2dModel()\n",
    "fitAnalyser = FitAnalyser(fitModel, fitDim=2)\n",
    "params = fitAnalyser.guess(dataSet_cropOD, guess_kwargs=dict(pureBECThreshold=0.3), dask=\"parallelized\")\n",
    "# params = fitAnalyser_1.fitModel.make_params()\n",
    "# params.add(name=\"amplitude\", value=  -4000, max=np.inf, min=-np.inf, vary=True)\n",
    "# params.add(name=\"center\", value=  0.435, max=np.inf, min=-np.inf, vary=True)\n",
    "# params.add(name=\"sigma\", value=  0.01, max=np.inf, min=  0, vary=True)\n",
    "# params.add(name=\"offset\", value=  1000, max=np.inf, min=-np.inf, vary=True)\n",
    "\n",
    "fitResult = fitAnalyser.fit(dataSet_cropOD, params).load()\n",
    "\n",
    "val = fitAnalyser.get_fit_value(fitResult)\n",
    "std = fitAnalyser.get_fit_std(fitResult)\n",
    "\n",
    "fitCurve = fitAnalyser.eval(fitResult, x=np.arange(100), y=np.arange(100), dask=\"parallelized\").load()\n",
    "\n",
    "fitCurve.plot.pcolormesh(cmap='jet', vmin=0, col=scanAxis[0], row=scanAxis[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measure trap frequency at the end of evap. 3, blink off time = 275 µs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The detected scaning axes and values are: \n",
      "\n",
      "{'blink_on_time': array([0.005 , 0.0057, 0.0064, 0.0071, 0.0078, 0.0085, 0.0092, 0.0099,\n",
      "       0.0106, 0.0113, 0.012 , 0.0127, 0.0134, 0.0141, 0.0148, 0.0155,\n",
      "       0.0162, 0.0169, 0.0176, 0.0183, 0.019 , 0.0197, 0.0204, 0.0211,\n",
      "       0.0218, 0.0225, 0.0232, 0.0239, 0.0246, 0.0253, 0.026 , 0.0267,\n",
      "       0.0274, 0.0281, 0.0288, 0.0295, 0.0302, 0.0309, 0.0316, 0.0323,\n",
      "       0.033 , 0.0337, 0.0344, 0.0351, 0.0358, 0.0365, 0.0372, 0.0379,\n",
      "       0.0386, 0.0393, 0.04  ]), 'runs': array([0., 1., 2.])}\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 28880 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "shotNum = \"0015\"\n",
    "filePath = folderPath + \"/\" + shotNum + \"/*.h5\"\n",
    "\n",
    "dataSetDict = {\n",
    "    dskey[groupList[i]]: read_hdf5_file(filePath, groupList[i], maxFileNum=153, excludeAxis = ['sweep_start_freq', 'sweep_stop_freq'])\n",
    "    for i in [0]\n",
    "}\n",
    "\n",
    "dataSet = dataSetDict[\"camera_0\"]\n",
    "\n",
    "print_scanAxis(dataSet)\n",
    "\n",
    "scanAxis = get_scanAxis(dataSet)\n",
    "\n",
    "dataSet = auto_rechunk(dataSet)\n",
    "\n",
    "dataSet = imageAnalyser.get_absorption_images(dataSet)\n",
    "\n",
    "imageAnalyser.center = (180, 958)\n",
    "imageAnalyser.span = (100, 100)\n",
    "imageAnalyser.fraction = (0.1, 0.1)\n",
    "\n",
    "dataSet_cropOD = imageAnalyser.crop_image(dataSet.OD)\n",
    "dataSet_cropOD = imageAnalyser.substract_offset(dataSet_cropOD).load()\n",
    "\n",
    "Ncount = imageAnalyser.get_Ncount(dataSet_cropOD)\n",
    "Ncount_mean = Ncount.mean(dim='runs')\n",
    "Ncount_std = Ncount.std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "\n",
    "Ncount_mean.plot.errorbar(ax=ax, yerr = None, fmt='ob')\n",
    "\n",
    "plt.ylabel('NCount')\n",
    "plt.tight_layout()\n",
    "#plt.ylim([0, 500])\n",
    "plt.grid(visible=1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "dataSet_cropOD.plot.pcolormesh(cmap='jet', vmin=0, col=scanAxis[0], row=scanAxis[1])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fitAnalyser = FitAnalyser(\"Gaussian-2D\", fitDim=2)\n",
    "params = fitAnalyser.guess(dataSet_cropOD, dask=\"parallelized\")\n",
    "# params = fitAnalyser_1.fitModel.make_params()\n",
    "# params.add(name=\"amplitude\", value=  -4000, max=np.inf, min=-np.inf, vary=True)\n",
    "# params.add(name=\"center\", value=  0.435, max=np.inf, min=-np.inf, vary=True)\n",
    "# params.add(name=\"sigma\", value=  0.01, max=np.inf, min=  0, vary=True)\n",
    "# params.add(name=\"offset\", value=  1000, max=np.inf, min=-np.inf, vary=True)\n",
    "\n",
    "fitResult = fitAnalyser.fit(dataSet_cropOD, params).load()\n",
    "\n",
    "val = fitAnalyser.get_fit_value(fitResult)\n",
    "std = fitAnalyser.get_fit_std(fitResult)\n",
    "\n",
    "fitCurve = fitAnalyser.eval(fitResult, x=np.arange(100), y=np.arange(100), dask=\"parallelized\").load()\n",
    "\n",
    "# fitCurve.plot.pcolormesh(cmap='jet', vmin=0, col=scanAxis[0], row=scanAxis[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 46620 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dataKey = 'sigmax'\n",
    "# dataKey = 'centerx'\n",
    "# dataKey = 'sigmay'\n",
    "# dataKey = 'centery'\n",
    "\n",
    "val_mean = val[dataKey].mean(dim='runs')\n",
    "std_mean = val[dataKey].std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "\n",
    "val_mean.plot.errorbar(ax=ax, yerr=std_mean, fmt='--ob')\n",
    "\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dampingOscillation(x, center=0, amplitude=1.0, frequency=1.0, decay=1.0, slope=1.0, intercept=0.0):\n",
    "    return amplitude * np.exp(-decay*x)*np.sin(2*np.pi*frequency*(x-center)) + (slope * x + intercept)\n",
    "\n",
    "fitModel = NewFitModel(dampingOscillation)\n",
    "\n",
    "fitAnalyser = FitAnalyser(fitModel, fitDim=1)\n",
    "\n",
    "params = fitAnalyser.fitModel.make_params()\n",
    "params.add(name=\"center\", value=  0, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"amplitude\", value=  1, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"frequency\", value=  260, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"decay\", value=  0.1, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"slope\", value=  30, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"intercept\", value=  5.5, max=np.inf, min=-np.inf, vary=True)\n",
    "\n",
    "fitResult = fitAnalyser.fit(val_mean, params, x=scanAxis[0]).load()\n",
    "\n",
    "x = np.linspace(val_mean[scanAxis[0]][0], val_mean[scanAxis[0]][-1], 500)\n",
    "fitCurve = fitAnalyser.eval(fitResult, x=x, dask=\"parallelized\").load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 58104 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f_y = 126.5119 ± 0.7136 kHz\n"
     ]
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "\n",
    "val_mean.plot.errorbar(ax=ax, yerr=std_mean, fmt='or')\n",
    "fitCurve.plot.errorbar(ax=ax, yerr=None, fmt='--g')\n",
    "\n",
    "plt.grid()\n",
    "\n",
    "plt.xlabel('hold time after switch on the trap (s)')\n",
    "plt.ylabel('y center (pixels)')\n",
    "\n",
    "plt.show()\n",
    "\n",
    "val = fitAnalyser.get_fit_full_result(fitResult)\n",
    "f = val.frequency.item() / 2\n",
    "\n",
    "print('f_y = %.4f \\u00B1 %.4f kHz'% tuple([f.n,f.s]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lmfit.models import LinearModel\n",
    "from Analyser.FitAnalyser import DampingOscillationModel\n",
    "\n",
    "fitModel = LinearModel() * DampingOscillationModel()\n",
    "\n",
    "fitAnalyser = FitAnalyser(fitModel, fitDim=1)\n",
    "\n",
    "params = fitAnalyser.fitModel.make_params()\n",
    "params.add(name=\"slope\", value=  30, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"intercept\", value=  5.5, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"center\", value=  0, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"amplitude\", value=  1, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"frequency\", value=  260, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"decay\", value=  0.1, max=np.inf, min=-np.inf, vary=True)\n",
    "params.add(name=\"offset\", value=  0, max=np.inf, min=-np.inf, vary=False)\n",
    "\n",
    "fitResult = fitAnalyser.fit(val_mean, params, x=scanAxis[0]).load()\n",
    "\n",
    "fitCurve = fitAnalyser.eval(fitResult, x=np.arange(100), y=np.arange(100), dask=\"parallelized\").load()\n",
    "\n",
    "# fitCurve.plot.pcolormesh(cmap='jet', vmin=0, col=scanAxis[0], row=scanAxis[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val_mean = val[dataKey].mean(dim='runs')\n",
    "std_mean = val[dataKey].std(dim='runs')\n",
    "val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "def damp_osci(t, t0, A, B, nu, gamma):\n",
    "    return A * np.exp(-gamma*t)*np.sin(2*np.pi*nu*(t-t0)) + B\n",
    "\n",
    "fitted_qtys_1 = val_mean.to_numpy()\n",
    "fitted_qtys_err_1 = std_mean.to_numpy()\n",
    "scan_para = dataSet_cropOD[scanAxis[0]].to_numpy()\n",
    "\n",
    "# fitted_qtys_1 = np.delete(fitted_qtys_1, 27)\n",
    "# fitted_qtys_err_1 = np.delete(fitted_qtys_err_1, 27)\n",
    "# scan_para = np.delete(scan_para, 27)\n",
    "\n",
    "plt.figure()\n",
    "popt_x, pcov_x = curve_fit(damp_osci, scan_para, fitted_qtys_1, np.array([0, 7.5, 42.5, 1.3e2, 0.1]))\n",
    "freqdata = np.linspace(0.005, 40e-3, 500)\n",
    "plt.plot(freqdata, damp_osci(freqdata, *popt_x), 'g--',label='fit: t0=%5.3f, A=%5.3f, B=%5.3f, nu=%5.3f, Gamma=%5.3f' % tuple(popt_x))\n",
    "plt.errorbar(scan_para, fitted_qtys_1, yerr=fitted_qtys_err_1, fmt='or')\n",
    "plt.xlabel('hold time after switch on the trap (s)')\n",
    "plt.ylabel('y center (pixels)')\n",
    "plt.tight_layout()\n",
    "plt.grid(visible=1)\n",
    "#plt.ylim([0,750])\n",
    "#plt.xlim([90, 121])\n",
    "#plt.legend(prop={'size': 14})\n",
    "plt.show()\n",
    "\n",
    "f_x = popt_x[3] / 1000\n",
    "df_x = pcov_x[3][3]**0.5 / 1000\n",
    "\n",
    "print('f_y = %.4f \\u00B1 %.4f kHz'% tuple([np.abs(f_x),df_x]))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measure trap frequency at the end of evap. 3, blink off time = 250 µs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "%matplotlib notebook\n",
    "shotNum = \"0015\"\n",
    "filePath = folderPath + \"/\" + shotNum + \"/*.h5\"\n",
    "\n",
    "dataSetDict = {\n",
    "    dskey[groupList[i]]: read_hdf5_file(filePath, groupList[i], maxFileNum=102, excludeAxis = ['sweep_start_freq', 'sweep_stop_freq'])\n",
    "    for i in [0]\n",
    "}\n",
    "\n",
    "dataSet = dataSetDict[\"camera_0\"]\n",
    "\n",
    "print_scanAxis(dataSet)\n",
    "\n",
    "scanAxis = get_scanAxis(dataSet)\n",
    "\n",
    "dataSet = auto_rechunk(dataSet)\n",
    "\n",
    "dataSet = imageAnalyser.get_absorption_images(dataSet)\n",
    "\n",
    "imageAnalyser.center = (180, 958)\n",
    "imageAnalyser.span = (100, 100)\n",
    "imageAnalyser.fraction = (0.1, 0.1)\n",
    "\n",
    "dataSet_cropOD = imageAnalyser.crop_image(dataSet.OD)\n",
    "dataSet_cropOD = imageAnalyser.substract_offset(dataSet_cropOD).load()\n",
    "\n",
    "Ncount = imageAnalyser.get_Ncount(dataSet_cropOD)\n",
    "Ncount_mean = Ncount.mean(dim='runs')\n",
    "Ncount_std = Ncount.std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "\n",
    "Ncount_mean.plot.errorbar(ax=ax, yerr = None, fmt='ob')\n",
    "\n",
    "plt.ylabel('NCount')\n",
    "plt.tight_layout()\n",
    "#plt.ylim([0, 500])\n",
    "plt.grid(visible=1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "dataSet_cropOD.plot.pcolormesh(cmap='jet', vmin=0, col=scanAxis[0], row=scanAxis[1])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fitAnalyser = FitAnalyser(\"Gaussian-2D\", fitDim=2)\n",
    "params = fitAnalyser.guess(dataSet_cropOD, dask=\"parallelized\")\n",
    "# params = fitAnalyser_1.fitModel.make_params()\n",
    "# params.add(name=\"amplitude\", value=  -4000, max=np.inf, min=-np.inf, vary=True)\n",
    "# params.add(name=\"center\", value=  0.435, max=np.inf, min=-np.inf, vary=True)\n",
    "# params.add(name=\"sigma\", value=  0.01, max=np.inf, min=  0, vary=True)\n",
    "# params.add(name=\"offset\", value=  1000, max=np.inf, min=-np.inf, vary=True)\n",
    "\n",
    "fitResult = fitAnalyser.fit(dataSet_cropOD, params).load()\n",
    "\n",
    "val = fitAnalyser.get_fit_value(fitResult)\n",
    "std = fitAnalyser.get_fit_std(fitResult)\n",
    "\n",
    "fitCurve = fitAnalyser.eval(fitResult, x=np.arange(100), y=np.arange(100), dask=\"parallelized\").load()\n",
    "\n",
    "# fitCurve.plot.pcolormesh(cmap='jet', vmin=0, col=scanAxis[0], row=scanAxis[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dataKey = 'sigmax'\n",
    "dataKey = 'centerx'\n",
    "# dataKey = 'sigmay'\n",
    "# dataKey = 'centery'\n",
    "\n",
    "val_mean = val[dataKey].mean(dim='runs')\n",
    "std_mean = val[dataKey].std(dim='runs')\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.gca()\n",
    "\n",
    "val_mean.plot.errorbar(yerr=std_mean, fmt='--ob')\n",
    "\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "def damp_osci(t, t0, A, B, nu, gamma):\n",
    "    return A * np.exp(-gamma*t)*np.sin(2*np.pi*nu*(t-t0)) + B\n",
    "\n",
    "fitted_qtys_1 = val_mean.to_numpy()\n",
    "fitted_qtys_err_1 = std_mean.to_numpy()\n",
    "scan_para = dataSet_cropOD[scanAxis[0]].to_numpy()\n",
    "\n",
    "# fitted_qtys_1 = np.delete(fitted_qtys_1, 27)\n",
    "# fitted_qtys_err_1 = np.delete(fitted_qtys_err_1, 27)\n",
    "# scan_para = np.delete(scan_para, 27)\n",
    "\n",
    "plt.figure()\n",
    "popt_x, pcov_x = curve_fit(damp_osci, scan_para, fitted_qtys_1, np.array([0, 7.5, 42.5, 1.3e2, 0.1]))\n",
    "freqdata = np.linspace(0.005, 40e-3, 500)\n",
    "plt.plot(freqdata, damp_osci(freqdata, *popt_x), 'g--',label='fit: t0=%5.3f, A=%5.3f, B=%5.3f, nu=%5.3f, Gamma=%5.3f' % tuple(popt_x))\n",
    "plt.errorbar(scan_para, fitted_qtys_1, yerr=fitted_qtys_err_1, fmt='or')\n",
    "plt.xlabel('hold time after switch on the trap (s)')\n",
    "plt.ylabel('y center (pixels)')\n",
    "plt.tight_layout()\n",
    "plt.grid(visible=1)\n",
    "#plt.ylim([0,750])\n",
    "#plt.xlim([90, 121])\n",
    "#plt.legend(prop={'size': 14})\n",
    "plt.show()\n",
    "\n",
    "f_x = popt_x[3] / 1000\n",
    "df_x = pcov_x[3][3]**0.5 / 1000\n",
    "\n",
    "print('f_y = %.4f \\u00B1 %.4f kHz'% tuple([np.abs(f_x),df_x]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sqrt(0.091**2 + 0.334**2 + 0.447**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "muB = 9.274e-24\n",
    "hbar = 6.626e-34 / (2 * np.pi)\n",
    "gJ = 1.24\n",
    "Delta = 2 * np.pi * 579.12 * 1e3\n",
    "\n",
    "Bz = (Delta*hbar) / (muB*gJ)\n",
    "print(Bz * 1e4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(fitted_SlopeInkHz * 0.5) + fitted_offsetInkHz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "l = list(np.arange(0.005, 0.04, 0.0007))\n",
    "# l = np.logspace(np.log10(250e-6), np.log10(500e-3), num=15)\n",
    "\n",
    "l = [round(item, 7) for item in l]\n",
    "#random.shuffle(l)\n",
    "\n",
    "print(l)\n",
    "print(len(l))\n",
    "np.mean(l)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ODT 1 Calibration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v_high = 2.7\n",
    "\"\"\"High Power\"\"\"\n",
    "P_arm1_high = 5.776 * v_high - 0.683\n",
    "\n",
    "v_mid = 0.2076\n",
    "\"\"\"Intermediate Power\"\"\"\n",
    "P_arm1_mid = 5.815 * v_mid - 0.03651\n",
    "\n",
    "v_low = 0.0587\n",
    "\"\"\"Low Power\"\"\"\n",
    "P_arm1_low = 5271 * v_low - 27.5\n",
    "\n",
    "print(round(P_arm1_high, 3))\n",
    "print(round(P_arm1_mid, 3))\n",
    "print(round(P_arm1_low, 3))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ODT 2 Power Calibration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = 0.7607\n",
    "P_arm2 = 2.302 * v - 0.06452\n",
    "print(round(P_arm2, 3))"
   ]
  }
 ],
 "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.9.13"
  },
  "vscode": {
   "interpreter": {
    "hash": "c05913ad4f24fdc6b2418069394dc5835b1981849b107c9ba6df693aafd66650"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
