{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc306bbc",
   "metadata": {},
   "source": [
    "======================== Import Packages =========================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17b9fb0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys, os, pdb, glob\n",
    "import numpy as np\n",
    "from astropy.table import Table, join\n",
    "from astroquery.vizier import Vizier\n",
    "import calc_dust_masses\n",
    "import warnings\n",
    "from astropy.logger import AstropyWarning\n",
    "warnings.filterwarnings('ignore', category=AstropyWarning)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b493b604",
   "metadata": {},
   "source": [
    "===================== Define Functions ==================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d74ebe6a",
   "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": "code",
   "execution_count": null,
   "id": "ec9ed059",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "def fix_table(t, region, col_name, col_spt, col_flux, col_eflux, col_mstar, dist, obs_wave, col_lflux=None, log_mstar=False):\n",
    "\n",
    "    \"\"\"\n",
    "    PURPOSE:    Make tables for different regions consistent\n",
    "\n",
    "    INPUT:      t = table from literature (AstroPy Table)\n",
    "                region = name of region (str)\n",
    "                col_X = column name for data values (str)\n",
    "                dist = distance to source in pc (float arr)\n",
    "                obs_wave = wavelength of observation in microns (float)\n",
    "                col_lflux = column name if fluxes for non-detections are upper limits (str; optional)\n",
    "                log_mstar = boolean flag if stellar mass is log (boolean; optional)\n",
    "\n",
    "    OUTPUT:     t = fixed data table (AstroPy Table)\n",
    "\n",
    "    \"\"\"\n",
    "\n",
    "   \n",
    "    ### FOR TAURUS, DO SOME EXTRA FIXING\n",
    "    t['ObsWave'] = obs_wave\n",
    "    if region == 'tau':\n",
    "        ind_1300 = np.where(t['Notes'] == 'e, m')\n",
    "        t[col_flux][ind_1300] = t['F1.3'][ind_1300]\n",
    "        t[col_lflux][ind_1300] = t['l_F1.3'][ind_1300]\n",
    "        t['ObsWave'][ind_1300] = 1300.\n",
    "        t[col_flux] *= 1e3\n",
    "        t[col_spt] = [x.replace(')', '').replace('(', '').split('+/-')[0].split('-')[0] for x in t['SpT']]\n",
    " \n",
    "    ### FIX COLUMN NAMES\n",
    "    t[col_name].name = 'Name'\n",
    "    t[col_spt].name = 'SpT'\n",
    "    t[col_mstar].name = 'Mstar'\n",
    "    t[col_flux].name = 'Flux'\n",
    "    t[col_eflux].name = 'e_Flux'\n",
    "    t['Dist'] = dist\n",
    "\n",
    "    ### FLAG (NON-)DETECTIONS & REPLACE WITH 3-SIGMA UPPER LIMITS\n",
    "    ### USE 3x ERROR IF TRUE ERRORS REPORTED\n",
    "    if col_lflux is None:\n",
    "        t['Det'] = np.repeat(0, len(t))\n",
    "        t['Det'][t['Flux'] / t['e_Flux'] >= 3.0] = 1\n",
    "        t['Flux'][np.where(t['Det'] == 0)] = 3.0 * t['e_Flux'][np.where(t['Det'] == 0)]\n",
    "\n",
    "    ### USE REPORTED FLUX IF UPPER LIMITS PROVIDED\n",
    "    else:\n",
    "        t['Det'] = np.repeat(1, len(t))\n",
    "        t['Det'][np.where(t[col_lflux] == '<')] = 0\n",
    "\n",
    "    ### ONLY KEEP STARS > 0.1 SOLAR MASS\n",
    "    if log_mstar:\n",
    "        t['Mstar'] = 10**t['Mstar']\n",
    "    t = t[t['Mstar'] >= 0.1]\n",
    "\n",
    "    ### FOR UPPER SCO, ONLY KEEP \"PRIMORDIAL\" DISKS \n",
    "    ### TO MATCH SAMPLES OF LUPUS & TAURUS\n",
    "    if region == 'usc':\n",
    "        t = t[np.where( (t['Type'] == 'Full') | (t['Type'] == 'Transitional') | (t['Type'] == 'Evolved'))] \n",
    "\n",
    "    ### CALCULATE DUST MASSES USING SAME METHOD AS LUPUS\n",
    "    mdust = []\n",
    "    for i, val in enumerate(t):\n",
    "        mdust.append(calc_dust_masses.get_dustmass(1.0, t['ObsWave'][i], t['Flux'][i], t['Dist'][i], 20.))\n",
    "    t['MDust'] = mdust\n",
    "\n",
    "    return t['Name', 'SpT', 'Dist', 'Mstar', 'Flux', 'ObsWave', 'Det', 'MDust']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f9f2d5b",
   "metadata": {},
   "source": [
    "========================== Code =========================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f08b54c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "### GET TAURUS DATA\n",
    "TT = get_data(\"J/ApJ/771/129/\")\n",
    "TT = fix_table(TT, 'tau', 'Name', 'SpT', 'F0.89', 'e_F0.89', 'logM_3', np.repeat(140., len(TT)), 890., col_lflux='l_F0.89', log_mstar=True)\n",
    "TT.write('../output/data_tau.txt', format='ascii.ipac')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64f27f2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "### GET LUPUS DATA\n",
    "TL = get_data(\"J/ApJ/828/46\")\n",
    "TL = fix_table(TL, 'lup', 'Name', 'SpT', 'F890', 'e_F890', 'Mass', TL['Dist'], 890.)\n",
    "TL.write('../output/data_lup.txt', format='ascii.ipac')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "187bb155",
   "metadata": {},
   "outputs": [],
   "source": [
    "### GET UPPER SCO DATA\n",
    "TU = get_data(\"J/ApJ/827/142\")\n",
    "TU = fix_table(TU, 'usc', '_2MASS', 'SpT', 'Snu', 'e_Snu', 'logM', np.repeat(145., len(TU)), 880., log_mstar=True)\n",
    "TU.write('../output/data_usc.txt', format='ascii.ipac')"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
