{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Make DESI photo-z tables for VO\n",
    "\n",
    "I was originally going to make a smaller VO table with only:\n",
    "    \n",
    "- field\n",
    "- desi_id\n",
    "- ra\n",
    "- dec\n",
    "- hp_idx\n",
    "- redshift\n",
    "- redshift_err\n",
    "- zspec\n",
    " \n",
    "\n",
    "The HELP field and hp_idx are made in this notebook.\n",
    "\n",
    "We later decided it would be advantageous to include all columns. Given the large size of the tables some of the work must be done with stilts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from astropy.table import Table, Column\n",
    "\n",
    "from herschelhelp.utils import position_to_field\n",
    "from herschelhelp_internal.utils import coords_to_hpidx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Make csv file with corrects columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Load a smaller version with just required fields made using Stilts\n",
    "#stilts tpipe cmd='keepcols \"ID RA DEC photo_z photo_zerr spec_z\"' \n",
    "#./data/dr6dr7_photoz_csp_totcat.fits omode=out out=./data/dr6dr7_reduced.fits\n",
    "\n",
    "small_tab='./data/LS_DR8_total_csp_reduced.fits'\n",
    "desi = Table.read(small_tab)['ID', 'RA', 'DEC'] #, 'photo_z', 'photo_zerr', 'spec_z']\n",
    "#desi = Table.read(full_tab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#desi['RA'].name = 'ra'\n",
    "#desi['DEC'].name = 'dec'\n",
    "#desi['ID'].name = 'desi_id'\n",
    "#desi['photo_z'].name = 'redshift'\n",
    "#desi['photo_zerr'].name = 'redshift_err'\n",
    "#desi['spec_z'].name = 'zspec'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "desi.add_column(Column(\n",
    "    data=coords_to_hpidx(desi['RA'], desi['DEC'], order=13),\n",
    "    name=\"hp_idx\"\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#desi.write('./data/desi_for_vo_1.fits', overwrite = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "desi.add_column(Column(\n",
    "    data=position_to_field(desi['RA'], desi['DEC'], dmu_loc='../../'),\n",
    "    name=\"field\"\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#cols = ['field', 'desi_id', 'ra', 'dec', 'hp_idx', 'redshift', 'redshift_err', 'zspec']\n",
    "#cols = ['field', 'ID', 'RA', 'DEC', 'hp_idx', 'photo_z', 'photo_zerr', 'spec_z']\n",
    "desi['field', 'hp_idx'].write('./data/desi_for_vo_temp.fits', overwrite = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#You then need to stack the tables using stilts with:\n",
    "#stilts tjoin nin=2 in1=LS_DR8_total_csp.fits in2=desi_for_vo_temp.fits out=combined.csv"
   ]
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
