#!/bin/bash

# Cone-search Chandra catalog
# cs_Chandra [-h] [-s] [-d] [-r [5]] [-v [1]] RA(deg) DEC(deg)
# -h help
# -s show header (one line per column)
# -r cone radius in arcseconds, [5]
# -d display both detections and non-detections (default: display only detections) 
# -v verbosity, [1] 2 or 3
#
# accepts on line parameters, from a pipe or process
#
# cs_Chandra 299.903083 +20.804028    (1957+20)
# cut -f1,2 radecfile | csChandra
# cs_Chandra $(cat radecfile|xargs)
#
# BUG: This will not run under zsh (which has a different way of reading a pipe)
# LAST REVISION: 25-January-2020
#------------------------------------------------------------------------
HELP=; SHOW=; RADIUS=5; VERB=1; BOTH=;
INVALID=;
#------------------------------------------------------------------------
TF="out_$$.vot"; DF="cxc.dat";HF="cxc.hdr"
trap "[ -e $TF ] && rm $TF; [ -e $DF ] && rm $DF; [ -e $HF ] &&  $HF" EXIT
#------------------------------------------------------------------------

while getopts hsdr:v: OPTION
do 
   case $OPTION in
	h) HELP=1;;
	s) SHOW=1;;
	d) BOTH=1;;
	r) RADIUS=$OPTARG;;
	v) VERB=$OPTARG;;
	*) INVALID=1;;
   esac	
done
shift $(($OPTIND-1))

[ $INVALID ] &&  exit -1

if [ $HELP ]; then
        echo "cs_Chandra [-r radius] [-s] [-h] RA(deg) Dec(deg)"
        echo "RA and Dec in degrees (must specify)"
        echo "-r .. radius of Cone Search in integer arcseconds [5]"
        echo "-v .. verbosity of output,[1],2,3"
	echo "-d .. display only detected sources"
        echo "-s .. show header, one column per line, on screen"
        echo "-h .. help (display)"
        echo " OUTPUT: tab-separated  data lines (one line per source"
        echo "example: cs_Chandra  310.827805 44.638884"
        exit;
fi

if ! [ $RADIUS -eq $RADIUS ] 2>/dev/null; then
        echo "error: cone radius (arcsec) must be an integer" 
        exit -1
fi
SR=$(echo "scale=6;$RADIUS/3600" | bc)

if ! [ $VERB -eq $VERB ] 2>/dev/null; then
	echo "Verbosity should be 1, 2 or 3"; 
        exit -1 
fi
#-----------------------------------------------------------------------
#End of parsing 
#-----------------------------------------------------------------------


#-----------------------------------------------------------------------
#input from pipe? if so, convert to positional parameters
#-----------------------------------------------------------------------
if [ -p /dev/stdin ]; then
    PIPE=$(cat -)
    set -- $PIPE
fi

i=1
while [ $# -ge 2 ]; do
    RA=$1; DEC=$2; 
    shift 2

    curl -s -o $TF "http://cda.cfa.harvard.edu/csc2scs/coneSearch?RA=$RA&DEC=$DEC&SR=$SR&VERB=$VERB"


#-----------------------------------------------------------------------
# Display header
#-----------------------------------------------------------------------
    if [ $SHOW ]; then
	sed -n '1,/FIELD/d;/DESCRIPTION/p;/DATA/q' $TF | \
	sed 's;\(^<DESCRIPTION>\)\(.*\)\(</DESCRIPTION>$\);\2;' | \
	nl | tee  $HF
    fi

#-----------------------------------------------------------------------
# Extract Data
#-----------------------------------------------------------------------
    sed -n '/TABLEDATA/,/\/TABLEDATA/p' $TF | \
    sed '1d;$d;s:<TD>::;s:<TR>::;s:</TD>:,:' | \
    tr '\n' ' ' | gsed 's:</TR>:\n:g' | sed 's/, *$//;$d' > $DF

    N=$(wc -l < $DF)
    if  [ $N -gt 0 ]; then
        sed "s/^/$i/" $DF
    else
	[ $BOTH ] && echo "$i,$RA,$DEC"
    fi
    i=$((i+1))
done
