#!/bin/bash

# csGaia .. cone search Gaia DR2 data base (ARI, Germany)
# ouptut is a delimited file [default: tsv] 
# csGaia [-r RADIUS][-d DFS] [-v VERBOSITY] [-g GaiaV] {-h} RAd Decd
#  RAd, DECd in degrees
# -r CONERADIUS in integer arcseconds [5]
# -d DFS is the field separator for output file
# -v VERBOSITY, [1],2,3
# -g Gaia verson, 1,2,[E]
# OUTPUT: header line followed by one per source
#         ordered by increasing radial distance from the given position
# Warning:  some Gaia fields have a ",", whence default of DFS=\t
#
# written by SR Kulkarni. 
# I am grateful to Campillo (ARI) for providing Gaia API details.
#
# LAST REVISION: 27-January-2020
#------------------------------------------------------------------------
RADIUS=5; DFS="\t"; VERBOSITY=1; GaiaVersion=E; HELP=; 
#------------------------------------------------------------------------
TF="OUT_csGAIA.tmp"  TFh=${TF}h TFt=${TF}t TFd=${TF}d
trap "[ -e $TF ]  && rm $TF; [ -e $TFh ] && rm $TFh; 
      [ -e $TFt ] && rm $TFt;[ -e $TFd ] && rm $TFd"  EXIT
#------------------------------------------------------------------------


while getopts r:d:v:g:h optval
do 
   case $optval in
	r) RADIUS=$OPTARG;;
	d) DFS=$OPTARG;;
	v) VERBOSITY=$OPTARG;;
	g) GaiaVersion=$OPTARG;;
	h) HELP=1;;
	*) echo "csGaia -h for help"; exit -1
   esac
done
shift $((OPTIND-1))


if [ $HELP ]; then
  echo "csGaia [-r RADIUS][-d DFS] [-v VERBOSITY] [-g GaiaV] {-s} {-h} RAd DECd RA DEC ..| pipe"
  echo "-r radius of cone search in arcseconds [5]"
  echo '-d DFS, the field separator for output ["\t"]'
  echo "-v VERBOSITY, [1],2,3 for increasing columns"
  echo "-g GaiaVersion, 1 for DR1 ,2,[E] for EDR3"
  exit
fi

! [[ $RADIUS =~ ^[0-9] ]]      && { echo "\"$RADIUS\":invalid radius"; exit -1; }
! [[ $VERBOSITY =~ ^[0-9] ]]   && { echo "\"$VERBOSITY\"invalid verbosity"; exit -1; }


#------------------------------------------------------------------------
#Form the URL for Gaia inquiry
#  URL="https://gaia.ari.uni-heidelberg.de/cone/search?"
#------------------------------------------------------------------------
  case $GaiaVersion in
    1) GaiaVersion="gaiadr1";;
    2) GaiaVersion="gaiadr2";;
    E) GaiaVersion="search";;
    *) echo "$GaiaVersion: invalid choice for Gaia version"; exit -1;;
  esac

  URL="https://gaia.ari.uni-heidelberg.de/cone/${GaiaVersion}?"

SR=$(echo "scale=6;$RADIUS/3600" | bc)

#-----------------------------------------------------------------------
#input from pipe? if so, convert to positional parameters
#-----------------------------------------------------------------------

if [ $# -eq 0 ]; then 
  if [ -p /dev/stdin ]; then
    PIPE=$(cat -)
    set -- $PIPE
  else
    echo "mergeGaia: no inputs from pipe or command line"; exit -1
  fi
fi

#-----------------------------------------------------------------------
# process each pair of coordinates
#-----------------------------------------------------------------------


if [ $# -lt 2 ];  then
   echo "csGia: need a minimum pair of RA, DEC"
   exit -1
fi

while [ $# -ge 2 ]
do
    RA=$1; DEC=$2; shift 2
    curl -s  "${URL}RA=$RA&DEC=$DEC&SR=$SR&VERB=$VERBOSITY" -o $TF

	#header 
    grep "<FIELD " $TF | sed 's/^.*name="//;s/".*$//;s/$/:/' | \
    xargs | gsed 's/ //g;s/.$//;s/:/'"$DFS"'/g' > $TFh

	#data
    sed -n '/<TABLEDATA>/,/<\/TABLEDATA>/{/<TABLEDATA>/d;/<.TABLEDATA/d;s/^.*<TD>//;p;}' \
    $TF > $TFt

    n=$(wc -l <$TFt)
    [ $n -eq 0 ] && { echo "nada"; exit;}

    gsed '/<TR>/d;s/^[ \t]*//' $TFt  | \
    awk -F"</TD>" '{$1=$1;gsub("\n","");gsub(" ","_");print}' RS="</TR>" OFS="$DFS" | gsed "s/$DFS"'$//;/^$/d' > $TFd

   NR=$(wc -l < $TFd)
   [ $NR -gt 0 ] & cat $TFh $TFd

done