
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a6eaeebf",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Access astronomical databases\n",
        "from pyvo import registry  # version >=1.6\n",
        "\n",
        "# Moc and HEALPix tools\n",
        "from mocpy import MOC\n",
        "\n",
        "# Coordinates manipulation\n",
        "from astropy.coordinates import SkyCoord\n",
        "\n",
        "# Sky visualization\n",
        "from ipyaladin import Aladin  # version >=0.4.0\n",
        "\n",
        "# For plots\n",
        "import matplotlib.pyplot as plt\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7ad7861f",
      "metadata": {},
      "source": [
        "# Welcome to VizieR example workflow\n",
        "\n",
        "[![Vizier](https://custom-icon-badges.demolab.com/badge/Vizier-gray.svg?logo=vizier&logoColor=orange&logoWidth=20)](https://vizier.cds.unistra.fr/viz-bin/VizieR \"https://vizier.cds.unistra.fr/viz-bin/VizieR\")\n",
        "\n",
        "**Notes:** \n",
        "\n",
        "It is a generic notebook, highlighting what can be done once you chose a catalog. This workflow is suggested by [CDS](https://cdsweb.unistra.fr/) (Strasbourg Astronomical Data Center, house of [VizieR](https://vizier.cds.unistra.fr/viz-bin/VizieR)).\n",
        "\n",
        "The notebook exploits [pyVO](https://pyvo.readthedocs.io/en/latest/), an advanced library  of The [Virtual Observatory](https://ivoa.net/).\n",
        "\n",
        "[Astroquery](https://astroquery.readthedocs.io/en/latest/vizier/vizier.html) (not used here) is a well-documented, user-friendly alternative.\n",
        "\n",
        "--------------------------------------------------------\n",
        "\n",
        "## 1. Setup\n",
        "\n",
        "This example notebook has the following dependencies: \n",
        "\n",
        "**Required**\n",
        "- pyvo : this library facilitates the access to the Virtual Observatory (VO) resources. VizieR is part of the VO.\n",
        "This notebook needs version >=1.4.1\n",
        "**Optional, for visualization**\n",
        "- ipyaladin : this is the Aladin-lite sky viewer, bundled as a jupyter widget. It allows to plot catalogs and multi-order coverages (MOC)\n",
        "- matplotlib : an other option to see catalog points and MOCs\n",
        "\n",
        "## 2. Metadata exploration with the Virtual Observatory registry\n",
        "\n",
        "This part uses [pyvo](https://pyvo.readthedocs.io/en) to connect to the VO registry."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "95888801",
      "metadata": {},
      "outputs": [],
      "source": [
        "# the catalogue name in VizieR\n",
        "CATALOGUE = \"J/A+A/676/A18\""
      ]
    },
    {
      "cell_type": "markdown",
      "id": "26ad3c69",
      "metadata": {},
      "source": [
        "We first retrieve the catalogue information."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6db57e6b",
      "metadata": {},
      "outputs": [],
      "source": [
        "# each resource in the VO has an identifier, called ivoid. For vizier catalogs,\n",
        "# the VO ids can be constructed like this:\n",
        "catalogue_ivoid = f\"ivo://CDS.VizieR/{CATALOGUE}\"\n",
        "# the actual query to the registry\n",
        "voresource = registry.search(ivoid=catalogue_ivoid)[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f3e975e3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# We can print metadata information about the catalogue\n",
        "voresource.describe(verbose=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "bd2a7ae3",
      "metadata": {},
      "source": [
        "We can also inspect in details the `resource` object and access the attributes not provided by the describe method. See for example, the first author of a resource: "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f5a53605",
      "metadata": {},
      "outputs": [],
      "source": [
        "voresource.creators[0]"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5a457510",
      "metadata": {},
      "source": [
        "## 3. Access the tabular data of this catalog\n",
        "\n",
        "We can have a look at the tables available in the catalogue."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "92ebe10f",
      "metadata": {},
      "outputs": [],
      "source": [
        "tables = voresource.get_tables()\n",
        "print(f\"In this catalogue, we have {len(tables)} tables.\")\n",
        "for table_name, table in tables.items():\n",
        "    print(f\"{table_name}: {table.description}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "81514878",
      "metadata": {},
      "outputs": [],
      "source": [
        "# We can also extract the tables names for later use\n",
        "tables_names = list(tables.keys())\n",
        "tables_names"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "eedac527",
      "metadata": {},
      "source": [
        "The actual data can then be accessed using any of the ``access_modes`` of the voresource."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "32db444e",
      "metadata": {},
      "outputs": [],
      "source": [
        "voresource.access_modes()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4a7dd42f",
      "metadata": {},
      "source": [
        "The web access is found by following the ``reference_url``"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "545f9bf8",
      "metadata": {},
      "outputs": [],
      "source": [
        "voresource.reference_url"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "abee6ce7",
      "metadata": {},
      "source": [
        "### 3.1 Execute a SQL/ADQL query\n",
        "\n",
        "The ``tap#aux`` in the ``access_mode`` response indicates that we can also do a SQL/ADQL query for these VizieR tables.\n",
        "\n",
        "On the first table of the catalogue, we execute an <a href='https://www.ivoa.net/documents/latest/ADQL.html'>ADQL</a> query."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3a8b8d60",
      "metadata": {},
      "outputs": [],
      "source": [
        "# get the first table of the catalogue\n",
        "first_table_name = tables_names[0]\n",
        "\n",
        "# execute a synchronous ADQL query\n",
        "tap_service = voresource.get_service(\"tap\")\n",
        "tap_records = tap_service.search(\n",
        "    f'select TOP 10 * from \"{first_table_name}\"',\n",
        ")\n",
        "tap_records"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
