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

if [ "$4" == "" ]; then
	models="all"
else
	models=$4
fi

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

cd ..

for year in $(seq ${eyear} -1 ${syear})
do
	months=""
	for mon in {01..12}
	do
		if [ "$months" == "" ]; then
			months="${year}01"
		else
			months="$months,$year$mon"
		fi
	done
	echo -n "Calculating $year :: $months ... "
	if [ "$obs" == "aeronet" ]; then
		(time ipython preproc/nc2scores_${obs}.py ${models} ${obs} $months &> ${year}.log)
	elif [ "$obs" == "modis" ]; then
	    if [ "$models" == "all" ]; then
		    for mod in `cat ./conf/models.json | grep '": {' | awk -F: '{ printf $1 }' | sed 's/"//g'`
		    do
			echo "Calculating $year for $mod ..."
			(time ipython preproc/nc2scores_${obs}.py ${mod} ${obs} $months &> ${year}_${mod}.log)
		    done
	    else
		    for mod in `echo monarch,cams,dream8-mac | sed 's/,/ /g'`
		    do 
			echo "Calculating $year for $mod ..."
			(time ipython preproc/nc2scores_${obs}.py ${mod} ${obs} $months &> ${year}_${mod}.log)
		    done
	    fi
	fi
	echo "done.";
done
