Using the nexsciTAP Server with the PyVO Client to Access NIRES Data At KOA In Synchronous Mode

The nexsciTAP server

The NASA Exoplanet Science Institute (NExScI) (visit https://nexsci.caltech.edu) has developed a Python-based server to implement an API that complies with the Virtual Observatory (VO) Table Access Protocol(TAP) version 1.1 (September 2019) (http://ivoa.net/documents/TAP/20190927/index.html), a standard recommended by the International Virtual Observatory alliance (IVOA) (http://ivoa.net).

The TAP API enables a rich variety of searches against tabular data, including cone, box or all-sky searches, temporal searches, combinations of spatial searches and temporal searches, searches against instrumental attributes and program attributes.

This tutorial supports demonstrates how to use the PyVO client to perform synchronous TAP-based queries for public raw science and calibation data acquired with the Near-Infrared Echellette Spectrometer (NIRES); these data are hosted at the Keck Observatory Archive (KOA; https://koa.ipac.caltech.edu).

Requirements

This tutorial uses PyVO version 1.1.1, and can be installed from PyPI:

$ pip install --upgrade PyVO

The tutorial requires Python 3.6 (or above), plus the table read and write functions from Astropy. We have tested with Astropy 4.0.1, but any version should work. We recommend using the Ananconda Python distribution.

A note on output records

The number of records returned here may differ from those returned here because new data are released daily.

Version 1.0 (May, 2021)

Set up

In [1]:
from pyvo.dal import tap 
koa = tap.TAPService("https://koa.ipac.caltech.edu/TAP")
import time

import sys
import os
import time

Query by date

In [2]:
sql = "select koaid, filehand from koa_nires where koaid like '%20181218%'"

results= koa.run_sync(sql)
print(results)


# write results to VOTable format
table=results.to_table()
table.write ('./table_ipacascii.vot',format='votable',overwrite=True)
<Table length=142>
        koaid                                 filehand                      
        object                                 object                       
---------------------- -----------------------------------------------------
NI.20181218.00833.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00833.fits
NI.20181218.00884.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00884.fits
NI.20181218.00900.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00900.fits
NI.20181218.00920.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00920.fits
NI.20181218.01053.fits /koadata32/NIRES/20181218/lev0/NI.20181218.01053.fits
NI.20181218.06720.fits /koadata32/NIRES/20181218/lev0/NI.20181218.06720.fits
NI.20181218.38030.fits /koadata32/NIRES/20181218/lev0/NI.20181218.38030.fits
NI.20181218.38072.fits /koadata32/NIRES/20181218/lev0/NI.20181218.38072.fits
NI.20181218.38931.fits /koadata32/NIRES/20181218/lev0/NI.20181218.38931.fits
NI.20181218.41511.fits /koadata32/NIRES/20181218/lev0/NI.20181218.41511.fits
                   ...                                                   ...
NR.20181218.58232.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58232.fits
NR.20181218.58315.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58315.fits
NR.20181218.58369.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58369.fits
NR.20181218.58431.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58431.fits
NR.20181218.58486.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58486.fits
NR.20181218.59684.fits /koadata32/NIRES/20181218/lev0/NR.20181218.59684.fits
NR.20181218.61034.fits /koadata32/NIRES/20181218/lev0/NR.20181218.61034.fits
NR.20181218.61377.fits /koadata32/NIRES/20181218/lev0/NR.20181218.61377.fits
NR.20181218.61713.fits /koadata32/NIRES/20181218/lev0/NR.20181218.61713.fits
NR.20181218.62049.fits /koadata32/NIRES/20181218/lev0/NR.20181218.62049.fits

PyVO by default returns results in VOTable format. Convert them to IPAC ASCII and CSV format.

In [3]:
# write results to an IPAC ASCII table
sql = "select koaid, filehand from koa_nires where koaid like '%20181218%' "
results = koa.run_sync(sql)
table=results.to_table()
table.write ('./table_ipacascii.tbl',format='ascii.ipac',overwrite=True)
In [4]:
#  write results to CSV file
sql = "select koaid, filehand from koa_nires where koaid like '%20181218%' "
results = koa.run_sync(sql)
table=results.to_table()
table.write ('./table_csv.csv',format='csv', overwrite=True)

Select all keywords for data acquired on UT Date

In [5]:
sql = "select * from koa_nires where koaid like '%20181218%' "
results = koa.run_sync(sql)
print (results)

## write to an output file
table_selectall=results.to_table()
table_selectall.write ('./table_selectall.vot',format='votable',overwrite=True)
<Table length=142>
        koaid              airmass      ...          z           spt_ind 
        object             float64      ...       float64         int32  
---------------------- ---------------- ... ------------------- ---------
NI.20181218.00833.fits  2.5499223339225 ... -0.0018423685161384 132000000
NI.20181218.00884.fits 2.52727144745537 ... -0.0018423685161384 132000000
NI.20181218.00900.fits 2.51987090273206 ... -0.0018423685161384 132000000
NI.20181218.00920.fits 1.41291034446565 ...   0.707106781186547 233211302
NI.20181218.01053.fits 1.41291034446565 ...   0.707106781186547 233211302
NI.20181218.06720.fits 1.41291034446565 ...   0.707106781186547 233211302
NI.20181218.38030.fits 2.10447878934741 ...   0.030416396917203 220212302
NI.20181218.38072.fits 2.09380205617529 ...   0.030464894585008 220212302
NI.20181218.38931.fits 1.89565151628755 ...  0.0303583042928143 220212302
NI.20181218.41511.fits 1.50422806361724 ...  0.0304141290388975 220212302
                   ...              ... ...                 ...       ...
NR.20181218.58232.fits  1.0563770346548 ...   0.375140389030245 212332031
NR.20181218.58315.fits 1.05439626120835 ...   0.375131490758868 212332031
NR.20181218.58369.fits 1.05309873300075 ...   0.375122592452924 212332031
NR.20181218.58431.fits 1.05171401297284 ...    0.37511385590073 212332031
NR.20181218.58486.fits 1.05041742467484 ...   0.375104957526282 212332031
NR.20181218.59684.fits 13.3748135344195 ...  0.0010297440766911 230210000
NR.20181218.61034.fits 13.3748135344195 ...  0.0010297440766911 230210000
NR.20181218.61377.fits 13.3748135344195 ...  0.0010297440766911 230210000
NR.20181218.61713.fits 13.3748135344195 ...  0.0010297440766911 230210000
NR.20181218.62049.fits 13.3748135344195 ...  0.0010297440766911 230210000

Select columns acquired on UT date

In [6]:
sql = "select koaid, filehand  from koa_nires where koaid like '%20181218%'"
results = koa.run_sync(sql)
print (results)

table   = results.to_table()
table.write('./select_columns.tbl',format='ascii.ipac',overwrite=True)
<Table length=142>
        koaid                                 filehand                      
        object                                 object                       
---------------------- -----------------------------------------------------
NI.20181218.00833.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00833.fits
NI.20181218.00884.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00884.fits
NI.20181218.00900.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00900.fits
NI.20181218.00920.fits /koadata32/NIRES/20181218/lev0/NI.20181218.00920.fits
NI.20181218.01053.fits /koadata32/NIRES/20181218/lev0/NI.20181218.01053.fits
NI.20181218.06720.fits /koadata32/NIRES/20181218/lev0/NI.20181218.06720.fits
NI.20181218.38030.fits /koadata32/NIRES/20181218/lev0/NI.20181218.38030.fits
NI.20181218.38072.fits /koadata32/NIRES/20181218/lev0/NI.20181218.38072.fits
NI.20181218.38931.fits /koadata32/NIRES/20181218/lev0/NI.20181218.38931.fits
NI.20181218.41511.fits /koadata32/NIRES/20181218/lev0/NI.20181218.41511.fits
                   ...                                                   ...
NR.20181218.58232.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58232.fits
NR.20181218.58315.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58315.fits
NR.20181218.58369.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58369.fits
NR.20181218.58431.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58431.fits
NR.20181218.58486.fits /koadata32/NIRES/20181218/lev0/NR.20181218.58486.fits
NR.20181218.59684.fits /koadata32/NIRES/20181218/lev0/NR.20181218.59684.fits
NR.20181218.61034.fits /koadata32/NIRES/20181218/lev0/NR.20181218.61034.fits
NR.20181218.61377.fits /koadata32/NIRES/20181218/lev0/NR.20181218.61377.fits
NR.20181218.61713.fits /koadata32/NIRES/20181218/lev0/NR.20181218.61713.fits
NR.20181218.62049.fits /koadata32/NIRES/20181218/lev0/NR.20181218.62049.fits

Select a maximum number of records

In [8]:
sql="select koaid, filehand, frameno from koa_nires \
    where koaid like '%20181218%'"
results = koa.run_sync(sql, maxrec=20)
print (results)

table   = results.to_table()
table.write('./select_maxrec.tbl',format='ascii.ipac',overwrite=True)
<Table length=20>
        koaid          ... frameno
        object         ...  int32 
---------------------- ... -------
NI.20181218.00833.fits ...       0
NI.20181218.00884.fits ...       1
NI.20181218.00900.fits ...       2
NI.20181218.00920.fits ...       3
NI.20181218.01053.fits ...       4
NI.20181218.06720.fits ...       1
NI.20181218.38030.fits ...       2
NI.20181218.38072.fits ...       3
NI.20181218.38931.fits ...       4
NI.20181218.41511.fits ...       5
NI.20181218.41553.fits ...       6
NI.20181218.41699.fits ...       7
NI.20181218.41782.fits ...       8
NI.20181218.43651.fits ...       9
NI.20181218.45361.fits ...      10
NI.20181218.45403.fits ...      11
NI.20181218.45827.fits ...      12
NI.20181218.45869.fits ...      13
NI.20181218.46040.fits ...      14
NI.20181218.46218.fits ...      15

Select records by time range

In [9]:
sql="select koaid, filehand from koa_nires \
     where (utdatetime >= to_date('2018-12-18 15:00:00', \
    'yyyy-mm-dd HH24:MI:SS') and \
     utdatetime <= to_date('2018-12-18 15:30:00', 'yyyy-mm-dd HH24:MI:SS'))"
results  = koa.run_sync(sql)

print (results)

table_daterange=results.to_table()
table.write ('./table_daterange.vot',format='votable',overwrite=True)
<Table length=7>
        koaid                                 filehand                      
        object                                 object                       
---------------------- -----------------------------------------------------
NI.20181218.55591.fits /koadata32/NIRES/20181218/lev0/NI.20181218.55591.fits
NI.20181218.55629.fits /koadata32/NIRES/20181218/lev0/NI.20181218.55629.fits
NI.20181218.55659.fits /koadata32/NIRES/20181218/lev0/NI.20181218.55659.fits
NR.20181218.54083.fits /koadata32/NIRES/20181218/lev0/NR.20181218.54083.fits
NR.20181218.54417.fits /koadata32/NIRES/20181218/lev0/NR.20181218.54417.fits
NR.20181218.54752.fits /koadata32/NIRES/20181218/lev0/NR.20181218.54752.fits
NR.20181218.55088.fits /koadata32/NIRES/20181218/lev0/NR.20181218.55088.fits

Select records by program ID

In [10]:
sql= ("select koaid, filehand, progid  \
    from koa_nires where (progid = 'Y025') ")
results  = koa.run_sync(sql)
print (results)

table_progid=results.to_table()
table.write ('./progid.vot',format='votable',overwrite=True)
<Table length=545>
        koaid          ... progid
        object         ... object
---------------------- ... ------
NI.20181218.06720.fits ...   Y025
NI.20181218.38030.fits ...   Y025
NI.20181218.38072.fits ...   Y025
NI.20181218.38931.fits ...   Y025
NI.20181218.41511.fits ...   Y025
NI.20181218.41553.fits ...   Y025
NI.20181218.41699.fits ...   Y025
NI.20181218.41782.fits ...   Y025
NI.20181218.43651.fits ...   Y025
NI.20181218.45361.fits ...   Y025
                   ... ...    ...
NR.20181229.59496.fits ...   Y025
NR.20181229.59633.fits ...   Y025
NR.20181229.59769.fits ...   Y025
NR.20181230.38256.fits ...   Y025
NR.20181230.38374.fits ...   Y025
NR.20181230.38492.fits ...   Y025
NR.20181230.38610.fits ...   Y025
NR.20181230.38727.fits ...   Y025
NR.20181230.38946.fits ...   Y025
NR.20181230.39082.fits ...   Y025

Spatial cone search with column selection

In [11]:
sql=("select koaid, filehand, ra, dec  from koa_nires where \
    contains(point('icrs', ra, dec), \
    circle('icrs', 194.25 22.03, 1.0)) = 1")

results  = koa.run_sync(sql)
print(results)

table_spatial_1=results.to_table()
table.write ('./table_spatial_1.vot',format='votable',overwrite=True)
<Table length=117>
        koaid          ...   dec   
        object         ... float64 
---------------------- ... --------
NI.20180131.55969.fits ... 22.03131
NI.20180131.55991.fits ... 22.03131
NI.20180131.56045.fits ... 22.03038
NI.20180131.56327.fits ... 22.03038
NI.20180131.56391.fits ...  22.0304
NI.20180131.56724.fits ... 22.03036
NI.20180131.57056.fits ... 22.03036
NI.20180131.57390.fits ...  22.0304
NI.20181218.56718.fits ... 22.03128
NI.20181218.56785.fits ... 22.03233
                   ... ...      ...
NR.20190519.17731.fits ... 22.03136
NR.20190519.17757.fits ... 22.03126
NR.20190519.17822.fits ... 22.03135
NR.20190519.17849.fits ... 22.03126
NR.20190519.17955.fits ... 22.03135
NR.20190519.17972.fits ... 22.03126
NR.20190519.31091.fits ... 22.03135
NR.20190519.31329.fits ... 22.03133
NR.20190519.31562.fits ... 22.03133
NR.20190519.31798.fits ... 22.03135
In [12]:
sql=("select koaid, filehand, ra, dec from koa_nires where \
     contains(point('icrs', ra, dec), \
     box('icrs',194.25 22.03,1.0,120)) = 1")

results = koa.run_sync(sql)
print (results)

table_spatial_box=results.to_table()
table.write('./table_spatial_box.vot',format='votable',overwrite=True)
<Table length=117>
        koaid          ...   dec   
        object         ... float64 
---------------------- ... --------
NI.20180131.55969.fits ... 22.03131
NI.20180131.55991.fits ... 22.03131
NI.20180131.56045.fits ... 22.03038
NI.20180131.56327.fits ... 22.03038
NI.20180131.56391.fits ...  22.0304
NI.20180131.56724.fits ... 22.03036
NI.20180131.57056.fits ... 22.03036
NI.20180131.57390.fits ...  22.0304
NI.20181218.56718.fits ... 22.03128
NI.20181218.56785.fits ... 22.03233
                   ... ...      ...
NR.20190519.17731.fits ... 22.03136
NR.20190519.17757.fits ... 22.03126
NR.20190519.17822.fits ... 22.03135
NR.20190519.17849.fits ... 22.03126
NR.20190519.17955.fits ... 22.03135
NR.20190519.17972.fits ... 22.03126
NR.20190519.31091.fits ... 22.03135
NR.20190519.31329.fits ... 22.03133
NR.20190519.31562.fits ... 22.03133
NR.20190519.31798.fits ... 22.03135
In [13]:
sql=("select koaid, filehand, ra, dec from koa_nires where \
    contains(point('icrs', ra, dec), \
    polygon('icrs',209.80225,53.34894,209.80225,55.34894,211.80225,54.34894)) = 1")

results = koa.run_sync(sql)
print(results)

table_spatial_P=results.to_table()
table.write ('./table_spatial_P.vot',format='votable',overwrite=True)
<Table length=35>
        koaid          ...   dec   
        object         ... float64 
---------------------- ... --------
NI.20180302.50965.fits ... 54.39806
NI.20180302.51018.fits ... 54.39711
NI.20180302.51208.fits ... 54.39895
NI.20180302.51624.fits ... 54.39801
NI.20180302.51921.fits ... 54.39895
NI.20180302.52284.fits ...  54.3999
NI.20180302.52673.fits ... 54.39895
NI.20180302.53019.fits ... 54.39801
NI.20180302.53396.fits ... 54.39895
NI.20180302.53991.fits ... 54.39982
                   ... ...      ...
NR.20180302.51921.fits ... 54.39895
NR.20180302.52284.fits ...  54.3999
NR.20180302.52673.fits ... 54.39895
NR.20180302.53019.fits ... 54.39801
NR.20180302.53396.fits ... 54.39895
NR.20180302.53991.fits ... 54.39982
NR.20180302.54384.fits ... 54.39793
NR.20180302.56342.fits ... 54.30151
NR.20180302.56693.fits ... 54.30223
NR.20180302.57162.fits ...  54.3008

Count number of records in date range

In [14]:
sql=("select count(*) as count from koa_nires where \
    date_obs=to_date('2018-12-18', 'YYYY-MM-DD')")

results = koa.run_sync(sql)
print(results)
<Table length=1>
count
int32
-----
  142

Count total number of records in table

In [15]:
sql="select count(*) as total from koa_nires"

results = koa.run_sync(sql)
print(results)
<Table length=1>
total
int32
-----
31626

Visit KOA at https://koa.ipac.caltech.edu.

The Keck Observatory Archive (KOA) is a collaboration between the NASA Exoplanet Science Institute (NExScI) and the W. M. Keck Observatory (WMKO). NExScI is sponsored by NASA's Exoplanet Exploration Program, and operated by the California Institute of Technology in coordination with the Jet Propulsion Laboratory (JPL).

Need help? Submit your questions to the KOA Help Desk at https://koa.ipac.caltech.edu/cgi-bin/Helpdesk/nph-genTicketForm?projname=KOA