#!/bin/sh
#=======================================================================
# Extract various quantities from td files                 --- xtd00 ---
# $Id: xtd00_jobdef 616 2009-05-29 22:21:17Z acrnrls $
#=======================================================================
#
# Original code written by Charles McLandress Feb/07
# Modified by Larry Solheim Feb/07 to be used by cccjob
#=======================================================================
#
#     keyword :: xtd00
# description :: extract various quantities from td files
#        hide :: yes
#

 set -a
 . betapath2

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

 jobname=xtd00;
 runid="job000"; uxxx='uxxx'; xtd00_uxxx=$uxxx;
 xtd00_prefix=${xtd00_uxxx}_$runid
 xtd00_prefix_=${xtd00_prefix}_
 crawork="${runid}_job"; username="acrnxxx"; user="XXX"

 stime="600"; memory1="16mb"; lopgm="lopgm";

 # xtd00_suffix_list is a white space separated list of suffixes
 # of file names to be generated here. Any suffix in this list may be
 # modified by appending a + followed by a comma separated list of
 # numbers (no white space is allowed within this modifier). Each
 # number within the modifier list will correspond to a month (1-12)
 # for which a file with this suffix is to be included. If the
 # modifier exists for a particular suffix then only those months
 # indicated in the modifier will be added to the file list.
 xtd00_suffix_list='td'

 # 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 "xtd00: current_year or current_month is not set"
   exit 1
 fi

 # files will be for months in the range from
 # xtd00_start_year,xtd00_start_mon to xtd00_end_year,xtd00_end_mon
 if [ $previous_year = "NotSet" -o $previous_month = "NotSet" ]; then
   if [ $next_year = "NotSet" -o $next_month = "NotSet" ]; then
     echo "xtd00: Neither previous_(year|month) nor next_(year|month) are set"
     exit 1
   else
     xtd00_start_year=$current_year
     xtd00_start_mon=$current_month
     xtd00_end_year=$next_year
     xtd00_end_mon=$next_month
   fi
 else
   xtd00_start_year=$previous_year
   xtd00_start_mon=$previous_month
   xtd00_end_year=$current_year
   xtd00_end_mon=$current_month
 fi

 xtd00_start_year=`echo $xtd00_start_year|awk '{printf "%3.3d",$1}' -`
 xtd00_start_mon=`echo $xtd00_start_mon|awk '{printf "%2.2d",$1}' -`
 xtd00_end_year=`echo $xtd00_end_year|awk '{printf "%3.3d",$1}' -`
 xtd00_end_mon=`echo $xtd00_end_mon|awk '{printf "%2.2d",$1}' -`

 if [ -z "$xtd00_start_year" ]; then
   echo "xtd00: xtd00_start_year is null"
   exit 1
 fi
 if [ -z "$xtd00_end_year" ]; then
   echo "xtd00: xtd00_end_year is null"
   exit 1
 fi
 if [ -z "$xtd00_start_mon" ]; then
   echo "xtd00: xtd00_start_mon is null"
   exit 1
 fi
 if [ -z "$xtd00_end_mon" ]; then
   echo "xtd00: xtd00_end_mon is null"
   exit 1
 fi
 if [ $xtd00_start_mon -gt 12 -o $xtd00_start_mon -lt 1 ]; then
   echo "xtd00: xtd00_start_mon=$xtd00_start_mon is out of range"
   exit 1
 fi
 if [ $xtd00_end_mon -gt 12 -o $xtd00_end_mon -lt 1 ]; then
   echo "xtd00: xtd00_end_mon=$xtd00_end_mon is out of range"
   exit 1
 fi
 if [ $xtd00_start_year -gt $xtd00_end_year ]; then
   echo "xtd00: xtd00_start_year=$xtd00_start_year is out of range"
   exit 1
 fi

 # Generate a list of files
 xtd00_curr_year=`echo $xtd00_start_year|awk '{y=$1-1;printf "%3.3d", y}' -`
 join=0
 while [ $xtd00_curr_year -lt $xtd00_end_year ]; do
   xtd00_curr_year=`echo $xtd00_curr_year|awk '{y=1+$1;printf "%3.3d", y}' -`
   if [ $xtd00_curr_year -eq $xtd00_start_year ]; then
     mm=`echo $xtd00_start_mon|awk '{m=$1-1;printf "%2.2d", m}' -`
   else
     mm=0
   fi
   if [ $xtd00_curr_year -eq $xtd00_end_year ]; then
     mm_end=$xtd00_end_mon
   else
     mm_end=12
   fi
   while [ $mm -lt $mm_end ]; do
     mm=`echo $mm|awk '{m=1+$1;printf "%2.2d", m}' -`

     for suffix in $xtd00_suffix_list; do
       mlist=`echo $suffix|awk -F'+' '{print $2}' -`
       mlist=`echo $mlist|sed 's/,/ /g'`
       if [ -n "$mlist" ]; then
         suffix=`echo $suffix|sed 's/+.*$//'`
         # assume that mlist is a white space separated list of numbers
         # indicating which months to dump
         for xx in $mlist; do
           if [ $xx -eq $mm ]; then
             join=`echo $join|awk '{j=1+$1;printf "%d",j}' -`
             join=`echo $join|sed -e 's/^ *//' -e 's/^0*//'`
             eval file${join}=${xtd00_prefix_}${xtd00_curr_year}_m${mm}_$suffix
           fi
         done
       else
         join=`echo $join|awk '{j=1+$1;printf "%d",j}' -`
         join=`echo $join|sed -e 's/^ *//' -e 's/^0*//'`
         eval file${join}=${xtd00_prefix_}${xtd00_curr_year}_m${mm}_$suffix
       fi
     done

   done
 done

#  * ............................ Condef Parameters ............................

 noprint=on
 nextjob=on

 ugwd=on      # extract u-comp of gravity wave drag
 trad=on      # extract SW and LW heating

#  * ............................. Deck Definition .............................

 time=600 ; memory=900mb

 . comjcl.cdk

cat > Execute_Script <<'end_of_script'

  nfile=0
  while [ $nfile -lt $join ]; do
    nfile=`echo $nfile|awk '{printf "%d",$1+1}' -`
    eval td=\$file$nfile
    access $td $td

    rm -f newtd td1 td2
    if [ "$ugwd" = on ] ; then
      rm -f utpn utpg
ccc   select $td utpn utpg <<EOF
 SELECT.  STEPS         0 999999999    1 LEVS-9001 1000 NAME UTPN UTPG
EOF
      murge utpn utpg newtd
    fi

    if [ "$trad" = on ] ; then
      rm -f ttps ttpl
ccc   select $td ttps ttpl <<EOF
 SELECT.  STEPS         0 999999999    1 LEVS-9001 1000 NAME TTPS TTPL
EOF
      if [ -s newtd ]; then
        murge ttps ttpl td2
        mv newtd td1
        murge td1 td2 newtd
      else
        murge ttps ttpl newtd
      fi
    fi

    release $td

    # save new td file as original file name with "s" appended
    save newtd ${td}s

  done

end_of_script

. endjcl.cdk

#end_of_job

