#   PLOTPGM1 for hours 00 and 12 
#
#########################################################################
# Name: explotpgm1       Author:  Bob Hollern
#
# Abstract: The script runs the programs to generate the plot files
#    to be sent to the Intergraph.  The script is to be executed at
#    0:40Z to generate the plot file for the 12 Hour Maximum
#    Temperature FAX/AFOS Chart, and to be run at 12:40Z to generate
#    the plot file for the Daily Weather Map and the 12 Hour Minimum
#    Temperature FAX/AFOS Chart.
#       The BUFR dump script program DUMPJB is executed to retrieve
#    land synoptic and metar station data for different hours.
#
##########################################################################
#    
# FLOW OF CONTROL
# 
# 1) Execute DUMPJB to dump land synoptic and metar station data at 
#    different hours.
#
# 2) Execute PLTPGM1 to generate the plotfiles.
#
# 3) Execute THINMXMN to thin the plotfiles.
#
# 4) Execute PLOTMXMN to make MAX/MIN temperature chart for fax.
#
##########################################################################
#
# History: 
#
#          March 1997 - 1st implementation
#
#          September 1997 - Get metar table from /nwprod/dictionaries
#                           directory 
#
#          November 1997 - Add steps to produce max/min plots directly
#                          on the Cray
#  
#           Oct. 2005 - Steve Lilly - converted fax graphics to T4
#           format and stopped writing to the stat file.
#           Removed the logic for snd2forgn
#########################################################################
set -x

cd $DATA

HOUR=$1

export FORM=${PDY}${HOUR}
DUMP=/nwprod/ush/dumpjb

ix=0

set +x
echo
echo "************"
echo "* PLOTPGM1"
echo "************"
echo
set -x

############################################
msg="Script expltpgm1.sh starting"
postmsg "$jlogfile" "$msg"
############################################

############################################
# dump synop and metar data
############################################

if test "$HOUR" = '12'
then
  HRM6=${PDY}06
  range=3
  TYPE=MIN
else
  HRM6=${PDYm1}18
  range=0.5
  TYPE=MAX
fi

#
#     DUMP THE SYNOPTIC DATA
#
group="000.001"
if test -s  synop.${PDY}${HOUR} 
then
  echo " synop.${PDY}${HOUR} already dumped."
  ix=`expr $ix + 1`
else
  $DUMP ${PDY}${HOUR} .5 synop
  errdmp1=$?
  if [ "$errdmp1" -eq 0 ]
  then
    count=`grep "$group  HAS" synop.${PDY}${HOUR} | awk '{print $3}'`
    msg="Dumped ${count} $TYPE reports for ${PDY}${HOUR}."
    postmsg  "$jlogfile" "$msg"
    ix=`expr $ix + 1`
  else
    msg="No synop data for ${PDY}${HOUR}!"
    postmsg  "$jlogfile" "$msg"
  fi
fi

#
#     DUMP THE METAR DATA
#
group="000.007"
if test -s metar.${PDY}${HOUR} 
then
  echo " metar.${PDY}${HOUR} already dumped."
  ix=`expr $ix + 1`
else
  $DUMP ${PDY}${HOUR} .5 metar
  errdmp2=$?
  if [ "$errdmp2" -eq 0 ]
  then
    count=`grep "$group  HAS" metar.${PDY}${HOUR} | awk '{print $3}'`
    msg="Dumped ${count} $TYPE reports for ${PDY}${HOUR}."
    postmsg  "$jlogfile" "$msg"
    ix=`expr $ix + 1`
  else
    msg="No synop data for ${PDY}${HOUR}!"
    postmsg  "$jlogfile" "$msg"
  fi
fi

#
#     DUMP THE 6-HOUR OLD METAR DATA
#

export FORM=${HRM6}
$DUMP ${HRM6} $range metar
errdmp3=$?
if [ "$errdmp3" -eq 0 ]
then
  count=`grep "$group  HAS" metar.${HRM6} | awk '{print $3}'`
  msg="Dumped ${count} $TYPE reports for ${HRM6}."
  postmsg  "$jlogfile" "$msg"
  ix=`expr $ix + 1`
else
  msg="No synop data for ${HRM6}!"
  postmsg  "$jlogfile" "$msg"
fi

if [ "$ix" -eq '0' ]
then
  msg="ERROR - NO DATA TO DUMP - ABORTING script expltpgm1.sh"
  postmsg "$jlogfile" "$msg"
  export pgm="dumping"
  err_exit
fi

echo $PDY$HOUR > date.input

export pgm=plotpgm1
. prep_step

# input
export XLFUNIT_15="synop.${PDY}12"
export XLFUNIT_16="metar.${PDY}12"
export XLFUNIT_17="metar.${PDY}06"
export XLFUNIT_18="synop.${PDY}00"
export XLFUNIT_19="metar.${PDY}00"
export XLFUNIT_20="metar.${PDYm1}18"
export XLFUNIT_30="/nwprod/dictionaries/metar.tbl"

# output
export XLFUNIT_55="plotfile1.dat12"
export XLFUNIT_56="plotfile1.dat00"
 
startmsg
$EXECgraph/plotpgm1 < date.input >> $pgmout 2> errfile
export err=$?;err_chk

export pgm=thinmxmn
. prep_step

export XLFUNIT_10="plotfile1.dat${HOUR}"
export XLFUNIT_12="$FIXutil/hourly_mxmn.table"
export XLFUNIT_50="plotfile1.thin${HOUR}"

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

# copy output to /pcom and call snd2forgn
# FTYPE and FOREIGN are needed for snd2forgn.

if test "$SENDCOM" = 'YES'
then
  cp plotfile1.thin${HOUR} $pcom/plotpgm1.plotfile${HOUR}.$job
  #if test "$SENDDBN" = 'YES'
  #then
  #  grep -i pltfrw2$HOUR $FTABLE >>mgot
  #  FTYPE=`awk '{print $2}' mgot`
  #  FOREIGN=`awk '{print $3}' mgot`
  #  rm mgot
  #  $USHutil/snd2forgn $FTYPE $FOREIGN $job $pcom/plotpgm1.plotfile${HOUR}.$job
  #fi
fi
 
# Make the max/min fax product

echo ${TYPE} > maxmin.input
echo ${HOUR} >> maxmin.input

cp $FIXutil/graph_gphbg/nh1302.pur nh1302.pur

export pgm=plotmxmn
. prep_step

jobn=rw2${HOUR}
FAXOUT=faxmxmn.${HOUR}

   export XLFUNIT_24="plotfile1.thin${HOUR}"      
   export XLFUNIT_41="nh1302.pur"
   export XLFUNIT_48="$FIXutil/graph_awpseed"
   export XLFUNIT_55="putlab.55"
   export XLFUNIT_60="f60"
   export XLFUNIT_61="f61"
   export XLFUNIT_62="f62"
   export XLFUNIT_63="f63"
   export XLFUNIT_76="outdgn1"
   export XLFUNIT_77="outdgn2"
   export XLFUNIT_80="faxext8"
   export XLFUNIT_89="pureras"
   export XLFUNIT_81="faxmxmn.${HOUR}"

$EXECgraph/plotmxmn < maxmin.input >> $pgmout 2> errfile
export err=$?;err_chk

# copy maxmin map to /pcom and call snd2forgntbl.sh to send map to Intergraph
# NCDC and OSO

model=hourly
export model


if [ ${cyc} = 00 ];
then
  cp $FAXOUT ObsMaximumTmp00Z.${cyc}
  FAXOUT=ObsMaximumTmp00Z.${cyc}
  for KEYW in MAX_TMP MAXT_g
  do

  grep $KEYW $FIXutil/identifyfax.tbl | read Keyword sub00 sub06 sub12 sub18 gif toc prt lprt name

  submn=$sub00

echo $FAXOUT $submn $name $Keyword $gif $toc $prt $jobn $lprt
export FAXOUT submn name Keyword gif toc prt jobn lprt

  $USHutil/mk_graphics.sh

 done

fi

if [ ${cyc} = 12 ];
then
  cp $FAXOUT ObsMinimumTmp12Z.${cyc}
  FAXOUT=ObsMinimumTmp12Z.${cyc}
  for KEYW in MIN_TMP MINT_g
  do

  grep $KEYW $FIXutil/identifyfax.tbl | read Keyword sub00 sub06 sub12 sub18 gif toc prt lprt name

  submn=$sub12

echo $FAXOUT $submn $name $Keyword $gif $toc $prt $jobn $lprt
export FAXOUT submn name Keyword gif toc prt jobn lprt

  $USHutil/mk_graphics.sh

 done

fi

#############################################################
msg='Script expltpgm.sh completed normally.'
postmsg "$jlogfile" "$msg"
#############################################################

# ---------------END OF EXPLTPGM SCRIPT ----------------------------
