set +x
#################################################
#----------------------------------------------------------------
# Name    : exwxdep.sh
# Author  : Chris Caruso 
# Purpose : This shell gets METAR data and
#           creates the aviation weather depiction
#           for FAX and/or AFOS
#           Script runs every 3 hours in RW1, RW3, RW4, and RW5.
#           AFOS products are made in RW1, RW3, RW4, and RW5.
#           FAX product is made in RW3 and RW4.    
#note:  use local versions of wxpx codes for testing.
# History :
#           Mar 1997  - first implementation
#           Sept 1997 - combined version
#           June 1998 - C. Caruso - removed job step wxpxdata
#           (which is now a subroutine of wxpxplot), removed
#           unnecessary assign statements, replace direct calls
#           to dbn_alert with calls to snd2forgn.
#           Jan. 1999 - C. Caruso - removed job step wxpxfrnt
#           (rewrote it and made it a subroutine of wxpxanal).
#           Feb. 1999 - C. Caruso - added job step fronts which
#           adds additional nodes to the nmap-generated ascii
#           text fronts file.  Output file with additional nodes
#           is then copied to pcom and used in job step wxpxanal.
#           Jan. 2000 - C. Caruso Magee - changed jlogfile messages
#           that are printed if fronts file is out of date to less
#           ominous wording. If job step wxpxfrnt has a non-zero
#           exit status for any reason, it's not a fatal error. 
#           A message will print stating that fronts control file
#           is being used, and wx dep. fax chart will have the message
#           "surface fronts are not available".  Also changed script
#           to only look back up to 3 hours for a fronts file, not
#           6 hours as before - don't want 6 hour old fronts shown
#           on the map!
#           Feb. 2005 - C. Caruso Magee - changed logic that looks for
#           fronts file to use new fronts filename of wxd.dat and
#           check 1st line of file for correct date/time.
#           Oct. 2005 - Steve Lilly - converted fax graphics to T4
#           format and stopped writing to the stat file.
#           Removed the logic for snd2forgn
#----------------------------------------------------------------
set -x

HOUR=$1

#################################################
#
# MAKEFAX=f to make fax chart ; MAKEFAX=x to omit fax chart
# MAKEAFOSPLOT=p to make afos plot chart ; MAKEAFOSPLOT=x to omit chart
# MAKEAFOSANAL=a to make afos analysis chart ; MAKEAFOSANAL=x to omit chart
#
#################################################
case $RUN in
rw1)  MAKEFAX=f          
      MAKEAFOSPLOT=p
      MAKEAFOSANAL=a ;;

rw3)  MAKEFAX=f
      MAKEAFOSPLOT=p
      MAKEAFOSANAL=a ;;

rw4)  MAKEFAX=f
      MAKEAFOSPLOT=p
      MAKEAFOSANAL=a ;;

rw5)  MAKEFAX=f
      MAKEAFOSPLOT=p
      MAKEAFOSANAL=a ;;
 
*)    exit ;;
 
esac
#################################################
#
# TESTMODE=o for operations
# TESTMODE=t for test
#
#################################################
TESTMODE=o
echo $MAKEFAX 
echo $MAKEAFOSPL0T
echo $MAKEAFOSANAL 
PARM1="${MAKEFAX}${MAKEAFOSPLOT}${MAKEAFOSANAL}XXX${TESTMODE}"

############################################
#
#     set name of fronts file.  check for
#     a fronts file up to 3 hours old. 
#
############################################
hourin=$HOUR
fasg=false
filedate=`head -1 /pcom/foreign/fronts/wxd.dat | cut -c1-8`
filehour=`head -1 /pcom/foreign/fronts/wxd.dat | cut -c9-10`
if [ ${hourin} -ge 3 ]; then
  if [ ${filedate} -eq $PDY ]; then
    for i in 0 1 2 3
    do
      hourp=`expr ${hourin} - ${i}`
      if [[ ${fasg} = false ]]; then
        if [ ${filehour} -eq ${hourp} ]; then
          FRONTSIN=/pcom/foreign/fronts/wxd.dat
          FRONTFIL=wxd.dat
          fasg=true
        fi
      fi
    done
    echo hourp="${hourp}"
  fi
elif [ ${hourin} -eq 1 -o ${hourin} -eq 2 ]; then
  if [ ${filedate} -eq $PDY -a ${filehour} -eq 0 ]; then
     FRONTSIN=/pcom/foreign/fronts/wxd.dat
     FRONTFIL=wxd.dat
     fasg=true
  fi
else
############################################
#
# comes here if hourin = 0 and only uses
# today's 00Z fronts or yesterday's 21Z fronts.
#
############################################
  if [ ${filedate} -eq $PDY -a ${filehour} -eq  0 ]; then
     FRONTSIN=/pcom/foreign/fronts/wxd.dat
     FRONTFIL=wxd.dat
     fasg=true
  elif [ ${filedate} -eq $PDYm1 -a ${filehour} -eq  21 ]; then
     FRONTSIN=/pcom/foreign/fronts/wxd.dat
     FRONTFIL=wxd.dat
     fasg=true
  fi
fi
echo  FRONTSIN=$FRONTSIN
echo fasg=$fasg

############################################
#
#     CREATE CONTROL FILES
#     run identifier
#
############################################
echo $RUN | tr '[a-z]' '[A-Z]' > runid
# redefine krun here; needs to be 3 or 4 for fax only.
krun=`echo $RUN | cut -c3-3`

######################################################
#
#     date file
#
######################################################
echo $PDY$HOUR > datetemp

######################################################
#
#     LOAD THE METAR DATA IF NOT DONE YET
#
######################################################
export FORM=${PDY}${HOUR}
DUMP=/nwprod/ush/dumpjb

######################################################
#
#     DUMP THE METAR DATA
#
######################################################
if test -s  metar.${PDY}${HOUR}
then
   echo " metar.${PDY}${HOUR} already dumped."
else
   $DUMP ${PDY}${HOUR} .5 metar
   errdmp2=$?

   if [ "$errdmp2" -ne 0 ]
   then
      msg1="No metar data for ${PDY}${HOUR}!"
      postmsg  "$jlogfile" "$msg1"
      msg2="The t${HOUR}z WX Depiction Chart for FAX/AFOS not made!"
      postmsg "$jlogfile" "$msg2"
      exit
   fi
fi

#############################################################
#
#    wxpxplot - READ AND PLOT DATA FOR AFOS AND SAVE IT 
#    IN A FILE FOR ANALYSIS STEP.
#
#############################################################
AFOSPLOTOUT=afos.wxpxplot.$RUN.$job

export pgm=wxpxplot
. prep_step

export XLFUNIT_11="$PARMhrly/hourly_wxpxplot.stntbl" #station priority list
export XLFUNIT_12="$PARMhrly/hourly_wxpxdata.cntl"
export XLFUNIT_13="metar.$PDY$HOUR"
export XLFUNIT_14="$PARMhrly/hourly_wxpxplot.$RUN"
export XLFUNIT_15="datetemp"
export XLFUNIT_16="runid"
export XLFUNIT_23="labelpass"
export XLFUNIT_28="label28.1"
export XLFUNIT_48="$FIXgraph/graph_pillist1"
export XLFUNIT_52="$AFOSPLOTOUT"
export XLFUNIT_55="label55.1"
export XLFUNIT_60="label60.1"
export XLFUNIT_61="label61.1"
export XLFUNIT_62="label62.1"
export XLFUNIT_63="label63.1"

startmsg
$EXEChrly/wxpxplot PARM="$PARM1" >> $pgmout 2> errfile    
export err=$?

if test $err -eq 0
then
   if test -s $DATA/fort.97
   then
        msg=`cat $DATA/fort.97`
        postmsg "$jlogfile" "$msg"
        exit
   fi
#
   err_chk
else
   msg1="$pgm FAILED with err=$err in exwxdep.sh!"
   postmsg "$jlogfile" "$msg1"
   msg2="The T${HOUR}Z WX Depiction Chart for FAX/AFOS not made!"
   postmsg "$jlogfile" "$msg2"
   err_chk
   exit
fi
######################################################
#
#  If fronts file was found, run through wxpxfrnt.x to
#  add more nodes to smooth out fronts.  Input is
#  $FRONTSIN; output is $FRONTSNEW.  If fronts file
#  was found but is out of date, or if there's an error 
#  from fronts.x, or if fronts file wasn't found (fasg
#  .eq. false), use fronts control file instead so 
#  wxpxanal job step doesn't fail.
#
######################################################
if [[ $fasg = true ]] then
   cp $FRONTSIN frontsin.t
   FRONTSNEW=frontx.t

   export pgm=wxpxfrnt
   . prep_step

   export XLFUNIT_11="frontsin.t"
   export XLFUNIT_51="$FRONTSNEW"

   startmsg
   $EXEChrly/wxpxfrnt >> $pgmout 2> errfile
   export err=$?

   if test $err -eq 0
   then
      err_chk
      if test $SENDCOM = YES
      then
         rm $pcom/$FRONTFIL
         cp $FRONTSNEW $pcom/$FRONTFIL
      fi
   else
      if test $err -eq 1
      then
         msg1="Fronts file is out of date!"
      elif test $err -eq 2
      then
         msg1="Error reading input fronts file (non-fatal)."
      elif test $err -eq 3
      then
         msg1="Error writing to output fronts file (non-fatal)."
      else
         msg1="Non-fatal error in wxpxfrnt."
      fi
      postmsg "$jlogfile" "$msg1"
      msg2="Using fronts control file instead."
      postmsg "$jlogfile" "$msg2"

      FRONTSNEW=$FIXhrly/hourly_nmapfronts.cntl
      echo  FRONTSNEW=$FRONTSNEW
   fi
else
   FRONTSNEW=$FIXhrly/hourly_nmapfronts.cntl
fi
echo  FRONTSNEW=$FRONTSNEW
######################################################
#
#   wxpxanal - READ ASCII SURFACE FRONTS CREATED VIA
#   NMAP, PERFORM ANALYSIS, CREATE PLOT FOR FAX
#   AND GRIB FILE TO BE ANALYZED AND PLOTTED VIA
#   AFOSMAKR90.
#
######################################################
cp ${FIXgraph}/graph_gphbg/nh1302.pur nh1302.pur

FAXOUT=wxpxmap${krun}"."${cyc}
export pgm=wxpxanal
. prep_step

export XLFUNIT_15="datetemp"
export XLFUNIT_17="$FRONTSNEW"
export XLFUNIT_23="labelpass"
export XLFUNIT_28="label28.1"
export XLFUNIT_30="$PARMhrly/graph_afosmakr.wxpx"
export XLFUNIT_48="$FIXgraph/graph_pillist1"
export XLFUNIT_49="$FIXgraph/graph_awpseed"
export XLFUNIT_52="afoslabel"
export XLFUNIT_54="label55.1"
export XLFUNIT_55="label55.2"
export XLFUNIT_60="label60.1"
export XLFUNIT_61="label61.2"
export XLFUNIT_62="label62.2"
export XLFUNIT_63="label63.2"
export XLFUNIT_75="label75.2"
export XLFUNIT_76="outdgn1.2"    # first output design file
export XLFUNIT_77="outdgn2.2"    # second output design file
export XLFUNIT_80="faxext8.2"
export XLFUNIT_81="$FAXOUT"
export XLFUNIT_83="afgrib"
export XLFUNIT_89="pureras.2"

startmsg
$EXEChrly/wxpxanal PARM="$PARM1" >> $pgmout 2> errfile
export err=$? 

if test $err -eq 0
then
   err_chk
else
   msg1="$pgm FAILED with err=$err in exwxdep.sh!"
   postmsg "$jlogfile" "$msg1"
   msg2="The T${HOUR}Z WX Depiction Chart for FAX/AFOS not made!"
   postmsg "$jlogfile" "$msg2"
   cat break
   ls -l
   cat break
   cat $pgmout
   cat errfile
   exit
fi

    jobn=${job}

    for xcycl in 01 04 07 10 13 16 19 22
    do 

    if [ ${cyc} = ${xcycl} ];
    then
      if [ ${krun} = '3' ];
      then 
       if [ ${job} = 'rw301' ]; then submn=4082; fi
       if [ ${job} = 'rw304' ]; then submn=4083; fi
       if [ ${job} = 'rw307' ]; then submn=4084; fi
       if [ ${job} = 'rw310' ]; then submn=4085; fi
       if [ ${job} = 'rw313' ]; then submn=4086; fi
       if [ ${job} = 'rw316' ]; then submn=4087; fi
       if [ ${job} = 'rw319' ]; then submn=4088; fi
       if [ ${job} = 'rw322' ]; then submn=4089; fi

         KEYW=WXDEP_g

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

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

     if [ ${krun} = '4' ];
     then
      if [ ${job} = 'rw401' ]; then submn=4092; fi
      if [ ${job} = 'rw404' ]; then submn=4093; fi
      if [ ${job} = 'rw407' ]; then submn=4094; fi
      if [ ${job} = 'rw410' ]; then submn=4095; fi
      if [ ${job} = 'rw413' ]; then submn=4096; fi
      if [ ${job} = 'rw416' ]; then submn=4097; fi
      if [ ${job} = 'rw419' ]; then submn=4098; fi
      if [ ${job} = 'rw422' ]; then submn=4099; fi

      for KEYW in WXDEP WXDEP_g
      do
      grep $KEYW $FIXutil/identifyfax.tbl | read Keyword sub00 sub06 sub12 sub18 gif toc prt lprt name
      name=${name}.${krun}

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

# check to see if we do AFOS
if [[ $MAKEAFOSANAL = 'a' ]] then
   ############################################
   #
   #    make grib index file
   #
   ############################################
   /nwprod/util/exec/grbindex afgrib afgribi
   head -1 afgribi

   ############################################
   #
   #    run afosmakr
   #
   ############################################
   export pgm=afosmakr
   . prep_step

   export XLFUNIT_11="afgrib"
   export XLFUNIT_30="$PARMhrly/graph_afosmakr.wxpx"
   export XLFUNIT_31="afgribi"
   export XLFUNIT_47="ncepdate"
   export XLFUNIT_48="$FIXgraph/graph_pillist1"
   export XLFUNIT_49="$FIXgraph/graph_awpseed"
   export XLFUNIT_51="tempafosl"
   export XLFUNIT_52="afosmap"

   startmsg
   $EXECgraph/afosmakr  >> $pgmout 2> errfile
   err=$?; export err

   if test $err -eq 0
   then
      err_chk
   else
      msg1="$pgm FAILED with err=$err in exwxdep.sh!"
      postmsg "$jlogfile" "$msg1"
      msg2="The T${HOUR}Z WX Depiction Chart for AFOS not made!"
      postmsg "$jlogfile" "$msg2"
      exit
   fi

   ###################################################
   #
   # afconcat - combine legend and contour together.
   #
   ###################################################
   parmsg1=pil1=90W,pil2=90W,pil3=90W
   AFOSANALOUT=afos.wxpxmap.$RUN.$job
 
   export pgm=afconcat
   . prep_step

   export XLFUNIT_21="afoslabel"
   export XLFUNIT_22="afosmap"
   export XLFUNIT_73="$AFOSANALOUT"

   startmsg
   $EXECgraph/afconcat parm=$parmsg1  >> $pgmout 2> errfile
   export err=$?

   if test $err -eq 0
   then
      err_chk
   else
      msg1="$pgm FAILED with err=$err in exwxdep.sh"
      postmsg "$jlogfile" "$msg1"
      msg2="The T${HOUR}Z WX Depiction Chart for AFOS not made!"
      postmsg "$jlogfile" "$msg2"
      exit
   fi

   if test $SENDCOM = YES
   then
      cp $AFOSPLOTOUT $pcom
      cp $AFOSANALOUT $pcom

   fi
# end of DOAFOS loop
fi

#   Convert the AFOS products to Redbook graphics and send to AWIPS


if [[ $MAKEAFOSPLOT = 'p' ]] then
   pils=p0w
   $USHutil/redbook_ntc.sh $AFOSPLOTOUT "${pils}"
fi

if [[ $MAKEAFOSANAL = 'a' ]] then
   pils=90w
   $USHutil/redbook_ntc.sh $AFOSANALOUT "${pils}"
fi
#############################################################

msg='Script exwxdep.sh completed Normally.'
echo $msg
postmsg "$jlogfile" "$msg"

# ---------------END OF EXWXDEP.SH SCRIPT ----------------------------

