{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "from matplotlib.patches import Polygon\n",
    "\n",
    "from astropy.table import Table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Upper left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_first_selection(df):\n",
    "    \"\"\"Plot spatial distribution of stars selected based on Proper Motion\n",
    "    \n",
    "    df: Tale or DataFrame with proper motion selected coordinates in GD-1 reference frame\n",
    "    \"\"\"\n",
    "    x = df['phi1']\n",
    "    y = df['phi2']\n",
    "\n",
    "    plt.plot(x, y, 'ko', markersize=0.3, alpha=0.3)\n",
    "\n",
    "    plt.xlabel('$\\phi_1$ [deg]')\n",
    "    plt.ylabel('$\\phi_2$ [deg]')\n",
    "    plt.title('Proper motion selection', fontsize='medium')\n",
    "\n",
    "    plt.axis('equal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 45636 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "filename_pm_selected = 'gd1_candidates.hdf5'\n",
    "candidate_df = pd.read_hdf(filename_pm_selected, 'candidate_df')\n",
    "plot_first_selection(candidate_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Upper right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_proper_motion(df):\n",
    "    \"\"\"Plot proper motion of spatially selected stars in GD-1\n",
    "    \n",
    "    df: Table or DataFrame with spatially selected proper motions in GD-1 reference frame\n",
    "    \"\"\"\n",
    "    pm1 = df['pm_phi1']\n",
    "    pm2 = df['pm_phi2']\n",
    "\n",
    "    plt.plot(pm1, pm2, 'ko', markersize=0.3, alpha=0.3)\n",
    "    \n",
    "    poly = Polygon(vertices, closed=True, \n",
    "                   facecolor='C1', alpha=0.4)\n",
    "    plt.gca().add_patch(poly)\n",
    "    \n",
    "    plt.xlabel('$\\mu_{\\phi_1} [\\mathrm{mas~yr}^{-1}]$')\n",
    "    plt.ylabel('$\\mu_{\\phi_2} [\\mathrm{mas~yr}^{-1}]$')\n",
    "\n",
    "    plt.xlim(-12, 8)\n",
    "    plt.ylim(-10, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pm1_min = -8.9\n",
    "pm1_max = -6.9\n",
    "pm2_min = -2.2\n",
    "pm2_max =  1.0\n",
    "\n",
    "pm1_rect = [pm1_min, pm1_min, pm1_max, pm1_max]\n",
    "pm2_rect = [pm2_min, pm2_max, pm2_max, pm2_min]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "vertices = np.transpose([pm1_rect, pm2_rect])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 108740 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "filename_pm = 'gd1_dataframe.hdf5'\n",
    "centerline = pd.read_hdf(filename_pm, 'centerline')\n",
    "plot_proper_motion(centerline)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lower left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_second_selection(df):\n",
    "    \"\"\"Plot spatial distribution of photometry selected points\n",
    "    \n",
    "    df: Table or DataFrame with photometry selected positions in GD1 reference frame\n",
    "    \"\"\"\n",
    "    x = df['phi1']\n",
    "    y = df['phi2']\n",
    "\n",
    "    plt.plot(x, y, 'ko', markersize=0.7, alpha=0.9)\n",
    "\n",
    "    plt.xlabel('$\\phi_1$ [deg]')\n",
    "    plt.ylabel('$\\phi_2$ [deg]')\n",
    "    plt.title('Proper motion + photometry selection', fontsize='medium')\n",
    "\n",
    "    plt.axis('equal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13436 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "filename = 'gd1_merged.hdf5'\n",
    "selected = pd.read_hdf(filename, 'selected')\n",
    "plot_second_selection(selected)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lower right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_cmd(table):\n",
    "    \"\"\"Plot a color magnitude diagram.\n",
    "    \n",
    "    table: Table or DataFrame with photometry data\n",
    "    \"\"\"\n",
    "    y = table['g_mean_psf_mag']\n",
    "    x = table['g_mean_psf_mag'] - table['i_mean_psf_mag']\n",
    "\n",
    "    plt.plot(x, y, 'ko', markersize=0.3, alpha=0.3)\n",
    "\n",
    "    plt.xlim([0, 1.5])\n",
    "    plt.ylim([14, 22])\n",
    "    plt.gca().invert_yaxis()\n",
    "\n",
    "    plt.ylabel('$g_0$')\n",
    "    plt.xlabel('$(g-i)_0$')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_poly = 'gd1_polygon.hdf5'\n",
    "coords_df = pd.read_hdf(filename_poly, 'coords_df')\n",
    "coords = coords_df.to_numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27368 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "merged = pd.read_hdf(filename, 'merged')\n",
    "plot_cmd(merged)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (AstronomicalData)",
   "language": "python",
   "name": "astronomicaldata"
  },
  "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.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
