{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook originally made by Matt Nicholl.
\n",
"Interesting transients are selected from the ZTF stream, then these are looked up in various ways:
\n",
" __[The Transient Name Server](https://wis-tns.weizmann.ac.il/)__
\n",
" __[PanSTARRS-1 Image Access](http://ps1images.stsci.edu/cgi-bin/ps1cutouts)__
\n",
" __[Open Astronomy Catalogs](https://astrocats.space/)__
\n",
" At the end, you see the chosen candidates with light curves from ZTF and OAC, as well as the PanStarrs cutouts."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"import mysql.connector\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import requests\n",
"import json\n",
"from collections import OrderedDict\n",
"import matplotlib.image as mpimg\n",
"import os\n",
"import csv\n",
"import io as StringIO"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"# connect to database. You should already have the settings.py file.\n",
"from ztf import settings\n",
"msl = mysql.connector.connect(\\\n",
" user =settings.DB_USER, \\\n",
" password=settings.DB_PASS, \\\n",
" host =settings.DB_HOST, \\\n",
" database='ztf')"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This notebook was run on 2019-11-27T14:09:08.386724\n",
"and the Julian Date is 2458815.089680409\n"
]
}
],
"source": [
"import time, datetime\n",
"now = datetime.datetime.now()\n",
"print (\"This notebook was run on \" + now.isoformat())\n",
"\n",
"jdnow = time.time()/86400 + 2440587.5\n",
"print (\"and the Julian Date is {}\".format(jdnow))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Object query is:\n",
"SELECT objectId, ra, decl, jd, magpsf, sigmapsf, fid, distpsnr1, sgscore1, sgmag1, srmag1 FROM candidates where objectId IN (SELECT objectId from objects WHERE ncandgp > 10 AND sgscore1 < 0.4 and jdmin > 2458785.1)\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Object query is:\\n\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mquery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcursor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mquery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 24\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcursor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrowcount\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'\\nfound %d candidates'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/mysql/connector/cursor.py\u001b[0m in \u001b[0;36mexecute\u001b[0;34m(self, operation, params, multi)\u001b[0m\n\u001b[1;32m 567\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 568\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 569\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_connection\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcmd_query\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstmt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 570\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mInterfaceError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 571\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_connection\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_have_next_result\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint: disable=W0212\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/mysql/connector/connection.py\u001b[0m in \u001b[0;36mcmd_query\u001b[0;34m(self, query, raw, buffered, raw_as_string)\u001b[0m\n\u001b[1;32m 551\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mquery\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbytes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 552\u001b[0m \u001b[0mquery\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mquery\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'utf-8'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 553\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_send_cmd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mServerCmd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mQUERY\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mquery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 555\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_have_next_result\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/mysql/connector/connection.py\u001b[0m in \u001b[0;36m_send_cmd\u001b[0;34m(self, command, argument, packet_number, packet, expect_response, compressed_packet_number)\u001b[0m\n\u001b[1;32m 312\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mexpect_response\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 313\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 314\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_socket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 315\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 316\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_send_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata_file\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msend_empty_packet\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/mysql/connector/network.py\u001b[0m in \u001b[0;36mrecv_plain\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 247\u001b[0m \u001b[0mpacket_len\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 248\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mpacket_len\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 249\u001b[0;31m \u001b[0mchunk\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mpacket_len\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 250\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mchunk\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 251\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mInterfaceError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merrno\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2013\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.7/ssl.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, buflen, flags)\u001b[0m\n\u001b[1;32m 1035\u001b[0m \u001b[0;34m\"non-zero flags not allowed in calls to recv() on %s\"\u001b[0m \u001b[0;34m%\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1036\u001b[0m self.__class__)\n\u001b[0;32m-> 1037\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbuflen\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1038\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1039\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbuflen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.7/ssl.py\u001b[0m in \u001b[0;36mread\u001b[0;34m(self, len, buffer)\u001b[0m\n\u001b[1;32m 911\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sslobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbuffer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 912\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 913\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sslobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 914\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mSSLError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 915\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mSSL_ERROR_EOF\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msuppress_ragged_eofs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# select good candidates\n",
"# can add further criteria...\n",
"\n",
"# From the last 3 months\n",
"start_date = '%.1f' % (jdnow - 30)\n",
"\n",
"# Further cuts on individual detections\n",
"cand_of_interest = 'sgscore1 < 0.4 and jdmin > ' + start_date\n",
"\n",
"cursor = msl.cursor(buffered=True, dictionary=True)\n",
"\n",
"# First query candidate database...\n",
"query_obj = 'SELECT objectId from objects WHERE '\n",
"query_obj += 'ncandgp > 10 AND '\n",
"query_obj += cand_of_interest\n",
"# ... making sure associated objects also pass cuts\n",
"\n",
"query = 'SELECT objectId, ra, decl, jd, magpsf, sigmapsf, fid, distpsnr1, sgscore1, sgmag1, srmag1 '\n",
"query += 'FROM candidates where objectId IN (%s)' % query_obj\n",
"\n",
"print (\"Object query is:\\n\" + query)\n",
"\n",
"results = cursor.execute(query)\n",
"n = cursor.rowcount\n",
"print ('\\nfound %d candidates' % n)\n",
"candidates = cursor.fetchall()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Make dictionary of objects with light curves and metadata\n",
"\n",
"# needed for light curve\n",
"lc_keys = ['jd', 'magpsf', 'sigmapsf', 'fid']\n",
"\n",
"# attibutes of the nearest PS1 object\n",
"host_keys = ['distpsnr1', 'sgscore1', 'sgmag1', 'srmag1'] \n",
"\n",
"objectdict = {}\n",
"\n",
"for row in candidates:\n",
" objectId = row['objectId']\n",
" if objectId in objectdict:\n",
" continue\n",
" objectdict[objectId] = row\n",
" objectdict[objectId]['lc'] = []\n",
" \n",
"for j in candidates:\n",
" lightcurvedict = dict((l, j[l]) for l in lc_keys if l in j)\n",
" objectdict[j['objectId']]['lc'].append(lightcurvedict)\n",
" if 'host' not in objectdict[j['objectId']]:\n",
" hostdict = dict((k, j[k]) for k in host_keys if k in j)\n",
" objectdict[j['objectId']]['host'] = hostdict"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Apply further filters here\n",
"\n",
"# If no filtering desired, simply set:\n",
"#objectdict = objectdict\n",
"\n",
"# For this example, using objects with at least 10 detections\n",
"minpoints = 6\n",
"\n",
"targetlist = []\n",
"\n",
"for l in objectdict:\n",
" if len(objectdict[l]['lc']) >= minpoints:\n",
" targetlist.append(l)\n",
" \n",
"print('found %d events with >=%d detections' % (len(targetlist),minpoints))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Query the Transient Name Server"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"# Query TNS for classifications\n",
"\n",
"tns_url = \"https://wis-tns.weizmann.ac.il/api/get/\"\n",
"api_key=\"9a8a461eb820b886d28d412290223ad6dc971ebc\"\n",
"\n",
"print('Searching TNS...')\n",
"print (\"\\nZTF objectId TNS id Type Sherlock\")\n",
"for i in targetlist:\n",
"\n",
" # start with cone search around ZTF coordinates\n",
" search_obj = OrderedDict([\n",
" (\"ra\",objectdict[i]['ra']), \n",
" (\"dec\",objectdict[i]['decl']), \n",
" (\"radius\",\"2.5\"), \n",
" (\"units\",\"arcsec\"), \n",
" (\"objname\",\"\"), \n",
" (\"internal_name\",\"\")]) \n",
" search_url = tns_url+'search'\n",
" search_data = [('api_key',(None, api_key)), ('data',(None,json.dumps(search_obj)))]\n",
"\n",
" r = requests.post(search_url, files=search_data)\n",
"\n",
" # If transient is known, will have an IAU name (AT/SN 20XXyy), add such names to our objects\n",
" if r.json()['data']['reply']:\n",
" iau_name = r.json()['data']['reply'][0]['objname']\n",
" objectdict[i]['iau_name'] = r.json()['data']['reply'][0]['prefix'] + iau_name\n",
"\n",
"\n",
" # Now check if object has a classification attached\n",
" # Always will for 'SN' names, but could also happen for 'AT', e.g. if TDE rather than SN\n",
" get_obj = OrderedDict([(\"objname\",iau_name), (\"photometry\",\"0\"), (\"spectra\",\"0\")])\n",
" get_url = tns_url+'object'\n",
" get_data = [('api_key',(None, api_key)), ('data',(None,json.dumps(get_obj)))]\n",
" \n",
" r2 = requests.post(get_url, files=get_data)\n",
" \n",
" # If classified, add type to dictionary\n",
" if r2.json()['data']['reply']['object_type']['name']:\n",
" objectdict[i]['class'] = r2.json()['data']['reply']['object_type']['name']\n",
" t = objectdict[i]['class']\n",
" else:\n",
" t = ' '\n",
"\n",
"\n",
" print (\"%s %s %s %s\" % (i, objectdict[i]['iau_name'], t,objectdict[i]['sherlock_classification']))\n",
"\n",
"print('\\nDone.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Apply more cuts based on TNS data\n",
"\n",
"targetlist2 = []\n",
"\n",
"# e.g. classified transients only, excluding SNe Ia:\n",
"for i in targetlist:\n",
" if 'class' in objectdict[i]:\n",
" if objectdict[i]['class'] != 'SN Ia':\n",
" targetlist2.append(i)\n",
" \n",
"print('%d events passing final cuts' % len(targetlist2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get PS1 image of field\n",
"\n",
"ps1_url = 'http://ps1images.stsci.edu/cgi-bin/ps1filenames.py?&filters=gri'\n",
"\n",
"cutout_url = 'http://ps1images.stsci.edu/cgi-bin/fitscut.cgi?&filetypes=stack&size=240'\n",
"\n",
"print('Downloading PS1 cutouts...')\n",
"\n",
"for i in targetlist2:\n",
"\n",
" dest_file = i + '.jpg'\n",
" objectdict[i]['cutout'] = dest_file\n",
"\n",
" if not os.path.exists(dest_file):\n",
"\n",
" # first step searches for the ps1 images matching target coordinates\n",
" ps1_search_url = ps1_url+'&ra='+str(objectdict[i]['ramean'])+'&dec='+str(objectdict[i]['decmean'])\n",
" print(i,objectdict[i]['ramean'],objectdict[i]['decmean'])\n",
" print(ps1_search_url)\n",
" ps1_im = requests.get(ps1_search_url)\n",
" #print(\"PS1 im.text = \")\n",
" #print(ps1_im.text)\n",
" \n",
" # consistent formatting, hardcoding positions of g,r,i image names within query result\n",
" #ps1_g = ps1_im.text.split(' ')[15]\n",
" #ps1_r = ps1_im.text.split(' ')[31]\n",
" #ps1_i = ps1_im.text.split(' ')[23]\n",
" #print(ps1_g,ps1_r,ps1_i)\n",
"\n",
" \n",
" # 2019-06-16 KWS Modified this to use a csv and StringIO object and present a dictionary.\n",
" response = csv.DictReader(StringIO.StringIO(ps1_im.text), delimiter=' ', skipinitialspace = True)\n",
" respList = []\n",
" \n",
" for row in response:\n",
" respList.append(row)\n",
" \n",
" ps1_g = respList[0]['filename']\n",
" ps1_r = respList[2]['filename']\n",
" ps1_i = respList[1]['filename']\n",
" #print(ps1_g,ps1_r,ps1_i)\n",
"\n",
" \n",
" cutout_search_url = cutout_url + '&blue='+ps1_g + '&green='+ps1_r + '&red='+ps1_i\n",
" cutout_search_url += '&ra='+str(objectdict[i]['ramean']) + '&dec='+str(objectdict[i]['decmean'])\n",
"\n",
" cmd = 'wget -O %s \"%s\"' % (dest_file, cutout_search_url)\n",
" #print(cmd)\n",
" os.system(cmd)\n",
" \n",
"print('Done.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Get PanSTARRS image cutouts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Light curves from Open Astronomy Catalogs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# If known transient, get data from Open Astronomy Catalogs\n",
"\n",
"oac_url = 'https://api.astrocats.space/' # object name goes here\n",
"oac_query_string = '/photometry/time+magnitude+e_magnitude+band?time&magnitude'\n",
"\n",
"for i in targetlist2:\n",
" if 'iau_name' in objectdict[i]:\n",
" query_url = oac_url + objectdict[i]['iau_name'] + oac_query_string\n",
" oac_phot = requests.get(query_url)\n",
" d = oac_phot.json()\n",
" objectname = objectdict[i]['iau_name']\n",
" if objectname in d:\n",
" objectdict[i]['oac'] = d[objectname]['photometry']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"# Plot light curves\n",
"\n",
"ztf_filts = {1:'g', 2:'r'}\n",
"ztf_markers = {1:'s', 2:'D'}\n",
"\n",
"# if more filters from other surveys, add to colour pallet here:\n",
"other_filts = {'orange':'orange', 'cyan':'c', 'G':'darkkhaki', 'C':'pink',\n",
" 'w':'m', 'g':'g', 'r':'r', 'i':'goldenrod', 'z':'k'}\n",
"\n",
"n = 1\n",
"\n",
"for i in targetlist2:\n",
" plt.figure(n,(14,6)) \n",
" plt.subplot(121)\n",
" for j in objectdict[i]['lc']:\n",
" plt.errorbar(j['jd']-2400000.5,j['magpsf'],j['sigmapsf'],markersize=6,\n",
" fmt=ztf_markers[j['fid']],color=ztf_filts[j['fid']],label='ZTF '+ztf_filts[j['fid']])\n",
"\n",
" # include photometry from open catalog:\n",
" if 'oac' in objectdict[i]:\n",
" for k in objectdict[i]['oac']:\n",
" try:\n",
" plt.errorbar(k[0],k[1],k[2],fmt='o',markersize=4,color=other_filts[k[3]],label=k[3])\n",
" except:\n",
" try:\n",
" plt.errorbar(k[0],k[1],fmt='o',markersize=4,color=other_filts[k[3]],label=k[3])\n",
" except:\n",
" plt.errorbar(k[0],k[1],fmt='o',markersize=4,color='0.7',label='unknown')\n",
"\n",
" # construct legend\n",
" handles, labels = plt.gca().get_legend_handles_labels()\n",
" by_label = OrderedDict(zip(labels, handles))\n",
" plt.legend(by_label.values(), by_label.keys())\n",
"\n",
" # format figure\n",
" plt.xlabel('MJD')\n",
" plt.ylabel('mag')\n",
" plt.gca().invert_yaxis()\n",
" figtitle = i\n",
" if 'iau_name' in objectdict[i]:\n",
" figtitle += ' = '+objectdict[i]['iau_name']\n",
" if 'class' in objectdict[i]:\n",
" figtitle += ' ('+objectdict[i]['class']+')'\n",
" plt.title(figtitle)\n",
" \n",
" # show cutout\n",
" if 'cutout' in objectdict[i]:\n",
" if os.path.exists(objectdict[i]['cutout']):\n",
" plt.subplot(122)\n",
" file = objectdict[i]['cutout']\n",
" try:\n",
" img = mpimg.imread(file)\n",
" plt.imshow(img)\n",
" except:\n",
" print(\"cannot find file \", file)\n",
"\n",
" print('https://lasair.roe.ac.uk/object/%s/' % i)\n",
" plt.show()\n",
" \n",
" n += 1\n",
" \n",
"print('Done.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}