{ "cells": [ { "cell_type": "markdown", "id": "98601fdf-e2df-46c9-b039-710e45aabfdc", "metadata": {}, "source": [ "# Retrieving data from the current widget's view\n", "\n", "So far, we've seen how to send information (tables, MOCs, ...) into the widget. The other way also works! This notebook contains a list of methods to extract diverse information about the current view. However, here are several information about retrieving data from the widget:\n", "\n", "- when the view is modified programmatically (*i.e.* not by clicking on the buttons), the update of its properties is done between cell execution. This means that you'll see a `WidgetNotReadyError` when you try to modify the view and retrieve information about it in the **same** cell. Simply switch the property-reading to the next cell and everything will work as intended!\n", "- if you generate different views of the same `Aladin()` instances -- either by calling `display` multiple times, or by using the `Create new view for cell output` button in Jupyter, the information contained in `wcs` and `fov_xy` will always correspond to the **most recently** created view." ] }, { "cell_type": "code", "execution_count": null, "id": "initial_id", "metadata": {}, "outputs": [], "source": [ "from astropy.coordinates import SkyCoord\n", "import astropy.units as u\n", "from astropy.wcs import WCS\n", "from astroquery.simbad import Simbad\n", "from astroquery.vizier import Vizier\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from ipyaladin import Aladin" ] }, { "cell_type": "code", "execution_count": null, "id": "2e62d34eb8543145", "metadata": {}, "outputs": [], "source": [ "aladin = Aladin(fov=5, height=600, target=\"M31\")\n", "aladin" ] }, { "cell_type": "markdown", "id": "ae0a5496-d621-49ef-a11a-3578c272ce92", "metadata": {}, "source": [ "## Getting the current WCS\n", "\n", "The World Coordinates System (WCS) describes the field of view, the projection, and it's rotation. It is returned as an `astropy.coordinates.WCS` object." ] }, { "cell_type": "code", "execution_count": null, "id": "84153657cb7cd837", "metadata": {}, "outputs": [], "source": [ "aladin.wcs # Recover the current WCS" ] }, { "cell_type": "markdown", "id": "998def1f-3963-405b-8be2-6d4ef4012634", "metadata": {}, "source": [ "If you edit the view either by modifiing the widget through its interface, or programmatically:" ] }, { "cell_type": "code", "execution_count": null, "id": "a63f210b-3a64-4860-8e70-42a4c66378fa", "metadata": {}, "outputs": [], "source": [ "aladin.height = 800\n", "aladin.survey = \"CDS/P/PLANCK/R2/HFI/color\"\n", "aladin.target = \"LMC\"\n", "aladin.frame = \"Galactic\"\n", "aladin.fov = 50" ] }, { "cell_type": "markdown", "id": "9c2221f3-6ecc-46d6-9d53-5dbefa71326d", "metadata": {}, "source": [ "The wcs is updated and you can print its new value in the **next cell**:" ] }, { "cell_type": "code", "execution_count": null, "id": "2ddc9637-b5c3-4412-8435-2302b6d86816", "metadata": {}, "outputs": [], "source": [ "aladin.wcs" ] }, { "cell_type": "markdown", "id": "f5add3a2-be30-488e-86df-426338b98f5d", "metadata": {}, "source": [ "If you try to recover the value in the **same cell**, you'll get a `WidgetCommunicationError` error. This is because the calculation of the WCS is done by Aladin Lite *between* cell executions.\n", "\n", "## Getting the field of view\n", "\n", "The field of view is printed in the bottom left corner of the view. You can grab the two values with:" ] }, { "cell_type": "code", "execution_count": null, "id": "9595ae02388b245a", "metadata": {}, "outputs": [], "source": [ "aladin.fov_xy # Recover the current field of view for the x and y axis" ] }, { "cell_type": "markdown", "id": "5ef087ca59d890ce", "metadata": {}, "source": [ "## Getting the selection\n", "First, load two catalogs:" ] }, { "cell_type": "code", "execution_count": null, "id": "bb48cb19a597e262", "metadata": {}, "outputs": [], "source": [ "m1 = SkyCoord.from_name(\"m1\")\n", "\n", "# if these don't work, do `pip install astroquery -U --pre`\n", "simbad = Simbad.query_region(m1, radius=0.1 * u.deg)\n", "usno = Vizier(catalog=\"I/284/out\", row_limit=-1).query_region(m1, radius=0.05 * u.deg)[\n", " 0\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "0030a996", "metadata": {}, "outputs": [], "source": [ "aladin.add_table(simbad, name=\"simbad\", color=\"purple\", shape=\"circle\", source_size=20)\n", "aladin.add_table(usno, name=\"usno\", color=\"teal\", shape=\"square\", source_size=20)\n", "aladin.target = m1\n", "aladin.fov = 0.3\n", "aladin.survey = \"CDS/P/PanSTARRS/DR1/g\"" ] }, { "cell_type": "markdown", "id": "ab287cf3d87bb5d2", "metadata": {}, "source": [ "After that, select a region in the Aladin Lite widget (you can also trigger a selection with `right click` then `select sources`):" ] }, { "cell_type": "code", "execution_count": null, "id": "3efb33016d863bf7", "metadata": {}, "outputs": [], "source": [ "aladin.selection(\"circle\")" ] }, { "cell_type": "markdown", "id": "4d0fead988d63736", "metadata": {}, "source": [ "Then, you can recover the selection as a list of astropy.Table objects with:" ] }, { "cell_type": "code", "execution_count": null, "id": "cda32891dd654568", "metadata": {}, "outputs": [], "source": [ "aladin.selected_objects" ] }, { "cell_type": "markdown", "id": "c84e856d82dbde63", "metadata": {}, "source": [ "## Getting the view as a fits file\n", "The following method allow you to retrieve the current view as a fits file." ] }, { "cell_type": "code", "execution_count": null, "id": "6b0d3e2131e9faa2", "metadata": {}, "outputs": [], "source": [ "fits = aladin.get_view_as_fits()" ] }, { "cell_type": "code", "execution_count": null, "id": "020d2c7f", "metadata": {}, "outputs": [], "source": [ "fits[0].header" ] }, { "cell_type": "markdown", "id": "36a996b424529c09", "metadata": {}, "source": [ "Display the fits file using matplotlib:" ] }, { "cell_type": "code", "execution_count": null, "id": "8327802bad8e8c87", "metadata": {}, "outputs": [], "source": [ "wcs = WCS(fits[0].header)\n", "\n", "plt.subplot(projection=wcs)\n", "plt.imshow(fits[0].data, cmap=\"binary_r\", norm=\"asinh\", vmin=0.001)" ] }, { "cell_type": "markdown", "id": "c64190a2757b707", "metadata": {}, "source": [ "## Saving the view as an image file\n", "\n", "`save_view_as_image` takes a screenshot of the widget. The first argument is the path where the file should be written, the second is the format (\"png\", \"jpeg\", \"webp\"), and the third is a boolean to indicate if you want to include the Aladin Lite logo in the image. \n", "These data are reflecting the pixels on the screen. For scientific use cases, opt for `get_view_as_fits` that is a bit longer since it retrieves the actual survey from a server." ] }, { "cell_type": "code", "execution_count": null, "id": "85f40dc5e6af3ae1", "metadata": {}, "outputs": [], "source": [ "screenshot_wcs = aladin.wcs\n", "aladin.save_view_as_image(path=\"./crab.png\", image_format=\"png\", with_logo=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "a3ca08a32aaaf3d4", "metadata": {}, "outputs": [], "source": [ "img = plt.imread(\"./crab.png\")\n", "plt.imshow(img)" ] }, { "cell_type": "markdown", "id": "dde7fe44", "metadata": {}, "source": [ "And with the WCS that we stored at the same time than the screenshot" ] }, { "cell_type": "code", "execution_count": null, "id": "89d8f226", "metadata": {}, "outputs": [], "source": [ "img = plt.imread(\"./crab.png\")\n", "plt.subplot(projection=screenshot_wcs)\n", "# in FITS, the lines of pixels are flipped, so we have to mimic this\n", "plt.imshow(np.flipud(img))" ] } ], "metadata": { "nbsphinx": { "execute": "never" } }, "nbformat": 4, "nbformat_minor": 5 }