{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4cf2a79f",
   "metadata": {},
   "source": [
    "======================== Import Packages =========================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c82af0d6",
   "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, HTML\n",
    "from tabulate import tabulate"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a30cbb90",
   "metadata": {},
   "source": [
    "===================== Define Functions ==================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d53d3b5d",
   "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": "ded942d1",
   "metadata": {},
   "source": [
    "========================== Code =========================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4e1965bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "### LOAD IN LUPUS DATA\n",
    "T = get_data(\"J/ApJ/828/46\")\n",
    "T.sort('RAJ2000')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "251f2726",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### WRITE HEADER INFO\n",
    "f = open('../output/table_02.tex', 'w')\n",
    "f.write(r'\\capstartfalse'                                  + ' \\n')\n",
    "f.write(r'\\begin{deluxetable*}{lrrrccccr}'                 + ' \\n')\n",
    "f.write(r'\\tabletypesize{\\footnotesize}'                   + ' \\n')\n",
    "f.write(r'\\centering'                                      + ' \\n')\n",
    "f.write(r'\\tablewidth{500pt}'                              + ' \\n')\n",
    "f.write(r'\\tablecaption{$890~\\mu$m Continuum Properties \\label{tab-cont}}' + ' \\n')\n",
    "f.write(r'\\tablecolumns{9} '                               + ' \\n')\n",
    "f.write(r'\\tablehead{'                                     + ' \\n')\n",
    "f.write(r' \\colhead{Source}'                               + ' \\n')\n",
    "f.write(r'&\\colhead{RA$_{\\rm J2000}$}'                     + ' \\n')\n",
    "f.write(r'&\\colhead{Dec$_{\\rm J2000}$}'                    + ' \\n')\n",
    "f.write(r'&\\colhead{$F_{\\rm cont}$}'                       + ' \\n')\n",
    "f.write(r'&\\colhead{rms}'                                  + ' \\n')\n",
    "f.write(r'&\\colhead{$a$}'                                  + ' \\n')\n",
    "f.write(r'&\\colhead{$i$}'                                  + ' \\n')\n",
    "f.write(r'&\\colhead{PA}'                                   + ' \\n')\n",
    "f.write(r'&\\colhead{$M_{\\rm dust}$} \\\\'                    + ' \\n')\n",
    "f.write(r' \\colhead{}'                                     + ' \\n')\n",
    "f.write(r'&\\colhead{}'                                     + ' \\n')\n",
    "f.write(r'&\\colhead{}'                                     + ' \\n')\n",
    "f.write(r'&\\colhead{(mJy)}'                                + ' \\n')\n",
    "f.write(r'&\\colhead{(mJy beam$^{-1}$)}'                    + ' \\n')\n",
    "f.write(r'&\\colhead{arcsec}'                               + ' \\n')\n",
    "f.write(r'&\\colhead{deg}'                                  + ' \\n')\n",
    "f.write(r'&\\colhead{(deg)}'                                + ' \\n')\n",
    "f.write(r'&\\colhead{($M_{\\oplus}$)}'                       + ' \\n')\n",
    "f.write(r'}'                                               + ' \\n')\n",
    "f.write(r'\\startdata'                                      + ' \\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "427cf5ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "table_body = ''\n",
    "\n",
    "lim = 10\n",
    "for i, val in enumerate(np.zeros(lim)):\n",
    "\n",
    "    source = T['Name'][i]\n",
    "    de =  T['DEJ2000'][i].replace(' ', ':')\n",
    "    ra = T['RAJ2000'][i].replace(' ', ':')\n",
    "    flx = \"{0:.2f}\".format(T['F890'][i]) + r' \\pm ' + \"{0:.2f}\".format(T['e_F890'][i])\n",
    "    rms = \"{0:.2f}\".format(T['rms'][i])\n",
    "    mdust = str(T['MDust'][i]) + r' \\pm ' + str(T['e_MDust'][i])\n",
    "\n",
    "    if np.ma.is_masked(T['a'][i]) is True:\n",
    "        bmaj = '...'\n",
    "    else:\n",
    "        bmaj  = str(T['a'][i]) + r' \\pm ' + str(T['e_a'][i])\n",
    "\n",
    "    if np.ma.is_masked(T['i'][i]) is True:\n",
    "        incl = '...'\n",
    "    elif np.ma.is_masked(T['e_i'][i]) is True:\n",
    "        incl = str(T['i'][i])\n",
    "    else:\n",
    "        incl  = str(T['i'][i]) + r' \\pm ' + str(T['e_i'][i])\n",
    "\n",
    "    if np.ma.is_masked(T['PA'][i]) is True:\n",
    "        pang = '...'\n",
    "    elif np.ma.is_masked(T['e_PA'][i]) is True:\n",
    "        pang = str(T['PA'][i])\n",
    "    else:\n",
    "        pang  = str(T['PA'][i]) + r' \\pm ' + str(T['e_PA'][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 + ' & ' + ra + ' & ' + de + ' & ' + flx + ' & ' + rms + ' & ' + \n",
    "            bmaj + ' & ' + incl + ' & ' + pang + ' & ' + mdust + end)\n",
    "    \n",
    "    table_body += source + ' & ' + ra + ' & ' + de + ' & ' + flx + ' & ' + rms + ' & ' + bmaj + ' & ' + incl + ' & ' + pang + ' & ' + mdust + end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c1a7f1e4",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "f.write(r'\\enddata' + ' \\n')\n",
    "f.write(r'\\tablenotetext{}{(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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5f767540",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>Source           </th><th>RA_J2000   </th><th>Dec_J2000   </th><th>F_cont (mJy)   </th><th style=\"text-align: right;\">  rms (mJy/beam)</th><th>a (arcsec)     </th><th>i (deg)  </th><th>PA (deg)  </th><th>M_dust (M_oplus)    </th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>Sz 65            </td><td>15:39:27.75</td><td>-34:46:17.56</td><td>64.49 \\pm 0.32 </td><td style=\"text-align: right;\">            0.3 </td><td>0.171 \\pm 0.002</td><td>0 \\pm 0  </td><td>0 \\pm 57  </td><td>15.1559 \\pm 0.0752 \\</td></tr>\n",
       "<tr><td>Sz 66            </td><td>15:39:28.26</td><td>-34:46:18.44</td><td>14.78 \\pm 0.29 </td><td style=\"text-align: right;\">            0.27</td><td>...            </td><td>...      </td><td>...       </td><td>3.4735 \\pm 0.0682 \\ </td></tr>\n",
       "<tr><td>J15430131-3409153</td><td>15:43:01.29</td><td>-34:09:15.40</td><td>0.01 \\pm 0.31  </td><td style=\"text-align: right;\">            0.39</td><td>...            </td><td>...      </td><td>...       </td><td>0.0024 \\pm 0.0729 \\ </td></tr>\n",
       "<tr><td>J15430227-3444059</td><td>15:43:02.29</td><td>-34:44:06.20</td><td>0.22 \\pm 0.27  </td><td style=\"text-align: right;\">            0.34</td><td>...            </td><td>...      </td><td>...       </td><td>0.0517 \\pm 0.0635 \\ </td></tr>\n",
       "<tr><td>J15445789-3423392</td><td>15:44:57.90</td><td>-34:23:39.50</td><td>-0.05 \\pm 0.18 </td><td style=\"text-align: right;\">            0.24</td><td>...            </td><td>...      </td><td>...       </td><td>-0.0118 \\pm 0.0423 \\</td></tr>\n",
       "<tr><td>J15450634-3417378</td><td>15:45:06.32</td><td>-34:17:38.28</td><td>15.00 \\pm 0.40 </td><td style=\"text-align: right;\">            0.34</td><td>0.096 \\pm 0.017</td><td>43 \\pm 28</td><td>24 \\pm 39 </td><td>3.5252 \\pm 0.094 \\  </td></tr>\n",
       "<tr><td>J15450887-3417333</td><td>15:45:08.85</td><td>-34:17:33.81</td><td>46.27 \\pm 0.50 </td><td style=\"text-align: right;\">            0.4 </td><td>0.173 \\pm 0.005</td><td>45 \\pm 4 </td><td>-16 \\pm 5 </td><td>10.874 \\pm 0.1175 \\ </td></tr>\n",
       "<tr><td>Sz 68            </td><td>15:45:12.84</td><td>-34:17:30.98</td><td>150.37 \\pm 0.46</td><td style=\"text-align: right;\">            0.61</td><td>0.159 \\pm 0.002</td><td>34 \\pm 2 </td><td>-5 \\pm 3  </td><td>35.3387 \\pm 0.1081 \\</td></tr>\n",
       "<tr><td>Sz 69            </td><td>15:45:17.39</td><td>-34:18:28.66</td><td>16.96 \\pm 0.28 </td><td style=\"text-align: right;\">            0.24</td><td>0.092 \\pm 0.012</td><td>69 \\pm 21</td><td>-39 \\pm 11</td><td>3.9858 \\pm 0.0658 \\ </td></tr>\n",
       "<tr><td>Sz 71            </td><td>15:46:44.71</td><td>-34:30:36.05</td><td>166.04 \\pm 0.63</td><td style=\"text-align: right;\">            0.37</td><td>0.558 \\pm 0.003</td><td>40 \\pm 0 </td><td>42 \\pm 1  </td><td>39.0213 \\pm 0.1481  </td></tr>\n",
       "</tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "headers = [\"Source\", \"RA_J2000\", \"Dec_J2000\", \"F_cont (mJy)\", \"rms (mJy/beam)\", \"a (arcsec)\", \"i (deg)\", \"PA (deg)\", \"M_dust (M_oplus)\"]\n",
    "\n",
    "table_data = []\n",
    "for row in table_body.split('\\\\\\n'):\n",
    "    table_data.append(row.split(' & '))\n",
    "\n",
    "display(HTML(tabulate(table_data, headers=headers, tablefmt='html')))"
   ]
  }
 ],
 "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
}
