{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Puts ALL WISE Astrometry reference catalogues into GAIA reference frame\n",
    "\n",
    "<img src=https://avatars1.githubusercontent.com/u/7880370?s=200&v=4>\n",
    "\n",
    "The WISE catalogues were produced by ../dmu16_allwise/make_wise_samples_for_stacking.csh\n",
    "\n",
    "In the catalogue, we keep:\n",
    "\n",
    "- The position;\n",
    "- The chi^2\n",
    "\n",
    "This astrometric correction is adapted from master list code  (dmu1_ml_XMM-LSS/1.8_SERVS.ipynb) written by Yannick Rohlly and Raphael Shirley\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "field=\"AKARI-SEP\"\n",
    "field=\"AKARI-NEP\"\n",
    "field='Herschel-Stripe-82' \n",
    "\n",
    "field='HDF-N' \n",
    "field='XMM-LSS' \n",
    "field='AKARI-NEP' \n",
    "field='AKARI-SEP' \n",
    "field='Bootes' \n",
    "field='CDFS-SWIRE' \n",
    "field='COSMOS' \n",
    "field='EGS' \n",
    "field='ELAIS-N1' \n",
    "field='ELAIS-N2' \n",
    "field='ELAIS-S1' \n",
    "field='GAMA-09' \n",
    "field='GAMA-12' \n",
    "field='GAMA-15' \n",
    "field='NGP' \n",
    "field='SGP' \n",
    "field='Lockman-SWIRE' \n",
    "field='SA13' \n",
    "field='SPIRE-NEP' \n",
    "field='SSDF' \n",
    "field='xFLS' \n",
    "field='XMM-13hr' \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This notebook was run with herschelhelp_internal version: \n",
      "0aab440 (Thu Mar 22 09:41:13 2018 +0000)\n"
     ]
    }
   ],
   "source": [
    "from herschelhelp_internal import git_version\n",
    "print(\"This notebook was run with herschelhelp_internal version: \\n{}\".format(git_version()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sjo/anaconda3/envs/herschelhelp_internal/lib/python3.6/site-packages/seaborn/apionly.py:6: UserWarning: As seaborn no longer sets a default style on import, the seaborn.apionly module is deprecated. It will be removed in a future version.\n",
      "  warnings.warn(msg, UserWarning)\n"
     ]
    }
   ],
   "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",
    "from collections import OrderedDict\n",
    "import os\n",
    "\n",
    "from astropy import units as u\n",
    "from astropy.coordinates import SkyCoord\n",
    "from astropy.table import Column, Table\n",
    "import numpy as np\n",
    "\n",
    "from herschelhelp_internal.flagging import  gaia_flag_column\n",
    "from herschelhelp_internal.masterlist import nb_astcor_diag_plot, remove_duplicates\n",
    "from herschelhelp_internal.utils import astrometric_correction, flux_to_mag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "OUT_DIR =  os.environ.get('TMP_DIR', \"../dmu16_allwise/data/\")\n",
    "try:\n",
    "    os.makedirs(OUT_DIR)\n",
    "except FileExistsError:\n",
    "    pass\n",
    "\n",
    "RA_COL = \"servs_ra\"\n",
    "DEC_COL = \"servs_dec\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "## I - Reading in WISE astrometric catalogue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<i>Table length=10</i>\n",
       "<table id=\"table4492755184-816156\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>idx</th><th>ra</th><th>dec</th><th>w4mag</th></tr></thead>\n",
       "<thead><tr><th></th><th>deg</th><th>deg</th><th>mag</th></tr></thead>\n",
       "<tr><td>0</td><td>203.4147077</td><td>37.4250645</td><td>9.378</td></tr>\n",
       "<tr><td>1</td><td>203.2962176</td><td>37.4727302</td><td>9.044</td></tr>\n",
       "<tr><td>2</td><td>203.296906</td><td>37.4777635</td><td>9.444</td></tr>\n",
       "<tr><td>3</td><td>203.3143497</td><td>37.4686183</td><td>9.464</td></tr>\n",
       "<tr><td>4</td><td>203.3123273</td><td>37.4762516</td><td>9.059</td></tr>\n",
       "<tr><td>5</td><td>203.3177577</td><td>37.4771595</td><td>9.533</td></tr>\n",
       "<tr><td>6</td><td>203.3318084</td><td>37.4661865</td><td>9.343</td></tr>\n",
       "<tr><td>7</td><td>203.3342913</td><td>37.4731416</td><td>8.726</td></tr>\n",
       "<tr><td>8</td><td>203.3454295</td><td>37.4668158</td><td>9.445</td></tr>\n",
       "<tr><td>9</td><td>203.2970083</td><td>37.4879948</td><td>9.164</td></tr>\n",
       "</table><style>table.dataTable {clear: both; width: auto !important; margin: 0 !important;}\n",
       ".dataTables_info, .dataTables_length, .dataTables_filter, .dataTables_paginate{\n",
       "display: inline-block; margin-right: 1em; }\n",
       ".paginate_button { margin-right: 5px; }\n",
       "</style>\n",
       "<script>\n",
       "\n",
       "var astropy_sort_num = function(a, b) {\n",
       "    var a_num = parseFloat(a);\n",
       "    var b_num = parseFloat(b);\n",
       "\n",
       "    if (isNaN(a_num) && isNaN(b_num))\n",
       "        return ((a < b) ? -1 : ((a > b) ? 1 : 0));\n",
       "    else if (!isNaN(a_num) && !isNaN(b_num))\n",
       "        return ((a_num < b_num) ? -1 : ((a_num > b_num) ? 1 : 0));\n",
       "    else\n",
       "        return isNaN(a_num) ? -1 : 1;\n",
       "}\n",
       "\n",
       "require.config({paths: {\n",
       "    datatables: 'https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min'\n",
       "}});\n",
       "require([\"datatables\"], function(){\n",
       "    console.log(\"$('#table4492755184-816156').dataTable()\");\n",
       "    \n",
       "jQuery.extend( jQuery.fn.dataTableExt.oSort, {\n",
       "    \"optionalnum-asc\": astropy_sort_num,\n",
       "    \"optionalnum-desc\": function (a,b) { return -astropy_sort_num(a, b); }\n",
       "});\n",
       "\n",
       "    $('#table4492755184-816156').dataTable({\n",
       "        order: [],\n",
       "        pageLength: 50,\n",
       "        lengthMenu: [[10, 25, 50, 100, 500, 1000, -1], [10, 25, 50, 100, 500, 1000, 'All']],\n",
       "        pagingType: \"full_numbers\",\n",
       "        columnDefs: [{targets: [0, 1, 2, 3], type: \"optionalnum\"}]\n",
       "    });\n",
       "});\n",
       "</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wise = Table.read(f\"../dmu16_allwise/data/Allwise_PSF_stack_{field}.fits\")\n",
    "wise_coords=SkyCoord(wise['ra'], wise['dec'])\n",
    "\n",
    "epoch = 2009\n",
    "\n",
    "wise[:10].show_in_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## III - Astrometry correction\n",
    "\n",
    "We match the astrometry to the Gaia one. We limit the Gaia catalogue to sources with a g band flux between the 30th and the 70th percentile. Some quick tests show that this give the lower dispersion in the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../../dmu0/dmu0_GAIA/data/GAIA_XMM-13hr.fits\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '../dmu0/dmu0_GAIA/data/GAIA_XMM-13hr.fits'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-7-595029524a0b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#gaia = Table.read(\"./dmu17_XMM-LSS/data/GAIA_XMM-LSS.fits\")\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"../../dmu0/dmu0_GAIA/data/GAIA_{field}.fits\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mgaia\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mTable\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"../dmu0/dmu0_GAIA/data/GAIA_{field}.fits\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0mgaia_coords\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSkyCoord\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgaia\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'ra'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgaia\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'dec'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/herschelhelp_internal/lib/python3.6/site-packages/astropy/table/table.py\u001b[0m in \u001b[0;36mread\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m   2529\u001b[0m         \u001b[0mpassed\u001b[0m \u001b[0mthrough\u001b[0m \u001b[0mto\u001b[0m \u001b[0mthe\u001b[0m \u001b[0munderlying\u001b[0m \u001b[0mdata\u001b[0m \u001b[0mreader\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m~\u001b[0m\u001b[0mastropy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mascii\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2530\u001b[0m         \"\"\"\n\u001b[0;32m-> 2531\u001b[0;31m         \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mio_registry\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2532\u001b[0m         \u001b[0;31m# For some readers (e.g., ascii.ecsv), the returned `out` class is not\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2533\u001b[0m         \u001b[0;31m# guaranteed to be the same as the desired output `cls`.  If so,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/herschelhelp_internal/lib/python3.6/site-packages/astropy/io/registry.py\u001b[0m in \u001b[0;36mread\u001b[0;34m(cls, format, *args, **kwargs)\u001b[0m\n\u001b[1;32m    500\u001b[0m                     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    501\u001b[0m                         \u001b[0mctx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_readable_fileobj\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mencoding\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'binary'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 502\u001b[0;31m                         \u001b[0mfileobj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__enter__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    503\u001b[0m                     \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    504\u001b[0m                         \u001b[0;32mraise\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/herschelhelp_internal/lib/python3.6/contextlib.py\u001b[0m in \u001b[0;36m__enter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     79\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__enter__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     80\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 81\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgen\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     82\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     83\u001b[0m             \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"generator didn't yield\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/herschelhelp_internal/lib/python3.6/site-packages/astropy/utils/data.py\u001b[0m in \u001b[0;36mget_readable_fileobj\u001b[0;34m(name_or_obj, encoding, cache, show_progress, remote_timeout)\u001b[0m\n\u001b[1;32m    191\u001b[0m                 \u001b[0mname_or_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcache\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshow_progress\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mshow_progress\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    192\u001b[0m                 timeout=remote_timeout)\n\u001b[0;32m--> 193\u001b[0;31m         \u001b[0mfileobj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mFileIO\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname_or_obj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    194\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mis_url\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    195\u001b[0m             \u001b[0mdelete_fds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfileobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '../dmu0/dmu0_GAIA/data/GAIA_XMM-13hr.fits'"
     ]
    }
   ],
   "source": [
    "#gaia = Table.read(\"./dmu17_XMM-LSS/data/GAIA_XMM-LSS.fits\")\n",
    "print(f\"../../dmu0/dmu0_GAIA/data/GAIA_{field}.fits\")\n",
    "gaia = Table.read(f\"../dmu0/dmu0_GAIA/data/GAIA_{field}.fits\")\n",
    "gaia_coords = SkyCoord(gaia['ra'], gaia['dec'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nb_astcor_diag_plot(wise_coords.ra, wise_coords.dec, \n",
    "                    gaia_coords.ra, gaia_coords.dec, near_ra0=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "delta_ra, delta_dec =  astrometric_correction(\n",
    "    wise_coords,\n",
    "    gaia_coords, near_ra0=True\n",
    ")\n",
    "\n",
    "print(\"RA correction: {}\".format(delta_ra))\n",
    "print(\"Dec correction: {}\".format(delta_dec))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( wise[\"ra\"])\n",
    "print(delta_ra.to(u.deg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#wise[\"ra\"] +=  delta_ra.to(u.deg)\n",
    "wise[\"ra\"] = wise[\"ra\"]+ delta_ra.to(u.deg)\n",
    "wise[\"dec\"] = wise[\"dec\"]+ delta_dec.to(u.deg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nb_astcor_diag_plot(wise[\"ra\"], wise[\"dec\"], \n",
    "                    gaia_coords.ra, gaia_coords.dec, near_ra0=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## V - Saving to disk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wise.write(f\"../dmu16_allwise/data/Allwise_PSF_stack_GAIA_{field}.fits\", overwrite=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python (herschelhelp_internal)",
   "language": "python",
   "name": "helpint"
  },
  "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": 1
}
