{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate a single HIFI data set, check rotframe test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ν = 271.0 MHz\n",
      "ν = 271.8 MHz\n",
      "ν = 271.0 MHz\n",
      "ν = 271.3 MHz\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(-1.0, 1.0)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 27916 characters]\n",
      "text/plain": [
       "<Figure size 720x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# %matplotlib\n",
    "import numpy as np\n",
    "from time import time\n",
    "from scipy.stats import norm\n",
    "import matplotlib.pyplot as P\n",
    "\n",
    "# time units µs, hence γ in (µs T)-1\n",
    "τ, γ = 2.197, 2*np.pi*135.5\n",
    "\n",
    "def mu(t,A,B,φ,σ):\n",
    "    print ('ν = {:.1f} MHz'.format(γ*B/2/np.pi))\n",
    "    return A*np.cos(γ*B*t+np.deg2rad(φ))*np.exp(-(σ*t)**2/2)\n",
    "\n",
    "def histo(N,t,p):\n",
    "    return N*np.exp(-t/τ)*(1+p)\n",
    "\n",
    "def rebin(x,y,start,stop,pack):\n",
    "    '''\n",
    "    * x,y are 2D arrays to be rebinned\n",
    "    * pack is the rebinning factor, e.g it returns::\n",
    "\n",
    "        xb = array([x[0:pack].sum()/pack])\n",
    "\n",
    "    * strstp = [start,stop] is a list of indices\n",
    "\n",
    "        rebinning is done on slices of x,y[,e]\n",
    "        such as x[start:stop]\n",
    "\n",
    "    use either::\n",
    "\n",
    "        xb,yb = rebin(x,y,strstp,pack)\n",
    "\n",
    "    or::\n",
    "\n",
    "       xb,yb,eyb = rebin(x,y,strstp,pack,ey) # the 5th is y error\n",
    "\n",
    "    Works also with pack = 1\n",
    "\n",
    "    Works for 1d array x and 2D ndarrays y, ey returning 2D arrays\n",
    "    xb, yb, eb, e.g.::\n",
    " \n",
    "         xb.shape = (1,25000), \n",
    "         yb.shape = (nruns,25000) \n",
    "\n",
    "    '''\n",
    "    m = int(np.floor((stop-start)/pack)) # length of rebinned xb\n",
    "    mn = m*pack # length of x slice \n",
    "    xx =x[start:start+mn] # slice of the first 2D array\n",
    "    yy = y[start:start+mn]  # slice row\n",
    "    xx = xx.reshape(m,pack) # temporaty 2d array\n",
    "    yy = yy.reshape(m,pack)  # temporaty 2d\n",
    "    xb = xx.sum(1)/pack # rebinned first ndarray\n",
    "    yb = yy.sum(1)/pack # rebinned row\n",
    "    return xb,yb\n",
    "\n",
    "def rotframe(a,t,nu):\n",
    "    n = t.shape[0]\n",
    "    m = int(n/2)\n",
    "    f = np.arange(-m,m)\n",
    "    flt = np.exp(-(4*f/m)**2)\n",
    "    lowpass = np.empty(n)\n",
    "    lowpass[:m] = flt[m:]\n",
    "    lowpass[m:] = flt[:m]\n",
    "    lowpass = np.where(f<-m/2,1,0)+np.where(f>m/2,1,0)\n",
    "    return 2*np.real(np.fft.ifft(np.fft.fft(a*np.cos(2*np.pi*nu*t))*lowpass))\n",
    "\n",
    "                           \n",
    "Nf, Nb, A, B, φ, σ = 10.0, 10.4, 0.15,2,0.,0.3\n",
    "α = Nb/Nf    \n",
    "t = np.linspace(0.,6.,200000)\n",
    "\n",
    "yf = histo(Nf,t,mu(t,A,B,φ,σ)+mu(t,A,B+0.006,φ,σ))\n",
    "yfn = yf + norm.rvs(scale=np.sqrt(yf))\n",
    "\n",
    "yb = histo(Nb,t,mu(t,A,B,φ+180,σ)+mu(t,A,B+0.002,φ+180,σ))\n",
    "ybn = yb + norm.rvs(scale=np.sqrt(yb))\n",
    "\n",
    "asymm = (yfn - α*ybn) / (Nf + Nb)\n",
    "\n",
    "fig, ax = P.subplots(1,2,figsize=(10,4))\n",
    "\n",
    "#tt,aa = rebin (t,asymm,100000,102000,10)\n",
    "ax[0].plot(t,asymm)\n",
    "ax[0].plot(t,(yf-α*yb)/(Nf+Nb))\n",
    "tt,aa = rebin (t,rotframe(asymm,t,266.),0,200000,1000)\n",
    "ax[1].plot(tt,aa)\n",
    "ax[1].set_ylim(-1,1)\n",
    "                           \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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
