#!/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

cd ..

for year in $(seq ${eyear} -1 ${syear})
do
	echo -n "Calculating  $year WINTER ... "
	year0=`expr $year - 1`
	if [ "$obs" == "aeronet" ]; then
		(time ipython preproc/nc2scores_${obs}.py all ${obs} ${year0}12,${year}01,${year}02 &> ${year}12_${year}02.log);
	elif [ "$obs" == "modis" ]; then
            for mod in `cat ./conf/models.json | grep '": {' | awk -F: '{ printf $1 }' | sed 's/"//g'`
	    do
		(time ipython preproc/nc2scores_${obs}.py $mod ${obs} ${year0}12,${year}01,${year}02 &> ${year}12_${year}02.log);
	    done
	fi
	wait
	echo "done.";
	echo -n "Calculating  $year SPRING ... "
	if [ "$obs" == "aeronet" ]; then
		(time ipython preproc/nc2scores_${obs}.py all ${obs} ${year}03,${year}04,${year}05 &> ${year}03_${year}05.log);
	elif [ "$obs" == "modis" ]; then
            for mod in `cat ./conf/models.json | grep '": {' | awk -F: '{ printf $1 }' | sed 's/"//g'`
		do
		(time ipython preproc/nc2scores_${obs}.py $mod ${obs} ${year}03,${year}04,${year}05 &> ${year}03_${year}05.log);
		done
	fi
	wait
	echo "done.";
	echo -n "Calculating  $year SUMMER ... ";
	if [ "$obs" == "aeronet" ]; then
		(time ipython preproc/nc2scores_${obs}.py all ${obs} ${year}06,${year}07,${year}08 &> ${year}06_${year}08.log);
	elif [ "$obs" == "modis" ]; then
            for mod in `cat ./conf/models.json | grep '": {' | awk -F: '{ printf $1 }' | sed 's/"//g'`
		do
		(time ipython preproc/nc2scores_${obs}.py $mod ${obs} ${year}06,${year}07,${year}08 &> ${year}06_${year}08.log);
		done
	fi
	wait
	echo "done.";
	echo -n "Calculating  $year AUTUMN ... ";
	if [ "$obs" == "aeronet" ]; then
		(time ipython preproc/nc2scores_${obs}.py all ${obs} ${year}09,${year}10,${year}11 &> ${year}09_${year}11.log);
	elif [ "$obs" == "modis" ]; then
            for mod in `cat ./conf/models.json | grep '": {' | awk -F: '{ printf $1 }' | sed 's/"//g'`
		do
		(time ipython preproc/nc2scores_${obs}.py $mod ${obs} ${year}09,${year}10,${year}11 &> ${year}09_${year}11.log);
		done
	fi
	wait
	echo "done."
done
