{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Downloading and processing of HSC DR2\n",
    "\n",
    "HELP DR1 included the HSC SSP DR1 release. Here we download DR2 with a view to including it in HELP. This may simply bematched in to the HELP DR1 or concievably be run a through a full rerun if it significanlty influences photo-zs, XID+ priors or CIGALE SED fits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dud = ['cosmos', 'sxds', 'elais_n1', 'xmm_lss', 'deep2_3', ]\n",
    "wide = []\n",
    "\n",
    "pdr2_wide.search_w01(object_id bigint) -> boolean\n",
    "pdr2_wide.search_w02(object_id bigint) -> boolean\n",
    "pdr2_wide.search_w03(object_id bigint) -> boolean\n",
    "pdr2_wide.search_w04(object_id bigint) -> boolean\n",
    "pdr2_wide.search_w05(object_id bigint) -> boolean\n",
    "pdr2_wide.search_w06(object_id bigint) -> boolean\n",
    "pdr2_wide.search_w07(object_id bigint) -> boolean\n",
    "pdr2_wide.search_aegis(object_id bigint) -> boolean\n",
    "pdr2_wide.search_gama09h(object_id bigint) -> boolean\n",
    "pdr2_wide.search_hectomap(object_id bigint) -> boolean\n",
    "pdr2_wide.search_vvds(object_id bigint) -> boolean\n",
    "pdr2_wide.search_wide01h(object_id bigint) -> boolean\n",
    "pdr2_wide.search_xmm_lss\n",
    "\n",
    "pdr2_cosmos_wide_depth_best\n",
    "pdr2_cosmos_wide_depth_media\n",
    "pdr2_cosmos_wide_depth_worst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "for field in :\n",
    "    sql = open('./sql/{}_dud_pdr2.sql'.format(field), 'w+')\n",
    "    sql.write(\"SELECT\\n\")\n",
    "    sql.write(\"    object_id, ra, dec,\\n\")\n",
    "\n",
    "    for band in ['g', 'r', 'i', 'z', 'y', 'n387', 'n816', 'n921']:\n",
    "        for ap in [10, 15, 20, 30, 40, 57, 84]:\n",
    "            sql.write(\"    {}_apertureflux_{}_flux,\\n\".format(band, ap))\n",
    "            sql.write(\"    {}_apertureflux_{}_fluxsigma,\\n\".format(band, ap))\n",
    "            sql.write(\"    {}_apertureflux_{}_mag,\\n\".format(band, ap))\n",
    "            sql.write(\"    {}_apertureflux_{}_magsigma,\\n\".format(band, ap))\n",
    "            sql.write(\"    {}_apertureflux_{}_flag,\\n\".format(band, ap))\n",
    "        \n",
    "    \n",
    "        sql.write(\"    {}_kronflux_flux,\\n\".format(band, ap))\n",
    "        sql.write(\"    {}_kronflux_fluxsigma,\\n\".format(band, ap))\n",
    "        sql.write(\"    {}_kronflux_mag,\\n\".format(band, ap))\n",
    "        sql.write(\"    {}_kronflux_magsigma,\\n\".format(band, ap))\n",
    "    \n",
    "        sql.write(\"    {}_extendedness_value,\\n\".format(band, ap))\n",
    "    \n",
    "    sql.write(\"    isprimary\\n\".format(band, ap))\n",
    "    sql.write(\"\"\"\n",
    "FROM\n",
    " \n",
    "\n",
    "  -- DEEP SURVEY (in PDR1 available in ELAIS-N1, XMM-LSS, E(xtended)-COSMOS, DEEP2-3)\n",
    "  pdr2_dud.forced\n",
    "  -- uncomment the survey tier of interest and comment the other\n",
    "  -- LEFT JOIN pdr1_udeep.afterburner USING(object_id)\n",
    "  LEFT JOIN pdr2_dud.forced2 USING (object_id)\n",
    "  LEFT JOIN pdr2_dud.forced3 USING (object_id)\n",
    "  LEFT JOIN pdr2_dud.forced4 USING (object_id)\n",
    "  LEFT JOIN pdr2_dud.forced5 USING (object_id)\n",
    "\n",
    "WHERE\n",
    "  -- uncomment the field and survey tier of interest and comment the others\n",
    "  pdr2_dud.search_{}(object_id)\n",
    "\n",
    "AND pdr2_dud.forced.isprimary\"\"\".format(field))\n",
    "    sql.close()"
   ]
  }
 ],
 "metadata": {
  "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
