#!/bin/sh

###############################################################################
####  UNIX Script Documentation Block                                         #
#                                                                             #
# Script name:         exofs_atl_post.sh.sms                                  #
# Script description:                                                         #
#                                                                             #
# Author:        Ilya Rivin       Org: NP23         Date: 2005-09-14          #
#                                                                             #
# Abstract: This script generates GRIB files on RTOFS (Atlantic)              #
#           native grid for the whole domain and                              #
#           on standard lat-lon grif for the whole Atlantic domain,           #
#           Gulf of Main, Gulf of Mexico and Gulf Stream subdomains           #
#                                                                             #
# Sub-scripts called:                                                         #
#                                                                             # 
#                                                                             #
#                                                                             #
# Script history log:                                                         #
# 2005-09-14  Dan Iredell                                                     #
#                                                                             #
###############################################################################


set -xa

export PS4='$SECONDS + '

cd $DATA

msg="ofs_atl_post has begun on `hostname`"
postmsg "$jlogfile" "$msg"

procstatus=0
export CNVGRIB=${utilexec}/cnvgrib
export WGRIB2=${utilexec}/wgrib2

# Set the starting forecast hour
typeset -Z3 fhr
typeset -Z2 chr
typeset -Z3 LEAD
echo $fhr $chr $LEAD

# Define the end forecast hour here:
# IMPORTANT: need to be changed if the forecast length is changed.
export ENDHOUR=${ENDHOUR:-144} # 144 hour forecast
ENDHOUR=$((ENDHOUR + 24))       # adding nowcast

# define what functions to do (default to operational settings)
export checking_restart=${checking_restart:-'t'}
export running_realtime=${running_realtime:-'t'}
export std_grib=${std_grib:-YES}
export hires_std_grib=${hires_std_grib:-YES}
export volume_3z=${volume_3z:-YES}
export volume_3d=${volume_3d:-YES}
export surface_z=${surface_z:-YES}
export surface_d=${surface_d:-YES}


export intvl_std=${intvl_std:-1}
export intvl_std_reg=${intvl_std_reg:-24}
export intvl_nav_2d=${intvl_nav_2d:-1}
export intvl_nav_3z=${intvl_nav_3z:-24}
if [ $intvl_nav_2d -le $intvl_std ]
then
  intvl=$intvl_nav_2d
else
  intvl=$intvl_std
fi
if [ $intvl_nav_3z -le $intvl ]
then
  intvl=$intvl_nav_3z
fi

#### Check for restart
if [ $checking_restart = 't' ]
then
if [ -s $COMIN/${RUN}_${modID}.t${cyc}z.nav.control ]
then
    fhr_rst=`cat $COMIN/${RUN}_${modID}.t${cyc}z.nav.control`
    fhr=`expr $fhr_rst + $intvl`
else
    fhr=001
fi
else
    fhr=001
fi
#### End checking for restart
chr=$fhr
hr_std=$fhr
hr_std_reg=`expr \( $fhr  \/ $intvl_std_reg  \) \* $intvl_std_reg + $intvl_std_reg`
hr_nav_2d=$fhr
hr_nav_3z=`expr \( $fhr  \/ $intvl_nav_3z  \) \* $intvl_nav_3z + $intvl_nav_3z`

# Initiate restart date
rest_jday_old=-1

# Set starting date for the Julian day calculation in HYCOM
basetime=1900123100   ## 1901010100


# Waiting time (in 10 sec)
icnt_max=180

# Define the Input files:
export DEPTHFILEa=$FIXofs/${RUN}_${modID}.${gridres}.regional.depth.a
export DEPTHFILEb=$FIXofs/${RUN}_${modID}.${gridres}.regional.depth.b
export GRIDFILEa=$FIXofs/${RUN}_${modID}.${gridres}.regional.grid.a
export GRIDFILEb=$FIXofs/${RUN}_${modID}.${gridres}.regional.grid.b
                          
rm -rf grib.out grib_output fort.51 archvgrib.log

# Copy in the Fix files:
cp  $GRIDFILEa  regional.grid.a
cp  $GRIDFILEb  regional.grid.b
cp  $DEPTHFILEa regional.depth.a
cp  $DEPTHFILEb regional.depth.b

# domains for "standard" gid regions
cat > atl_domain<<EOF
0
EOF
cat > gs_domain<<EOF
1
EOF
cat > gm_domain<<EOF
2
EOF
cat > gma_domain<<EOF
3
EOF

echo fhr $fhr $ENDHOUR ENDHOUR
while [ $fhr -le $ENDHOUR ]
do
 
  # construct name of the current HYCOM-type archive file
  YYYYMMDDHH=`${utilexec}/ndate +$fhr $PDYm1$cyc`
  YYYYMMDD=`echo $YYYYMMDDHH | cut -c1-8`
  YYYYDDD=`sh $utilscript/date2jday.sh $YYYYMMDD`
  YYYY=`echo $YYYYMMDD | cut -c1-4`
  DDD=`echo $YYYYDDD | cut -c5-7`
  chr=`expr $fhr \- \( 24 \* \( $fhr \/ 24 \) \)`
  if [ $chr -eq 24 ] ; then chr=00 ; fi
  arfile_tplate=${RUN}_${modID}.t${cyc}z.${YYYY}_${DDD}_${chr}
  
  #LEAD is the number of hours lead in archv.a,.b file
  LEAD=`${utilexec}/nhour ${YYYYMMDD}${chr} ${PDY}${cyc}  `
  if [ $LEAD -le 0 ] 
  then 
    runtype='n' # nowcast
    runtype3z='N'
    FCSTDATA=$ANAL_DATA
  else
    runtype='f' # forecast
    runtype3z='F'
    FCSTDATA=$FCST_DATA
  fi
  if [ $running_realtime = 't' ]
  then
     art=archv.${YYYY}_${DDD}_${chr}
  else
     art=$arfile_tplate
  fi


  # Check if a new restrt file is written and copy it in 'ges' directory 
  if [ $runtype = 'f' ] 
  then
     if [ $checking_restart = 't' ]
     then
     if [ -s $FCSTDATA/restart_out.b -o -s $FCSTDATA/restart_out1.b ] 
     then
        for rstfile in `ls $FCSTDATA/restart_out*.b`
        do
          hdate=`sed -n 2,2p $rstfile |awk '{print $5}'|cut -d. -f1`
          rstday=`${utilscript}/date_hycom2normal.sh $hdate` 
          fcsthr=`${utilexec}/nhour $rstday ${PDY}${cyc}`
          rstfilea=$rstfile
          rstfileb=`echo $rstfile |sed "s/\.b/\.a/g"`
          rstgesa=$GESdir/${RUN}_${modID}.t${cyc}z.restart_f${fcsthr}.a
          rstgesb=$GESdir/${RUN}_${modID}.t${cyc}z.restart_f${fcsthr}.b
          if [ ! -s $rstgesa -o ! -s $rstgesb ]
          then
             sleep 30
             cp $rstfileb $rstgesa
             cp $rstfilea $rstgesb
          fi
        done
     fi
     fi
  fi

  # wait for the new forecast output to be available:
  icnt=1
  if [ $running_realtime = 't' ]  ####
  then
  while [ $icnt -lt $icnt_max ]
  do
    if [ -s $FCSTDATA/${art}.log ]
    then
      break
    fi

    print Sleeping $icnt times while waiting for $FCSTDATA/${art}.log
    sleep 10
    icnt=$((icnt + 1))
  done
  fi  # running_realtime

  if [ $icnt -ge $icnt_max ]
  then
    echo Post timed out, $FCSTDATA/${art}.log not available after $icnt_max iterations.
    echo "NOTdone" >$COMOUT/${RUN}_${modID}.t${cyc}z.nav.log
    export err=2; err_chk
  fi

  if [ $running_realtime = 't' ]
  then
    cp $FCSTDATA/${art}.a $COMOUT/${arfile_tplate}.a
    cp $FCSTDATA/${art}.b $COMOUT/${arfile_tplate}.b
  fi

  # link current archive to the working directory
  rm -rf archv.a archv.b
  ln -s -f $FCSTDATA/${art}.a archv.a
  ln -s -f $FCSTDATA/${art}.b archv.b

  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" >$COMOUT/${RUN}_${modID}.t${cyc}z.nav.log
    export err=1; err_chk
  fi

  if [ $LEAD -ge 0 ]
  then
    export grib_date=$PDY
    export cLEAD=$LEAD
  else
    export grib_date=$PDYm1
    export cLEAD=$((24 + LEAD))
  fi

  # produce latlon gribs for this forecast time for native grid and each subdomain
  $USHofs/ofs_latlon.sh
  
  export yyyy=`echo $grib_date | cut -c1-4`
  yy=`echo $grib_date | cut -c3-4`
  export mm=`echo $grib_date | cut -c5-6`
  export dd=`echo $grib_date | cut -c7-8`
  if [ `echo $cLEAD | cut -c1` -eq 0 ] 
  then
    export cLEAD=`echo $cLEAD | cut -c2-3`
  fi

  touch out
  echo FRom STANDARD GRID/GRIB hr_std $hr_std intvl_std $intvl_std fhr $fhr >> out
  echo FRom STANDARD GRID/GRIB hr_std $hr_std intvl_std $intvl_std fhr $fhr 
  if [ $hr_std -eq $fhr ]
  then
    #
    # Gribbing on the "standard" grid
    #
    if [ $std_grib = YES ]
    then
    for domain_name in atl 
    do

      grbfile=${RUN}_${modID}.t${cyc}z.${runtype}${LEAD}.${domain_name}.grb.std

      $EXECofs/ofs_ingrib_array $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_array_${domain_name}.out
      export err=$?; err_chk
      if [ -s grib_output ]; then  mv grib_output ${grbfile} ;fi

      $EXECofs/ofs_ingrib_vector $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_vector_${domain_name}.out
      export err=$?; err_chk
      if [ -s grib_output ]; then cat grib_output >> ${grbfile} ; fi

# For high resolution

     hires_grbfile=${RUN}_${modID}.t${cyc}z.${runtype}${LEAD}.${domain_name}.hires.grb.std

     $EXECofs/ofs_hires_ingrib_array $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_array_${domain_name}.out
     export err=$?; err_chk
     if [ -s grib_output ]; then  mv grib_output ${hires_grbfile} ;fi

     $EXECofs/ofs_hires_ingrib_vector $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_vector_${domain_name}.out
      export err=$?; err_chk
      if [ -s grib_output ]; then cat grib_output >> ${hires_grbfile} ; fi
      rm grib_output
      if [ -s $grbfile ] || [ -s $hires_grbfile ]
      then
        procstatus=0
      else
        procstatus=1
      fi
      if [ $procstatus = 0 ]
      then
        if [ $SENDCOM = YES ]
        then
          test -f $grbfile && cp $grbfile $COMOUT/.
          test -f $hires_grbfile && cp $hires_grbfile $COMOUT/.
        fi

        if [ $SENDDBN = YES ]
        then
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_STD $job $COMOUT/$grbfile
        fi

        ####################################################################
        # Convert to grib2 format and put the process to run on background
        ####################################################################
        ($CNVGRIB -g12 -p40 $grbfile ${grbfile}.grib2
        $WGRIB2 ${grbfile}.grib2 -s > ${grbfile}.grib2.idx
       
        $CNVGRIB -g12 -p40 $hires_grbfile ${hires_grbfile}.grib2
        $WGRIB2 ${hires_grbfile}.grib2 -s > ${hires_grbfile}.grib2.idx

        if [ $SENDCOM = YES ]
        then
          cp ${grbfile}.grib2 $COMOUT/.
          cp ${grbfile}.grib2.idx $COMOUT/.
          cp ${hires_grbfile}.grib2 $COMOUT/.
          cp ${hires_grbfile}.grib2.idx $COMOUT/.
        fi

        if [ $SENDDBN_GB2 = YES ]
        then
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${grbfile}.grib2
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${grbfile}.grib2.idx
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${hires_grbfile}.grib2
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${hires_grbfile}.grib2.idx
        fi )&
        
#-    else
#-      msg="6. $modid run from ANALYSIS run script: ABNORMAL EXIT"
#-      postmsg "$jlogfile" "$msg"
#-      exit -1
      fi
    done
    fi
    hr_std=$((hr_std + intvl_std))
  fi


  if [ $hr_std_reg -eq $fhr ]
  then
    #
    # Gribbing on the "standard" grid
    #
    if [ $std_grib = YES ]
    then
    for domain_name in atl gs gm gma
    do

      grbfile=${RUN}_${modID}.t${cyc}z.${runtype}${LEAD}.${domain_name}.grb.std 

      $EXECofs/ofs_ingrib_array $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_array_${domain_name}.out
      export err=$?; err_chk
      if [ -s grib_output ]; then  mv grib_output ${grbfile} ;fi

      $EXECofs/ofs_ingrib_vector $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_vector_${domain_name}.out
      export err=$?; err_chk
      if [ -s grib_output ]; then cat grib_output >> ${grbfile} ; fi

      if [ $domain_name = 'atl' ]
      then
      hires_grbfile=${RUN}_${modID}.t${cyc}z.${runtype}${LEAD}.${domain_name}.hires.grb.std 

      $EXECofs/ofs_hires_ingrib_array $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_array_${domain_name}.out
      export err=$?; err_chk
      if [ -s grib_output ]; then  mv grib_output ${hires_grbfile} ;fi

      $EXECofs/ofs_hires_ingrib_vector $yy $mm $dd $cLEAD < ${domain_name}_domain >> ingrib_vector_${domain_name}.out
      export err=$?; err_chk
      if [ -s grib_output ]; then cat grib_output >> ${hires_grbfile} ; fi
      rm grib_output
      fi

      if [ -s $grbfile ] || [ -s $hires_grbfile ]
      then
        procstatus=0
      else
        procstatus=1
      fi
      if [ $procstatus = 0 ]
      then
        if [ $SENDCOM = YES ]
        then
          test -f $grbfile && cp $grbfile $COMOUT/.
          test -f $hires_grbfile && cp $hires_grbfile $COMOUT/.
        fi
        if [ $SENDDBN = YES ]
        then
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_STD $job $COMOUT/$grbfile
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_STD $job $COMOUT/$hires_grbfile
        fi

        ####################################################################
        # Convert to grib2 format and put the process to run on background
        ####################################################################
        ($CNVGRIB -g12 -p40 $grbfile ${grbfile}.grib2
        $WGRIB2 ${grbfile}.grib2 -s > ${grbfile}.grib2.idx
       
        $CNVGRIB -g12 -p40 $hires_grbfile ${hires_grbfile}.grib2
        $WGRIB2 ${hires_grbfile}.grib2 -s > ${hires_grbfile}.grib2.idx

        if [ $SENDCOM = YES ]
        then
          cp ${grbfile}.grib2 $COMOUT/.
          cp ${grbfile}.grib2.idx $COMOUT/.
          cp ${hires_grbfile}.grib2 $COMOUT/.
          cp ${hires_grbfile}.grib2.idx $COMOUT/.
        fi

        if [ $SENDDBN_GB2 = YES ]
        then
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${grbfile}.grib2
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${grbfile}.grib2.idx
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${hires_grbfile}.grib2
           $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${hires_grbfile}.grib2.idx
        fi )&

#-    else
#-      msg="6. $modid run from ANALYSIS run script: ABNORMAL EXIT"
#-      postmsg "$jlogfile" "$msg"
#-      exit -1
      fi
    done
    fi
    hr_std_reg=$((hr_std_reg + intvl_std_reg)) 
  fi
  #
  # Gribbing on the "native" grid 
  #

    if [ $hr_nav_3z -eq $fhr ]
    then 
      # 3z only
      hr_nav_3z=$((hr_nav_3z + intvl_nav_3z)) 
      if [ $volume_3z = YES ]
      then
# CREATE 3Z FILE
      infile=$PARMofs/${RUN}_${modID}.archv2data_3z.in
      outfile=${RUN}_${modID}.t${cyc}z.${runtype3z}${LEAD}.grb
      executable=$EXECofs/ofs_archv2data3z_insitu
      export gridno=120
      $USHofs/ofs_gribit.sh

      export err=$?; err_chk
      echo ' ' >> $pgmout
      if [ -s fort.51 ] ; then mv fort.51 $outfile ;fi

      if [ -s $outfile ]
      then
        procstatus=0
        cat latlon.grb >> $outfile
      else
        procstatus=1
      fi
      if [ $procstatus = 0 ]
      then
         if [ $SENDCOM = YES ]
         then
           # Copy the grib file to /com:
           cp $outfile $COMOUT/.
         fi

         if [ $SENDDBN = YES ] ; then
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB $job $COMOUT/$outfile
         fi

         ####################################################################
         # Convert to grib2 format and put the process to run on background
         ####################################################################
         ($CNVGRIB -g12 -p40 $outfile ${outfile}.grib2
         $WGRIB2 ${outfile}.grib2 -s > ${outfile}.grib2.idx
       
         if [ $SENDCOM = YES ]
         then
           cp ${outfile}.grib2 $COMOUT/.
           cp ${outfile}.grib2.idx $COMOUT/.
         fi

         if [ $SENDDBN_GB2 = YES ]
         then
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${outfile}.grib2
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${outfile}.grib2.idx
         fi )&
      fi
      fi

      if [ $volume_3d = YES ]
      then
# CREATE 3d volume file
      infile=$PARMofs/${RUN}_${modID}.archv2data_3d.in
      outfile=${RUN}_${modID}.t${cyc}z.${runtype3z}${LEAD}.3d.grb
      executable=$EXECofs/ofs_archv2data2d
      export gridno=120
      $USHofs/ofs_gribit.sh

      export err=$?; err_chk
      echo ' ' >> $pgmout
      if [ -s fort.51 ] ; then mv fort.51 $outfile ;fi

      if [ -s $outfile ]
      then
        procstatus=0
        cat latlon.grb >> $outfile
      else
        procstatus=1
      fi
      if [ $procstatus = 0 ]
      then

         if [ $SENDCOM = YES ]
         then
           # Copy the grib file to /com:
           cp $outfile $COMOUT/.
         fi

       if [ $SENDDBN = YES ] ; then
          $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB $job \
             $COMOUT/$outfile
       fi

         ##############################
         # Convert to grib2 format
         ##############################
       ( $CNVGRIB -g12 -p40 $outfile ${outfile}.grib2
         $WGRIB2 ${outfile}.grib2 -s > ${outfile}.grib2.idx
          
         if [ $SENDCOM = YES ]
         then
           cp ${outfile}.grib2 $COMOUT/.
           cp ${outfile}.grib2.idx $COMOUT/.
         fi

         if [ $SENDDBN_GB2 = YES ]
         then
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${outfile}.grib2
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${outfile}.grib2.idx
         fi )&
      fi
# subdomain here
      $USHofs/ofs_grib_subdomain.sh
      fi


    fi

    if [ $hr_nav_2d -eq $fhr ]
    then
      # 2d only
      hr_nav_2d=$((hr_nav_2d + intvl_nav_2d)) 
      if [ $surface_z = YES ]
      then
# CREATE 2D FILE
      infile=$PARMofs/${RUN}_${modID}.archv2data_2z.in
      outfile=${RUN}_${modID}.t${cyc}z.${runtype}${LEAD}.grb
      executable=$EXECofs/ofs_archv2data2d_insitu
      export gridno=120
      $USHofs/ofs_gribit.sh

      export err=$?; err_chk
      echo ' ' >> $pgmout
      if [ -s fort.51 ] ; then mv fort.51 $outfile ;fi
      if [ -s $outfile ]
      then
        procstatus=0
        cat latlon.grb >> $outfile
      else
        procstatus=1
      fi
      if [ $procstatus = 0 ]
      then

         if [ $SENDCOM = YES ]
         then
           # Copy the grib file to /com:
           cp $outfile $COMOUT/.
         fi

         if [ $SENDDBN = YES ] ; then
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB $job $COMOUT/$outfile
         fi

         ##############################
         # Convert to grib2 format
         ##############################
         ($CNVGRIB -g12 -p40 $outfile ${outfile}.grib2
         $WGRIB2 ${outfile}.grib2 -s > ${outfile}.grib2.idx

         if [ $SENDCOM = YES ]
         then
           cp ${outfile}.grib2 $COMOUT/.
           cp ${outfile}.grib2.idx $COMOUT/.
         fi

         if [ $SENDDBN_GB2 = YES ]
         then
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${outfile}.grib2
            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${outfile}.grib2.idx
         fi )&

        fi
      fi

      if [ $surface_d = YES ]
      then
# CREATE 2D FILE
      infile=$PARMofs/${RUN}_${modID}.archv2data_2d.in
      outfile=${RUN}_${modID}.t${cyc}z.${runtype}${LEAD}.3d.grb
      executable=$EXECofs/ofs_archv2data2d
      export gridno=120
      $USHofs/ofs_gribit.sh

      export err=$?; err_chk
      echo ' ' >> $pgmout
      if [ -s fort.51 ] ; then mv fort.51 $outfile ;fi
      if [ -s $outfile ]
      then
        procstatus=0
        cat latlon.grb >> $outfile
        echo "$fhr" >$COMOUT/${RUN}_${modID}.t${cyc}z.nav.control
      else
        procstatus=1
      fi
      if [ $procstatus = 0 ]
      then

         if [ $SENDCOM = YES ]
         then
           # Copy the grib file to /com:
           cp $outfile $COMOUT/.
         fi

#?       if [ $SENDDBN = YES ] ; then
#?          $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB $job \
#?             $COMOUT/$outfile
#?       fi

         ##############################
         # Convert to grib2 format
         ##############################
#?         ($CNVGRIB -g12 -p40 $outfile ${outfile}.grib2
#?         $WGRIB2 ${outfile}.grib2 -s > ${outfile}.grib2.idx

#?         if [ $SENDCOM = YES ]
#?         then
#?           cp ${outfile}.grib2 $COMOUT/.
#?           cp ${outfile}.grib2.idx $COMOUT/.
#?         fi

#?         if [ $SENDDBN_GB2 = YES ]
#?         then
#?            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2 $job $COMOUT/${outfile}.grib2
#?            $DBNROOT/bin/dbn_alert MODEL OFSATL_GRB_GB2_WIDX $job $COMOUT/${outfile}.grib2.idx
#?         fi )&
      fi
# subdomain here
      $USHofs/ofs_grib_subdomain.sh
      fi

    fi

  # --------------------------------------------------------------------------- #
  # 6.  Ending output

  fhr=$((fhr + intvl)) 

done

if [ $procstatus = 0 ] 
then

    if [ $running_realtime = 't' ]
    then
      md5=/usr/bin/md5sum
      if [ -x $md5 ]
      then
        cd $COMOUT
        # need definition of files 
        for gfile in *.grb
        do
           $md5 $gfile >> $DATA/csum.$PDY$cyc
        done
        for gfile in `ls -C1 ${RUN}_${modID}.t${cyc}z.[FN]???.3d.grb ${RUN}_${modID}.t${cyc}z.[fn]???.grb`
        do
           $md5 $gfile >> $DATA/csum_nodc.$PDY$cyc
        done
        for exten in '' '_nodc'
        do
          cp $DATA/csum${exten}.$PDY$cyc $COMOUT/${RUN}_${modID}.t${cyc}z.checksum${exten}.log
          cd $COMOUT
          $md5 ${RUN}_${modID}.t${cyc}z.checksum${exten}.log >> ${RUN}_${modID}.t${cyc}z.checksum${exten}.log
          if [ $SENDDBN = YES ]
          then
            $DBNROOT/bin/dbn_alert MODEL OFSATL_LOG $job $COMOUT/${RUN}_${modID}.t${cyc}z.checksum${exten}.log
          fi 
        done
      fi
    fi

    echo "done" >$COMOUT/${RUN}_${modID}.t${cyc}z.nav.log
    msg='THE OFS_ATL_POST JOB HAS ENDED NORMALLY.'
    postmsg "$jlogfile" "$msg"
fi
################## END OF SCRIPT #######################
