{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This notebook was run with herschelhelp_internal version: \n",
      "1407877 (Mon Feb 4 12:56:29 2019 +0000) [with local modifications]\n",
      "This notebook was executed on: \n",
      "2021-01-14 10:13:41.736847\n"
     ]
    }
   ],
   "source": [
    "from herschelhelp_internal import git_version\n",
    "print(\"This notebook was run with herschelhelp_internal version: \\n{}\".format(git_version()))\n",
    "import datetime\n",
    "print(\"This notebook was executed on: \\n{}\".format(datetime.datetime.now()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "#%config InlineBackend.figure_format = 'svg'\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rc('figure', figsize=(10, 6))\n",
    "\n",
    "import os\n",
    "import time\n",
    "import gc\n",
    "import glob\n",
    "\n",
    "from astropy import units as u\n",
    "from astropy.coordinates import SkyCoord\n",
    "from astropy.table import Column, Table, join, vstack, hstack\n",
    "import numpy as np\n",
    "from pymoc import MOC\n",
    "\n",
    "from herschelhelp_internal.masterlist import merge_catalogues, nb_merge_dist_plot, specz_merge\n",
    "from herschelhelp_internal.utils import coords_to_hpidx, ebv, gen_help_id, inMoc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "22"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gc\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## I. Calculate %sources through HELP pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_photoz_selection_flag(catalogue, all_bands, ndet=4):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "    \n",
    "    catalogue = catalogue.copy()\n",
    "    nb_band = np.zeros(len(catalogue))\n",
    "    r_flux = np.zeros(len(catalogue))\n",
    "    \n",
    "    for idx, bands in enumerate(all_bands):\n",
    "        nb_filter = np.zeros(len(catalogue))\n",
    "        for filt in bands:\n",
    "            nb_filter += (1 * ~np.isnan(catalogue[filt]))   \n",
    "        \n",
    "            if '_r' in filt:\n",
    "                r_flux += (1 * ~np.isnan(catalogue[filt])) \n",
    "\n",
    "\n",
    "        nb_band += 1 * (nb_filter >= 1)\n",
    "\n",
    "\n",
    "    bands_det = nb_band >= ndet\n",
    "    has_r_flux = r_flux >= 1\n",
    "\n",
    "\n",
    "    catalogue.add_column(\n",
    "    Column(\n",
    "        1 * has_r_flux + 2 * bands_det,\n",
    "        name=\"flag_photoz_sel_det\")\n",
    "        )\n",
    "\n",
    "#     catalogue.write(\"{}{}_{}\".format(file_list[][49:81], DATE, file_list[90:]), overwrite=True)\n",
    "\n",
    "    return catalogue\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def percentages_help_pipeline_tiles(file_list, all_bands, field):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "\n",
    "    n=0\n",
    "    photoz_r    = 0\n",
    "    photoz_4b   = 0\n",
    "    cigale_2opt = 0 \n",
    "    cigale_2nir = 0 \n",
    "    \n",
    "    photoz = 0\n",
    "    cigale = 0 \n",
    "    both   = 0\n",
    "    \n",
    "    photoz_just_r    = 0\n",
    "    photoz_just_4b   = 0\n",
    "    cigale_just_2opt = 0 \n",
    "    cigale_just_2nir = 0 \n",
    "    \n",
    "    total_obj = 0\n",
    "\n",
    "\n",
    "    for sub_file in range(len(file_list)):\n",
    "        sub_catalogue = Table.read(file_list[sub_file])\n",
    "\n",
    "        if 'flag_photoz_sel_det' not in sub_catalogue.colnames:\n",
    "            sub_catalogue = add_photoz_selection_flag(sub_catalogue, all_bands)\n",
    "\n",
    "        sources_photoz_4b   = ((sub_catalogue['flag_photoz_sel_det'] == 2) | (sub_catalogue['flag_photoz_sel_det'] == 3))\n",
    "        sources_photoz_r    = ((sub_catalogue['flag_photoz_sel_det'] == 1) | (sub_catalogue['flag_photoz_sel_det'] == 3))\n",
    "        sources_cigale_2opt = ((sub_catalogue['flag_optnir_det'] == 1) | (sub_catalogue['flag_optnir_det'] == 3) | \n",
    "                               (sub_catalogue['flag_optnir_det'] == 5)| (sub_catalogue['flag_optnir_det'] == 7))\n",
    "        sources_cigale_2nir = ((sub_catalogue['flag_optnir_det'] == 2) | (sub_catalogue['flag_optnir_det'] == 3) | \n",
    "                               (sub_catalogue['flag_optnir_det'] == 6)| (sub_catalogue['flag_optnir_det'] == 7))\n",
    "        \n",
    "        sources_photoz = ((sub_catalogue['flag_photoz_sel_det'] == 3) | (~np.isnan(sub_catalogue['zspec'])))\n",
    "        sources_cigale = ((sub_catalogue['flag_optnir_det'] == 3) | (sub_catalogue['flag_optnir_det'] == 7))\n",
    "        sources_both   = ((sub_catalogue['flag_photoz_sel_det'] == 3) & ((sub_catalogue['flag_optnir_det'] == 3) | (sub_catalogue['flag_optnir_det'] == 7)))\n",
    "        \n",
    "        sources_photoz_just_4b   = (sub_catalogue['flag_photoz_sel_det'] == 2)\n",
    "        sources_photoz_just_r    = (sub_catalogue['flag_photoz_sel_det'] == 1)\n",
    "        sources_cigale_just_2opt = ((sub_catalogue['flag_optnir_det'] == 1) | (sub_catalogue['flag_optnir_det'] == 5)) \n",
    "        sources_cigale_just_2nir = ((sub_catalogue['flag_optnir_det'] == 2) | (sub_catalogue['flag_optnir_det'] == 6)) \n",
    "\n",
    "        photoz_r    += sources_photoz_r.sum() \n",
    "        photoz_4b   += sources_photoz_4b.sum() \n",
    "        cigale_2opt += sources_cigale_2opt.sum()\n",
    "        cigale_2nir += sources_cigale_2nir.sum()\n",
    "        \n",
    "        photoz += sources_photoz.sum() \n",
    "        cigale += sources_cigale.sum()\n",
    "        both   += sources_both.sum() \n",
    "        \n",
    "        photoz_just_r    += sources_photoz_just_r.sum() \n",
    "        photoz_just_4b   += sources_photoz_just_4b.sum() \n",
    "        cigale_just_2opt += sources_cigale_just_2opt.sum()\n",
    "        cigale_just_2nir += sources_cigale_just_2nir.sum()\n",
    "        \n",
    "        total_obj += len(sub_catalogue)\n",
    "\n",
    "        n += 1\n",
    "\n",
    "    percentage_photoz_r    = photoz_r / total_obj\n",
    "    percentage_photoz_4b   = photoz_4b / total_obj\n",
    "    percentage_cigale_2opt = cigale_2opt / total_obj \n",
    "    percentage_cigale_2nir = cigale_2nir / total_obj \n",
    "    \n",
    "    percentage_photoz = photoz / total_obj\n",
    "    percentage_cigale = cigale / total_obj   \n",
    "    percentage_both   = both / total_obj\n",
    "    \n",
    "    percentage_photoz_just_r    = photoz_just_r / total_obj\n",
    "    percentage_photoz_just_4b   = photoz_just_4b / total_obj\n",
    "    percentage_cigale_just_2opt = cigale_just_2opt / total_obj\n",
    "    percentage_cigale_just_2nir = cigale_just_2nir / total_obj\n",
    "\n",
    "    print(\"field: \", field)\n",
    "    print(\"total objects in masterlist: \", total_obj)\n",
    "    print(\"percentage sources photoz_4band sel: {:.3%}\".format(percentage_photoz_r))\n",
    "    print(\"percentage sources photoz_r sel: {:.3%}\".format(percentage_photoz_4b))\n",
    "    print(\"percentage sources cigale_2opt sel: {:.3%}\".format(percentage_cigale_2opt))\n",
    "    print(\"percentage sources cigale_2nir sel: {:.3%}\".format(percentage_cigale_2nir))\n",
    "    print(\"||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\")\n",
    "    print(\"percentage sources photoz sel: {:.3%}\".format(percentage_photoz))\n",
    "    print(\"percentage sources cigale sel: {:.3%}\".format(percentage_cigale))\n",
    "    print(\"percentage sources both sel: {:.3%}\".format(percentage_both))\n",
    "    print(\"||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\")\n",
    "    print(\"percentage sources with just photoz_4bands (not r detection): {:.3%}\".format(percentage_photoz_just_r))\n",
    "    print(\"percentage sources with just photoz_r (not 4bands detection): {:.3%}\".format(percentage_photoz_just_4b))\n",
    "    print(\"percentage sources with just cigale_2opt (not 2nir detection): {:.3%}\".format(percentage_cigale_just_2opt))\n",
    "    print(\"percentage sources with just cigale_2nir (not 2opt detection): {:.3%}\".format(percentage_cigale_just_2nir))\n",
    "    \n",
    "    \n",
    "    return total_obj, photoz, cigale, both"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "SUFFIX = '20180307'\n",
    "file_list = glob.glob('../../dmu1/dmu1_ml_Herschel-Stripe-82/data/tiles/sub_catalogue_herschel-stripe-82_{}_*.fits'.format(SUFFIX))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['f_suprime_g',\n",
       " 'f_suprime_r',\n",
       " 'f_suprime_i',\n",
       " 'f_suprime_z',\n",
       " 'f_suprime_y',\n",
       " 'f_suprime_n921',\n",
       " 'f_suprime_n816',\n",
       " 'f_vista_y',\n",
       " 'f_vista_h',\n",
       " 'f_wircam_j',\n",
       " 'f_vista_j',\n",
       " 'f_wircam_ks',\n",
       " 'f_vista_ks',\n",
       " 'f_ukidss_y',\n",
       " 'f_ukidss_j',\n",
       " 'f_ukidss_h',\n",
       " 'f_ukidss_k',\n",
       " 'f_gpc1_g',\n",
       " 'f_gpc1_r',\n",
       " 'f_gpc1_i',\n",
       " 'f_gpc1_z',\n",
       " 'f_gpc1_y',\n",
       " 'f_irac_i1',\n",
       " 'f_irac_i2',\n",
       " 'f_decam_i',\n",
       " 'f_decam_y',\n",
       " 'f_decam_g',\n",
       " 'f_decam_r',\n",
       " 'f_decam_z',\n",
       " 'f_megacam_g',\n",
       " 'f_megacam_r',\n",
       " 'f_megacam_i',\n",
       " 'f_megacam_z',\n",
       " 'f_megacam_y',\n",
       " 'f_sdss_u',\n",
       " 'f_sdss_g',\n",
       " 'f_sdss_r',\n",
       " 'f_sdss_i',\n",
       " 'f_sdss_z']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat = Table.read(file_list[0])\n",
    "bands = []\n",
    "for col in cat.colnames:\n",
    "    if col.startswith('f_') and not col.startswith('f_ap'):\n",
    "        bands.append(col)\n",
    "bands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_bands = [\n",
    " ['f_sdss_u'],\n",
    "    ['f_suprime_g',\n",
    " #'f_gpc1_g',\n",
    " 'f_decam_g',\n",
    " #'f_megacam_g',\n",
    " 'f_sdss_g'],\n",
    " ['f_suprime_r',\n",
    " #'f_gpc1_r',\n",
    " 'f_decam_r',\n",
    " #'f_megacam_r',\n",
    " 'f_sdss_r'],\n",
    " ['f_suprime_i',\n",
    " #'f_gpc1_i',\n",
    " 'f_decam_i',\n",
    " #'f_megacam_i',\n",
    " 'f_sdss_i'],\n",
    " ['f_suprime_z',\n",
    " #'f_gpc1_z',\n",
    " 'f_decam_z',\n",
    " #'f_megacam_z',\n",
    " 'f_sdss_z'],\n",
    "# 'f_suprime_n921',\n",
    "# 'f_suprime_n816',\n",
    " ['f_suprime_y',\n",
    " 'f_vista_y',\n",
    " 'f_ukidss_y',\n",
    " #'f_gpc1_y',\n",
    " 'f_decam_y'],\n",
    " #'f_megacam_y'],\n",
    " ['f_vista_h',\n",
    " 'f_ukidss_h'],\n",
    " #['f_wircam_j',\n",
    " ['f_ukidss_j',\n",
    " 'f_vista_j'],\n",
    " #['f_wircam_ks',\n",
    " ['f_vista_ks',\n",
    " 'f_ukidss_k'],\n",
    " ['f_irac_i1'],\n",
    " ['f_irac_i2']\n",
    "]\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "field:  Herschel-Stripe-82\n",
      "total objects in masterlist:  50196455\n",
      "percentage sources photoz_4band sel: 54.929%\n",
      "percentage sources photoz_r sel: 43.475%\n",
      "percentage sources cigale_2opt sel: 58.565%\n",
      "percentage sources cigale_2nir sel: 9.569%\n",
      "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n",
      "percentage sources photoz sel: 42.512%\n",
      "percentage sources cigale sel: 8.121%\n",
      "percentage sources both sel: 7.099%\n",
      "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n",
      "percentage sources with just photoz_4bands (not r detection): 12.450%\n",
      "percentage sources with just photoz_r (not 4bands detection): 0.996%\n",
      "percentage sources with just cigale_2opt (not 2nir detection): 50.444%\n",
      "percentage sources with just cigale_2nir (not 2opt detection): 1.448%\n"
     ]
    }
   ],
   "source": [
    "total_obj, photoz_hs82, cigale_hs82, both_hs82 = percentages_help_pipeline_tiles(file_list, all_bands, 'Herschel-Stripe-82')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Check sources in cigale catalogue\n",
    "cigale_cat = Table.read('../../dmu28/dmu28_Herschel-Stripe-82/data/herschel_stripe_82_Ldust_prediction.fits', memmap=True)\n",
    "\n",
    "# Check sources in photo-z catalogue\n",
    "photoz_cat = Table.read('../../dmu24/dmu24_Herschel-Stripe-82/data/master_catalogue_herschel-stripe-82_20180307_photoz_20180509.fits', memmap=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.891689861365707"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(cigale_cat) / total_obj * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "43.05846100088144"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(photoz_cat) / total_obj * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "250982.275"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_obj * 0.005"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def percentages_help_pipeline(masterlist, all_bands, field):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "    # ref: 2.4_Merging.ipynb\n",
    "    split_length = 100000 #number of sources to include in every sub catalogue\n",
    "    num_files = int(np.ceil(len(masterlist)/split_length))\n",
    "    print('num files: ', num_files)\n",
    "\n",
    "    n=0\n",
    "    photoz_r    = 0\n",
    "    photoz_4b   = 0\n",
    "    cigale_2opt = 0 \n",
    "    cigale_2nir = 0 \n",
    "    \n",
    "    photoz = 0\n",
    "    cigale = 0 \n",
    "    both   = 0\n",
    "    \n",
    "    photoz_just_r    = 0\n",
    "    photoz_just_4b   = 0\n",
    "    cigale_just_2opt = 0 \n",
    "    cigale_just_2nir = 0 \n",
    "    \n",
    "    total_obj = 0\n",
    "\n",
    "    for sub_file in range(num_files):\n",
    "        # ref: 2.4_Merging.ipynb\n",
    "        sub_catalogue = masterlist[n*split_length:(n+1)*split_length].copy()\n",
    "        #print(n)\n",
    "\n",
    "        if 'flag_photoz_sel_det' not in sub_catalogue.colnames:\n",
    "            sub_catalogue = add_photoz_selection_flag(sub_catalogue, all_bands)\n",
    "\n",
    "        sources_photoz_4b   = ((sub_catalogue['flag_photoz_sel_det'] == 2) | (sub_catalogue['flag_photoz_sel_det'] == 3))\n",
    "        sources_photoz_r    = ((sub_catalogue['flag_photoz_sel_det'] == 1) | (sub_catalogue['flag_photoz_sel_det'] == 3))\n",
    "        sources_cigale_2opt = ((sub_catalogue['flag_optnir_det'] == 1) | (sub_catalogue['flag_optnir_det'] == 3) | \n",
    "                               (sub_catalogue['flag_optnir_det'] == 5)| (sub_catalogue['flag_optnir_det'] == 7))\n",
    "        sources_cigale_2nir = ((sub_catalogue['flag_optnir_det'] == 2) | (sub_catalogue['flag_optnir_det'] == 3) | \n",
    "                               (sub_catalogue['flag_optnir_det'] == 6)| (sub_catalogue['flag_optnir_det'] == 7))\n",
    "        \n",
    "        sources_photoz = ((sub_catalogue['flag_photoz_sel_det'] == 3) | (~np.isnan(sub_catalogue['zspec'])))\n",
    "        sources_cigale = ((sub_catalogue['flag_optnir_det'] == 3) | (sub_catalogue['flag_optnir_det'] == 7))\n",
    "        sources_both   = ((sub_catalogue['flag_photoz_sel_det'] == 3) & ((sub_catalogue['flag_optnir_det'] == 3) | (sub_catalogue['flag_optnir_det'] == 7)))\n",
    "        \n",
    "        sources_photoz_just_4b   = (sub_catalogue['flag_photoz_sel_det'] == 2)\n",
    "        sources_photoz_just_r    = (sub_catalogue['flag_photoz_sel_det'] == 1)\n",
    "        sources_cigale_just_2opt = ((sub_catalogue['flag_optnir_det'] == 1) | (sub_catalogue['flag_optnir_det'] == 5)) \n",
    "        sources_cigale_just_2nir = ((sub_catalogue['flag_optnir_det'] == 2) | (sub_catalogue['flag_optnir_det'] == 6)) \n",
    "\n",
    "        photoz_r    += sources_photoz_r.sum() \n",
    "        photoz_4b   += sources_photoz_4b.sum() \n",
    "        cigale_2opt += sources_cigale_2opt.sum()\n",
    "        cigale_2nir += sources_cigale_2nir.sum()\n",
    "        \n",
    "        photoz += sources_photoz.sum() \n",
    "        cigale += sources_cigale.sum()\n",
    "        both   += sources_both.sum() \n",
    "        \n",
    "        photoz_just_r    += sources_photoz_just_r.sum() \n",
    "        photoz_just_4b   += sources_photoz_just_4b.sum() \n",
    "        cigale_just_2opt += sources_cigale_just_2opt.sum()\n",
    "        cigale_just_2nir += sources_cigale_just_2nir.sum()\n",
    "\n",
    "        n += 1\n",
    "    \n",
    "        total_obj += len(sub_catalogue)\n",
    "\n",
    "    percentage_photoz_r    = photoz_r / total_obj\n",
    "    percentage_photoz_4b   = photoz_4b / total_obj\n",
    "    percentage_cigale_2opt = cigale_2opt / total_obj \n",
    "    percentage_cigale_2nir = cigale_2nir / total_obj \n",
    "    \n",
    "    percentage_photoz = photoz / total_obj\n",
    "    percentage_cigale = cigale / total_obj   \n",
    "    percentage_both   = both / total_obj\n",
    "    \n",
    "    percentage_photoz_just_r    = photoz_just_r / total_obj\n",
    "    percentage_photoz_just_4b   = photoz_just_4b / total_obj\n",
    "    percentage_cigale_just_2opt = cigale_just_2opt / total_obj\n",
    "    percentage_cigale_just_2nir = cigale_just_2nir / total_obj\n",
    "\n",
    "#     percentage_photoz_r    = photoz_r / len(masterlist)\n",
    "#     percentage_photoz_4b   = photoz_4b / len(masterlist)\n",
    "#     percentage_cigale_2opt = cigale_2opt / len(masterlist) \n",
    "#     percentage_cigale_2nir = cigale_2nir / len(masterlist) \n",
    "    \n",
    "#     percentage_photoz = photoz / len(masterlist)\n",
    "#     percentage_cigale = cigale / len(masterlist)    \n",
    "#     percentage_both   = both / len(masterlist)\n",
    "    \n",
    "#     percentage_photoz_just_r    = photoz_just_r / len(masterlist)\n",
    "#     percentage_photoz_just_4b   = photoz_just_4b / len(masterlist)\n",
    "#     percentage_cigale_just_2opt = cigale_just_2opt / len(masterlist) \n",
    "#     percentage_cigale_just_2nir = cigale_just_2nir / len(masterlist) \n",
    "\n",
    "    print(\"field: \", field)\n",
    "    print(\"total objects in masterlist: \", len(masterlist))\n",
    "    print(\"percentage sources photoz_4band sel: {:.3%}\".format(percentage_photoz_r))\n",
    "    print(\"percentage sources photoz_r sel: {:.3%}\".format(percentage_photoz_4b))\n",
    "    print(\"percentage sources cigale_2opt sel: {:.3%}\".format(percentage_cigale_2opt))\n",
    "    print(\"percentage sources cigale_2nir sel: {:.3%}\".format(percentage_cigale_2nir))\n",
    "    print(\"||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\")\n",
    "    print(\"percentage sources photoz sel: {:.3%}\".format(percentage_photoz))\n",
    "    print(\"percentage sources cigale sel: {:.3%}\".format(percentage_cigale))\n",
    "    print(\"percentage sources both sel: {:.3%}\".format(percentage_both))\n",
    "    print(\"||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\")\n",
    "    print(\"percentage sources with just photoz_4bands (not r detection): {:.3%}\".format(percentage_photoz_just_r))\n",
    "    print(\"percentage sources with just photoz_r (not 4bands detection): {:.3%}\".format(percentage_photoz_just_4b))\n",
    "    print(\"percentage sources with just cigale_2opt (not 2nir detection): {:.3%}\".format(percentage_cigale_just_2opt))\n",
    "    print(\"percentage sources with just cigale_2nir (not 2opt detection): {:.3%}\".format(percentage_cigale_just_2nir))\n",
    "    \n",
    "    return len(masterlist), photoz, cigale, both"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bands:  ['f_wfc_u', 'f_wfc_g', 'f_wfc_r', 'f_wfc_i', 'f_wfc_z', 'f_megacam_u', 'f_megacam_g', 'f_megacam_r', 'f_megacam_z', 'f_suprime_g', 'f_suprime_r', 'f_suprime_i', 'f_suprime_z', 'f_suprime_y', 'f_suprime_n921', 'f_gpc1_g', 'f_gpc1_r', 'f_gpc1_i', 'f_gpc1_z', 'f_gpc1_y', 'f_ukidss_j', 'f_ukidss_k', 'f_irac_i3', 'f_irac_i4', 'f_irac_i1', 'f_irac_i2']\n"
     ]
    }
   ],
   "source": [
    "# Read original masterlist file\n",
    "\n",
    "masterfile = '../../dmu1/dmu1_ml_ELAIS-N1/data/master_catalogue_elais-n1_20171016.fits'\n",
    "masterlist = Table.read(masterfile, memmap=True)\n",
    "\n",
    "bands = []\n",
    "for col in masterlist.colnames:\n",
    "    if col.startswith('f_') and not col.startswith('f_ap'):\n",
    "        bands.append(col)\n",
    "        \n",
    "print('bands: ' , bands)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_bands = [['f_wfc_u',\n",
    " 'f_megacam_u'],\n",
    " ['f_wfc_g',\n",
    " 'f_megacam_g',\n",
    " 'f_suprime_g',\n",
    " 'f_gpc1_g'],\n",
    " ['f_wfc_r',\n",
    " 'f_megacam_r',\n",
    " 'f_suprime_r',\n",
    " 'f_gpc1_r'], \n",
    "#['f_suprime_n921'],\n",
    " ['f_wfc_i',\n",
    " 'f_suprime_i'],\n",
    " #'f_gpc1_i'],\n",
    " ['f_wfc_z',\n",
    " 'f_megacam_z',\n",
    " 'f_suprime_z'],\n",
    " #'f_gpc1_z'],\n",
    " ['f_suprime_y',\n",
    " 'f_gpc1_y'],\n",
    " ['f_ukidss_j'],\n",
    " ['f_ukidss_k'],\n",
    " ['f_irac_i3'],\n",
    " ['f_irac_i4'],\n",
    " ['f_irac_i1'],\n",
    " ['f_irac_i2']]\n",
    "\n",
    "## ELAIS-N1\n",
    "## CIGALE bands used\n",
    "#g_bands = ['suprime_g', 'megacam_g', 'wfc_g', 'gpc1_g']\n",
    "#u_bands = ['megacam_u', 'wfc_u']\n",
    "#r_bands = ['suprime_r', 'megacam_r', 'wfc_r', 'gpc1_r']\n",
    "#i_bands = ['suprime_i', 'wfc_i']\n",
    "#z_bands = ['suprime_z', 'megacam_z', 'wfc_z']\n",
    "#y_bands = ['suprime_y', 'gpc1_y']\n",
    "\n",
    "\n",
    "## photo-z bands used\n",
    "# megacam, ukidss, irac\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num files:  41\n",
      "field:  ELAIS-N1\n",
      "total objects in masterlist:  4026292\n",
      "percentage sources photoz_4band sel: 79.562%\n",
      "percentage sources photoz_r sel: 71.777%\n",
      "percentage sources cigale_2opt sel: 71.241%\n",
      "percentage sources cigale_2nir sel: 12.631%\n",
      "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n",
      "percentage sources photoz sel: 70.995%\n",
      "percentage sources cigale sel: 11.504%\n",
      "percentage sources both sel: 11.421%\n",
      "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n",
      "percentage sources with just photoz_4bands (not r detection): 8.572%\n",
      "percentage sources with just photoz_r (not 4bands detection): 0.787%\n",
      "percentage sources with just cigale_2opt (not 2nir detection): 59.737%\n",
      "percentage sources with just cigale_2nir (not 2opt detection): 1.127%\n"
     ]
    }
   ],
   "source": [
    "total_obj_en1, photoz_en1, cigale_en1, both_en1 = percentages_help_pipeline(masterlist, all_bands, 'ELAIS-N1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num files:  41\n",
      "field:  ELAIS-N1\n",
      "total objects in masterlist:  4026292\n",
      "percentage sources photoz_4band sel: 75.007%\n",
      "percentage sources photoz_r sel: 71.874%\n",
      "percentage sources cigale_2opt sel: 71.241%\n",
      "percentage sources cigale_2nir sel: 12.631%\n",
      "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n",
      "percentage sources photoz sel: 70.741%\n",
      "percentage sources cigale sel: 11.504%\n",
      "percentage sources both sel: 11.313%\n",
      "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n",
      "percentage sources with just photoz_4bands (not r detection): 4.286%\n",
      "percentage sources with just photoz_r (not 4bands detection): 1.154%\n",
      "percentage sources with just cigale_2opt (not 2nir detection): 59.737%\n",
      "percentage sources with just cigale_2nir (not 2opt detection): 1.127%\n"
     ]
    }
   ],
   "source": [
    "total_obj_en1, photoz_en1, cigale_en1, both_en1 = percentages_help_pipeline(masterlist, all_bands, 'ELAIS-N1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11.358440967520487"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check sources in cigale catalogues\n",
    "cigale_cat_en1 = Table.read('../../dmu28/dmu28_ELAIS-N1/data/ELAIS_N1_Ldust_prediction_results.fits', memmap=True)\n",
    "\n",
    "len(cigale_cat_en1) / len(masterlist) * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71.65689920154821"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check sources in cigale catalogues\n",
    "photoz_cat_en1 = Table.read('../../dmu24/dmu24_ELAIS-N1/data/master_catalogue_elais-n1_20170706_photoz_20170725_irac1_optimised.fits', memmap=True)\n",
    "\n",
    "len(photoz_cat_en1) / len(masterlist) * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_en1_photoz_flag = add_photoz_selection_flag(masterlist, all_bands)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "photo_z_flag = ((cat_en1_photoz_flag['flag_photoz_sel_det'] == 3) | (~np.isnan(cat_en1_photoz_flag['zspec'])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "70.74057221880578"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "photo_z_flag.sum() / len(masterlist) * 100"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (herschelhelp_python)",
   "language": "python",
   "name": "helpext"
  },
  "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
