#!/usr/bin/env bash

if [ "$1" == "" ]; then
	echo "Start year required. Exit."
	exit 1
elif [ "$2" == "" ]; then
	echo "End year required. Exit."
	exit 1
elif [ "$3" == "" ]; then
	echo "Observation required. Exit."
	exit 1
fi

syear=$1
eyear=$2
obs=$3
month=$4

if [ "$month" == "" ]; then
	month="all"
elif [ "$month" == "01" ]; then
	month0="12"
else
	month0=$(printf "%02d" `expr $month - 1`)
fi

function calc_monthly ()
{
	cur_year=$1
	cur_month=$2
	PYTHON=$HOME/dust-dashboard/bin/ipython
	echo -n "Calculating $cur_year $cur_month ... "
	time $PYTHON preproc/nc2scores_${obs}.py all ${obs} ${cur_year}${cur_month} &> ${cur_year}${cur_month}_${obs}.log
	wait
	echo "done.";
}

cd ..

for year in $(seq ${eyear} -1 ${syear})
do
	year0=`expr $year - 1`
	case $month in
	01) # monthly, # annual
		calc_monthly $year0 $month0
		./calc_annual.sh $year0 $year0 $obs
		;;
	03) # monthly, # seasonal
		calc_monthly $year $month0
		./calc_seasonal.sh $year $year $obs winter
		;;
	06) # monthly, # seasonal
		calc_monthly $year $month0
		./calc_seasonal.sh $year $year $obs spring
		;;
	09) # monthly, # seasonal
		calc_monthly $year $month0
		./calc_seasonal.sh $year $year $obs summer
		;;
	12) # monthly, # seasonal
		calc_monthly $year $month0
		./calc_seasonal.sh $year $year $obs autumn
		;;
	all) # annual
		./calc_annual.sh $year $year $obs
		;;
	*) # monthly
		calc_monthly $year $month0
	esac
done
