#!/bin/bash

# query Simbad by  position
# csSim [-r RADIUS] {-h} RAd DECd | pipe
# -r radius of cone-search in arcseconds, [5]

# LAST REVISION: 25-January-2020
#-----------------------------------------------------------------------
RADIUS=5
URL="http://simbad.u-strasbg.fr/simbad/sim-coo?"
#-----------------------------------------------------------------------

while getopts r:h OPTVAL
do
   case $OPTVAL in
        r) RADIUS=$OPTARG;;
        h) HELP=1;;
        *) echo "csSim -h for help"; exit -1;;
   esac
done
shift $((OPTIND-1))

if [ $HELP ]; then
  echo "cone search Simbad catalog"; echo
  echo "csSim [-r RADIUS] {-h} RAd DECd | pipe"
  echo " -r radius of cone search in arcseconds [5]"
  echo "RAd and DECd in degrees; can accept pipe inputs"
  exit
fi

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

if [ -p /dev/stdin ]; then
    PIPE=$(cat -)
    set -- $PIPE
fi

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

IND=0;
while [ $# -ge 2 ]
do
    RA=$1; DEC=$2; shift 2
    IND=$((IND+1));
    curl -s "${URL}Coord=$RA%20$DEC&Radius=$RADIUS&Radius.unit=arcsec&output.format=ASCII" 

done
