#!/bin/sh
#############################################################################
#
# r.sun_monthly_sum.sh
#
# SCRIPT:   	r.sun automation
# AUTHOR(S):	Paolo Zatelli - 2003/08/11
# PURPOSE:  	Automatically sums dayly r.sun maps for each month
#   	    	
# COPYRIGHT:	2003 Paolo Zatelli
# LICENZE:	GPL 2.0 or (at your option) any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#############################################################################
# r.sun_monthly_sum.sh sums dayly solar radiation maps, as evaluated by r.sun
# used with the r.sun_year.sh script, for each month. A map for each month is 
# created named after the input map name with suffix indicating the month:
# 'energy_Jan', 'energy_Feb' and so on.
# Beware: you have to be careful on leap years :)

MONTH_LENGTH=(31 28 31 30 31 30 31 31 30 31 30 31)
MONTH_NAME=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
START=0
INPUT=""
DAY=0

if [ $# -ne 1 ] ; then
	echo "Usage: sh r.sun_monthly_sum.sh energy_map"
	echo "Where energy_map is the base name for the energy r.sun output map"
	echo "energy maps must be named energy_map_1 ... energy_map_365"
	exit
fi

START_DATE=`date`
echo "Start: "$START_DATE

for i in $(seq 0 11); do
	echo "Month: ${MONTH_NAME[i]} - lenght ${MONTH_LENGTH[i]} - start $START+1"
 for j in $(seq 1 ${MONTH_LENGTH[i]}); do
	DAY=$(($START+$j))
	if [ $j -eq 1 ]; then
		INPUT=${1}"_"$DAY
	else
		INPUT=$INPUT+${1}"_"$DAY
	fi
 done
echo $INPUT
r.mapcalc ${1}"_"${MONTH_NAME[i]}=$INPUT
INPUT=""
START=$(($START+${MONTH_LENGTH[i]}))

done

echo "Done!"
STOP_DATE=`date`
echo "Start: "$START_DATE
echo "Stop: "$STOP_DATE

