#!/bin/sh
#=======================================================================
# Run tseries.dk as a diagnostic string                   --- tsdiag ---
# tseries.dk will create a time series file containing selected
# variables from history or diagnostic files
# $Id: tsdiag_jobdef 616 2009-05-29 22:21:17Z acrnrls $
#=======================================================================
#     keyword :: tsdiag
# description :: Run tseries as a diag deck (1 month at a time)
#
# Larry Solheim  ...Jul,2008

 set -a
 . betapath2

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

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

 jobname=tsdiag; time="2400" ; memory="1000mb"; lopgm=lopgm

 year=yyy; mon=mm   # memory99=1

 crawork=${runid}_tsdiag; username="acrnxxx"; user="XXX"
 runid=aaa; uxxx=uxxx;

 # RUNPATH may be set if desired
 RUNPATH=$RUNPATH

 # It is often more efficient to set CCRNTMP = $RUNPATH
 CCRNTMP=$CCRNTMP

 # Alternate path to a directory where .queue/.crawork will be found
 JHOME=''

 if [ -n "$JHOME" -a x"$JHOME" != x"$HOME" ]; then
   # Allow optional reset of DATAPATH/RUNPATH
   JHOME_DATA=''
   DATAPATH=${JHOME_DATA:=$DATAPATH}
   RUNPATH=${JHOME_DATA:=$RUNPATH}
   # Allow optional reset of CCRNTMP
   JHOME_RUN=''
   CCRNTMP=${JHOME_RUN:=$CCRNTMP}
 fi

 # CCCma file names are typically of the form
 #   ${uxxx}_${runid}_${year}_m${mon}_$suffix
 # Diagnostic files have a "gp" or "xp" suffix and history files have
 # an "ss", "gs", "td", "gz" or "cm" suffix. Other suffixes are used in
 # special cases. In general the prefix "${uxxx}_${runid}_" can be
 # anything but for "official" runs uxxx must be a particular 2 character
 # string and runid is assigned.

 # tsdiag_uxxx is the first part of output time series file names.
 # The default value of tsdiag_uxxx is set to 'sc' which is what is
 # required for an "official" time series file name in a coupled run.
 # The user should change this as desired.
 # Note: The "# memory99=1" comment appended to these lines is required
 #       to circumvent a bug in the submission scripts.
 tsdiag_uxxx='sc'           # memory99=1
 tsdiag_flabel_uxxx=$uxxx   # memory99=1
 tsdiag_model_uxxx=$uxxx    # memory99=1

 tsdiag_prefix=${tsdiag_uxxx}_${runid}                # memory99=1
 tsdiag_flabel_prefix=${tsdiag_flabel_uxxx}_${runid}  # memory99=1
 tsdiag_model_prefix=${tsdiag_model_uxxx}_${runid}    # memory99=1

 # If flabel is set non null then it will be used to determine file names
 # of diagnostic files. In particular, it will look for files with names
 # like ${flabel}gp or ${flabel}xp
 # These names will, in turn, be overidden by the corresponding TS_XXfile
 # variable, if set below.
 flabel="${tsdiag_flabel_prefix}_${year}_m${mon}_";

 # If model1 is set non null then it will be used to determine file names
 # of history files. In particular, it will look for files with names
 # like ${model1}gs, ${model1}ss, ${model1}td, ${model1}gz, etc.
 # These names will, in turn, be overidden by the corresponding TS_XXfile
 # variable, if set below.
 model1="${tsdiag_model_prefix}_${year}_m${mon}_";

 # TS_file is the name under which the output file containing the time series
 # will be saved. If TS_file already exists it will be appended to,
 # if it does not exist it will be created (except see TS_file_clobber)
 TS_file=${tsdiag_prefix}_${year}_m${mon}_ts

 # If TS_file_clobber is set to on then the file named in TS_file
 # will always be overwritten
 TS_file_clobber=off

 # TS_file_split flags writing multiple output files with one variable
 # per output file. Each individual file will be created if it does not
 # exist or appended to if it exists and TS_file_clobber is false.
 TS_file_split=''

 # TS_config is the name of a time series configuration file. This must be
 # set to a full pathname or a file name that exists in DATAPATH/RUNPATH.
 TS_config=''

 # TS_verbose indicates the amount of output
 TS_verbose=0
 [ "$TS_verbose" = 'on' ]  && eval TS_verbose\=1
 [ "$TS_verbose" = 'off' ] && eval TS_verbose\=0
 [ "$TS_verbose" = 'yes' ] && eval TS_verbose\=1
 [ "$TS_verbose" = 'no' ]  && eval TS_verbose\=0

 # TS_sp2gg flags conversion of spectral records to grid records. TS_sp2gg is
 # either "on" or "off". Tdefault is "on" if TS_sp2gg is not set or null.
 TS_sp2gg=''

 # The TS_require_XX variables are used to determine if a file with the
 # extension XX is to be read by the tseries script. This is only required
 # if the user has supplied decks that require files not already available.
 TS_require_gs=''
 TS_require_ss=''
 TS_require_gz=''
 TS_require_gp=''
 TS_require_td=''
 TS_require_xp=''
 TS_require_ie=''

 # The TS_XXfile variables are used to specify a non-default file name
 # for files with the extension XX
 TS_gsfile=''
 TS_ssfile=''
 TS_gpfile=''
 TS_xpfile=''
 TS_tdfile=''
 TS_gzfile=''
 TS_iefile=''

 # TS_clean flags removal of temporary directories that contain
 # "gs", "ss" or "td" split files
 TS_clean=''

 # If delt is set then this value will be used as the time step in seconds
 # for all files read by the tseries deck. If it is not set (the default)
 # then delt will be read from the PARM record, if found, in a particular
 # file. If the PARM record does not exist for a file then delt from another
 # file type that does contain a PARM record will be used. delt should only
 # be set here when no file types used contain a PARM record.
 delt=''

 # The delt_XX variables are used to specify a file specific time step
 # (in seconds) or to override the default.
 delt_gs=''
 delt_ss=''
 delt_gp=''
 delt_xp=''
 delt_td=''
 delt_gz=''
 delt_ie=''

 # If plid is set then this value will be used as the lid pressure
 # for all files read by the tseries deck. If it is not set (the default)
 # then plid will be read from the PARM record, if found, in a particular
 # file. If the PARM record does not exist for a file then plid from another
 # file type that does contain a PARM record will be used. plid should only
 # be set here when no file types used contain a PARM record.
 plid=''

 # The plid_XX variables are used to specify a file specific lid pressure
 # or to override the default.
 plid_gs=''
 plid_ss=''
 plid_gp=''
 plid_xp=''
 plid_td=''
 plid_gz=''
 plid_ie=''

 # TS_ccmval indicates that CCMVal time series are being generated
 TS_ccmval=off
 [ "$TS_ccmval" = 'on' ]  && eval TS_ccmval\=1
 [ "$TS_ccmval" = 'off' ] && eval TS_ccmval\=0
 [ "$TS_ccmval" = 'yes' ] && eval TS_ccmval\=1
 [ "$TS_ccmval" = 'no' ]  && eval TS_ccmval\=0

 # Define a default set of pressure levels to be used by the tseries deck
 if [ $TS_ccmval -eq 1 ]; then
   # This is the set of pressure levels used in CCMVal diagnostics
   p01="-4700"; p02="-3100"; p03="-3150"; p04="-3200"; p05="-3300";
   p06="-3400"; p07="-3500"; p08="-3700"; p09="-2100"; p10="-2150";
   p11="-2200"; p12="-2300"; p13="-2400"; p14="-2500"; p15="-2700";
   p16="-1100"; p17="-1150"; p18="-1200"; p19="-1300"; p20="-1400";
   p21="-1500"; p22="-1700"; p23="-0100"; p24="-0150"; p25="-0200";
   p26="-0300"; p27="-0400"; p28="-0500"; p29="-0700"; p30="   10";
   p31="   15"; p32="   20"; p33="   25"; p34="   30"; p35="   35";
   p36="   40"; p37="   50"; p38="   60"; p39="   70"; p40="   80";
   p41="   90"; p42="  100"; p43="  115"; p44="  130"; p45="  150";
   p46="  170"; p47="  200"; p48="  250"; p49="  285"; p50="  300";
   p51="  350"; p52="  400"; p53="  450"; p54="  500"; p55="  600";
   p56="  650"; p57="  700"; p58="  750"; p59="  780"; p60="  800";
   p61="  850"; p62="  925"; p63=" 1000"; p64=''
 else
   # This is the "standard" set of pressure levels used in cccma diagnostics
   p01="   10"; p02="   20"; p03="   30"; p04="   50"; p05="   70";
   p06="  100"; p07="  150"; p08="  200"; p09="  250"; p10="  300";
   p11="  400"; p12="  500"; p13="  600"; p14="  700"; p15="  850";
   p16="  925"; p17=" 1000"; p18=''
 fi

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

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

 nextjob=on
 noprint=on
 debug=off

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

 # Generate the time series defined in the config file specified by the
 # variable TS_config
 . tseries.dk

#end_of_job
