{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analyse 2M++ resims"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/scratch/pyenvs/versions/3.6.5/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pylab as plt\n",
    "from src.read_all_h5 import explore_chain, rebuild_spliced_h5\n",
    "from src.special_regions import *\n",
    "import healpy as hp\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Just some helper Routines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#little reading routine\n",
    "def load_resim(fname,ncpu=1):\n",
    "    print (\"reading : \", fname)\n",
    "  \n",
    "    if ncpu>0:\n",
    "            a=rebuild_spliced_h5(fname,['u_pos','u_vel'],ncpu,verbose=False)\n",
    "            return np.array(a[\"u_pos\"][...]),np.array(a[\"u_vel\"][...])\n",
    "    else:\n",
    "            print ('Error: need number of processes to read files !')\n",
    "    \n",
    "    return None\n",
    "#print special regions\n",
    "special_coords.keys()\n",
    "\n",
    "#set observer\n",
    "corner=np.array([-0.5*677.77,-0.5*677.77,-0.5*677.77])\n",
    "\n",
    "#change coordinates\n",
    "def cart2sphere(pos,corner=np.array([0,0,0]),rmax=100):\n",
    "    r   = np.sqrt((pos[:,0]+corner[0])**2+(pos[:,1]+corner[1])**2+(pos[:,2]+corner[2])**2)\n",
    "    foo=np.where(r<rmax)\n",
    "    r   = r[foo]\n",
    "    \n",
    "    phi,theta=hp.vec2ang(np.array([pos[foo][:,0]+corner[0],pos[foo][:,1]+corner[1],pos[foo][:,2]+corner[2]]).T, lonlat=True)\n",
    "    \n",
    "    return r,phi,theta\n",
    "\n",
    "\n",
    "def get_densities(r,ra,dec,rmax,region='full'):\n",
    "    rho_mean=(512./677.77)**3\n",
    "    \n",
    "    if(region=='full'):\n",
    "        hist, binEdges = np.histogram(r,bins=600, density=False,range=[0,rmax])\n",
    "        dv             = np.pi*4./3.*(binEdges[1:]**3-binEdges[:-1]**3)\n",
    "    else:\n",
    "        foo,da = sky_region(region,ra,dec)\n",
    "        hist, binEdges = np.histogram(r[foo],bins=600, density=False,range=[0,rmax])\n",
    "        dv             = da/3.*(binEdges[1:]**3-binEdges[:-1]**3)\n",
    "        \n",
    "    \n",
    "    bincenters     = 0.5*(binEdges[1:]+binEdges[:-1])\n",
    "    \n",
    "    rho            = hist/dv\n",
    "    delta          = rho/rho_mean -1.\n",
    "    \n",
    "    aux       = np.cumsum(hist)\n",
    "    auxv      = 4./3.*np.pi*binEdges[1:]**3\n",
    "    rho_cum   = aux/auxv\n",
    "    delta_cum = rho_cum/rho_mean-1.\n",
    "    \n",
    "    return bincenters,delta, binEdges[1:], delta_cum \n",
    "\n",
    "def find_ncpu(fbase):\n",
    "    import glob\n",
    "    nn = len(glob.glob(fbase+\"*\"))\n",
    "    return nn\n",
    "\n",
    "def plot_radial_density(r,ra,dec):\n",
    "    \n",
    "    rmax=300\n",
    "    foo=sky_region('R3',ra,dec)\n",
    "    x_R1,delta_R1,x_cum_R1,delta_cum_R1=get_densities(r[foo],rmax)\n",
    "\n",
    "    f, (ax1, ax2, ax3) = plt.subplots(1,3, sharex=True, sharey=True,figsize=(12,4))\n",
    "    \n",
    "    \n",
    "    ax1.set_xlim([0,rmax])\n",
    "    ax1.set_ylim([-1,1])\n",
    "    ax1.set_xlabel(r'$r$')\n",
    "    ax1.set_ylabel(r'$\\delta(r)$')\n",
    "    ax1.plot(x_R1,delta_R1,color='red')\n",
    "    ax1.plot(x_R1,delta_R1*0.,color='black',linestyle='--')\n",
    "    \n",
    "    ax2.set_xlim([0,rmax])\n",
    "    ax2.set_ylim([-1,1])\n",
    "    ax2.set_xlabel(r'$r$')\n",
    "    ax2.yaxis.set_ticks_position('none') \n",
    "    ax2.plot(x_R1,delta_R1,color='red')\n",
    "    ax2.plot(x_R1,delta_R1*0.,color='black',linestyle='--')\n",
    "    \n",
    "    ax3.set_xlim([0,rmax])\n",
    "    ax3.set_ylim([-1,1])\n",
    "    ax3.set_xlabel(r'$r$')\n",
    "    ax3.yaxis.set_ticks_position('none') \n",
    "    ax3.plot(x_R1,delta_R1,color='red')\n",
    "    ax3.plot(x_R1,delta_R1*0.,color='black',linestyle='--')\n",
    "    \n",
    "    \n",
    "    plt.show()\n",
    "    \n",
    "fins=['output_7430.h5',\n",
    "'output_7440.h5',\n",
    "'output_7450.h5',\n",
    "'output_7460.h5',\n",
    "'output_7470.h5',\n",
    "'output_7480.h5',\n",
    "'output_7490.h5',\n",
    "'output_7500.h5',\n",
    "'output_7510.h5',\n",
    "'output_7520.h5',\n",
    "'output_7530.h5',\n",
    "'output_7540.h5',\n",
    "'output_7550.h5',\n",
    "'output_7570.h5',\n",
    "'output_7590.h5',\n",
    "'output_7610.h5',\n",
    "'output_7630.h5',\n",
    "'output_7650.h5',\n",
    "'output_7670.h5',\n",
    "'output_7690.h5',\n",
    "'output_7710.h5',\n",
    "'output_7730.h5',\n",
    "'output_7750.h5',\n",
    "'output_7770.h5',\n",
    "'output_7790.h5',\n",
    "'output_7810.h5',\n",
    "'output_7830.h5',\n",
    "'output_7850.h5',\n",
    "'output_7870.h5',\n",
    "'output_7890.h5',\n",
    "'output_7910.h5',\n",
    "'output_7930.h5',\n",
    "'output_7950.h5',\n",
    "'output_7970.h5',\n",
    "'output_7990.h5',\n",
    "'output_8010.h5',\n",
    "'output_8030.h5',\n",
    "'output_8050.h5',\n",
    "'output_8070.h5',\n",
    "'output_8090.h5',\n",
    "'output_8110.h5',\n",
    "'output_8130.h5',\n",
    "'output_8150.h5',\n",
    "'output_8170.h5',\n",
    "'output_8190.h5',\n",
    "'output_8210.h5',\n",
    "'output_8230.h5',\n",
    "'output_8250.h5',\n",
    "'output_8270.h5',\n",
    "'output_8290.h5',\n",
    "'output_8310.h5',\n",
    "'output_8330.h5',\n",
    "'output_8350.h5',\n",
    "'output_8370.h5',\n",
    "'output_8390.h5',\n",
    "'output_8410.h5',\n",
    "'output_8430.h5',\n",
    "'output_8450.h5',\n",
    "'output_8470.h5',\n",
    "'output_8490.h5',\n",
    "'output_8510.h5',\n",
    "'output_8530.h5',\n",
    "'output_8550.h5',\n",
    "'output_8570.h5',\n",
    "'output_8590.h5',\n",
    "'output_8610.h5',\n",
    "'output_8630.h5',\n",
    "'output_8650.h5',\n",
    "'output_8670.h5',\n",
    "'output_8690.h5',\n",
    "'output_8710.h5',\n",
    "'output_8730.h5',\n",
    "'output_8750.h5',\n",
    "'output_8770.h5',\n",
    "'output_8790.h5',\n",
    "'output_8810.h5',\n",
    "'output_8830.h5',\n",
    "'output_8850.h5',\n",
    "'output_8870.h5',\n",
    "'output_8890.h5',\n",
    "'output_8910.h5',\n",
    "'output_8930.h5',\n",
    "'output_8950.h5',\n",
    "'output_8970.h5',\n",
    "'output_8990.h5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7430.h5\n",
      "hi 7.379306853242552e-07 359.9999969390372 -89.99126552546272 89.99550581118774\n",
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7440.h5\n",
      "hi 2.9451484856277518e-06 359.999997570849 -89.9889787218639 89.99511084033364\n",
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7450.h5\n",
      "hi 3.385283781994141e-07 359.99999553714446 -89.98808898802002 89.99533411827332\n",
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7460.h5\n",
      "hi 1.8074473344467155e-06 359.9999977874951 -89.98371284950048 89.99201676085508\n",
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7470.h5\n",
      "hi 3.2910328257955735e-06 359.99999773190456 -89.99272054208683 89.99425095008178\n",
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7480.h5\n",
      "hi 1.1389157106431394e-05 359.99999830154223 -89.98472794623657 89.99811377865286\n",
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7490.h5\n",
      "hi 2.874475098358866e-06 359.999990777863 -89.99649122931581 89.97667660204161\n",
      "reading :  /data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7500.h5\n"
     ]
    }
   ],
   "source": [
    "#get Cartesian coordinates for coma:\n",
    "pcoma,rc=get_objpos_range('coma')\n",
    "\n",
    "#load particles\n",
    "rmax=300\n",
    "xx_full=[]\n",
    "xx_R1=[]\n",
    "xx_R2=[]\n",
    "xx_R3=[]\n",
    "\n",
    "\n",
    "rcoma=[]\n",
    "racoma=[]\n",
    "deccoma=[]\n",
    "velcoma=[]\n",
    "for fname in fins:\n",
    "    ncpu=find_ncpu('/data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/'+fname)\n",
    "    pos1,vel1=load_resim('/data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/'+fname,ncpu=ncpu)\n",
    "    \n",
    "    dcoma   = np.sqrt((pos1[:,0]+corner[0]-pcoma[0])**2+(pos1[:,1]+corner[1]-pcoma[1])**2+(pos1[:,2]+corner[2]-pcoma[2])**2)\n",
    "    foo=np.where(dcoma<20.)\n",
    "    \n",
    "    r,ra,dec = cart2sphere(pos1,corner=corner,rmax=2000)\n",
    "    \n",
    "    print ('hi',np.min(ra),np.max(ra),np.min(dec),np.max(dec))\n",
    "    \n",
    "    velcoma.append(vel1[foo])\n",
    "    rcoma.append(r[foo])\n",
    "    racoma.append(ra[foo])\n",
    "    deccoma.append(dec[foo])\n",
    "    \n",
    "    np.savez('/scratch/jasche/coma_particles', r = rcoma, ra = racoma, dec = deccoma, vel=velcoma)\n",
    "    \n",
    "    xx_full.append(get_densities(r,ra,dec,rmax))\n",
    "    \n",
    "    xx_R1.append(get_densities(r,ra,dec,rmax,region='R1'))\n",
    "    xx_R2.append(get_densities(r,ra,dec,rmax,region='R2'))\n",
    "    xx_R3.append(get_densities(r,ra,dec,rmax,region='R3'))\n",
    "    np.savez('/scratch/jasche/home_radial_dens', full = xx_full, R1=xx_R1, R2=xx_R2,R3=xx_R3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r,ra,dec = cart2sphere(pos1,corner=corner,rmax=300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16968 characters]\n",
      "text/plain": [
       "<Figure size 864x288 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_radial_density(r,ra,dec)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "511.99999999999983\n"
     ]
    }
   ],
   "source": [
    "print (np.shape(pos1)[0]**(1./3.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "import glob\n",
    "aux = glob.glob(\"/data/lavaux/PM_HOT_0.30_try2_epsilon_0.01/output_7610.h5_*\")\n",
    "print(len(aux))\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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
