#!/bin/sh

#####################################################################
#  AWIPS Redbook Graphics Generation for GFS
#  Script: exgfs_redbook.sh.smd
#  History: OCT 2004 Julia Zhu --First Implementation
#           Oct 2005 L. Sager  --Replace afstoawp to redbook_ntc to 
#                                send products via NTC rather than
#                                status file.
#####################################################################

fhr=$1
cd $DATA

#####################################################################
#
# START FLOW OF CONTROL
#
# 1) Get the GRIB files from /com
# 2) Make AFOS Maps on 90N and 90S
#    and Convert it to AWIPS format (for hour=72 only)
#
#####################################################################

set -x
###############################################
# Wait for the availability of the pgrib file
###############################################
icnt=1
while [ $icnt -lt 1000 ]
do
  if [ -s $COMIN/${RUN}.${cycle}.pgrbf$fhr ]
  then
     break
  fi

  sleep 10
  icnt=$((icnt + 1))
  if [ $icnt -ge 180 ]
  then
      msg="ABORTING after 30 min of waiting for the pgrib filei!"
      err_exit $msg
  fi
done

########################################
msg="HAS BEGUN"
postmsg "$jlogfile" "$msg"
########################################

hr=00
inc=12
while [ $hr -le $fhr ]
do
   cp $COMIN/${RUN}.${cycle}.pgrbf${hr} pgrbf${hr}
   cp $COMIN/${RUN}.${cycle}.pgrbif${hr} pgrbif${hr}
   hr=`expr $hr + $inc`
   if [ $hr -lt 10 ]; then hr=0$hr; fi
done



##########################################
if [ $cyc -eq 00 -a $fhr -eq 72 ]
then

  set +x
  echo "######################################"
  echo " MAKE AFOS MAPS 90N and 90X."
  echo "######################################"
  set -x

  export pgm=graph_maxmintt
  . prep_step

  export AFOSMXMN=afos.maxmintt${cyc}.${job}
  export XLFUNIT_12="$FIXgfs/mdl_constant"
  export XLFUNIT_16="$PARMgraph/graph_maxmintt${cyc}.control"
  export XLFUNIT_20="$FIXgraph/graph_pillist1"
  export XLFUNIT_71="$AFOSMXMN"

  startmsg
  $EXECgraph/graph_maxmintt PARM="C" >> $pgmout 2> errfile
  err=$?;export err; err_chk

  set +x
  echo "#############################################################"
  echo " Convert AFOS MAPS 90N and 90X to AWIPS format."
  echo "#############################################################"
  set -x

  pils='90n 90x'
  $USHutil/redbook_ntc.sh $AFOSMXMN "${pils}"
  
fi
##################################################################


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

msg='HAS COMPLETED NORMALLY.'
postmsg "$jlogfile" "$msg"

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