###################################################################
echo "----------------------------------------------------"
echo "exnawips - convert NCEP GRIB files into GEMPAK Grids"
echo "----------------------------------------------------"
echo "History: Mar 2000 - First implementation of this new script."
echo "S Lilly: May 2008 - add logic to make sure that all of the "
echo "                    data produced from the restricted ECMWF"
echo "                    data on the CCS is properly protected."
#####################################################################

set -xa

cd $DATA

msg="Begin job for $job"
postmsg "$jlogfile" "$msg"

#
NAGRIB_TABLE=${NAGRIB_TABLE:-/nwprod/gempak/fix/nagrib.tbl}
utilfix_nam=${utilfix_nam:-/nwprod/util/fix}
NAGRIB=nagrib_nc
#

entry=`grep "^$RUN " $NAGRIB_TABLE | awk 'index($1,"#") != 1 {print $0}'`

if [ "$entry" != "" ] ; then
  cpyfil=`echo $entry  | awk 'BEGIN {FS="|"} {print $2}'`
  garea=`echo $entry   | awk 'BEGIN {FS="|"} {print $3}'`
  gbtbls=`echo $entry  | awk 'BEGIN {FS="|"} {print $4}'`
  maxgrd=`echo $entry  | awk 'BEGIN {FS="|"} {print $5}'`
  kxky=`echo $entry    | awk 'BEGIN {FS="|"} {print $6}'`
  grdarea=`echo $entry | awk 'BEGIN {FS="|"} {print $7}'`
  proj=`echo $entry    | awk 'BEGIN {FS="|"} {print $8}'`
  output=`echo $entry  | awk 'BEGIN {FS="|"} {print $9}'`
else
  cpyfil=gds
  garea=dset
  gbtbls=
  maxgrd=4999
  kxky=
  grdarea=
  proj=
  output=T
fi  
pdsext=no

maxtries=180
fhcnt=$fstart
while [ $fhcnt -le $fend ] ; do
  if [ $fhcnt -ge 100 ] ; then
    typeset -Z3 fhr
  else
    typeset -Z2 fhr
  fi
  fhr=$fhcnt

  fhr3=$fhcnt
  typeset -Z3 fhr3
  GRIBIN=$COMIN/${model}.${cycle}.${GRIB}${fhr}${EXT}
  GEMGRD=${RUN}_${PDY}${cyc}f${fhr3}

  if [ $RUN = rap13 -o $RUN = rap32 ]; then
    NAGRIB=nagrib2
  fi

  if [ $RUN = "rap" -o $RUN = "rap20" ]; then 
    GRIBIN_chk=$GRIBIN.grib2.idx
  elif [ $RUN = "rap13" -o $RUN = "rap32" ]; then
    GRIBIN_chk=$GRIBIN.idx
  fi

  icnt=1
  while [ $icnt -lt 1000 ]
  do
    if [ -r $GRIBIN_chk ] ; then
      break
    else
      let "icnt=icnt+1"
      sleep 20
    fi
    if [ $icnt -ge $maxtries ]
    then
      msg="ABORTING after 1 hour of waiting for F$fhr to end."
      err_exit $msg
    fi
  done

  cp $GRIBIN grib$fhr

  startmsg

  $GEMEXE/$NAGRIB << EOF
   GBFILE   = grib$fhr
   INDXFL   = 
   GDOUTF   = $GEMGRD
   PROJ     = $proj
   GRDAREA  = $grdarea
   KXKY     = $kxky
   MAXGRD   = $maxgrd
   CPYFIL   = $cpyfil
   GAREA    = $garea
   OUTPUT   = $output
   GBTBLS   = $gbtbls
   GBDIAG   = 
   PDSEXT   = $pdsext
  l
  r
EOF
  export err=$?;err_chk

  #####################################################
  # GEMPAK DOES NOT ALWAYS HAVE A NON ZERO RETURN CODE
  # WHEN IT CAN NOT PRODUCE THE DESIRED GRID.  CHECK
  # FOR THIS CASE HERE.
  #####################################################

  if [ "$NAGRIB" = "nagrib2" ] ; then
    gpend
  fi

  #
  # Create ZAGL level products for the rap
  #
  if [ "$RUN" = "rap" -o "$RUN" = "rap20" -o "$RUN" = "rap32" ] ; then
    $GEMEXE/gdvint << EOF
     GDFILE   = $GEMGRD
     GDOUTF   = $GEMGRD
     GDATTIM  = f${fhr}
     GVCORD   = pres/zagl
     GLEVEL   = 500-9000-500
     MAXGRD   = 5000
     GAREA    = $garea
     VCOORD   = mslv;esfc
     l
     r
EOF
  fi


  #
  # Create theta level prodcuts for the RUC
  # Delete 2 Hour Precip accumulations
  #
  if [ "$RUN" = "rap" -o "$RUN" = "rap20" -o "$RUN" = "rap32" ] ; then
    $GEMEXE/gdvint << EOF
     GDFILE   = $GEMGRD
     GDOUTF   = $GEMGRD
     GDATTIM  = f${fhr}
     GVCORD   = pres/thta
     GLEVEL   = 270-330-3 
     MAXGRD   = 5000 
     GAREA    = 25;-120;50;-60 
     VCOORD   = /l 
     l
     r 
EOF
    if [ "$fhr" = "05" -o  "$fhr" = "08" -o "$fhr" = "11" ] ; then
      $GEMEXE/gddelt_gn << EOF
      GDFILE   = $GEMGRD
      GDATTIM  = f${fhr}
      GVCORD   = none
      GLEVEL   = 0
      GFUNC    = s02m;c02m
      l
      r
EOF
    fi
  fi
  if [ $SENDCOM = "YES" ] ; then
     mv $GEMGRD $COMOUT/$GEMGRD
     if [ $SENDDBN = "YES" ] ; then
       $DBNROOT/bin/dbn_alert MODEL ${DBN_ALERT_TYPE} $job \
           $COMOUT/$GEMGRD
     else
       echo "##### DBN_ALERT_TYPE is: ${DBN_ALERT_TYPE} #####"
     fi
  fi

  let fhcnt=fhcnt+finc
done

if [ $RUN = rap ]; then 
  $SMSBIN/setev release_meta
fi

#####################################################################
# GOOD RUN
set +x
echo "**************JOB $RUN NAWIPS COMPLETED NORMALLY ON THE IBM"
echo "**************JOB $RUN NAWIPS COMPLETED NORMALLY ON THE IBM"
echo "**************JOB $RUN NAWIPS COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################

msg='Job completed normally.'
echo $msg
postmsg "$jlogfile" "$msg"

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