{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3d50c932",
   "metadata": {},
   "source": [
    "======================== Import Packages =========================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9413aa39",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys, pdb, glob\n",
    "import numpy as np\n",
    "from astropy.table import Table, join\n",
    "from astroquery.vizier import Vizier\n",
    "import warnings\n",
    "from astropy.logger import AstropyWarning\n",
    "warnings.filterwarnings('ignore', category=AstropyWarning)\n",
    "from IPython.display import display, Latex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43c0697d",
   "metadata": {},
   "source": [
    "===================== Define Functions ==================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3328ea95",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data(catalog, join_key='Name'):\n",
    "\n",
    "    \"\"\"\n",
    "    PURPOSE:    Get data from literature with Vizier\n",
    "\n",
    "    INPUT:      catalog = ctalog name on Vizier (str)\n",
    "                join_key = column header to join tables, if multiple (str; optional)\n",
    "\n",
    "    OUTPUT:     t = data table (AstroPy Table)\n",
    "\n",
    "    \"\"\"\n",
    "\n",
    "    ### GET FULL CATALOG (ALL COLUMNS, ALL ROWS)\n",
    "    viz = Vizier(catalog=catalog, columns=['**'])\n",
    "    viz.ROW_LIMIT = -1\n",
    "    tv = viz.get_catalogs(catalog)\n",
    "\n",
    "    ### IF MULTIPLE TABLES, JOIN THEN\n",
    "    for i, val in enumerate(tv.keys()):\n",
    "        if i == 0:\n",
    "            t = tv[val]\n",
    "        else:\n",
    "            tt = tv[val]\n",
    "            if join_key in tt.columns:\n",
    "                t = join(t, tt, join_type='inner', keys=join_key)\n",
    "\n",
    "    return t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7baa8a0f",
   "metadata": {},
   "source": [
    "========================== Code =========================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "18fc2b90",
   "metadata": {},
   "outputs": [],
   "source": [
    "### LOAD IN LUPUS DATA\n",
    "T = get_data(\"J/ApJ/828/46\")\n",
    "T.sort('RAJ2000')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "43381b2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### WRITE HEADER INFO\n",
    "f = open('../output/table_01.tex', 'w')\n",
    "f.write(r'\\capstartfalse'                                     + ' \\n')\n",
    "f.write(r'\\begin{deluxetable}{lrrrr}'                         + ' \\n')\n",
    "f.write(r'\\tabletypesize{\\footnotesize}'                      + ' \\n')\n",
    "f.write(r'\\centering'                                         + ' \\n')\n",
    "f.write(r'\\tablewidth{240pt}'                                 + ' \\n')\n",
    "f.write(r'\\tablecaption{Stellar Properties \\label{tab-star}}' + ' \\n')\n",
    "f.write(r'\\tablecolumns{5} '                                  + ' \\n')\n",
    "f.write(r'\\tablehead{'                                        + ' \\n')\n",
    "f.write(r' \\colhead{Source}'                                  + ' \\n')\n",
    "f.write(r'&\\colhead{$d$ (pc)}'                                + ' \\n')\n",
    "f.write(r'&\\colhead{SpT}'                                     + ' \\n')\n",
    "f.write(r'&\\colhead{$M_{\\ast}$/$M_{\\odot}$}'                  + ' \\n')\n",
    "f.write(r'&\\colhead{Ref}'                                     + ' \\n')\n",
    "f.write(r'}'                                                  + ' \\n')\n",
    "f.write(r'\\startdata'                                         + ' \\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "00cc0696",
   "metadata": {},
   "outputs": [],
   "source": [
    "### WRITE FIRST 10 LINES\n",
    "lim = 10\n",
    "for i, val in enumerate(np.zeros(lim)):\n",
    "\n",
    "    source = T['Name'][i]\n",
    "    dist = str(T['Dist'][i])\n",
    "\n",
    "    if np.ma.is_masked(T['Ref'][i]) is True:\n",
    "        ref = '...'\n",
    "    else:\n",
    "        ref = str(T['Ref'][i])\n",
    "\n",
    "    if T['SpT'][i] == '':\n",
    "        spt = '...'\n",
    "    else:\n",
    "        spt = T['SpT'][i]\n",
    "\n",
    "    if np.ma.is_masked(T['Mass'][i]) is True:\n",
    "        mstar = '...'\n",
    "    else:\n",
    "        mstar  = str(T['Mass'][i]) + r' $\\pm$ ' + str(T['e_Mass'][i])\n",
    "\n",
    "    ### END OF LINE\n",
    "    if (i < lim - 1):\n",
    "        end = r' \\\\' + '\\n'\n",
    "    else:\n",
    "        end = '\\n'\n",
    "\n",
    "    f.write(source + ' & ' + dist + ' & ' + spt + ' & ' + mstar + ' & ' + ref + end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ac746255",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{array}{lrrrr} \\hline\n",
       "\\text{Source} & d\\text{ (pc)} & \\text{SpT} & M_{\\ast}/M_{\\odot} & \\text{Ref} \\\\\n",
       "\\hline\n",
       "Sz 65 & 150 & K7.0 & 0.76 \\pm 0.18 & 2 \\\\\n",
       "Sz 66 & 150 & M3.0 & 0.31 \\pm 0.04 & 1 \\\\\n",
       "J15430131-3409153 & 150 & ... & ... & ... \\\\\n",
       "J15430227-3444059 & 150 & ... & ... & ... \\\\\n",
       "J15445789-3423392 & 150 & M5.0 & 0.12 \\pm 0.03 & 1 \\\\\n",
       "J15450634-3417378 & 150 & ... & ... & ... \\\\\n",
       "J15450887-3417333 & 150 & M5.5 & 0.14 \\pm 0.03 & 2 \\\\\n",
       "Sz 68 & 150 & K2.0 & 2.13 \\pm 0.34 & 2 \\\\\n",
       "Sz 69 & 150 & M4.5 & 0.19 \\pm 0.03 & 1 \\\\\n",
       "Sz 71 & 150 & M1.5 & 0.42 \\pm 0.11 & 1 \\\\\n",
       "\\hline\n",
       "\\end{array}\n"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table_header = r'\\begin{array}{lrrrr} \\hline' + '\\n'\n",
    "table_header += r'\\text{Source} & d\\text{ (pc)} & \\text{SpT} & M_{\\ast}/M_{\\odot} & \\text{Ref} \\\\' + '\\n'\n",
    "table_header += r'\\hline' + '\\n'\n",
    "\n",
    "table_body = ''\n",
    "lim = 10\n",
    "for i, val in enumerate(np.zeros(lim)):\n",
    "    source = T['Name'][i]\n",
    "    dist = str(T['Dist'][i])\n",
    "\n",
    "    if np.ma.is_masked(T['Ref'][i]) is True:\n",
    "        ref = '...'\n",
    "    else:\n",
    "        ref = str(T['Ref'][i])\n",
    "\n",
    "    if T['SpT'][i] == '':\n",
    "        spt = '...'\n",
    "    else:\n",
    "        spt = T['SpT'][i]\n",
    "\n",
    "    if np.ma.is_masked(T['Mass'][i]) is True:\n",
    "        mstar = '...'\n",
    "    else:\n",
    "        mstar  = str(T['Mass'][i]) + r' \\pm ' + str(T['e_Mass'][i])\n",
    "\n",
    "    table_body += source + ' & ' + dist + ' & ' + spt + ' & ' + mstar + ' & ' + ref + r' \\\\' + '\\n'\n",
    "\n",
    "table_footer = r'\\hline' + '\\n'\n",
    "table_footer += r'\\end{array}' + '\\n'\n",
    "\n",
    "table = table_header + table_body + table_footer\n",
    "\n",
    "display(Latex(table))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6b1b8204",
   "metadata": {},
   "outputs": [],
   "source": [
    "### WRITE FOOTER INFO\n",
    "f.write(r'\\enddata' + ' \\n')\n",
    "f.write(r'\\tablenotetext{}{References:' +\n",
    "        r' (1) \\cite{2014A&A...561A...2A},' +\n",
    "        r' (2) Alcal\\'{a} et al. (in prep),' +\n",
    "        r' (3) \\cite{2013MNRAS.429.1001A},' +\n",
    "        r' (4) \\cite{2011MNRAS.418.1194M},' +\n",
    "        r' (5) \\cite{2008ApJS..177..551M},' +\n",
    "        r' (6) Cleeves et al. (in prep),' +\n",
    "        r' (7) \\cite{2015A&A...578A..23B},' +\n",
    "        r' (8) \\cite{2008hsf2.book..295C}.' +\n",
    "        r' (This table is available in its entirety in machine-readible form.)}' + ' \\n')\n",
    "f.write(r'\\end{deluxetable}' + ' \\n')\n",
    "f.write(r'\\capstartfalse' + ' \\n')\n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
