#!/bin/bash

# csSXPS [-r RADIUS] [-v VERBOSITY] {-t} {-s} {-h} ra(deg) dec(deg) 
#
# SXSPS cone-search service provided by Dr. P. Evans, Leicester, UK
# see NOTE (end of script)

#-----------------------------------------------------------------------
RADIUS=30; VERBOSITY=1; HELP=; FORMAT=1; SUMERIAN=1;
#-----------------------------------------------------------------------
TF="OUT_csSwift"; TFt=${TF}.tmp;
trap "[ -e $TFt ] && rm $TFt" EXIT
#-----------------------------------------------------------------------

while getopts r:v:tsh OPTVAL
do
    case $OPTVAL in
	r) RADIUS=$OPTARG;;
	v) VERBOSITY=$VERBOSITY;;
	t) FORMAT=2;;
	s) SUMERIAN=0;;
	h) HELP=1;;
	*) echo "csSXPS -h for help"; exit -1;;
    esac
done
shift $((OPTIND-1))


if [ $HELP ]; then
    printf "\tcone search Swift Serendipitous X-ray catalog\n"
    printf "\tcsSwift [-r RADIUS] [-v VERBOSITY] {-t} {-h} ra(deg) dec(deg)\n"
    printf "\t-r .. cone RADIUS in arcseconds [30]\n"
    printf "\t-v .. verbosity [1] basic, 2 all, 3 minimal\n"
    printf "\t-s .. Sumerian (sexagesimal) output\n"
    printf "\t-t .. format, default is csv; if set, tsv\n"
    exit
fi

#-----------------------------------------------------------------------
# extract  RA & DEC
#-----------------------------------------------------------------------
if [ $# -ne 2 ]; then
    echo "got $#; need two positional parameters: RA(deg) and Dec(Deg)"
    exit -1
else
 RA=$1; DEC=$2
fi

#-----------------------------------------------------------------------
#The Heavy lift!
#-----------------------------------------------------------------------

curl -s "https://www.swift.ac.uk/2SXPS/doSimpleSearch.php?dataOnly=1&searchpos="$RA"%20"$DEC"8&searchrad="$RADIUS"&searchWhat=0&getErr=1&getWhat="$VERBOSITY"&subset=0&coordType="$SUMERIAN"&retType="$FORMAT"" > $TFt

sed '1,/^.*START DATA/d;/^ *$/d;/^.*END DATA/,$d;' $TFt

#-----------------------------------------------------------------------
# options (DOCUMENTATION)
#-----------------------------------------------------------------------
# dataOnly=1. Mandatory, otherwise you will get a full HTML page.
# searchpos: Mandatory. A string: either a position or a source
#	name to resolve via SIMBAD. The position is hopefully freeform -
#	anything that works on the website, but just make sure it's properly
#	escaped before sending over the URL, so that it gets parsed properly.
# searchrad:  radius to search, in arcsec: if not supplied will be 20
# searchWhat: Mandatory. Which catalogue to search: 
#	0 = sources table, 1 = datasets table
# getErr: Optional. Whether to get error columns. 
#	Should be "on" (i.e. getErr=on) if desired
# getWhat: Mandatory - which columns to get. 
#	Values can be 1 = Basic, 2 = All, 3 = Minimal
# subset: Mandatory if querying the sources table: which subset of
#	sources to get. Values can be 0 = whole catalogue, 1 = clean set,
#	2 = ultra-clean set
# dssubset : Mandatory if querying the dataset table: which subset
#       of datasets to get. 
#	Values can be 0 = all, 1 = individual observations, 2 = stacked image
# coordType: Mandatory. Format for the returned coordinates. 
#	0 = sexagesimal, 1 = decimal
# retType: Mandatory: format of output. 
#	0 = HTML, 1 = CSV, 2 = TSV
