#!/bin/bash
# queries TNS and writes file of classified SN per month in TSV format
#
#-----------------------------------------------------------------------
# usage: QueryTNS_SN year [month_start [month_end]]
#-----------------------------------------------------------------------
#
# Examples:
# QueryTNS_SN 2018 (Jan thru Dec of 2018)
# QueryTNS_SN2018 2 3 (February & March 2018)
# QueryTNS_SN 2018 6 (only June 2018)
# Output: SN_YYYY_MM.tsv e.g. SN_2018_6.tsv for above example
#
# Warning: issued if more SN found than internal TNS buffer size
#
# Obscure Usage: if year is negative then header line is included
# QueryTNS_SN -2018 2 3
#
# Weaknesses:
# no error checking for valid years or months
# all months have 31 days! (apparently TNS does not mind this)
#
# 30 December 2019 (SRK)
#-----------------------------------------------------------------------
MAX=500 #TNS, as of 2019, set a limit of 500 records
#-----------------------------------------------------------------------
get1month () {
#-----------------------------------------------------------------------
#
#usage: get1month(YYYY,M) where YYYY is year & M=[1-12] is month number
# output file SN_YYYY_MM.tsv (with one line er SN from TNS)
# if YYYY<0 then header is included in the output.
Year=$1; Month=$2;
#build output file name
sgn=1;
[ $1 -lt 0 ] && { Year=${Year:1}; sgn=-1; }
Outfile="SN_"$Year"_"$(printf "%02d" $2)".tsv"
#compose start and end date for search
start_date=$Year"-"$Month"-"01
end_date=$Year"-"$Month"-"31
#---------------forming string for inquiry with TNS--------------------
Head="https://www.wis-tns.org/search?&classified_sne=1"
Date="&date_start%5Bdate%5D="
Date+=$start_date
Date+="&date_end%5Bdate%5D="
Date+=$end_date
Tail="&num_page="
Tail+=$MAX #MAX is maximum buffer size, set by TNS
Tail+="&format=tsv"
cmd=$Head$Date$Tail #html inquiry now fully formed
#----------------launch call to TNS-------------------------------------
echo -n "Date range: " $start_date to $end_date ".."
if [ $sgn = 1 ]; then
curl -s "$cmd" | sed '1d' > $Outfile
nSN=$(wc -l < $Outfile)
else
curl -s "$cmd" | sed -n 'p' > $Outfile
nSN=$(grep -c "" $Outfile)
nSN=$((nSN-1)) #do not count header line
fi
echo " outfile:" $Outfile " $nSN (SNe)"
#---------------------Check buffer size---------------------------------
[ $nSN -eq $MAX ] && echo "Warning; internal buffer size insufficient"
return $?
}
#-----------------------------------------------------------------------
# Main program
#---------------check input parameters, determine months ----------------
#check if input parameters are sufficient
[ $# = 0 ] && { echo "no year"; exit -1; }
#determine the range of months
[ $# = 1 ] && { mstart=1; mend=12; }
[ $# = 2 ] && { mstart=$2; mend=$2; }
[ $# = 3 ] && { mstart=$2; mend=$3; }
for ((Month = mstart; Month <= mend; Month += 1)); do
get1month $1 $Month
done