{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1546ea83-a266-4f13-8eb8-d5cc6f72ee56",
   "metadata": {},
   "source": [
    "# Coordinate Query"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6ebc1f0-ff62-4a44-be62-7860e5a55155",
   "metadata": {},
   "source": [
    "This notebook demonstrates how to query the survey for datacubes based on a coordinate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "49765cb5-4f3e-41a2-b7b1-157a8b1bfae0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path as op\n",
    "import numpy as np\n",
    "from astropy.table import Table\n",
    "\n",
    "from astropy.coordinates import SkyCoord\n",
    "import astropy.units as u\n",
    "from astropy.io import fits\n",
    "\n",
    "from astropy.wcs import WCS\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.colors import LogNorm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "12f8ce30-0650-44ff-800c-2ebd942effde",
   "metadata": {},
   "outputs": [],
   "source": [
    "pdr_dir = '/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/'\n",
    "if not op.exists(pdr_dir):\n",
    "    pdr_dir = '/home/jovyan/work/pdr1/'\n",
    "    \n",
    "ifu_data = Table.read(op.join( pdr_dir, 'ifu-index.fits'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "054cc55e-af0d-4c77-ae65-a4af4d1c6685",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create SkyCoord Object array for IFU centers\n",
    "ifu_coords = SkyCoord( ra=ifu_data['ra_cen']*u.deg, dec=ifu_data['dec_cen']*u.deg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6f9816a-6c85-43a7-a1dd-c6b425b84c69",
   "metadata": {},
   "source": [
    "## Single Coordinate Search"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71877d7b-6cd0-4168-8624-a12e4fc281c8",
   "metadata": {},
   "source": [
    "This is an example of a big Lyman Alpha Blob at z=2.53. Check out notebook 07-CollapsingCubes.ipynb to show you how to create a narrowband line flux map of this object\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "01c279fb-81bc-46dd-8ba4-768fd0307014",
   "metadata": {},
   "outputs": [],
   "source": [
    "coord = SkyCoord(ra=228.78581*u.deg, dec=51.268036*u.deg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8aee251f-d09d-4016-9a0e-82b04e4e378b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# find list of possible datacubes with coverage\n",
    "sel = coord.separation( ifu_coords) < 37*u.arcsec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "013838e8-45e5-433c-b420-7e9d5a3215ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><i>Table length=1</i>\n",
       "<table id=\"table140458768996672\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>shotid</th><th>ifuslot</th><th>ra_cen</th><th>dec_cen</th><th>flag</th><th>flag_badamp</th><th>flag_badfib</th><th>flag_meteor</th><th>flag_satellite</th><th>flag_shot</th><th>flag_throughput</th><th>field</th><th>objid</th><th>date</th><th>obsid</th><th>ra_shot</th><th>dec_shot</th><th>pa</th><th>n_ifu</th><th>fwhm_virus</th><th>fwhm_virus_err</th><th>response_4540</th><th>ambtemp</th><th>datevobs</th><th>dewpoint</th><th>exptime</th><th>humidity</th><th>mjd</th><th>nstars_fit_fwhm</th><th>obsind</th><th>pressure</th><th>structaz</th><th>time</th><th>trajcdec</th><th>trajcpa</th><th>trajcra</th><th>fluxlimit_4600</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>bytes3</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>bytes12</th><th>bytes18</th><th>int32</th><th>int32</th><th>float64</th><th>float64</th><th>float64</th><th>int32</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>bytes12</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th><th>int32</th><th>int32</th><th>float32</th><th>float32</th><th>bytes7</th><th>float32</th><th>float32</th><th>float32</th><th>float32</th></tr></thead>\n",
       "<tr><td>20180618017</td><td>024</td><td>228.79074</td><td>51.264973</td><td>1.0</td><td>1.0</td><td>1.0</td><td>1.0</td><td>1.0</td><td>1.0</td><td>1.0</td><td>dex-spring</td><td>HS00444d2_W</td><td>20180618</td><td>17</td><td>228.871039</td><td>51.349437</td><td>292.50071</td><td>40</td><td>1.4246124</td><td>0.16366434</td><td>0.1826</td><td>18.612223</td><td>20180618v017</td><td>9.869445</td><td>367.6003</td><td>55.798</td><td>58287.285</td><td>19</td><td>1</td><td>804.809</td><td>317.00266</td><td>0648106</td><td>51.3479</td><td>290.8775</td><td>228.87</td><td>1.028041</td></tr>\n",
       "</table></div>"
      ],
      "text/plain": [
       "<Table length=1>\n",
       "   shotid   ifuslot   ra_cen   dec_cen  ... trajcpa  trajcra fluxlimit_4600\n",
       "   int64     bytes3  float32   float32  ... float32  float32    float32    \n",
       "----------- ------- --------- --------- ... -------- ------- --------------\n",
       "20180618017     024 228.79074 51.264973 ... 290.8775  228.87       1.028041"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ifu_data[sel]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3e49f704-d31d-4d5c-b8f6-60c687e8dadf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20180618017/dex_cube_20180618017_024.fits\n"
     ]
    }
   ],
   "source": [
    "# print the data path to the datacubes\n",
    "for row in ifu_data[sel]:\n",
    "    \n",
    "    shotid = row['shotid']\n",
    "    ifuslot= row['ifuslot']\n",
    "    datacube_path = op.join( pdr_dir, 'datacubes', str(shotid), 'dex_cube_{}_{}.fits'.format( shotid, ifuslot))\n",
    "    print( datacube_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d5878999-ba6c-4192-a28f-26c49ba7d1a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# Note that for exact coverage you will need to check the datacube WCS\n",
    "for row in ifu_data[sel]:\n",
    "    \n",
    "    shotid = row['shotid']\n",
    "    ifuslot= row['ifuslot']\n",
    "    datacube_path = op.join( pdr_dir, 'datacubes', str(shotid), 'dex_cube_{}_{}.fits'.format( shotid, ifuslot))\n",
    "\n",
    "        # --- Load cube and WCS ---\n",
    "    with fits.open(datacube_path, memmap=False) as hdul:\n",
    "        \n",
    "        w = WCS(hdul['DATA'].header).celestial  # celestial WCS\n",
    "        # check for exact coverage within the datacube.\n",
    "        # Check containment with footprint_contains\n",
    "        inside = w.footprint_contains(coord)\n",
    "        print(inside) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "178fa19e-e818-4a6e-a719-87dc1c7a6f4c",
   "metadata": {},
   "source": [
    "## AGN Catalog Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f6b4f035-289d-4a0b-bd1b-29aead07fb46",
   "metadata": {},
   "outputs": [],
   "source": [
    "sdss_agn = Table.read('dr16q_hdr5.fits')\n",
    "                          \n",
    "agn_coords = SkyCoord(ra = sdss_agn['ra'], dec= sdss_agn['dec'], unit='deg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17fef702-10fd-438a-876c-83e281d5fc16",
   "metadata": {},
   "source": [
    "We will want all tiles that overlap with this list of coordinates not just the closest match. \n",
    "Astropy SkyCoord Class function \"search_around_sky\" is great for this. Here we will search 26 arcsec within every sources in the catalog list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fadd5c5b-c619-4483-96e5-1f5d38f64dc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "idxc, idxcatalog, d2d, d3d = agn_coords.search_around_sky(ifu_coords, 26*u.arcsec)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8bc025c-e772-4f65-9f5d-783239aa6910",
   "metadata": {},
   "source": [
    "Here we list the datapath of the cubes that overlap with coordinate catalog. To see how to access each of these cubes and extract spectra see 08-ExtractingSpectra.ipynb."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b1535b33-5af7-414d-a5e4-e59a4a407c56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170222012/dex_cube_20170222012_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170222012/dex_cube_20170222012_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170222012/dex_cube_20170222012_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170301015/dex_cube_20170301015_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170301015/dex_cube_20170301015_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170306018/dex_cube_20170306018_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170306018/dex_cube_20170306018_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170322011/dex_cube_20170322011_073.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170322011/dex_cube_20170322011_073.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170322014/dex_cube_20170322014_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170322014/dex_cube_20170322014_083.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170328010/dex_cube_20170328010_105.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170328010/dex_cube_20170328010_105.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170328010/dex_cube_20170328010_105.fits\n",
      "/home/jovyan/Hobby-Eberly-Public/HETDEX/pdr/pdr1/datacubes/20170328010/dex_cube_20170328010_105.fits\n"
     ]
    }
   ],
   "source": [
    "for row in ifu_data[ idxc][0:15]:    \n",
    "    shotid = row['shotid']\n",
    "    ifuslot= row['ifuslot']\n",
    "    datacube_path = op.join( pdr_dir, 'datacubes', str(shotid), 'dex_cube_{}_{}.fits'.format( shotid, ifuslot))\n",
    "    print( datacube_path)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
