------------------------------------------------------------------------
Querying NED (TAP)
------------------------------------------------------------------------

BACKGROUND. Here, I discuss querying the NASA Extragalactic Database
(NED) maintained by IPAC. It is a useful starting point to understand
"table access protocol" (TAP) defines a service protocol for accessing
general table data, including astronomical catalogs as well as
general database tables. Access is provided for both database and
table metadata as well as for actual table data.  The fundamental
reference is https://www.ivoa.net/documents/TAP/20100327/REC-TAP-1.0.html

The query language of TAP is "Astronomical Data Query Language"
(ADQL), though some may support variants of SQL. TAP services support
data queries, metadata queries (which allow a user to discover the names
of tables and columns to be in data queries), and  Virtual
Observatory Support Interface (VOSI; informs users of the
capabilities of VO serviced offered). TAP requests can be synchronous
asynchronous.

The main query parameters are QUERY, LANG, REQUEST, FORMAT (optional),
PHASE (optional, async query only), EXECUTIONDURATION (optional),
and MAXREC (optional).  For small inquiries synchronous calls are
sufficient:
https://ned.ipac.caltech.edu/tap/sync?QUERY={QUERY}&LANG=ADQL&REQUEST=doQuery&FORMAT={FORMAT}&MAXREC={MAXREC}

------------------------------------------------------------------------
NED
------------------------------------------------------------------------
This is an excellent starting point for the vocabulary of NED
https://ned.ipac.caltech.edu/help/ui/ned_help?popup=1

Reference: https://ned.ipac.caltech.edu/Documents/Guides/Interface/TAP
I found the IPAC URL to be very helpful.

------------------------------------------------------------------------
NED: Cone-search
------------------------------------------------------------------------

For instance the call below is a cone-search of NED with RA, DEC
and Radius, all in deciamal degrees. The ouput form is plain "TEXT".
Other options include VOTABLE,FITS,JSON,CSV,TSV,TEXT,HTML. Note that 
"+" here stands for " ". URLencoding can be painful and ideally you
should URLencode anything other than alphnumberic, "-","_",".".
[Look up my Unix page for URL encoding for other details]

curl -o out.txt "https://ned.ipac.caltech.edu/tap/sync?
query=SELECT+*+FROM+objdir+WHERE+CONTAINS(POINT('J2000',ra,dec),
CIRCLE('J2000',66.76957,26.10453,0.01))=1&LANG=ADQL&REQUEST=doQuery&FORMAT=text"

$ head -1 out.dat | sed 's/ *//g' | tr '|' '\n' | nl -s " "
     1 prefname
     2 ra
     3 dec
     4 uncmaja
     5 uncmina
     6 uncposa
     7 gallon
     8 gallat
     9 bhextin
    10 refcode
    11 pretype
    12 z
    13 zunc
    14 zflag
    15 zrefcode
    16 n_crosref
    17 n_notes
    18 n_gphot
    19 n_posd
    20 n_zdf
    21 n_ddf
    22 n_assoc
    23 n_dist
    24 n_class
    25 n_images
    26 n_spectra
    27 objid
    28 pos_refid
    29 z_refid

See Appendix on how the meaning of the keywords. From the keyword list
we select prefname(1), ra(2), dec(3), pretype(11), z(12), zunc (13)

$ sed 2d out.dat | sed 's/  //g;s/ "/"/g;' | awk -F'|' '{print $1,$2,$3,$11,$12,$13}'





------------------------------------------------------------------------
Appendix: Key word schema 
------------------------------------------------------------------------

Keywords are found in TAP Schema.  The relevant table is
"TAP_SCHEMA.columns" We need only the keywords associated with NED
objects. Next, for readability, we rid of all the spaces that IPAC
tables revel in


$ curl -o out.txt "https://ned.ipac.caltech.edu/tap/sync?QUERY=SELECT+*+FROM+TAP_SCHEMA.columns&REQUEST=doQuery&LANG=ADQL&FORMAT=text"


$ cat out.txt | grep "NEDTAP.objdir" | sed 's/  //g' | awk -F'|' '{print $3"\t"$4}'
     1 "objid"         "Internal identifier for the object in NED"
     2 "prefname"      "preferred object name"
     3 "pretype"       "NED preferred object type"
     4 "ra"            "RA in decimal degrees J2000.0"
     5 "dec"           "DEC in decimal degrees J2000.0"
     6 "gallon"        "Galactic Longitude in degrees of the object"
     7 "gallat"        "Galactic Latitude in degrees of the object"
     8 "bhextin"       "Foreground Galactic Extinction Burstein & Heiles"
     9 "uncmaja"       "Uncertainty Ellipse, Semimajor Axis in arcseconds"
    10 "uncmina"       "Uncertainty Ellipse, Semiminor Axis in arcseconds"
    11 "uncposa"       "Uncertainty Ellipse, PA in degrees (East of North)"
    12 "refcode"       "Reference Code for the object position"
    13 "zflag"         "Redshift flag"
    14 "z"             "Redshift in z."
    15 "zunc"          "Redshift Uncertainty"
    16 "zrefcode"      "NED-used refcode for redshift of this oobject"
    17 "pos_refid"     "Internal Refcode ID for position"
    18 "z_refid"       "Internal Refcode ID for redshift in this position"
    19 "n_crosref"     "Number of crosref data frames"
    20 "n_notes"       "Number of notes data frames"
    21 "n_gphot"       "Number of photometric data frames"
    22 "n_posd"        "Number of position data frames"
    23 "n_zdf"         "Number of redshift data frames"
    24 "n_assoc"       "Number of association data frames"
    25 "n_ddf"         "Number of diameter data frames"
    26 "n_images"      "Number of image data frames"
    27 "n_spectra"     "Number of spectra data frames"
    28 "n_dist"        "Number of distance data frames"
    29 "n_class"       "Number of classification data frames"

------------------------------------------------------------------------
Appendix: Galaxy types: NED
------------------------------------------------------------------------
*	Star or Point Source
**	Double star
*Ass	Stellar association
*Cl	Star cluster
AbLS	Absorption line system
Blue*	Blue star
C*	Carbon star
EmLS	Emission line source
EmObj	Emission object
exG*	Extragalactic star (not a member of an identified galaxy)
Flare*	Flare star
G	Galaxy
GammaS	Gamma ray source
GClstr	Cluster of galaxies
GGroup	Group of galaxies
GPair	Galaxy pair
GTrpl	Galaxy triple
G_Lens	Lensed image of a galaxy
HII	HII region
IrS	Infrared source
MCld	Molecular cloud
Neb	Nebula
Nova	Nova
Other	Other classification (e.g. comet; plate defect)
PN	Planetary nebula
PofG	Part of galaxy
Psr	Pulsar
QGroup	Group of QSOs
QSO	Quasi-stellar object
Q_Lens	Lensed image of a QSO
RadioS	Radio source
Red*	Red star
RfN	Reflection nebula
SN	Supernova
SNR	Supernova remnant
UvES	Ultraviolet excess source
UvS	Ultraviolet source
V*	Variable star
VisS	Visual source
WD*	White dwarf
WR*	Wolf-Rayet star
XrayS	X-ray source
!*	Galactic star
!**	Galactic double star
!*Ass	Galactic star association
!*Cl	Galactic Star cluster
!Blue*	Galactic blue star
!C*	Galactic carbon star
!EmObj	Galactic emission line object
!Flar*	Galactic flare star
!HII	Galactic HII region
!MCld	Galactic molecular cloud
!Neb	Galactic nebula
!Nova	Galactic nova
!PN	Galactic planetary nebula
!Psr	Galactic pulsar
!RfN	Galactic reflection nebula
!Red*	Galactic red star
!SN	Galactic supernova
!SNR	Galactic supernova remnant
!V*	Galactic variable star
!WD*	Galactic white dwarf
!WR*	Galactic Wolf-Rayet star