#!/bin/sh

###############################################################################
####  UNIX Script Documentation Block                                         #
#                                                                             #
# Script name:         exrtofs_et_WPAb_post.sh.sms                            #
# Script description:                                                         #
#                                                                             #
# Authors: Bhavani Rajan & Ilya Rivin  Org: NP23         Date: 2011-07-20     #
#                                                                             #
# Abstract: This is the post-processing script for RTOFS_et_WPAb              #
#                                                                             #
# Sub-scripts called:                                                         # 
#                    rtofs__et_WPAb_glo3z_6hrly.sh                            #
#                                                                             # 
# Imported variables:                                                         #
#                    RUN                                                      #
#                    modID                                                    #
#                    PARMrtofs                                                #
#                    USHrtofs                                                 #
#                    DATA                                                     #
#                    COMOUT                                                   #
#                    RUN_MODE                                                 #  
#                    SENDCOM                                                  #
#                    PDY                                                      #  
#                    cyc                                                      #
#                    analdays                                                 #
#                    inputgrid                                                #
#                                                                             #
#                                                                             #
# Script history log:                                                         #
# 2012-03-13  Hae-Cheol Kim                                                   #
#                                                                             #
###############################################################################

set -xa

export PS4='$SECONDS + '

#######################################
cd $DATA

###
### NOTE: Move copying to forecast step
###

msg="RTOFS_ET_WPA_POST JOB has begun on `hostname` at `date`"
#postmsg "$jlogfile" "$msg"

procstatus=0

typeset -Z3 fhr
typeset -Z3 fhr0
typeset -Z3 intvl_6hrly
typeset -Z3 ENDHOUR

# Define the end forecast hour here:

if [ ${RUN_MODE} = 'analysis' ]
then
  export analdays=${analdays:-1}
  export enddate=${analysis_end:-$PDY}
  export startdate=`$utilexec/ndate -\` expr $analdays \* 24 \`  ${enddate}'00' | cut -c1-8`
  export ENDHOUR=`expr $analdays \* 24`
fi
if [ ${RUN_MODE} = 'forecast' ]
then
  export fcstdays=${fcstdays:-1}
  export fcstdays_before_thisstep=00
  export startdate=${startdate:-${PDY}}
#hCk  export enddate=`$utilexec/ndate \` expr $fcstdays \* 24 \`  ${startdate}${mycyc} | cut -c1-8`
  export enddate=`$utilexec/ndate -\` expr $fcstdays \* 24 \`  ${startdate}'00' | cut -c1-8`
  export ENDHOUR=`expr \( $fcstdays \+ ${fcstdays_before_thisstep} \) \* 24 `
fi

# define what functions to do (default to operational settings)
export running_realtime=${running_realtime:-YES}
export volume_3z_6hrly=${volume_3z_6hrly:-YES}
export intvl_6hrly=${intvl_6hrly:-6}
export no_procs=${NPROCS:-4} # no of processors used in poe


# Waiting time (in 10 sec)
icnt_max=180

# Define the Input files:
export DEPTHFILEa=${FIXofs}/${RUN}_${modID}.regional.depth.a
export DEPTHFILEb=${FIXofs}/${RUN}_${modID}.regional.depth.b
export GRIDFILEa=${FIXofs}/${RUN}_${modID}.regional.grid.a
export GRIDFILEb=${FIXofs}/${RUN}_${modID}.regional.grid.b
                          
# Copy in the Fix files:
cp -f -p $GRIDFILEa  ${DATA}/regional.grid.a
cp -f -p $GRIDFILEb  ${DATA}/regional.grid.b
cp -f -p $DEPTHFILEa ${DATA}/regional.depth.a
cp -f -p $DEPTHFILEb ${DATA}/regional.depth.b


if [ ${RUN_MODE} = 'analysis' ]
then
  fhr=00
  export mode=n
  analhrs=`expr $analdays \* 24`
fi
if [ ${RUN_MODE} = 'forecast' ]
then
  fhr=`expr \${fcstdays_before_thisstep} \* 24`
  export mode=f
fi
export fhr0=$fhr
#export fhr=`expr $fhr + 6`
echo fhr $fhr ENDHOUR $ENDHOUR
# Output NC header information for surface AND volume files

export CDF_TITLE='HYCOM ATLb2.00'
export CDF_INST="National Centers for Environmental Prediction"

while [ $fhr -lt $ENDHOUR ]
#hCk while [ $fhr -le $ENDHOUR ]
do
  # Some reverse engineering
  if [ $fhr -le 100 ] 
  then
    typeset -Z2 fhr2
    fhr2=$fhr
    #hCk fhr2=`printf %2.2i $fhr`
  else
    typeset -Z3 fhr2
    fhr2=$fhr
  fi
  echo *********fhr fhr $fhr $fh2 ***********
  if [ ${RUN_MODE} = 'forecast' ]
  then
    if [ $fhr2 -eq 00 ]; then
       let "fhr2=24+$fhr2"
       fhr2=`printf %2.2i $fhr2`
       arfile_tplate=${RUN}_${modID}.t${cyc}z.${mode}${fhr2}.archv
    else
       arfile_tplate=${RUN}_${modID}.t${cyc}z.${mode}${fhr2}.archv
    fi
  fi
  if [ ${RUN_MODE} = 'analysis' ]
  then
    typeset -Z2 fhr3
    fhr3=`expr $analhrs - $fhr2`
    if [ $fhr2 -eq 00 ]; then
       arfile_tplate=${RUN}_${modID}.t${cyc}z.${mode}${fhr2}.archv
    else
       arfile_tplate=${RUN}_${modID}.t${cyc}z.${mode}-${fhr3}.archv
    fi
  fi
  #*********************************
  if [ $run_parallel = 'YES' ]; then
    # wait for the new forecast output to be available:
    icnt=1
    if [ ! -f ${arfile_tplate}.a ]; then
      break
      print Sleeping $icnt times while waiting for ${arfile_tplate}.a
      sleep 10
      icnt=$((icnt + 1))
      if [ $icnt -ge $icnt_max ]
      then
        echo Post timed out, arfile_tplate not available after $icnt_max iterations.
        echo "NOTdone" >${RUN}_${modID}.t${mycyc}z.nav.log
        export err=2; err_chk
      fi
    fi
  fi # end loop for run_parallel
  #*********************************
  # link current archive to the working directory
  rm -rf archv.a archv.b > /dev/null
  if [ -s $COMIN/${arfile_tplate}.a ]; then
    ln -s -f $COMIN/${arfile_tplate}.a archv.a
    ln -s -f $COMIN/${arfile_tplate}.b archv.b
  else
    echo Missing archv file $COMOUT/${arfile_tplate}.
    echo "NOTdone due to missing archv file" >${RUN}_${modID}.t${mycyc}z.nav.log
    export err=1; err_chk  
  fi
  missing=no
  for fn in regional.depth.a regional.depth.b regional.grid.a regional.grid.b archv.a archv.b 
  do
    if [ ! -f $fn ]
    then
      missing=yes
      echo Missing file $fn, will not be able to run
    fi
  done
  if [ $missing = 'yes' ]
  then
    echo Cannot run due to missing files.
    echo "NOTdone" >${RUN}_${modID}.t${mycyc}z.nav.log
    export err=1; err_chk
    exit
  fi

#*********************************************************************

  # 6 hourlies for volume files in 3 regions
  #
  if [ $volume_3z_6hrly = 'YES' ]
  then
    for reg in reg1 
    do
      cfile=${RUN}_${modID}_3dz_${mode}${fhr2}_6hrly_${reg}.nc
      export CDF033=$cfile
      cfile2=${RUN}_${modID}_3dz_${mode}${fhr2}_6hrly_${reg}_tracer.nc
      export CDF031=$cfile2
      cfile3=${RUN}_${modID}_2ds_${mode}${fhr2}_6hrly_${reg}_ssh.nc
      export CDF050=$cfile3

      ${USHofs}/${RUN}_${modID}_glo3z_6hrly.sh $reg
      if [ $SENDCOM = 'YES' ]
      then
        #####hCk  cfile=${RUN}_${modID}_3dz_${mode}${fhr}_6hrly_${reg}.nc
        cp -f -p $cfile  $COMOUT/.
        cp -f -p $cfile2  $COMOUT/.
        cp -f -p $cfile3  $COMOUT/.
        if [ $SENDDBN = YES ]
        then
          $DBNROOT/bin/dbn_alert MODEL RTOFS_ET_WPA_NETCDF $job $COMOUT/$cfile
        else
          msg="File $COMOUT/$cfile not posted to db_net."
          postmsg "$jlogfile" "$msg"
        fi
      fi
    done  # region loop
  fi # 6hrly loop 
  fhr=`expr $fhr + $intvl_6hrly` 
done

echo "done" >$COMOUT/${RUN}_${modID}.t${mycyc}z.nav.log

#################################################
msg='THE RTOFS_ET_WPA_POST JOB HAS ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################
