#!/bin/sh
#=======================================================================
# Average a number of years of monthly averaged _rf files --- rf_avg ---
# and create time series of select radiative forcing fields
# $Id: rf_avg_jobdef 616 2009-05-29 22:21:17Z acrnrls $
#=======================================================================
#
# rf files used will be of the form
#   ${rf_avg_prefix}_${year}_m${mon}_rf
# for all months from previous_year, previous_month to current_year,
# current_month or from current_year, current_month to next_year,
# next_month depending on which of previous_(year|month) or
# next_(year|month) are set.
#
# The variables current_year, current_month, previous_year and
# previous_month are set when the job string is created.
#
# explicitly setting rf_avg_start_year overrides previous_year
# explicitly setting rf_avg_start_mon  overrides previous_month
# explicitly setting rf_avg_end_year   overrides current_year
# explicitly setting rf_avg_end_mon    overrides current_month
#
# rf_avg_prefix_ can be set to override the value of rf_avg_prefix
# files dumped will then be of the form
#    ${rf_avg_prefix_}${year}_m${mon}_$suffix
# note the missing underscore between rf_avg_prefix_ and year.
#=======================================================================
#
#     keyword :: rf_avg
# description :: radiative forcing average climatology time series
#

set -a
. betapath2

#  * ........................... Parmsub Parameters ............................

jobname=rf_avg; username="acrnxxx"; user="XXX"
runid="job000"; crawork="${runid}_job"

time="3600"; memory="1000mb"
nextjob=on
noprint=on

. comjcl.cdk
cat > Execute_Script <<'end_of_script'

 runid="job000"; uxxx='uxxx'; rf_avg_uxxx=$uxxx;
 rf_avg_prefix=${rf_avg_uxxx}_$runid
 rf_avg_prefix_=${rf_avg_prefix}_

 # These variables are set when the job string is created
 # Normally only one of previous_(year|month) or next_(year|month) are set
 # while current_(year|month) must always be set.
 run_start_year=NotSet
 run_start_month=NotSet
 run_stop_year=NotSet
 run_stop_month=NotSet
 current_year=NotSet
 current_month=NotSet
 previous_year=NotSet
 previous_month=NotSet
 next_year=NotSet
 next_month=NotSet
 if [ $current_year = "NotSet" -o $current_month = "NotSet" ]; then
   echo "rf_avg: current_year or current_month is not set"
   exit 1
 fi

 # files loaded from cfs will be for months in the range from
 # rf_avg_start_year,rf_avg_start_mon  to
 # rf_avg_end_year,  rf_avg_end_mon
 if [ $previous_year = "NotSet" -o $previous_month = "NotSet" ]; then
   if [ $next_year = "NotSet" -o $next_month = "NotSet" ]; then
     echo "rf_avg: Neither previous_(year|month) nor next_(year|month) are set"
     exit 1
   else
     rf_avg_start_year=$current_year
     rf_avg_start_mon=$current_month
     rf_avg_end_year=$next_year
     rf_avg_end_mon=$next_month
   fi
 else
   rf_avg_start_year=$previous_year
   rf_avg_start_mon=$previous_month
   rf_avg_end_year=$current_year
   rf_avg_end_mon=$current_month
 fi

 # format year/month strings
 rf_avg_start_year=`echo $rf_avg_start_year|awk '{printf "%3.3d",$1}' -`
 rf_avg_start_mon=`echo $rf_avg_start_mon|awk '{printf "%2.2d",$1}' -`
 rf_avg_end_year=`echo $rf_avg_end_year|awk '{printf "%3.3d",$1}' -`
 rf_avg_end_mon=`echo $rf_avg_end_mon|awk '{printf "%2.2d",$1}' -`

 # cursory checks on year/month values
 if [ -z "$rf_avg_start_year" ]; then
   echo "rf_avg: rf_avg_start_year is null"
   exit 1
 fi
 if [ -z "$rf_avg_end_year" ]; then
   echo "rf_avg: rf_avg_end_year is null"
   exit 1
 fi
 if [ -z "$rf_avg_start_mon" ]; then
   echo "rf_avg: rf_avg_start_mon is null"
   exit 1
 fi
 if [ -z "$rf_avg_end_mon" ]; then
   echo "rf_avg: rf_avg_end_mon is null"
   exit 1
 fi
 if [ $rf_avg_start_mon -gt 12 -o $rf_avg_start_mon -lt 1 ]; then
   echo "rf_avg: rf_avg_start_mon=$rf_avg_start_mon is out of range"
   exit 1
 fi
 if [ $rf_avg_end_mon -gt 12 -o $rf_avg_end_mon -lt 1 ]; then
   echo "rf_avg: rf_avg_end_mon=$rf_avg_end_mon is out of range"
   exit 1
 fi
 if [ $rf_avg_start_year -gt $rf_avg_end_year ]; then
   echo "rf_avg: rf_avg_start_year=$rf_avg_start_year is out of range"
   exit 1
 fi

 # Average radiative forcing data in monthly averaged _rf files, create
 # time series of monthly averages and save to DATAPATH/RUNPATH
 startrf="${rf_avg_start_year}:$rf_avg_start_mon"
 stoprf="${rf_avg_end_year}:$rf_avg_end_mon"
 rf_avg -p $rf_avg_prefix -s "$startrf" -e "$stoprf"

end_of_script

. endjcl.cdk

#end_of_job

