Using the nexsciTAP Server with the PyVO Client to Access NIRSPEC 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 NIRSPEC cross-dispersed echelle spectrograph; 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 (July, 2021)

Set up

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

import sys
import os
import time
import warnings

Query by date

In [11]:
sql = "select koaid, filehand from koa_nirspec where koaid like '%20170605%'"

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=137>
        koaid          ...
        object         ...
---------------------- ...
NC.20170605.45884.fits ...
NC.20170605.45978.fits ...
NC.20170605.46085.fits ...
NC.20170605.46186.fits ...
NC.20170605.46226.fits ...
NS.20170605.44190.fits ...
NS.20170605.44221.fits ...
NS.20170605.44231.fits ...
NS.20170605.44242.fits ...
NS.20170605.44252.fits ...
                   ... ...
NS.20170605.55455.fits ...
NS.20170605.55467.fits ...
NS.20170605.55480.fits ...
NS.20170605.55491.fits ...
NS.20170605.55602.fits ...
NS.20170605.55628.fits ...
NS.20170605.55645.fits ...
NS.20170605.55754.fits ...
NS.20170605.55794.fits ...
NS.20170605.55811.fits ...

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

In [12]:
# write results to an IPAC ASCII table
sql = "select koaid, filehand from koa_nirspec where koaid like '%20170605%' "
results = koa.run_sync(sql)
table=results.to_table()
table.write ('./table_ipacascii.tbl',format='ascii.ipac',overwrite=True)
In [13]:
#  write results to CSV file
sql = "select koaid, filehand from koa_nirspec where koaid like '%20170605%' "
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 [14]:
sql = "select * from koa_nirspec where koaid like '%20170605%' "
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=137>
        koaid          propint       date_obs      ...     z      spt_ind 
                        months                     ...                    
        object          int32         object       ...  float64    int32  
---------------------- ------- ------------------- ... --------- ---------
NC.20170605.45884.fits      18 2017-06-05 00:00:00 ... -0.405123 130120032
NC.20170605.45978.fits      18 2017-06-05 00:00:00 ... -0.405275 130120032
NC.20170605.46085.fits      18 2017-06-05 00:00:00 ... -0.405275 130120032
NC.20170605.46186.fits      18 2017-06-05 00:00:00 ... -0.405275 130120032
NC.20170605.46226.fits      18 2017-06-05 00:00:00 ... -0.405275 130120032
NS.20170605.44190.fits      18 2017-06-05 00:00:00 ... -0.499979 133300131
NS.20170605.44221.fits      18 2017-06-05 00:00:00 ... -0.500021 133300131
NS.20170605.44231.fits      18 2017-06-05 00:00:00 ... -0.500021 133300131
NS.20170605.44242.fits      18 2017-06-05 00:00:00 ...      -0.5 133300131
                   ...     ...                 ... ...       ...       ...
NS.20170605.55455.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55467.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55480.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55491.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55602.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55628.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55645.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55754.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55794.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000
NS.20170605.55811.fits      18 2017-06-05 00:00:00 ...   0.00103 230210000

Select columns acquired on UT date

In [15]:
sql = "select koaid, filehand  from koa_nirspec where koaid like '%20170605%'"
results = koa.run_sync(sql)
print (results)

table   = results.to_table()
table.write('./select_columns.tbl',format='ascii.ipac',overwrite=True)
<Table length=137>
        koaid          ...
        object         ...
---------------------- ...
NC.20170605.45884.fits ...
NC.20170605.45978.fits ...
NC.20170605.46085.fits ...
NC.20170605.46186.fits ...
NC.20170605.46226.fits ...
NS.20170605.44190.fits ...
NS.20170605.44221.fits ...
NS.20170605.44231.fits ...
NS.20170605.44242.fits ...
NS.20170605.44252.fits ...
                   ... ...
NS.20170605.55455.fits ...
NS.20170605.55467.fits ...
NS.20170605.55480.fits ...
NS.20170605.55491.fits ...
NS.20170605.55602.fits ...
NS.20170605.55628.fits ...
NS.20170605.55645.fits ...
NS.20170605.55754.fits ...
NS.20170605.55794.fits ...
NS.20170605.55811.fits ...

Select a maximum number of records

In [16]:
sql="select koaid, filehand, frameno from koa_nirspec \
    where koaid like '%20170605%'"
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 
---------------------- ... -------
NC.20170605.45884.fits ...       1
NC.20170605.45978.fits ...       2
NC.20170605.46085.fits ...       3
NC.20170605.46186.fits ...       4
NC.20170605.46226.fits ...       5
NS.20170605.44190.fits ...       1
NS.20170605.44221.fits ...       2
NS.20170605.44231.fits ...       3
NS.20170605.44242.fits ...       4
NS.20170605.44252.fits ...       5
NS.20170605.44263.fits ...       6
NS.20170605.44274.fits ...       7
NS.20170605.44285.fits ...       8
NS.20170605.44296.fits ...       9
NS.20170605.44307.fits ...      10
NS.20170605.44317.fits ...      11
NS.20170605.44327.fits ...      12
NS.20170605.44338.fits ...      13
NS.20170605.44350.fits ...      14
NS.20170605.44361.fits ...      15

Select records by time range

In [17]:
sql="select koaid, filehand from koa_nirspec \
     where (utdatetime >= to_date('2017-06-05 13:00:00', \
    'yyyy-mm-dd HH24:MI:SS') and \
     utdatetime <= to_date('2017-06-05 14:00: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=29>
        koaid          ...
        object         ...
---------------------- ...
NS.20170605.46805.fits ...
NS.20170605.46982.fits ...
NS.20170605.47079.fits ...
NS.20170605.47170.fits ...
NS.20170605.47245.fits ...
NS.20170605.47336.fits ...
NS.20170605.47719.fits ...
NS.20170605.47785.fits ...
NS.20170605.47837.fits ...
NS.20170605.47903.fits ...
                   ... ...
NS.20170605.49047.fits ...
NS.20170605.49125.fits ...
NS.20170605.49219.fits ...
NS.20170605.49457.fits ...
NS.20170605.49551.fits ...
NS.20170605.49634.fits ...
NS.20170605.49728.fits ...
NS.20170605.50038.fits ...
NS.20170605.50188.fits ...
NS.20170605.50325.fits ...

Select records by program ID

In [18]:
sql= ("select koaid, filehand, progid  \
    from koa_nirspec where (progid = 'K236') ")
results  = koa.run_sync(sql)
print (results)

table_progid=results.to_table()
table.write ('./progid.vot',format='votable',overwrite=True)
<Table length=282>
        koaid          ... progid
        object         ... object
---------------------- ... ------
NC.20170605.45884.fits ...   K236
NC.20170605.45978.fits ...   K236
NC.20170605.46085.fits ...   K236
NC.20170605.46186.fits ...   K236
NC.20170605.46226.fits ...   K236
NC.20170608.45997.fits ...   K236
NC.20170608.46091.fits ...   K236
NC.20170608.46147.fits ...   K236
NC.20170608.46243.fits ...   K236
NC.20170608.46278.fits ...   K236
                   ... ...    ...
NS.20170608.56048.fits ...   K236
NS.20170608.56060.fits ...   K236
NS.20170608.56072.fits ...   K236
NS.20170608.56086.fits ...   K236
NS.20170608.56157.fits ...   K236
NS.20170608.56189.fits ...   K236
NS.20170608.56208.fits ...   K236
NS.20170608.56281.fits ...   K236
NS.20170608.56309.fits ...   K236
NS.20170608.56326.fits ...   K236

Spatial cone search with column selection

In [19]:
sql=("select koaid, filehand, ra, dec  from koa_nirspec where \
    contains(point('J2000', ra, dec), \
    circle('J2000', 277.93 -9.985, 0.5)) = 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=28>
        koaid          ...   dec   
        object         ... float64 
---------------------- ... --------
NC.20080621.43529.fits ...  -9.9875
NC.20080621.43543.fits ... -9.98611
NS.20080621.43867.fits ... -9.98775
NS.20080621.43983.fits ... -9.98755
NS.20080621.44085.fits ...  -9.9876
NS.20080621.44204.fits ... -9.98777
NS.20080621.44432.fits ... -9.98772
NS.20080621.44545.fits ... -9.98759
NS.20080621.44648.fits ... -9.98764
NS.20080621.44762.fits ... -9.98775
                   ... ...      ...
NS.20130726.38560.fits ... -9.98795
NS.20130726.38713.fits ...   -9.988
NS.20150808.27197.fits ... -9.98865
NS.20150808.27314.fits ... -9.98605
NS.20150808.27418.fits ...   -9.986
NS.20150808.27536.fits ... -9.98848
NS.20170605.50038.fits ... -9.98569
NS.20170605.50188.fits ... -9.98571
NS.20170605.50325.fits ... -9.98563
NS.20170605.50479.fits ...  -9.9856
In [20]:
sql=("select koaid, filehand, ra, dec from koa_nirspec where \
     (contains(point('J2000',ra ,dec), box('J2000', 277.93 -9.985, 0.5, 0.5)) = 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=28>
        koaid          ...   dec   
        object         ... float64 
---------------------- ... --------
NC.20080621.43529.fits ...  -9.9875
NC.20080621.43543.fits ... -9.98611
NS.20080621.43867.fits ... -9.98775
NS.20080621.43983.fits ... -9.98755
NS.20080621.44085.fits ...  -9.9876
NS.20080621.44204.fits ... -9.98777
NS.20080621.44432.fits ... -9.98772
NS.20080621.44545.fits ... -9.98759
NS.20080621.44648.fits ... -9.98764
NS.20080621.44762.fits ... -9.98775
                   ... ...      ...
NS.20130726.38560.fits ... -9.98795
NS.20130726.38713.fits ...   -9.988
NS.20150808.27197.fits ... -9.98865
NS.20150808.27314.fits ... -9.98605
NS.20150808.27418.fits ...   -9.986
NS.20150808.27536.fits ... -9.98848
NS.20170605.50038.fits ... -9.98569
NS.20170605.50188.fits ... -9.98571
NS.20170605.50325.fits ... -9.98563
NS.20170605.50479.fits ...  -9.9856
In [19]:
sql=("select koaid, filehand, ra, dec from koa_nirspec where \
    contains(point('icrs', ra, dec), \
    polygon('icrs',277.43 -10.485 277.43 -9.485 278.43 -9.485 278.43 -10.485)) = 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=28>
        koaid          ...   dec   
        object         ... float64 
---------------------- ... --------
NC.20080621.43529.fits ...  -9.9875
NC.20080621.43543.fits ... -9.98611
NS.20080621.43867.fits ... -9.98775
NS.20080621.43983.fits ... -9.98755
NS.20080621.44085.fits ...  -9.9876
NS.20080621.44204.fits ... -9.98777
NS.20080621.44432.fits ... -9.98772
NS.20080621.44545.fits ... -9.98759
NS.20080621.44648.fits ... -9.98764
NS.20080621.44762.fits ... -9.98775
                   ... ...      ...
NS.20130726.38560.fits ... -9.98795
NS.20130726.38713.fits ...   -9.988
NS.20150808.27197.fits ... -9.98865
NS.20150808.27314.fits ... -9.98605
NS.20150808.27418.fits ...   -9.986
NS.20150808.27536.fits ... -9.98848
NS.20170605.50038.fits ... -9.98569
NS.20170605.50188.fits ... -9.98571
NS.20170605.50325.fits ... -9.98563
NS.20170605.50479.fits ...  -9.9856

Count number of records in date range

In [21]:
sql=("select count(*) as count from koa_nirspec where \
    date_obs=to_date('2017-06-05', 'YYYY-MM-DD')")

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

Count total number of records in table

In [22]:
sql="select count(*) as total from koa_nirspec"

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

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