#!/bin/sh
#=======================================================================
# Ocean diagnostics from time series of gz files        --- gztsdiag ---
#
# This deck requires certain time series files to be present on disk.
# The files required depend on the deck which is invoked as follows
#
#   "*_gz_*"
#   "*_gz_annual_*"
#   when gztsdiag_deck includes only the "standard" set of diagnostics
#
#   *_gz_*
#   *_gz_annual_*
#   *_gz_derived_*
#   *_gz_glbavg_*
#   *_gz_zonal_*
#   *_cp_*
#   *_dd_*
#   *_dp_*
#   *_de_*
#   *_ds_*
#   *_dd_derived_*
#   when gztsdiag_deck includes the extra IPCC diagnostics
#
# $Id: gztsdiag_jobdef 657 2012-02-22 23:48:49Z acrnrls $
#
# Larry Solheim  June,2011
#=======================================================================
#     keyword :: gztsdiag
# description :: ocean diagnostics from time series files

 set -a
 . betapath2

 username="acrnxxx"; user="XXX";
 runid=job000; uxxx='uxxx'; crawork="${runid}_job"
 nqsprfx="${runid}_"; nqsext=''

 jobname=gztsdiag; time="7200" ; memory="2000mb" ;

 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

 nextjob=on
 noprint=on

 # Temporary directory where this script will run
 gztsdiag_CCRNTMP=''
 CCRNTMP=${gztsdiag_CCRNTMP:=$CCRNTMP}

 # RUNPATH on the execution machine
 gztsdiag_RUNPATH=''
 RUNPATH=${gztsdiag_RUNPATH:=$RUNPATH}

 # 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

 . comjcl.cdk

cat > Execute_Script <<'end_of_script'

  # ---Start_submit_ignore_code----

  set -a

  # 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

  # Define a unique stamp for use in file names etc
  stamp=`date "+%j%H%M%S"$$`

  # Determine the name of the machine on which this script is running
  this_host=`hostname|cut -d'.' -f1`

  # Use -e option if recognized by echo
  if [ "X`echo -e`" = "X-e" ]; then
    echo_e() { echo ${1+"$@"}; }
  else
    echo_e() { echo -e ${1+"$@"}; }
  fi

  # bail is a simple error exit routine
  # Note: we write the error directly to a file in ~/.queue so that this
  #       info is not lost if/when stdout is not returned
  error_out="${JHOME:-$HOME}/.queue/error_gztsdiag_${runid}_$stamp"
  [ ! -z "$error_out" ] && rm -f $error_out
  bail(){
    echo_e `date`" --- gztsdiag: $*"
    echo_e `date`" --- gztsdiag: $*" >>$error_out
    exit 1
  }

  add2path(){
    if echo $PATH|/bin/grep -Evq "(^|:)$1($|:)" ; then
       if [ "$2" = "atend" ] ; then
          PATH=$PATH:$1
       else
          PATH=$1:$PATH
       fi
    fi
  }

  # Determine the name of a dir containing non-standard scripts and executables
  # that may be required below. Programs in this dir will superceed similar
  # programs found elswhere on the users path.
  path_to_add=/users/tor/acrn/cbn/bin_linux_ar5
  case $this_host in
    # This is pollux, use 64 bit binaries
    ib3-*) path_to_add='/users/tor/acrn/cbn/bin_linux64_ar5' ;;
    c[0-9]*) path_to_add='/users/tor/acrn/cbn/bin_aix64_ar5' ;;
  esac

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

  # These variables are set when the job string is created
  previous_year=NotSet
  previous_month=NotSet

  current_year=NotSet
  current_month=NotSet

  next_year=NotSet
  next_month=NotSet

  run_start_year=NotSet
  run_start_month=NotSet
  run_stop_year=NotSet
  run_stop_month=NotSet

  # If either reset_start_year or reset_stop_year are set then they must be
  # of the form old_year:new_year (ie a colon separated pair of integers)
  # where the first integer is the year that needs to be changed
  # and the second integer is the year that it will be changed to.
  # These may potentially change the value of start_year or stop_year that
  # are defined after the call to make_file_name_list below
  gztsdiag_reset_start_year=''
  reset_start_year=${gztsdiag_reset_start_year:=''}
  gztsdiag_reset_end_year=''
  gztsdiag_reset_stop_year=$gztsdiag_reset_end_year
  reset_stop_year=${gztsdiag_reset_stop_year:=''}

  # This invocation of make_file_name_list will process the *_year and *_months
  # variables defined above and output a file containing definitions for
  # start_year, start_mon, stop_year, stop_mon
  tmp_file_list="gztsdiag_date_list_${runid}_${stamp}"
  make_file_name_list --dates_only $tmp_file_list >>$error_out 2>&1 ||\
    bail "Problem in make_file_name_list"
  rm -f $error_out

  # Verify that the output list is not empty
  [ ! -s "$tmp_file_list" ] && bail "Unable to create file list"

  # A file list was created ...source it
  # This will define start_year, start_mon, stop_year, stop_mon
  : ; . $tmp_file_list
  rm -f $tmp_file_list

  # Define start and stop dates
  gztsdiag_start_year=$start_year
  gztsdiag_start_mon=$start_mon
  gztsdiag_stop_year=$stop_year
  gztsdiag_stop_mon=$stop_mon
  gztsdiag_end_year=$stop_year
  gztsdiag_end_mon=$stop_mon

  # Add time_(start|stop)_(year|mon) to be used by gztsdiag_deck
  time_start_year=`echo $gztsdiag_start_year|awk '{printf "%3.2d",$1}' -`
  time_start_mon=`echo $gztsdiag_start_mon|awk '{printf "%2.2d",$1}' -`
  time_stop_year=`echo $gztsdiag_stop_year|awk '{printf "%3.2d",$1}' -`
  time_stop_mon=`echo $gztsdiag_stop_mon|awk '{printf "%2.2d",$1}' -`

  # Define a prefix for time series files to be read by gztsdiag_deck
  if [ $time_start_mon -ne 1 -o $time_stop_mon -ne 12 ] ; then
    # use yyyymm format if first or last year is incomplete
    range="${time_start_year}${time_start_mon}_${time_stop_year}${time_stop_mon}"
  else 
    # use yyyy format if the first and last year are complete
    range="${time_start_year}_${time_stop_year}"
  fi
  gz_tseries="sc_${runid}_${range}_gz_";
  gz_ann_tseries="sc_${runid}_${range}_gz_annual_";
  gz_drv_tseries="sc_${runid}_${range}_gz_derived_";
  gz_glb_tseries="sc_${runid}_${range}_gz_glbavg_";
  gz_znl_tseries="sc_${runid}_${range}_gz_zonal_";
  cp_tseries="sc_${runid}_${range}_cp_";
  dd_tseries="sc_${runid}_${range}_dd_";
  dp_tseries="sc_${runid}_${range}_dp_";
  ds_tseries="sc_${runid}_${range}_ds_";
  dd_drv_tseries="sc_${runid}_${range}_dd_derived_";

  salinity_ref="iga_2066_2075_salinity_ref";

  # PhysA = on/off     physical atmosphere variables
  # PhysO = on/off     physical ocean variables
  # CarbA = on/off     atmosphere carbon variables
  # CarbO = on/off     ocean carbon variables
  # CarbL = on/off     land carbon variables
  PhysA=on
  PhysO=on
  CarbA=on
  CarbO=on
  CarbL=on

  # Set the name of the default deck
  # This may be overridden by the user
  gztsdiag_deck=''
  [ -z "$gztsdiag_deck" ] && bail "Missing gztsdiag_deck"

  # Look on the users path for $gztsdiag_deck and assign fullpath if found
  fullpath=''
  [ -z "$PATH" ] && bail "PATH is not set"
  for pdir in `echo $PATH|sed 's/:/ /g'`; do
    if [ -s "$pdir/$gztsdiag_deck" ]; then
      fullpath=$pdir/$gztsdiag_deck
      break
    fi
  done

  # If not found on the users path then look in the path $local_gztsdiag_path
  # This may be overridden by a user supplied gztsdiag_path
  if [ -z "$fullpath" ]; then
    gztsdiag_path=''
    local_gztsdiag_path="$path_to_add"
    [ -n "$gztsdiag_path" ] && local_gztsdiag_path="$gztsdiag_path"
    [ -z "$local_gztsdiag_path" ] && bail "Missing local_gztsdiag_path"
    for pdir in `echo $local_gztsdiag_path|sed 's/:/ /g'`; do
      if [ -s "$pdir/$gztsdiag_deck" ]; then
        fullpath=$pdir/$gztsdiag_deck
        break
      fi
    done
  fi

  # If not found look in the "standard" location for diagnostic decks
  if [ -z "$fullpath" ]; then
    std_deck_location=$CCRNSRC/source/diag4
    [ -z "$std_deck_location" ] && bail "Missing std_deck_location"
    [ -s "$std_deck_location/$gztsdiag_deck" ] && \
        fullpath=$std_deck_location/$gztsdiag_deck
  fi

  [ -z "$fullpath"   ] && bail "$gztsdiag_deck is missing or empty"
  [ ! -s "$fullpath" ] && bail "$fullpath is missing or empty"

  echo "Using gztsdiag deck found in $fullpath"

  eval shebang=\#\!\/usr\/bin\/perl
  cat >rip_here_docs <<EOF
$shebang
EOF

  cat >> rip_here_docs << 'end_rip_here_docs'
  BEGIN {$p=0}
  $script = $ARGV[0];
  die "$0 requires a file name on the command line.\n  Stopped" unless $script;
  open (FI, "<$script") || die "Unable to open $script";
  while (<FI>) {
      if (/^\s*cat.*?<<\S+/) {
          # The start of a here doc 
          ($end_cat)  = m!^\s*cat.*?<<\s*([^>\s]+)\s*!;
          $end_cat =~ s/^\s*[\'\"\`]\s*//;
          $end_cat =~ s/\s*[\'\"\`]\s*$//;
          die "Unable to determine here doc delimiter.\n$_\n  Stopped" unless $end_cat;
          ($doc_name) = m!^\s*cat.*?(?:>|>>)\s*([^<\s]+)\s*!;
          $doc_name =~ s/^\s*[\'\"\`]\s*//;
          $doc_name =~ s/\s*[\'\"\`]\s*$//;
          die "Unable to determine here doc name.\n$_\n  Stopped" unless $doc_name;
          if ($doc_name =~ /^Execute_Script/) {
            # Avoid overwriting any existing Execute_Script* file
            my ($sfx) = $doc_name =~ /^Execute_Script(.*)/;
            $doc_name = "EXEscript$sfx";
          }
          if ($doc_name =~ /^Input_Cards/) {
            # Avoid overwriting any existing Input_Cards file
            my ($sfx) = $doc_name =~ /^Input_Cards(.*)/;
            $doc_name = "EXEinput$sfx";
          }
          $p = 1;
          open(FO,">$doc_name") || die "Cannot open $doc_name";
          print "$doc_name ";
          next;
      }
      if ($p and $end_cat and /^\s*$end_cat\s*$/) {
          # The end of a here doc
          $p=0;
          close FO;
      }
      print FO "$_" if $p;
  }
  close(FI);
end_rip_here_docs

  rm -f EXEscript
  chmod u+x rip_here_docs
  DOC_list=`./rip_here_docs $fullpath`
  echo "DOC_list: $DOC_list"

  [ ! -s EXEscript ] && bail "Execute_Script was not found in $fullpath"

  # Add a dir to PATH so that non standard scripts/binaries used by this deck are found
  [ -n "$path_to_add" ] && add2path $path_to_add

  : ; . EXEscript

  # ---Stop_submit_ignore_code----

end_of_script

 . endjcl.cdk

#end_of_job
