#!/bin/bash

# query TNS by month [1-12], given year YYYY
# output: return tsv file with header line, folllowed by one line per event
#
# qmTNS [-h] [-t type] YYYY [START_MONTH [END_MONTH]]] 
# ..  -h help
# ..  -t type where type=unclassified (u), classified [c] 
#
# output: 
# TNSc_YYYY_MM.tsv (classified) 
# TNSu_YYYY_MM.tsv (unclasffied)
#
# usage: 
# qmTNS      2021 1      % classified events for Jan, 2021 
# qmTNS -t u 2021 1      % unclassified events for Jan, 2021 
# qmTNS      2020 1 2    % classified events for Jan & Feb  2020
# qmTNS      2020        % classified events for all of 2020 
#
#YETI: FRB option not implemented
#YETI=yet to be implemented
#
# S.R.Kulkarni, 3-January-2021
# 5-Jan-2021 ... call for each month specifies the start and end
#                day of the month exactly (not relying on a bug in TNS!)

#=======================================================================
# internal function: gets TNS events for a given month-year
get1month () {
#-----------------------------------------------------------------------

PAGENO=0; N=1
s0="https://www.wis-tns.org/search?&"

printf "%d %d- " $YEAR $MONTH 
	#Number of days in this month is given by $LASTDAY
LASTDAY=$(cal $MONTH $YEAR | xargs | awk '{print $NF}')  

while [ $N -ne 0 ]
do 

  s2="date_start%5Bdate%5D=$YEAR-$MONTH-01&date_end%5Bdate%5D=$YEAR-$MONTH-$LASTDAY&"
  s3="num_page=$NMAX&page=$PAGENO&format=tsv"

  printf "%d:" $PAGENO

  curl -s -o $TFILE "${s0}${s1}${s2}${s3}"
  echo "" >> $TFILE		#supplying \n for last record
  N=$(($(wc -l < $TFILE)-1))	#number of records (header not counted)

		#delete header line for subsequent pages 
  [ $PAGENO -gt 0 ] && sed -it 1d $TFILE 

  cat $TFILE >> $OFILE

  printf "%d " $N
  PAGENO=$((PAGENO+1))

done
return $?
}


#-----------------------------------------------------------------------
#set defaults
#-----------------------------------------------------------------------
	NMAX=500; TYPE=c; HELP=; INVALID=
	FNAME="TNS"
        TFILE="z_$$.tsv"; touch $TFILE ${TFILE}t  #temporary files
	trap "rm $TFILE ${TFILE}t" EXIT		  #clean up after exit

#-----------------------------------------------------------------------
#decode options 
#-----------------------------------------------------------------------
  while getopts t:h OPTION
  do
      case $OPTION in
	t) TYPE=$OPTARG;;        #classified (c) or unclassified (u)
	h) HELP=1;;
	*) INVALID=1;;
      esac
  done
  [ $INVALID ] && exit -1
  shift $(($OPTIND-1))
  if [ $HELP ]; then
     echo "qmTNS [-h] [-t type] YYYY [START_MONTH [END_MONTH]]]"
     echo "-t type "u" for unclassified, "c" for classified"
     echo " MONTH is 1-12 and YYYY is 2016-present"
     exit;
  fi
	
	# call to database determined by request (classified/unclassified)
	# name of output file, $FNAME, constructed accordingly

  case $TYPE in
	[cC]) U=0; C=1; FNAME=$FNAME"c";;
	[uU]) U=1; C=0; FNAME=$FNAME"u";;
	*) echo "quit - invalid choice of type " $TYPE; exit -1;
  esac 
  s1="unclassified_at=$U&classified_sne=$C&include_frb=0&"

#-----------------------------------------------------------------------
# decode & verify: year (YEAR), start-month (MSTART) & end-month (MEND) 
#-----------------------------------------------------------------------

	# at minimum, the YEAR has to be specified
	# note: TNS started in YEAR=2016

  [ $# -lt 1 ] && {echo "quit - year not specified"; exit -1}
  [ $1 -lt 2016 ] && {echo "quit - $1 TNS started in 2016"; exit -1}   
  YEAR=$1; shift

			#Decode the months 
  case $# in
    0) MSTART=01; MEND=12;;
    1) MSTART=$1; MEND=$1;;
    2) MSTART=$1; MEND=$2;;
    *) echo "quit - parameters exceed pair for month start and end"; exit -1;;
  esac
		#months should be on a scale of 01 to 12 (zero prefix)
  MSTART=$(printf "%02d" $MSTART); MEND=$(printf "%02d" $MEND);    

		#simple sanity checks
  if [ $MSTART -gt $MEND ]; then
	echo "quit - start month $MSTART > end month $MEND"; exit -1
  fi
  if [ $MSTART -gt 12 ] || [ $MSTART -lt 1 ] || [ $MEND -gt 12 ] || [ $MEND -lt 1 ]
  then
	echo "quit - invalid month values $MSTART, $MEND"; exit -1
  fi
  
#-----------------------------------------------------------------------
# Loop through the months
#-----------------------------------------------------------------------

  for MONTH in {$MSTART..$MEND}
  do
			#request month-year in the future?
    YR=$(date "+%Y"); M=$(date "+%m"); 
    if [ $YEAR -gt $YR ] || ([ $YEAR -eq $YR  ] && [ $MONTH -gt $M ]); then
      echo "quit - unable to access the future"; exit
    fi

    OFILE="${FNAME}_${YEAR}_$MONTH.tsv"; :>$OFILE

    get1month 

    n=$(sed '/^"ID/d' $OFILE | wc -l)
    print " $OFILE (${n// /} events)"    #elegant way to get rid of blanks in $n

  done