{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Merge MF blind catalgoues from the 3 SPIRE bands\n",
    "![HELP LOGO](https://avatars1.githubusercontent.com/u/7880370?s=100&v=4>)\n",
    "\n",
    "\n",
    "1. Reads the 250um, 350um and 500um Blind Matched Filter catalgoue \n",
    "2. Merges the table with 12'' and 18'' matching radius\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Combining the MF catalgoues\n",
    "### Creates the file \"name_field\"_all.fits, for all fields\n",
    "### The RA and Dec from \"name_field\"_all.fits is used as input for XID+"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from astropy.io import fits\n",
    "from astropy.table import Table, vstack\n",
    "import os\n",
    "%matplotlib inline  \n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from astropy.wcs import WCS\n",
    "from matplotlib.patches import Ellipse\n",
    "from astropy.coordinates import SkyCoord\n",
    "from astropy import units as u\n",
    "from astropy.table import Column\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The SPIRE fields in HELP:\n",
    "all_names =  ['GAMA-09_SPIRE','GAMA-12_SPIRE','GAMA-15_SPIRE','HATLAS-NGP_SPIRE','HATLAS-SGP_SPIRE','SSDF_SPIRE',\\\n",
    "              'AKARI-SEP_SPIRE','Bootes_SPIRE','CDFS-SWIRE_SPIRE','COSMOS_SPIRE','EGS_SPIRE',\\\n",
    "              'ELAIS-N1_SPIRE','ELAIS-N2_SPIRE','ELAIS-S1_SPIRE','HDF-N_SPIRE','Lockman-SWIRE_SPIRE','SA13_SPIRE',\\\n",
    "              'SPIRE-NEP_SPIRE','xFLS_SPIRE','XMM-13hr_SPIRE','XMM-LSS_SPIRE','AKARI-SEP_SPIRE']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_names=['Herschel-Stripe-82_SPIRE']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "band = ['250','350','500']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dmu22_Herschel-Stripe-82/data/\n"
     ]
    }
   ],
   "source": [
    "j=0\n",
    "loc = 'dmu22_'+all_names[j][0:-6]+'/data/'\n",
    "print(loc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# loops over the matched filterd catalogues and creates a merged catalgoue for every field.\n",
    "# Duplicated are removed in the 350um catalogue when the object is within a 12 arcsec radius of a 250um detection\n",
    "# Duplicated are removed in the 500um catalogue when the object is within a 18 arcsec radius of a 250um or\n",
    "# 350um detection\n",
    "\n",
    "\n",
    "for j in range(np.size(all_names)):\n",
    "    loc = 'dmu22_'+all_names[j][0:-6]+'/data/'\n",
    "\n",
    "\n",
    "    name = all_names[j]+'250_cat_MF.fits'\n",
    "    cat1=Table.read(loc+name)\n",
    "\n",
    "    RA,DEC = cat1['RA']*u.degree,cat1['Dec']*u.degree\n",
    "    cat1.add_column(Column(np.zeros(np.size(RA))),name='flag')\n",
    "\n",
    "    cat1['F_BLIND_pix_SPIRE_250'].name = 'F_BLIND_pix_SPIRE'\n",
    "    cat1['FErr_BLIND_pix_SPIRE_250'].name = 'FErr_BLIND_pix_SPIRE'\n",
    "\n",
    "    name = all_names[j]+'350_cat_MF.fits'\n",
    "    cat2=Table.read(loc+name)\n",
    "\n",
    "    RA_m,DEC_m = cat2['RA']*u.degree,cat2['Dec']*u.degree\n",
    "    cat2.add_column(Column(np.zeros(np.size(RA_m))+1),name='flag')\n",
    "\n",
    "    cat2['F_BLIND_pix_SPIRE_350'].name = 'F_BLIND_pix_SPIRE'\n",
    "    cat2['FErr_BLIND_pix_SPIRE_350'].name = 'FErr_BLIND_pix_SPIRE'\n",
    "\n",
    "\n",
    "    catalog = SkyCoord(ra=RA, dec=DEC)  \n",
    "    c = SkyCoord(ra=RA_m, dec=DEC_m)\n",
    "\n",
    "    idx, d2d, d3d = c.match_to_catalog_sky(catalog) \n",
    "    use = np.array(d2d)*3600 > 12.\n",
    "    cat2 = cat2[use]\n",
    "\n",
    "    cat_all = vstack([cat1, cat2])\n",
    "\n",
    "    name = all_names[j]+'500_cat_MF.fits'\n",
    "    cat3=Table.read(loc+name)\n",
    "\n",
    "    RA_m,DEC_m = cat3['RA']*u.degree,cat3['Dec']*u.degree\n",
    "    cat3.add_column(Column(np.zeros(np.size(RA_m))+2),name='flag')\n",
    "\n",
    "    cat3['F_BLIND_pix_SPIRE_500'].name = 'F_BLIND_pix_SPIRE'\n",
    "    cat3['FErr_BLIND_pix_SPIRE_500'].name = 'FErr_BLIND_pix_SPIRE'\n",
    "\n",
    "    catalog = SkyCoord(ra=cat_all['RA']*u.degree, dec=cat_all['Dec']*u.degree)  \n",
    "    c = SkyCoord(ra=RA_m, dec=DEC_m)\n",
    "\n",
    "    idx, d2d, d3d = c.match_to_catalog_sky(catalog) \n",
    "    use = np.array(d2d)*3600 > 18.\n",
    "    cat3 = cat3[use]\n",
    "\n",
    "    cat_all = vstack([cat_all, cat3])\n",
    "\n",
    "    cat_all.write(loc+all_names[j]+'_all.fits', format='fits',overwrite=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*This is a default HELP jupyter notebook *\n",
    "\n",
    " ![HELP LOGO](https://avatars1.githubusercontent.com/u/7880370?s=75&v=4)\n",
    "\n",
    "**Authors**: S. Duivenvoorden, P.D. Hurley\n",
    "\n",
    " \n",
    "For a full description of the database and how it is organised in to `dmu_products` please the top level [readme](../readme.md).\n",
    " \n",
    "The Herschel Extragalactic Legacy Project, ([HELP](http://herschel.sussex.ac.uk/)), is a [European Commission Research Executive Agency](https://ec.europa.eu/info/departments/research-executive-agency_en)\n",
    "funded project under the SP1-Cooperation, Collaborative project, Small or medium-scale focused research project, FP7-SPACE-2013-1 scheme, Grant Agreement\n",
    "Number 607254.\n",
    "\n",
    "[Acknowledgements](http://herschel.sussex.ac.uk/acknowledgements)"
   ]
  },
  {
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
