# NAME     : exafossta.sh
# Author   : Chris Caruso
# Purpose  : This script generates AFOS charts P0X (observed max 
#            temperature), P0N (observed min temperature), P0P 
#            (observed 24 hr precip), and P0S (observed total snow
#            cover).  P0P and P0S use the output from expltpgm2.sh
#            as input, and P0X and P0N use the output from expltpgm1.sh
#            as input.  P0P, P0S, and P0N are produced at 12Z and P0X
#            is produced at 00Z.
# History  : 98-01-27 Chris Caruso 
#          : 98-03-16 Peter Henrichsen /Chris Caruso - Add 3 afstoawp 
#                     steps to convert AFOS maps into awips format. 
#          : 05-08-31 Larry Sager      Replace afstoawp with redbook_ntc
#                     to transfer graphics to TOC via NTC
#
# Arguments: cychr (argument 1)    - cycle hour           
#
#___________________________________________________________________________
#

set -x
cychr=$1

ix=0
iy=0
###########################################
#     copy the appropriate plotfile
###########################################
if test -s  $pcom/plotpgm1.plotfile${cychr}.$job
then
  ix=`expr $ix + 1`
  cp $pcom/plotpgm1.plotfile${cychr}.$job plotfile1.${cychr}
else
  msg1="Plotfile from plotpgm1 not available for ${cychr}!"
  postmsg  "$jlogfile" "$msg1"
fi
if [ $cychr -eq 12 ]
then
  if test -s $pcom/plotpgm2.plotfile${cychr}.$job
  then
    iy=`expr $iy + 1`
    cp $pcom/plotpgm2.plotfile${cychr}.$job plotfile2.${cychr}
  else
    msg1="Plotfile from plotpgm2 not available for ${cychr}!"
    postmsg  "$jlogfile" "$msg1"
  fi
fi

if [ $cychr -eq 00 -a $ix -eq 0 ] 
then
  msg="ERROR NO PLOTFILE AVAILABLE - ABORTING script exafossta"
  postmsg "$jlogfile" "$msg"
  export pgm="dumping"
  err_exit
fi
if [ $cychr -eq 12 ] 
then
  if [ $ix -eq 0 -a $iy -eq 0 ] 
  then
    msg="ERROR NO PLOTFILES AVAILABLE - ABORTING script exafossta"
    postmsg "$jlogfile" "$msg"
    export pgm="dumping"
    err_exit
  fi
fi

if [ $cychr -eq 00 -a $ix -eq 1 ]
then
  ###########################################
  # run afossta to make AFOS chart P0X
  ###########################################
  afosfilex=afossta.P0X.$job
  export pgm=afossta
  . prep_step

  export XLFUNIT_11="$PARMutil/hourly_afossta.max"
  export XLFUNIT_12="plotfile1.${cychr}"
  export XLFUNIT_20="$FIXgraph/graph_pillist1"
  export XLFUNIT_75="LOGMSG"
  export XLFUNIT_71="$afosfilex"

  startmsg
  $EXEChrly/afossta $cychr >> $pgmout 2> errfile
  export err=$?;err_chk

  if test "$SENDCOM" = 'YES'
  then
    cp $afosfilex $pcom/$afosfilex
    if test "$SENDDBN" = 'YES'
    then
      $USHutil/snd2forgn AFOS hrly $job $pcom/$afosfilex
    fi

    set +x
    echo " "
    echo "#############################################################"
    echo " Convert AFOS MAP P0X to AWIPS format."
    echo "#############################################################"
    echo " "
    set -x

    pils='p0x'
    $USHutil/redbook_ntc.sh $pcom/$afosfilex "${pils}"
  fi
fi

if [ $cychr -eq 12 -a $ix -eq 1 ]
then
  ###########################################
  # run afossta to make AFOS chart P0N
  ###########################################
  afosfilen=afossta.P0N.$job
  export pgm=afossta
  . prep_step

  export XLFUNIT_11="$PARMutil/hourly_afossta.min"
  export XLFUNIT_12="plotfile1.${cychr}"
  export XLFUNIT_20="$FIXgraph/graph_pillist1"
  export XLFUNIT_75="LOGMSG"
  export XLFUNIT_71="$afosfilen"

  startmsg
  $EXECutil/afossta $cychr >> $pgmout 2> errfile
  export err=$?;err_chk

  if test "$SENDCOM" = 'YES'
  then
    cp $afosfilen $pcom
    if test "$SENDDBN" = 'YES'
    then
      $USHutil/snd2forgn AFOS hrly $job $pcom/$afosfilen
    fi

    set +x
    echo " "
    echo "#############################################################"
    echo " Convert AFOS MAP P0N to AWIPS format."
    echo "#############################################################"
    echo " "
    set -x

    pils='p0n'

    $USHutil/redbook_ntc.sh $pcom/$afosfilen "${pils}"


  fi
fi

if [ $cychr -eq 12 -a $iy -eq 1 ]
then
  ###########################################
  # run afossta to make AFOS charts P0P & P0S
  ###########################################
  afosfile=afossta.P0PP0S.$job
  export pgm=afossta
  . prep_step

  export XLFUNIT_11="$PARMutil/hourly_afossta.precip"
  export XLFUNIT_12="plotfile2.${cychr}"
  export XLFUNIT_20="$FIXgraph/graph_pillist1"
  export XLFUNIT_75="LOGMSG"
  export XLFUNIT_71="$afosfile"

  startmsg
  $EXEChrly/afossta $cychr >> $pgmout 2> errfile
  export err=$?;err_chk

  if test "$SENDCOM" = 'YES'
  then
    cp $afosfile $pcom
    if test "$SENDDBN" = 'YES'
    then
      $USHutil/snd2forgn AFOS hrly $job $pcom/$afosfile
    fi

    set +x
    echo " "
    echo "#############################################################"
    echo " Convert AFOS MAP P0P and P0S to AWIPS format."
    echo "#############################################################"
    echo " "
    set -x

    pils='p0p p0s'

    $USHutil/redbook_ntc.sh $pcom/$afosfile "${pils}"

  fi
fi

set +x

echo " "
echo "Leaving shell script exafossta.sh"
echo " "
