{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table width=\"100%\" style=\"padding:0; margin-left:-6px;\">\n",
    "    <tr style=\"padding=0;\" width=\"100%\">\n",
    "        <td width=\"auto\">\n",
    "           <hr style=\"border:2px solid darkblue\">\n",
    "            <h1> nb8. Query using astroquery</h1>\n",
    "           <hr style=\"border:2px solid darkblue\">\n",
    "        </td>\n",
    "        <td width=\"60px\">\n",
    "           <img src=\"ALMAsmall.png\" align=\"right\"/> \n",
    "        </td>\n",
    "    </tr>\n",
    "</table>  \n",
    "\n",
    "In a similar manner to the previous notebooks, it is also possible to run TAP queries using the [astroquery](https://astroquery.readthedocs.io/en/latest/alma/alma.html) Python package. \n",
    "\n",
    "The very lastest version of astroquery is needed. This can be installed with:\n",
    "```bash\n",
    "pip3 install --upgrade --pre astroquery\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-11-16T10:27:23.562145Z",
     "iopub.status.busy": "2021-11-16T10:27:23.561514Z",
     "iopub.status.idle": "2021-11-16T10:27:24.526489Z",
     "shell.execute_reply": "2021-11-16T10:27:24.525964Z"
    }
   },
   "outputs": [],
   "source": [
    "from astroquery.alma import Alma\n",
    "import numpy as np\n",
    "import astropy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can obtain keywords, description and units of the ALMA TAP service by calling:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-11-16T10:27:24.531366Z",
     "iopub.status.busy": "2021-11-16T10:27:24.530478Z",
     "iopub.status.idle": "2021-11-16T10:27:24.536025Z",
     "shell.execute_reply": "2021-11-16T10:27:24.536519Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Most common ALMA query keywords are listed below. These keywords are part of the ALMA ObsCore model, an IVOA standard for metadata representation (3rd column). They were also present in original ALMA Web form and, for backwards compatibility can be accessed with their old names (2nd column).\n",
      "More elaborate queries on the ObsCore model are possible with `query_sia` or `query_tap` methods\n",
      "  Description                       Original ALMA keyword               ObsCore keyword                    \n",
      "-------------------------------------------------------------------------------------------------------\n",
      "\n",
      "Position\n",
      "  Source name (astropy Resolver)    source_name_resolver                SkyCoord.from_name                 \n",
      "  Source name (ALMA)                source_name_alma                    target_name                        \n",
      "  RA Dec (Sexagesimal)              ra_dec                              s_ra, s_dec                        \n",
      "  Galactic (Degrees)                galactic                            gal_longitude, gal_latitude        \n",
      "  Angular resolution (arcsec)       spatial_resolution                  spatial_resolution                 \n",
      "  Largest angular scale (arcsec)    spatial_scale_max                   spatial_scale_max                  \n",
      "  Field of view (arcsec)            fov                                 s_fov                              \n",
      "\n",
      "Energy\n",
      "  Frequency (GHz)                   frequency                           frequency                          \n",
      "  Bandwidth (GHz)                   bandwidth                           bandwidth                          \n",
      "  Spectral resolution (KHz)         spectral_resolution                 em_resolution                      \n",
      "  Band                              band_list                           band_list                          \n",
      "\n",
      "Time\n",
      "  Observation date                  start_date                          t_min                              \n",
      "  Integration time (s)              integration_time                    t_exptime                          \n",
      "\n",
      "Polarization\n",
      "  Polarisation type (Single, Dual, Full) polarisation_type                   pol_states                         \n",
      "\n",
      "Observation\n",
      "  Line sensitivity (10 km/s) (mJy/beam) line_sensitivity                    sensitivity_10kms                  \n",
      "  Continuum sensitivity (mJy/beam)  continuum_sensitivity               cont_sensitivity_bandwidth         \n",
      "  Water vapour (mm)                 water_vapour                        pvw                                \n",
      "\n",
      "Project\n",
      "  Project code                      project_code                        proposal_id                        \n",
      "  Project title                     project_title                       obs_title                          \n",
      "  PI name                           pi_name                             obs_creator_name                   \n",
      "  Proposal authors                  proposal_authors                    proposal_authors                   \n",
      "  Project abstract                  project_abstract                    proposal_abstract                  \n",
      "  Publication count                 publication_count                   NA                                 \n",
      "  Science keyword                   science_keyword                     science_keyword                    \n",
      "\n",
      "Publication\n",
      "  Bibcode                           bibcode                             bib_reference                      \n",
      "  Title                             pub_title                           pub_title                          \n",
      "  First author                      first_author                        first_author                       \n",
      "  Authors                           authors                             authors                            \n",
      "  Abstract                          pub_abstract                        pub_abstract                       \n",
      "  Year                              publication_year                    pub_year                           \n",
      "\n",
      "Options\n",
      "  Public data only                  public_data                         data_rights                        \n",
      "  Science observations only         science_observation                 science_observation                \n",
      "\n",
      "Examples of queries:\n",
      "Alma.query('proposal_id':'2011.0.00131.S'}\n",
      "Alma.query({'band_list': ['5', '7']}\n",
      "Alma.query({'source_name_alma': 'GRB021004'})\n",
      "Alma.query(payload=dict(project_code='2017.1.01355.L', source_name_alma='G008.67'))\n"
     ]
    }
   ],
   "source": [
    "Alma.help()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----------------------------------------\n",
    "\n",
    "## Example 8a: Query one object by a resolved source name and then by coordinates using TAP\n",
    "\n",
    "This is equivalent to the queries we show in notebook 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-11-16T10:27:24.541912Z",
     "iopub.status.busy": "2021-11-16T10:27:24.541312Z",
     "iopub.status.idle": "2021-11-16T10:27:26.115316Z",
     "shell.execute_reply": "2021-11-16T10:27:26.116003Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>access_url</th>\n",
       "      <th>access_format</th>\n",
       "      <th>proposal_id</th>\n",
       "      <th>data_rights</th>\n",
       "      <th>gal_longitude</th>\n",
       "      <th>gal_latitude</th>\n",
       "      <th>obs_publisher_did</th>\n",
       "      <th>obs_collection</th>\n",
       "      <th>facility_name</th>\n",
       "      <th>instrument_name</th>\n",
       "      <th>...</th>\n",
       "      <th>frequency</th>\n",
       "      <th>velocity_resolution</th>\n",
       "      <th>obs_creator_name</th>\n",
       "      <th>pub_title</th>\n",
       "      <th>first_author</th>\n",
       "      <th>qa2_passed</th>\n",
       "      <th>bib_reference</th>\n",
       "      <th>science_keyword</th>\n",
       "      <th>scientific_category</th>\n",
       "      <th>lastModified</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00008.SV</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.512363</td>\n",
       "      <td>19.416239</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00008.SV</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>238.261613</td>\n",
       "      <td>1183.847494</td>\n",
       "      <td>observatory, ALMA</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>Azeez, Jazeel H. Israel, F. P. McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2014A&amp;A...562A..96I 2017AdAst2017E...6A 2017Ap...</td>\n",
       "      <td>Galactic centres/nuclei, Active Galactic Nucle...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00008.SV</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.512363</td>\n",
       "      <td>19.416239</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00008.SV</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>238.261613</td>\n",
       "      <td>1183.847494</td>\n",
       "      <td>observatory, ALMA</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>Azeez, Jazeel H. Israel, F. P. McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2014A&amp;A...562A..96I 2017AdAst2017E...6A 2017Ap...</td>\n",
       "      <td>Galactic centres/nuclei, Active Galactic Nucle...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00008.SV</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.512363</td>\n",
       "      <td>19.416239</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00008.SV</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>238.261613</td>\n",
       "      <td>1183.847494</td>\n",
       "      <td>observatory, ALMA</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>Azeez, Jazeel H. Israel, F. P. McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2014A&amp;A...562A..96I 2017AdAst2017E...6A 2017Ap...</td>\n",
       "      <td>Galactic centres/nuclei, Active Galactic Nucle...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00008.SV</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.512363</td>\n",
       "      <td>19.416239</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00008.SV</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>238.261613</td>\n",
       "      <td>1183.847494</td>\n",
       "      <td>observatory, ALMA</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>Azeez, Jazeel H. Israel, F. P. McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2014A&amp;A...562A..96I 2017AdAst2017E...6A 2017Ap...</td>\n",
       "      <td>Galactic centres/nuclei, Active Galactic Nucle...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00010.S</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.515914</td>\n",
       "      <td>19.417224</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00010.S</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>219.025328</td>\n",
       "      <td>663.940713</td>\n",
       "      <td>Ott, Juergen</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2017ApJ...851...76M</td>\n",
       "      <td>Active Galactic Nuclei (AGN)/Quasars (QSO), Me...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 63 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          access_url access_format  \\\n",
       "0  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "1  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "2  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "3  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "4  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "\n",
       "       proposal_id data_rights  gal_longitude  gal_latitude  \\\n",
       "0  2011.0.00008.SV      Public     309.512363     19.416239   \n",
       "1  2011.0.00008.SV      Public     309.512363     19.416239   \n",
       "2  2011.0.00008.SV      Public     309.512363     19.416239   \n",
       "3  2011.0.00008.SV      Public     309.512363     19.416239   \n",
       "4   2011.0.00010.S      Public     309.515914     19.417224   \n",
       "\n",
       "              obs_publisher_did obs_collection facility_name instrument_name  \\\n",
       "0  ADS/JAO.ALMA#2011.0.00008.SV           ALMA           JAO            ALMA   \n",
       "1  ADS/JAO.ALMA#2011.0.00008.SV           ALMA           JAO            ALMA   \n",
       "2  ADS/JAO.ALMA#2011.0.00008.SV           ALMA           JAO            ALMA   \n",
       "3  ADS/JAO.ALMA#2011.0.00008.SV           ALMA           JAO            ALMA   \n",
       "4   ADS/JAO.ALMA#2011.0.00010.S           ALMA           JAO            ALMA   \n",
       "\n",
       "   ...   frequency velocity_resolution   obs_creator_name  \\\n",
       "0  ...  238.261613         1183.847494  observatory, ALMA   \n",
       "1  ...  238.261613         1183.847494  observatory, ALMA   \n",
       "2  ...  238.261613         1183.847494  observatory, ALMA   \n",
       "3  ...  238.261613         1183.847494  observatory, ALMA   \n",
       "4  ...  219.025328          663.940713       Ott, Juergen   \n",
       "\n",
       "                                           pub_title  \\\n",
       "0  ALMA Observations of the Physical and Chemical...   \n",
       "1  ALMA Observations of the Physical and Chemical...   \n",
       "2  ALMA Observations of the Physical and Chemical...   \n",
       "3  ALMA Observations of the Physical and Chemical...   \n",
       "4  ALMA Observations of the Physical and Chemical...   \n",
       "\n",
       "                                 first_author  qa2_passed  \\\n",
       "0  Azeez, Jazeel H. Israel, F. P. McCoy, Mark           T   \n",
       "1  Azeez, Jazeel H. Israel, F. P. McCoy, Mark           T   \n",
       "2  Azeez, Jazeel H. Israel, F. P. McCoy, Mark           T   \n",
       "3  Azeez, Jazeel H. Israel, F. P. McCoy, Mark           T   \n",
       "4                                 McCoy, Mark           T   \n",
       "\n",
       "                                       bib_reference  \\\n",
       "0  2014A&A...562A..96I 2017AdAst2017E...6A 2017Ap...   \n",
       "1  2014A&A...562A..96I 2017AdAst2017E...6A 2017Ap...   \n",
       "2  2014A&A...562A..96I 2017AdAst2017E...6A 2017Ap...   \n",
       "3  2014A&A...562A..96I 2017AdAst2017E...6A 2017Ap...   \n",
       "4                                2017ApJ...851...76M   \n",
       "\n",
       "                                     science_keyword  scientific_category  \\\n",
       "0  Galactic centres/nuclei, Active Galactic Nucle...      Active galaxies   \n",
       "1  Galactic centres/nuclei, Active Galactic Nucle...      Active galaxies   \n",
       "2  Galactic centres/nuclei, Active Galactic Nucle...      Active galaxies   \n",
       "3  Galactic centres/nuclei, Active Galactic Nucle...      Active galaxies   \n",
       "4  Active Galactic Nuclei (AGN)/Quasars (QSO), Me...      Active galaxies   \n",
       "\n",
       "              lastModified  \n",
       "0  2021-09-30T16:34:41.133  \n",
       "1  2021-09-30T16:34:41.133  \n",
       "2  2021-09-30T16:34:41.133  \n",
       "3  2021-09-30T16:34:41.133  \n",
       "4  2021-09-30T16:34:41.133  \n",
       "\n",
       "[5 rows x 63 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coordinates = astropy.coordinates.SkyCoord.from_name(\"Cen A\")\n",
    "output = Alma.query_tap(f\"SELECT * FROM ivoa.obscore WHERE INTERSECTS(CIRCLE('ICRS',{coordinates.ra.degree},{coordinates.dec.degree},0.16),s_region)=1\").to_table().to_pandas()\n",
    "output.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Print all the target names associated with these coordinates:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-11-16T10:27:26.123386Z",
     "iopub.status.busy": "2021-11-16T10:27:26.122459Z",
     "iopub.status.idle": "2021-11-16T10:27:26.126525Z",
     "shell.execute_reply": "2021-11-16T10:27:26.125922Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['1322-427', '3FGL_J1325.4-4301', 'CenA', 'Cen_A', 'Centaurus A',\n",
       "       'Centaurus_A', 'Centaurus_a', 'J1325-430', 'J1325-4301',\n",
       "       'NGC_5128'], dtype=object)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(output['target_name']) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-------------------------------------------\n",
    "\n",
    "## Example 8b: Query one object by the ALMA source name, using the standard astroquery\n",
    "\n",
    "As in notebook 1, querying by source name should use the name resolver, e.g. Example 8a. Only in very special occasions it can be useful to query for the source name that the PI has put into their proposal. Many sources have a large variety of different names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-11-16T10:27:26.132897Z",
     "iopub.status.busy": "2021-11-16T10:27:26.130646Z",
     "iopub.status.idle": "2021-11-16T10:27:26.311305Z",
     "shell.execute_reply": "2021-11-16T10:27:26.312106Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>access_url</th>\n",
       "      <th>access_format</th>\n",
       "      <th>proposal_id</th>\n",
       "      <th>data_rights</th>\n",
       "      <th>gal_longitude</th>\n",
       "      <th>gal_latitude</th>\n",
       "      <th>obs_publisher_did</th>\n",
       "      <th>obs_collection</th>\n",
       "      <th>facility_name</th>\n",
       "      <th>instrument_name</th>\n",
       "      <th>...</th>\n",
       "      <th>frequency</th>\n",
       "      <th>velocity_resolution</th>\n",
       "      <th>obs_creator_name</th>\n",
       "      <th>pub_title</th>\n",
       "      <th>first_author</th>\n",
       "      <th>qa2_passed</th>\n",
       "      <th>bib_reference</th>\n",
       "      <th>science_keyword</th>\n",
       "      <th>scientific_category</th>\n",
       "      <th>lastModified</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00010.S</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.515914</td>\n",
       "      <td>19.417224</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00010.S</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>111.417365</td>\n",
       "      <td>322.835911</td>\n",
       "      <td>Ott, Juergen</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2017ApJ...851...76M</td>\n",
       "      <td>Active Galactic Nuclei (AGN)/Quasars (QSO), Me...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00010.S</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.515914</td>\n",
       "      <td>19.417224</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00010.S</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>111.417365</td>\n",
       "      <td>322.835911</td>\n",
       "      <td>Ott, Juergen</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2017ApJ...851...76M</td>\n",
       "      <td>Active Galactic Nuclei (AGN)/Quasars (QSO), Me...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00010.S</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.515914</td>\n",
       "      <td>19.417224</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00010.S</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>111.417365</td>\n",
       "      <td>322.835911</td>\n",
       "      <td>Ott, Juergen</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2017ApJ...851...76M</td>\n",
       "      <td>Active Galactic Nuclei (AGN)/Quasars (QSO), Me...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00010.S</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.515914</td>\n",
       "      <td>19.417224</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00010.S</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>111.417365</td>\n",
       "      <td>322.835911</td>\n",
       "      <td>Ott, Juergen</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2017ApJ...851...76M</td>\n",
       "      <td>Active Galactic Nuclei (AGN)/Quasars (QSO), Me...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>http://almascience.org/aq?member_ous_id=uid://...</td>\n",
       "      <td>text/html</td>\n",
       "      <td>2011.0.00010.S</td>\n",
       "      <td>Public</td>\n",
       "      <td>309.515914</td>\n",
       "      <td>19.417224</td>\n",
       "      <td>ADS/JAO.ALMA#2011.0.00010.S</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>JAO</td>\n",
       "      <td>ALMA</td>\n",
       "      <td>...</td>\n",
       "      <td>92.655678</td>\n",
       "      <td>368.331902</td>\n",
       "      <td>Ott, Juergen</td>\n",
       "      <td>ALMA Observations of the Physical and Chemical...</td>\n",
       "      <td>McCoy, Mark</td>\n",
       "      <td>T</td>\n",
       "      <td>2017ApJ...851...76M</td>\n",
       "      <td>Active Galactic Nuclei (AGN)/Quasars (QSO), Me...</td>\n",
       "      <td>Active galaxies</td>\n",
       "      <td>2021-09-30T16:34:41.133</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 63 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          access_url access_format  \\\n",
       "0  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "1  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "2  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "3  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "4  http://almascience.org/aq?member_ous_id=uid://...     text/html   \n",
       "\n",
       "      proposal_id data_rights  gal_longitude  gal_latitude  \\\n",
       "0  2011.0.00010.S      Public     309.515914     19.417224   \n",
       "1  2011.0.00010.S      Public     309.515914     19.417224   \n",
       "2  2011.0.00010.S      Public     309.515914     19.417224   \n",
       "3  2011.0.00010.S      Public     309.515914     19.417224   \n",
       "4  2011.0.00010.S      Public     309.515914     19.417224   \n",
       "\n",
       "             obs_publisher_did obs_collection facility_name instrument_name  \\\n",
       "0  ADS/JAO.ALMA#2011.0.00010.S           ALMA           JAO            ALMA   \n",
       "1  ADS/JAO.ALMA#2011.0.00010.S           ALMA           JAO            ALMA   \n",
       "2  ADS/JAO.ALMA#2011.0.00010.S           ALMA           JAO            ALMA   \n",
       "3  ADS/JAO.ALMA#2011.0.00010.S           ALMA           JAO            ALMA   \n",
       "4  ADS/JAO.ALMA#2011.0.00010.S           ALMA           JAO            ALMA   \n",
       "\n",
       "   ...   frequency velocity_resolution  obs_creator_name  \\\n",
       "0  ...  111.417365          322.835911      Ott, Juergen   \n",
       "1  ...  111.417365          322.835911      Ott, Juergen   \n",
       "2  ...  111.417365          322.835911      Ott, Juergen   \n",
       "3  ...  111.417365          322.835911      Ott, Juergen   \n",
       "4  ...   92.655678          368.331902      Ott, Juergen   \n",
       "\n",
       "                                           pub_title  first_author  \\\n",
       "0  ALMA Observations of the Physical and Chemical...   McCoy, Mark   \n",
       "1  ALMA Observations of the Physical and Chemical...   McCoy, Mark   \n",
       "2  ALMA Observations of the Physical and Chemical...   McCoy, Mark   \n",
       "3  ALMA Observations of the Physical and Chemical...   McCoy, Mark   \n",
       "4  ALMA Observations of the Physical and Chemical...   McCoy, Mark   \n",
       "\n",
       "   qa2_passed        bib_reference  \\\n",
       "0           T  2017ApJ...851...76M   \n",
       "1           T  2017ApJ...851...76M   \n",
       "2           T  2017ApJ...851...76M   \n",
       "3           T  2017ApJ...851...76M   \n",
       "4           T  2017ApJ...851...76M   \n",
       "\n",
       "                                     science_keyword  scientific_category  \\\n",
       "0  Active Galactic Nuclei (AGN)/Quasars (QSO), Me...      Active galaxies   \n",
       "1  Active Galactic Nuclei (AGN)/Quasars (QSO), Me...      Active galaxies   \n",
       "2  Active Galactic Nuclei (AGN)/Quasars (QSO), Me...      Active galaxies   \n",
       "3  Active Galactic Nuclei (AGN)/Quasars (QSO), Me...      Active galaxies   \n",
       "4  Active Galactic Nuclei (AGN)/Quasars (QSO), Me...      Active galaxies   \n",
       "\n",
       "              lastModified  \n",
       "0  2021-09-30T16:34:41.133  \n",
       "1  2021-09-30T16:34:41.133  \n",
       "2  2021-09-30T16:34:41.133  \n",
       "3  2021-09-30T16:34:41.133  \n",
       "4  2021-09-30T16:34:41.133  \n",
       "\n",
       "[5 rows x 63 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output = Alma.query({'source_name_alma': 'CenA'}).to_pandas()\n",
    "output.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-------------------------------------------\n",
    "\n",
    "## Example 8c: Query one object by resolved ALMA source name, using TAP through astroquery\n",
    "\n",
    "Similar to notebook 1, TAP can also be used for the queries through Astroquery. In SQL, the % is the special character for \"any string\", equivalent to * in the shell. Similarly, in SQL the _ is the special character for \"any character\", equivalent to ? in the shell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-11-16T10:27:26.319936Z",
     "iopub.status.busy": "2021-11-16T10:27:26.319310Z",
     "iopub.status.idle": "2021-11-16T10:27:28.110183Z",
     "shell.execute_reply": "2021-11-16T10:27:28.110664Z"
    }
   },
   "outputs": [],
   "source": [
    "output = Alma.query_tap(f\"SELECT * FROM ivoa.obscore WHERE target_name like '%Cen%A%'\").to_table().to_pandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The variations of the source name which include 'Cen' and 'A' are the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-11-16T10:27:28.116214Z",
     "iopub.status.busy": "2021-11-16T10:27:28.115578Z",
     "iopub.status.idle": "2021-11-16T10:27:28.118332Z",
     "shell.execute_reply": "2021-11-16T10:27:28.119090Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Cen A - Xray N5', 'CenA', 'CenA - CO knot S1', 'CenA - Xray N1',\n",
       "       'CenA - Xray N2', 'CenA - Xray N3', 'CenA - Xray N4', 'Cen_A',\n",
       "       'Centaurus A', 'Centaurus_A'], dtype=object)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(output['target_name']) "
   ]
  }
 ],
 "metadata": {
  "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.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
