# NAME     : exgeofielv.sh
# Author   : Chris Caruso/Peter Henrichsen/Paula Stone
# Purpose  : This script generates a grib file containing 
#            sfc geostrophic u and v wind components and
#            sfc geostrophic vorticity.  It runs afwndplv
#            (to produce AFOS chart 9AM (sfc geo wind)), afosmakr
#            (to produce AFOS chart 9AV (sfc geo rel vort)), and
#            faxmakr (to produce a composite fax chart depicting
#            sfc geo winds and rel vort).  All 3 use the grib
#            file generated in the first step as input.
# History  : 97-03-28 Paula Stone/Mary Jacobs
#            97-09-30 Chris Caruso - modified from afosfchart1.sh,
#                                    to plot sfc geostrophic winds
#                                    running in RW0.
#            97-12-17 Chris Caruso - modified production version to
#                                    run faxmakr and produce composite
#                                    fax chart.
#            98-01-27 Chris Caruso - modified production version to
#                                    have input units in GEOFIELV
#                                    step conform to standard
#                                    numbering convention.
#            98-02-26 Chris Caruso - adding check for ruc grib file
#                                    and assign statement for ruc
#                                    grib and index files.  Ruc data
#                                    now being used as additional
#                                    input to geofielv.  Adding
#                                    nesdis land/sea tags file as
#                                    additional input file to
#                                    geofielv.
#            98-04-08 Chris Caruso - adding code to ftp ruc2 files
#                                    from Cray3 if this job runs on
#                                    either Cray4 or Cray5
#            05-08-31 Larry Sager  - Replace afstoawp.sh with redbook_ntc.sh
#                                    to send graphics to TOC on NTC 
#
# Location : This script is found on cray4 and cray5 as:
#          : /nwprod/util/scripts/exgeofielv
#
# Arguments: cychr (argument 1)    - cycle hour           
#
#___________________________________________________________________________
#
set -x

cychr=$1
hour=$cychr

COMNAM=/com/nam/prod/nam.$PDY
COMNAMm1=/com/nam/prod/nam.$PDYm1
COMRUC=/com/ruc/prod

cd $DATA

#########################################
# DUMP THE DATA IF NOT DUMPED
#########################################
export FORM=${PDY}${cychr}
DUMP=/nwprod/ush/dumpjb

ix=0

for TYPE in metar ships dbuoy mbuoy lcman
do
   if test -s  $TYPE.${PDY}${cychr}
   then
      ix=`expr $ix + 1`
   else
      $DUMP ${PDY}${cychr} .5 $TYPE
      err=$?
      if [ "$err" -eq 0 ]
      then
         ix=`expr $ix + 1`
      else
         msg="No $TYPE data for ${PDY}${cychr}!"
         postmsg  "$jlogfile" "$msg"
      fi
   fi
done

if [ "$ix" -eq '0' ] 
then
   msg="ERROR NO DATA DUMPED - ABORTING script exgeofielv"
   postmsg "$jlogfile" "$msg"
   export pgm="dumping"
   err_exit
fi

#########################################
# Copy the nam grib file and index file.
# If index file isn't there, make it and 
# then do the copy.
#########################################
err6=0
err7=0
err8=0

if test -f $COMNAM/nam.t00z.grbgrd00.tm00
then
   cp $COMNAM/nam.t00z.grbgrd00.tm00 nam.terrain
   err6=$?
   if test -f $COMNAM/nam.t00z.grbgrdi00
   then
      cp $COMNAM/nam.t00z.grbgrdi00 nam.terrain.index
      err7=$?
   else
      $EXECutil/grbindex nam.terrain nam.terrain.index
      err8=$?
   fi
elif test -f $COMNAMm1/nam.t18z.grbgrd00.tm00
then
   cp $COMNAMm1/nam.t18z.grbgrd00.tm00 nam.terrain
   err6=$?
   if test -f $COMNAMm1/nam.t18z.grbgrdi00
   then
      cp $COMNAMm1/nam.t18z.grbgrdi00 nam.terrain.index
      err7=$?
   else
      $EXECutil/grbindex nam.terrain nam.terrain.index
      err8=$?
   fi
fi

tot1=`expr $err6 + $err7 + $err8`

if test "$tot1" -ne 0
then
   msg="UNABLE TO COPY COM ETA GRBGRD FILE"
   postmsg "$jlogfile" "$msg"
   exit 99
fi

#########################################
# ftp and/or copy the ruc grib files, make an index file and then 
# copy the index file.  check for current cycle hour, then minus
# one hour, then minus 2 hours before quitting.
#########################################
hom1=`expr $hour - 1`
typeset -Z2 hom1
typeset -Z2 hom2
dam1=$PDY
if test $hom1 -lt 0
then
   hom1=23
   dam1=$PDYm1
fi
hom2=`expr $hour - 2`
if test $hom1 -lt 0
then
   hom2=22
   dam1=$PDYm1
fi

err9=0; err10=0; err11=0 
if test -f $COMRUC/ruc2a.$PDY/ruc2.t${hour}z.sgrbanl 
then
  cp $COMRUC/ruc2a.$PDY/ruc2.t${hour}z.sgrbanl rucgrb
  err9=$?
  $EXECutil/grbindex rucgrb rucigrb 
  err10=$?
  echo Using $COMRUC/ruc2a.$PDY/ruc2.t${hour}z.sgrbanl
elif test -f $COMRUC/ruc2a.$dam1/ruc2.t${hom1}z.sgrbanl
then
  cp $COMRUC/ruc2a.$dam1/ruc2.t${hom1}z.sgrbanl rucgrb
  err9=$?
  $EXECutil/grbindex rucgrb rucigrb 
  err10=$?
  echo Using $COMRUC/ruc2a.$dam1/ruc2.t${hom1}z.sgrbanl
elif test -f $COMRUC/ruc2a.$dam1/ruc2.t${hom2}z.sgrbanl
then
  cp $COMRUC/ruc2a.$dam1/ruc2.t${hom2}z.sgrbanl rucgrb
  err9=$?
  $EXECutil/grbindex rucgrb rucigrb 
  err10=$?
  echo Using $COMRUC/ruc2a.$dam1/ruc2.t${hom2}z.sgrbanl
else
  err11=1
fi
tot2=`expr $err9 + $err10`

if [ "$tot2" -ne '0' -o "$err11" -ne '0' ]
then
  msg="UNABLE TO COPY COM RUC GRIB FILE from /com/ruc/prod/ruc2a...NON-FATAL ERROR"
  postmsg "$jlogfile" "$msg"
fi
echo $cychr > dumptim

export pgm=geofielv
. prep_step

export XLFUNIT_11="nam.terrain"
export XLFUNIT_12="$PARMutil/hourly_geofielv.rw2"
export XLFUNIT_13="$PARMutil/hourly_geofielv.bitmap.rw2"
export XLFUNIT_14="dumptim"
export XLFUNIT_15="metar.${PDY}${cychr}"
export XLFUNIT_16="ships.${PDY}${cychr}"
export XLFUNIT_17="dbuoy.${PDY}${cychr}"
export XLFUNIT_18="mbuoy.${PDY}${cychr}"
export XLFUNIT_19="lcman.${PDY}${cychr}"
export XLFUNIT_21="rucgrb"
export XLFUNIT_25="$PARMutil/nesdis.lstags"
export XLFUNIT_31="nam.terrain.index"
export XLFUNIT_41="rucigrb"
export XLFUNIT_51="geofielv.$cycle.grib"
export XLFUNIT_75="LOGMSG"

startmsg
$EXECutil/geofielv PARM='X' >> $pgmout 2> errfile
export err=$?;err_chk

$EXECutil/grbindex geofielv.$cycle.grib geofielv.$cycle.gribindex

if test "$SENDCOM" = 'YES'
then
   cp geofielv.$cycle.grib $COMOUT
   cp geofielv.$cycle.gribindex $COMOUT
fi

afosfile=AFOS_wnd.9AM.$job
export pgm=graph_afwndplv
. prep_step

export XLFUNIT_11="$PARMutil/hourly_afwnd_geo.landsea.rw2"
export XLFUNIT_12="geofielv.$cycle.grib"
export XLFUNIT_15="$PARMutil/hourly_afwndplv.geovort.rw2"
export XLFUNIT_20="$FIXgraph/graph_pillist1"
export XLFUNIT_32="geofielv.$cycle.gribindex"
export XLFUNIT_71="$afosfile"
export XLFUNIT_75="LOGMSG"

startmsg
$EXECgraph/graph_afwndplv PARM='XXXXXX' >> $pgmout 2> errfile
export err=$?;err_chk

#######################################################
#  Convert the AFOS 9AM to Redbook graphics and send to
#  AWIPS
#######################################################
pils='9am'
$USHutil/redbook_ntc.sh $afosfile "${pils}"

#######################################################
# Make AFOS Chart
#######################################################
AFOSOUT=geo9av.afos.$job
AFOSPARM=graph_afosmakr.geovort.rw2
sh $USHutil/mkafos.sh "geofielv.$cycle.grib" $AFOSPARM $AFOSOUT

#######################################################
#  Convert AFOS pil 9av to Redbook graphics and 
#  send to AWIPS
#######################################################
pils='9av'
$USHutil/redbook_ntc.sh $AFOSOUT "${pils}"


#######################################################
set +x
echo " "
echo "Leaving shell script exgeofielv.sh"
echo " "
set -x
#######################################################
