#! /bin/bash
#*******************************************************************************************************
# a script that reads output from qpfStats.sh and produces an image per date/experiment combination
# using only stations-points data
#
# usage: $0 inFile
#*******************************************************************************************************
set -ue

#************************************************************************************** global constants
declare -r stationCoords=$(dirname $0)/var/stationsQpf.txt
declare -r outTxtDir=$(dirname $0)/text
declare -r outPngDir=$(dirname $0)/images
declare -r pallete=$(dirname $0)/var/rain.cpt

#************************************************************************************* getStationCoord()
# parses $stationCoords until it locates a requested station and returns its lon-lat
#*******************************************************************************************************
function getStationCoord()
{
local -r station=$1
awk -v name=$1 '($3==name) {print $1, $2; exit; }' $stationCoords
}


#*******************************************************************************************************
# creates all the lon-lat-totPreci triplets files for every date-expNo combination
#***************************************************************************** produceTxt()
function produceTxt()
{
local -r kind=${1:-contours}
local date expNo station totPreci outFile mmValue
local -i mm
[ -d $outTxtDir ] || mkdir -pv $outTxtDir || return 1
rm -fvr $outTxtDir/* || echo nothing to rm in $outTxtDir
while  read -e -u 10 -a line; do
  [ ${#line[*]} -eq 6 ] || continue
  date=${line[0]}; expNo=${line[1]}; station=${line[2]}; totPreci=${line[3]}
  [ -n "${date//[\-0-9]/}" ] && { echo ignored ${line[*]}; continue; }
  outFile=${date}_$expNo.txt
  [ -f $outFile ] || echo creating $outFile for exp. No$expNo of $date
  if [ $kind == contours ]; then
    echo $(getStationCoord ${station#*_}) $totPreci>>$outTxtDir/$outFile
  elif [ $kind == points ]; then 
    mm=${totPreci%.*}; mmValue=[${mm}]; [ $mm -eq 0 ] && mmValue="";
    echo $(getStationCoord ${station#*_}) $mmValue>>$outTxtDir/_$outFile
    fi
  done
}

#****************************************************************************************** producePng()
function producePng()
{
[ -r $pallete ] || { echo cannot locate $pallete; return 1; } 
[ -d $outPngDir ] || mkdir -pv $outPngDir || return 1
local -r kind=${1:-contours}
local txtFiles txtFile pngFile title
if [ $kind == contour ]; then
  txtFiles=$(ls $outTxtDir/[^_]*.txt)
else
  txtFiles=$(ls $outTxtDir/_*.txt)
  fi
for txtFile in $txtFiles; do
  pngFile=$outPngDir/$(basename $txtFile); pngFile=${pngFile%.txt}
  title="QPF: $(basename $pngFile)"
  if [ $kind == contours ]; then
    gmtMap.pl -g 120 -z -c $txtFile -C $pallete -o $pngFile -t "$title"
  elif [ $kind == points ]; then 
    gmtMap.pl -g 120 -z -a B5 -p $txtFile -o $pngFile -t "$title points"
    fi
  done
rm -fv $outPngDir/*.ps
}

#************************************************************************************************** main
[ $# -lt 1 ] && { echo usage: $0 inCombined file; exit 0; }
[ -r $1 ] || { echo cannot read file $1; exit 1; }
produceTxt 0<$1
producePng
produceTxt points 0<$1
producePng points
