{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Accessing NED data via VO services " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The following were tested using:\n", " - OS X 10.10.5\n", " - conda 4.5.4\n", " - python 3.6.4\n", " - ipython 6.2.1\n", "- Modules used in this notebook - run the following to test if you have all the required modules installed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "import requests\n", "import urllib.parse\n", "import io\n", "import matplotlib.pyplot as plt\n", "from astropy.table import Table\n", "\n", "# VO TAP queries via astroquery:\n", "from astroquery.utils.tap.core import TapPlus\n", "\n", "# VO TAP queries via pyvo:\n", "import pyvo as vo\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Last tested on: 15 June 2018\n", "- Additional information see: [link to docs](https://ned.ipac.caltech.edu)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contents:\n", "\n", "(1) Object Lookup - Find basic object information using object name\n", "\n", "(2) TAP - Cone Search\n", "\n", "(3) TAP - Polygon Search\n", "\n", "(4) TAP - Polygon Search with Redshift Constraint\n", "\n", "(5) SED Plot - Fetch photometry data for a given list of object names and plot the SED" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (1) Object Lookup - Find basic object information using object name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Use NED Object Lookup api to fetch basic object information for given object name\n", "# Documentation: http://vo.ned.ipac.caltech.edu/ui/Documents/ObjectLookup\n", "\n", "import json\n", "import requests\n", "import urllib.parse\n", "\n", "object_name = 'cyg a'\n", "encoded_name = urllib.parse.quote_plus(object_name) # need to encode special characters in object name \n", "\n", "NED_object_lookup = \"http://ned.ipac.caltech.edu/srs/ObjectLookup?\"\n", "object_name_packet = 'json=' + json.dumps({\"name\":{\"v\":encoded_name}}, separators=(',',':'))\n", "NED_object_lookup_response = requests.post(NED_object_lookup, data = object_name_packet)\n", "if NED_object_lookup_response.status_code == 200:\n", " ned_object_basic_info = json.loads(NED_object_lookup_response.content)\n", " print(json.dumps(ned_object_basic_info, sort_keys=True,indent=4, separators=(',', ': ')))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (2) TAP - Cone Search" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using NED TAP service with previously retrieved NED object position to perform 0.2 arcmin cone search\n", "# Documentation: https://ned.ipac.caltech.edu/tap/\n", "\n", "ned_tab = 'https://ned.ipac.caltech.edu/tap'\n", "table_name = 'ned_objdir'\n", "columns = 'prefname,ra,dec,z,zflag'\n", "coord_sys = 'J2000'\n", "coord_ra = 299.8681525 # From previous example - ned_object_basic_info['Preferred']['Position']['RA'] \n", "coord_dec = 40.73391556 # From previous example - ned_object_basic_info['Preferred']['Position']['Dec']\n", "sr_deg = 0.2 / 60 # 0.2 arcmin search radius\n", "cone = 'CONTAINS(POINT(\\'' + str(coord_sys) + '\\', ra, dec),CIRCLE(\\'' + str(coord_sys) + '\\',' + str(coord_ra) + ',' + str(coord_dec) + ',' + str(sr_deg) + ' ))=1'\n", "query = 'SELECT ' + columns + ' FROM ' + table_name + ' WHERE ' + cone\n", "\n", "# Using astroquery\n", "\n", "from astroquery.utils.tap.core import TapPlus\n", "ned = TapPlus(url=ned_tab)\n", "job = ned.launch_job_async(query)\n", "out = job.get_results()\n", "out.pprint()\n", "\n", "# Using PyVO\n", "\n", "import pyvo as vo\n", "ned_TAP = vo.dal.TAPService(ned_tab)\n", "ned_out = ned_TAP.search(query)\n", "ned_out.table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (3) TAP - Polygon Search" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using NED TAP service to perform polygon search for object count arount M51\n", "# Documentation: https://ned.ipac.caltech.edu/tap/\n", "\n", "ned_tab = 'https://ned.ipac.caltech.edu/tap'\n", "table_name = 'ned_objdir'\n", "columns = 'count(*)'\n", "coord_sys = 'J2000'\n", "polygon = 'CONTAINS(POINT(\\'' + str(coord_sys) + '\\',ra,dec),POLYGON(\\'' + str(coord_sys) + '\\',202.333,47.033,202.667,47.033,202.667,47.367,202.333,47.367))=1'\n", "query = 'SELECT ' + columns + ' FROM ' + table_name + ' WHERE ' + polygon\n", "\n", "# Using astroquery\n", "\n", "from astroquery.utils.tap.core import TapPlus\n", "ned = TapPlus(url=ned_tab)\n", "job = ned.launch_job_async(query)\n", "out = job.get_results()\n", "out.pprint()\n", "\n", "# Using PyVO\n", "\n", "import pyvo as vo\n", "ned_TAP = vo.dal.TAPService(ned_tab)\n", "ned_out = ned_TAP.search(query)\n", "ned_out.table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (4) TAP - Polygon Search with redshift constraint" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using NED TAP service to perform polygon search for object count arount M51 for objects within |dcz| = 500 km/s\n", "# Documentation: https://ned.ipac.caltech.edu/tap/\n", "\n", "ned_tab = 'https://ned.ipac.caltech.edu/tap'\n", "table_name = 'ned_objdir'\n", "columns = 'count(*)'\n", "coord_sys = 'J2000'\n", "polygon = 'CONTAINS(POINT(\\'' + str(coord_sys) + '\\',ra,dec),POLYGON(\\'' + str(coord_sys) + '\\',202.333,47.033,202.667,47.033,202.667,47.367,202.333,47.367))=1'\n", "z_range = 'z BETWEEN 0.000333 AND 0.003667'\n", "query = 'SELECT ' + columns + ' FROM ' + table_name + ' WHERE ' + polygon + ' AND ' + z_range\n", "\n", "# Using astroquery\n", "\n", "from astroquery.utils.tap.core import TapPlus\n", "ned = TapPlus(url=ned_tab)\n", "job = ned.launch_job_async(query)\n", "out = job.get_results()\n", "out.pprint()\n", "\n", "# Using PyVO\n", "\n", "import pyvo as vo\n", "ned_TAP = vo.dal.TAPService(ned_tab)\n", "ned_out = ned_TAP.search(query)\n", "ned_out.table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (5) SED Plot - Fetch photometry data for a given list of object names and plot the SED" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using NED SED service \n", "\n", "import io\n", "import requests\n", "import urllib.parse\n", "import matplotlib.pyplot as plt\n", "from astropy.table import Table\n", "\n", "object_names = ('arp220','3c273','BL Lac')\n", "color = ('r', 'b', 'g')\n", "\n", "NED_sed = 'http://vo.ned.ipac.caltech.edu/services/accessSED?'\n", "\n", "plt.figure(figsize=(15,12))\n", "plt.rcParams.update({'font.size': 22})\n", "\n", "i=0\n", "for name in object_names:\n", " paramters = { 'REQUEST':'getData','TARGETNAME':name }\n", " NED_sed_response = requests.get(NED_sed, params = paramters)\n", " if NED_sed_response.status_code == 200:\n", " ned_data_table = Table.read(io.BytesIO(NED_sed_response.content))\n", " x=ned_data_table['DataSpectralValue']\n", " y=ned_data_table['DataFluxValue']\n", " plt.plot(x, y, color[i])\n", " j=-i*15\n", " plt.annotate(name, xy=(x[j],y[j]), xytext=(x[j]*100,y[j]*100),arrowprops=dict(facecolor=color[i], shrink=0.05),color=color[i], fontsize=28)\n", " i+=1\n", "plt.xlim(1e6,1e20)\n", "plt.ylim(1e-9,1e3)\n", "plt.yscale('log')\n", "plt.xscale('log')\n", "plt.show()\n" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }