#!/bin/sh
#=======================================================================
# Create an _rf file from a _gs file                  --- rf_extract ---
# $Id: rf_extract_jobdef 616 2009-05-29 22:21:17Z acrnrls $
#=======================================================================
#
# gs and ss files used will be of the form
#   ${rf_extract_prefix}_${year}_m${mon}_[gs|ss]
# 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_extract_start_year overrides previous_year
# explicitly setting rf_extract_start_mon  overrides previous_month
# explicitly setting rf_extract_end_year   overrides current_year
# explicitly setting rf_extract_end_mon    overrides current_month
#
# rf_extract_prefix_ can be set to override the value of rf_extract_prefix
# files dumped will then be of the form
#    ${rf_extract_prefix_}${year}_m${mon}_$suffix
# note the missing underscore between rf_extract_prefix_ and year.
#=======================================================================
#
#     keyword :: rf_extract
# description :: extract radiative forcing data from history files
#

set -a
. betapath2

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

jobname=rf_extract; 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_extract_uxxx=$uxxx;
 rf_extract_prefix=${rf_extract_uxxx}_$runid
 rf_extract_prefix_=${rf_extract_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_extract: current_year or current_month is not set"
   exit 1
 fi

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

 # format year/month strings
 rf_extract_start_year=`echo $rf_extract_start_year|awk '{printf "%3.3d",$1}' -`
 rf_extract_start_mon=`echo $rf_extract_start_mon|awk '{printf "%2.2d",$1}' -`
 rf_extract_end_year=`echo $rf_extract_end_year|awk '{printf "%3.3d",$1}' -`
 rf_extract_end_mon=`echo $rf_extract_end_mon|awk '{printf "%2.2d",$1}' -`

 # cursory checks on year/month values
 if [ -z "$rf_extract_start_year" ]; then
   echo "rf_extract: rf_extract_start_year is null"
   exit 1
 fi
 if [ -z "$rf_extract_end_year" ]; then
   echo "rf_extract: rf_extract_end_year is null"
   exit 1
 fi
 if [ -z "$rf_extract_start_mon" ]; then
   echo "rf_extract: rf_extract_start_mon is null"
   exit 1
 fi
 if [ -z "$rf_extract_end_mon" ]; then
   echo "rf_extract: rf_extract_end_mon is null"
   exit 1
 fi
 if [ $rf_extract_start_mon -gt 12 -o $rf_extract_start_mon -lt 1 ]; then
   echo "rf_extract: rf_extract_start_mon=$rf_extract_start_mon is out of range"
   exit 1
 fi
 if [ $rf_extract_end_mon -gt 12 -o $rf_extract_end_mon -lt 1 ]; then
   echo "rf_extract: rf_extract_end_mon=$rf_extract_end_mon is out of range"
   exit 1
 fi
 if [ $rf_extract_start_year -gt $rf_extract_end_year ]; then
   echo "rf_extract: rf_extract_start_year=$rf_extract_start_year is out of range"
   exit 1
 fi

 # Extract radiative forcing data from history files, generate monthly
 # averages and then save it to DATAPATH/RUNPATH
 rf_extract_curr_year=`echo $rf_extract_start_year|\
                         awk '{y=$1-1;printf "%3.3d", y}' -`
 join=0
 while [ $rf_extract_curr_year -lt $rf_extract_end_year ]; do
   rf_extract_curr_year=`echo $rf_extract_curr_year|\
                           awk '{y=1+$1;printf "%3.3d", y}' -`
   if [ $rf_extract_curr_year -eq $rf_extract_start_year ]; then
     mm=`echo $rf_extract_start_mon|awk '{m=$1-1;printf "%2.2d", m}' -`
   else
     mm=0
   fi
   if [ $rf_extract_curr_year -eq $rf_extract_end_year ]; then
     mm_end=$rf_extract_end_mon
   else
     mm_end=12
   fi
   while [ $mm -lt $mm_end ]; do
     mm=`echo $mm|awk '{m=1+$1;printf "%2.2d", m}' -`
     GS=${rf_extract_prefix_}${rf_extract_curr_year}_m${mm}_gs
     rf_extract -nocopy $GS
   done
 done
end_of_script

. endjcl.cdk

#end_of_job

