#!/bin/sh
#=======================================================================
# Plot time series created by tseries.dk                  --- tsplot ---
# $Id: tsplot_jobdef 657 2012-02-22 23:48:49Z acrnrls $
#=======================================================================
#     keyword :: tsplot
# description :: Plot time series
#
# Larry Solheim  ...May,2008

 set -a
 . betapath2

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

 jobname=tsplot ; time="7200" ; memory="1000mb"

 # bail is a simple error exit routine
 bail(){
   echo "tsplot: "$1
   echo " " >> haltit
   exit 1
 }

 # TS_file is the name of the 'ts' file containing time series to plot
 TS_file=''

 # TS_yoff is a year offset used to set time axis values
 # when TS_plot is set 'on'. This does not affect any fields
 # written to the concatenated output file.
 TS_yoff=0

 # gz_start_year is the first year of any data from the gz file
 # This is required because time info from the gz file is not reliable
 gz_start_year=''

 # gz_avg_start, if set > 1 will average the first gz_avg_start elements
 # of any gz TIME records (after concatenation)
 gz_avg_start=''

 # old_gz flags a particular treatment of GZ TIME records
 # old_gz is true if set, unless it is set to "off"
 old_gz=''

 # TS_ptitle will form part of the plot title of every plot
 TS_ptitle=''

 # plunit is only used when TS_plot is set 'on'
 plunit="VIC"

 nextjob=on
 noprint=on

 # This is required for ocean data or high resolution atm files
 lopgm=lopgm_o4xa
 this_host=`hostname|cut -d'.' -f1`
 case $this_host in
   # This is pollux, use 64 bit binaries
   ib3-*) lopgm=lopgm_o4xal ;;
 esac

 # tsplot_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. If no
 # modifier exists for a particular suffix then all months with that
 # suffix will be added to the file list.
 tsplot_suffix_list='ts'

 # These variables are set when the job string is created
 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

 year=yyy; mon=mm   # memory99=1

 runid="tsplot"; uxxx='uxxx'; tsplot_uxxx=$uxxx;
 tsplot_prefix="${tsplot_uxxx}_${runid}"   # memory99=1
 tsplot_prefix_=${tsplot_prefix}_          # memory99=1
 crawork="${runid}_job"; username="acrnxxx"; user="XXX";

 # ---Start_submit_ignore_code----

 # Time series files used will be for months in the range from
 # tsplot_start_year,tsplot_start_mon to tsplot_end_year,tsplot_end_mon
 [ $current_year = "NotSet" -o $current_month = "NotSet" ] && \
   bail "current_year or current_month is not set"
 if [ $previous_year = "NotSet" -o $previous_month = "NotSet" ]; then
   [ $next_year = "NotSet" -o $next_month = "NotSet" ] && \
     bail "tsplot: Neither previous_(year|month) nor next_(year|month) are set"
   tsplot_start_year=$current_year
   tsplot_start_mon=$current_month
   tsplot_end_year=$next_year
   tsplot_end_mon=$next_month
 else
   tsplot_start_year=$previous_year
   tsplot_start_mon=$previous_month
   tsplot_end_year=$current_year
   tsplot_end_mon=$current_month
 fi
 tsplot_start_year=`echo $tsplot_start_year|awk '{printf "%3.3d",$1}' -`
 tsplot_start_mon=`echo $tsplot_start_mon|awk '{printf "%2.2d",$1}' -`
 tsplot_end_year=`echo $tsplot_end_year|awk '{printf "%3.3d",$1}' -`
 tsplot_end_mon=`echo $tsplot_end_mon|awk '{printf "%2.2d",$1}' -`

 # Perform checks on variables that are set dynamically
 [ -z "$tsplot_start_year" ] && bail "tsplot_start_year is null"
 [ -z "$tsplot_end_year" ]   && bail "tsplot_end_year is null"
 [ -z "$tsplot_start_mon" ]  && bail "tsplot_start_mon is null"
 [ -z "$tsplot_end_mon" ]    && bail "tsplot_end_mon is null"
 [ $tsplot_start_mon -gt 12 -o $tsplot_start_mon -lt 1 ] &&\
   bail "tsplot_start_mon=$tsplot_start_mon is out of range"
 [ $tsplot_end_mon -gt 12 -o $tsplot_end_mon -lt 1 ] &&\
   bail "tsplot_end_mon=$tsplot_end_mon is out of range"
 [ $tsplot_start_year -gt $tsplot_end_year ] &&\
   bail "tsplot_start_year=$tsplot_start_year is out of range"

 # range is a string used in the output file name
 # This extra step of defining start_stop_equal is required to get around
 # a bug in submit3 (the same bug that causes problems in the while loop below)
 start_stop_equal=0
 [ $tsplot_start_year -eq $tsplot_end_year -a \
   $tsplot_start_mon  -eq $tsplot_end_mon ] && start_stop_equal=1
 if [ $start_stop_equal -eq 1 ]; then
   # When start and stop times are equal, range is of the form YYY_mMM
   range="${tsplot_start_year}_m${tsplot_start_mon}"
 else
   # range is a string of the form YYYmMM_YYYmMM
   range="${tsplot_start_year}m${tsplot_start_mon}"
   range="${range}_${tsplot_end_year}m$tsplot_end_mon"
 fi

 # tsplot_runavg>0 will add a running average to each plot
 # The running average will be over tsplot_runavg elements of each time series
 tsplot_runavg=0

# Note: TS_[start|stop]_[year|mon] will override tsplot_[start|stop]_[year|mon]
#       if TS_* variables are set by the user

# TS_start_year is the first year to plot
  TS_start_year=${TS_start_year:=$tsplot_start_year}

# TS_start_mon is the first month of TS_start_year to plot
  TS_start_mon=${TS_start_mon:=$tsplot_start_mon}

# TS_stop_year is the last year to plot
  TS_stop_year=${TS_stop_year:=$tsplot_end_year}

# TS_stop_mon is the last month of TS_stop_year to plot
  TS_stop_mon=${TS_stop_mon:=$tsplot_end_mon}

# TS_plot_all_time=1 means do not use TS_[start|stop]_[year|mon] to determine
# the window along the time axis to display on plots but rather display all
# times that are present in the data.
# TS_plot_all_time=0 means use TS_[start|stop]_[year|mon] as defined
  TS_plot_all_time=${TS_plot_all_time:=0}
  [ "$TS_plot_all_time" = 'on'  ] && eval TS_plot_all_time\=1
  [ "$TS_plot_all_time" = 'off' ] && eval TS_plot_all_time\=0
  [ "$TS_plot_all_time" = 'yes' ] && eval TS_plot_all_time\=1
  [ "$TS_plot_all_time" = 'no'  ] && eval TS_plot_all_time\=0

 # <<INSERT>> any user supplied variable definitions here.

 if [ -n "$TS_file" ]; then

   # There is a single input time series file
   tsplot01="$TS_file"

 else

   # Generate a list of time series file names and assign these names
   # to variables tsplot01, tsplot02, ...
   tsplot_curr_year=`echo $tsplot_start_year|awk '{y=$1-1;printf "%3.3d", y}' -`
   nnts=0
   while [ $tsplot_curr_year -lt $tsplot_end_year ]; do
     tsplot_curr_year=`echo $tsplot_curr_year|awk '{y=1+$1;printf "%3.3d", y}' -`
     if [ $tsplot_curr_year -eq $tsplot_start_year ]; then
       mm=`echo $tsplot_start_mon|awk '{m=$1-1;printf "%2.2d", m}' -`
     else
       mm=0
     fi
     if [ $tsplot_curr_year -eq $tsplot_end_year ]; then
       mm_end=$tsplot_end_mon
     else
       mm_end=12
     fi
     while [ $mm -lt $mm_end ]; do
       mm=`echo $mm|awk '{m=1+$1;printf "%2.2d", m}' -`
       bname=${tsplot_prefix_}${tsplot_curr_year}_m${mm}_
       for suffix in $tsplot_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
               if [ $nnts -lt 99 ]; then
                 nnts=`echo $nnts|awk '{printf "%2.2d",$1+1}' -`
               else
                 nnts=`echo $nnts|awk '{printf "%3.3d",$1+1}' -`
               fi
               eval tsplot${nnts}=$bname$suffix
             fi
           done
         else
           if [ $nnts -lt 99 ]; then
             nnts=`echo $nnts|awk '{printf "%2.2d",$1+1}' -`
           else
             nnts=`echo $nnts|awk '{printf "%3.3d",$1+1}' -`
           fi
           eval tsplot${nnts}=$bname$suffix
         fi
       done
  
     done
   done

 fi

 # ---Stop_submit_ignore_code----

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

 . tsplot.dk

#end_of_job
