########################################################################
# Name: explotpgm2.sh         Author: Bob Hollern 
#
# Abstract:  This script runs the program to generate plot files
#    which are used by graphic programs to generate AFOS products.
#    The program is executed at 0040Z, 0640Z, 1240Z, and 1840Z.
#    The plot files contain precipitation and snow amounts from
#    North American land synoptic, metar, and supplementary
#    climatological station reports.
#
# History:
#
#          March 1997 - 1st implementation   
#
#          September 1997 - Get metar table from /nwprod/dictionaries
#                           directory
#
#          November 1997  - Get date (year, month, day of month)
#                           for plot file from script variables
#                           environmental variables
#
#          August 1999    - Converted program to run on IBM SP              
#
#           Oct. 2005 - Steve Lilly - converted fax graphics to T4
#           format and stopped writing to the stat file.
#           Removed the logic for snd2forgn
#
#           Dec. 2005 - Steve Lilly - added util reducemap.sh so HPC
#           could received missing data over the West coast.
#########################################################################

set +x
echo
echo "************"
echo "  PLOTPGM2"
echo "************"
echo
set -x

cd $DATA

############################################# 
export msg="Script expltpgm2.sh starting"
postmsg "$jlogfile" "$msg"
############################################# 

############################################# 
#  HOUR indicates whether this is a 00Z, 06Z, 12Z, or 18Z run
############################################# 
HOUR=$1
 
############################################# 
#  dump script
############################################# 
DUMP=/nwprod/ush/dumpjb

ix=0

############################################# 
# date record of output file
############################################# 
echo ${PDY}${HOUR} > date.input

############################################# 
#     DUMP THE SYNOPTIC DATA
############################################# 
if test -s  $DATA/synop.${PDY}${HOUR} 
then
  msg=" $DATA/synop.${PDY}${HOUR} already dumped."
  postmsg "$jlogfile" "$msg"
  ix=`expr $ix + 1`
else
  TYPE='synop'
  RADIUS=.5
  DATE=${PDY}${HOUR}
  export FORM=$DATE
  $DUMP $DATE $RADIUS $TYPE
  errdmp1=$?
 
  if [ "$errdmp1" -eq 0 ]
  then
     ix=`expr $ix + 1`
  else
     msg="No synop data for ${PDY}${HOUR}!"
     postmsg "$jlogfile" "$msg"
  fi
fi

############################################# 
#     DUMP THE METAR DATA
############################################# 
if test -s  $DATA/metar.${PDY}${HOUR} 
then
  msg=" $DATA/metar.${PDY}${HOUR} already dumped."
  postmsg "$jlogfile" "$msg"
  ix=`expr $ix + 1`
else
  TYPE='metar'
  RADIUS=.5
  DATE=${PDY}${HOUR}
  export FORM=$DATE
  $DUMP $DATE $RADIUS $TYPE
  errdmp2=$?
 
  if [ "$errdmp2" -eq 0 ]
  then
    ix=`expr $ix + 1`
  else
     msg="No metar data for ${PDY}${HOUR}!"
     postmsg "$jlogfile" "$msg"
  fi
fi

############################################# 
#     DUMP THE SCD STATION DATA
############################################# 
if test -s  $DATA/scd.${PDY}${HOUR} 
then
  msg=" $DATA/scd.${PDY}${HOUR} already dumped."
  postmsg "$jlogfile" "$msg"
  ix=`expr $ix + 1`
else
  TYPE='scd'
  RADIUS=.5
  DATE=${PDY}${HOUR}
  export FORM=$DATE
  $DUMP $DATE $RADIUS $TYPE
  errdmp3=$?
 
  if [ "$errdmp3" -eq 0 ]
  then
     ix=`expr $ix + 1`
  else
     msg="No scd data for ${PDY}${HOUR}!"
     postmsg "$jlogfile" "$msg"
  fi
fi

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

export pgm=plotpgm2
. prep_step

# input files
export XLFUNIT_15="$DATA/synop.${PDY}${HOUR}"
export XLFUNIT_16="$DATA/metar.${PDY}${HOUR}"
export XLFUNIT_17="$DATA/scd.${PDY}${HOUR}"
export XLFUNIT_30="/nwprod/dictionaries/metar.tbl"

# output plot files
export XLFUNIT_55="plotfile2.dat00"
export XLFUNIT_56="plotfile2.dat06"
export XLFUNIT_57="plotfile2.dat12"
export XLFUNIT_58="plotfile2.dat18"

startmsg
$EXECutil/plotpgm2 < date.input >> $pgmout 2>errfile
export err=$?;err_chk

export pgm=thinprec
. prep_step

export XLFUNIT_10="plotfile2.dat${HOUR}"
export XLFUNIT_50="plotfile2.thin${HOUR}"

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

############################################
# copy plotfile to /pcom 
############################################
if test "$SENDCOM" = 'YES'
then
  cp plotfile2.thin${HOUR} $pcom/plotpgm2.plotfile${HOUR}.$job
fi

#  Make the fax 6-hr, 24-hr and snow cover fax charts


#  Create the charts:

#  00Z  -   In-house 6-hr precip & snow cover charts
#  06Z  -   In-house 6-hr precip & snow cover charts
#  12Z  -   In-house 6-hr precip, 24-hr precip & snow cover charts
#       -   Fax 24-hr precip & snow cover charts
#  18Z  -   In-house 6-hr precip & snow cover charts

PMAP=SNOW6HR
if test "${HOUR}" = '12'
then
   PMAP=24HRSNOW6HR
fi

echo ${PMAP}  > plotprec.input
echo ${HOUR} >> plotprec.input

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

export pgm=plotprec
. prep_step

   export XLFUNIT_12="$FIXgraph/hrly_verf.table"
   export XLFUNIT_24="plotfile2.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="precmaps.${HOUR}"

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


#  Create the fax charts at 12Z

if test "${HOUR}" = '12'
then

  PMAP=SNOF

  echo ${PMAP}  > snowplot.input
  echo ${HOUR} >> snowplot.input

export pgm=plotprec
. prep_step

   export XLFUNIT_12="$FIXgraph/hrly_verf.table"
   export XLFUNIT_24="plotfile2.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="6HrSnowCover12Z.${cyc}"

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

  PMAP=24HF

  echo ${PMAP}  > 24hrplot.input
  echo ${HOUR} >> 24hrplot.input

export pgm=plotprec
. prep_step

   export XLFUNIT_12="$FIXgraph/hrly_verf.table"
   export XLFUNIT_24="plotfile2.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="24Hrprecplot12Z.${cyc}"

$EXECgraph/plotprec  < 24hrplot.input >> $pgmout 2> errfile
export err=$?;err_chk

fi

jobn=${cyc}.rw2${cyc}
FAXOUT=precmaps.${HOUR} 

    for KEYW in SNOWOB_p 06HPPT_p
    do

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

  if [ ${cyc} = '00' ]; then submn=$sub00; fi
  if [ ${cyc} = '06' ]; then submn=$sub06; fi
  if [ ${cyc} = '12' ]; then submn=$sub12; fi
  if [ ${cyc} = '18' ]; then submn=$sub18; fi

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

#   use reducemap.sh  to print on a small 8X11 paper !

   export member=reducemap.sh

INFILE=$FAXOUT
lprntr=${lprt}
subs=$submn

  $USHutil/$member $INFILE $lprntr "${subs}"

  lpr -P ${lprt} $pcom/pcl.$submn.$job

 done


if [ ${cyc} = 12 ];
then
  for KEYW in 24HRPCPN 24HPPT_g 12ZSNOW 12ZSNO_g 24HPPT_p
  do

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

  submn=$sub12

  if [ ${submn} = 2254 ]; then FAXOUT=6HrSnowCover12Z.${cyc}; fi
  if [ ${submn} = 2830 ]; then FAXOUT=precmaps.${HOUR}; fi
  if [ ${submn} = 2831 ]; then FAXOUT=24Hrprecplot12Z.${cyc}; fi
 
if [ ${submn} = 2254 -o ${submn} = 2831 ]
then
 if test "$SENDCOM" = 'YES'
   then
     cp ${FAXOUT} ${COMOUT}
     if test "$SENDDBN" = 'YES'
     then
       $DBNROOT/bin/dbn_alert MODEL NCDCGIF ${job} ${COMOUT}/$FAXOUT
     fi
 fi

echo $FAXOUT $submn $name $Keyword $gif $toc $prt $jobn $lprt
export FAXOUT submn name Keyword gif toc prt jobn lprt
  $USHutil/mk_graphics.sh

fi

if [ ${submn} = 2830 ]
then

#   use reducemap.sh  to print on a small 8X11 paper !

   export member=reducemap.sh

INFILE=$FAXOUT
lprntr=${lprt}
subs=$submn

  $USHutil/$member $INFILE $lprntr "${subs}"

  lpr -P ${lprt} $pcom/pcl.$submn.$job

fi

 done
fi
########################################
msg="expltpgm2.sh completed"
postmsg "$jlogfile" "$msg"
########################################

exit
