#####################################################################
echo "------------------------------------------------"
echo "JUKMET - UKMET postprocessing"
echo "------------------------------------------------"
echo "History: AUG 1995 - First implementation of this new script  "
echo "                     for processing 1.25 degree grids FTPd   "
echo "                     from the HDS.                           "
echo "          OCT 1996 - Standardized and added SMS hooks.       "
echo "          JAN 1997 - Moved processing from Cray3 to Cray4.   "
echo "                     Now processes 2.5 and 1.25 degree       "
echo "                     bulletins from /dcom instead of HDS     "
echo "                     preprocessed/FTPd files.                "
echo "          APR 1997 - Added AFOS/AWIPS UKMET graphics.        "
echo "          APR 1997 - Added FAX UKMET graphics.               "
echo "          APR 1997 - Change date-line for the 12Z UKMET      "
echo "                     graphics from PDYm1 to PDY.             "
echo "          AUG 1997 - Added copygb to make 00-72hr 2.5 degree "
echo "                     files from 1.25 degree files            "
echo "          MAR 1998 - Add lines to kick off script to track "
echo "                     tropical storms in UKMET GRIB files -- Marchok "
echo "          APR 1998 - modified to use afstoawp.sh to convert"
echo "                     AFOS maps U06 and U17 into awips format-"
echo "                     Peter Henrichsen. "
echo "          AUG 1998 - modified to make GRIB processing Y2K"
echo "                     compliant.  -- W. Bostelman"
echo "          SEP 1998 - modified to run for CYCLES 00Z/12Z separately-" 
echo "                     made appropriate script changes to run f-90 codes-" 
echo "                     faxmakr90, afosmakr90 & ukmetdp3 with Y2K "
echo "                     compliance. -- Bill Facey/Krishna Kumar "
echo "          JAN 1999 - modified to convert UKMET AFOS graphics U01-  " 
echo "                     U18 to Redbook graphics format for AWIPS      " 
echo "          SEP 1999 - Converted to IBM SP                     " 
echo "          FEB 2002 - Modified to create a non-restricted UKMET "
echo "          JUN 2005 - Restored production of pils u05, u06, u07, "
echo "                     u16, u17, u18                              "
echo "          AUG 2005 - Restored UKMET 500MB D+3 HGT/DN and a ECMWF "
echo "                     for HPC.                                    "
echo "          OCT 2005 - Send Redbook graphics to TOC via the NTC    "
echo "          APR 2006 - Move Redbook processing to separate script "
echo "          FEB 2012 - Modify script so that if any missing data  "
echo "                     it still continues processing the data until"
echo "                     the end then the job will be aborted."
#####################################################################

cd $DATA


########################################################################
#
# START FLOW OF CONTROL
#
#  1) Get the Date from /com/date
#     -- Now done in J-job, not here.
#  2) Initialize the run control file for the UKMET 1.25 processing.
#  3) Extract UKMET 1.25 degree data from /dcom holding tank.
#  4) Form GRIB Grid 45 (1.25) fields from input 1.25 degree GRIB fields.
#  5) Convert 00-72hr 1.25 fields to 2.5 degree fields. 
#  6) Initialize the run control file for the UKMET 2.5 processing.
#  7) Extract UKMET 2.5 degree data from /dcom holding tank.
#  8) Form GRIB Grid 2 (2.5 deg) fields from input 2.5 degree GRIB fields.
#  9) Form D+3 z500 average GRIB file and the Departure from climatology.
# 10) FAXMAKR90 - generate UKMET FAX/varian charts.
# 11) AFOSMAKR90 - generate UKMET bulletins up to hour 72.
# 12) AFOSMAKR90 - generate UKMET AFOS maps U06 and U17.
# 13) start script afstoawp.sh to convert UKMET AFOS graphics   
#               into AWIPs redbook format.
# 14) Kick off script to track tropical storms in UKMET GRIB files.
#########################################################################


########################################
set -x
msg="STARTED!"
postmsg "$jlogfile" "$msg"
########################################

##############################################################
#     Process the 1.25 degree UKMET bulletins
##############################################################
set +x
echo " "
echo "#############################################"
echo " Initialize UKMET run control file (bullproi)"
echo "#############################################"
echo " "
set -x

#
#  Get Run Date and GRIB processing file name/path name
#
rm RDATE

RDY=$PDY
RDYm1=$PDYm1

tot_err=0

grbfile_id=ukwafs
if test "$grbfile_id" != 'grdbul'
then
   Filedate=$RDY
   RCYCL=$cyc
else
   Filedate=$RDYm1
   RCYCL=12
fi

GRBFILE=${dcom}/${Filedate}/wgrbbul/${grbfile_id}_${RCYCL}
echo "DATE  ${Filedate}${RCYCL}WASHINGTON">RDATE

rm hdrfile
rm hdrinv 

export pgm=bullproi
. prep_step

export XLFUNIT_11="RDATE"
export XLFUNIT_12="$PARMgrib/grib_listbul.uk125"
export XLFUNIT_51="hdrfile"
export XLFUNIT_52="hdrinv"

startmsg
$EXECgrib/bullproi >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr
 
if test "$bullerr" -ne '0'
then
   set +x
   echo "Something wrong with input cards"
   set -x

   msg="SOMETHING WRONG WITH UKMET 1.25 INPUT CARDS"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi
 

#  Copy GRIB Bulletin File to the Working Directory for pgm BULL2SEQ 

cp ${GRBFILE} grbulls

set +x
echo " "
echo "#########################################"
echo " Extract UKMET data from /dcom (bull2seq)"
echo "#########################################"
echo " "
set -x

#     execute program bull2seq to extract 1.25 GRIB/GRID bulletins 
#     from the incoming bulletin holding files and create
#     a file from which they may be accessed for processing.
#                
export pgm=bull2seq
. prep_step

#   GRIB Input File
export XLFUNIT_11="hdrfile"
export XLFUNIT_12="grbulls"
#   GRIB Output Files
export XLFUNIT_51="hdrinv"
export XLFUNIT_61="ukgribf00"
export XLFUNIT_62="ukgribf06"
export XLFUNIT_63="ukgribf12"
export XLFUNIT_64="ukgribf18"
export XLFUNIT_65="ukgribf24"
export XLFUNIT_66="ukgribf30"
export XLFUNIT_67="ukgribf36"
export XLFUNIT_68="ukgribf42"
export XLFUNIT_69="ukgribf48"
export XLFUNIT_70="ukgribf60"
export XLFUNIT_71="ukgribf72"

startmsg
$EXECgrib/bull2seq >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr
 
if test "$bullerr" -ne '0'
then
   set +x
   echo "No UKMET 1.25 data found in /dcom"
   set -x

   msg="NO UKMET 1.25 DATA FOUND IN /DCOM"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi
 
set +x
echo " "
echo "####################################################"
echo " Unpack and order UKMET 1.25 grids into ordered sets"
echo "####################################################"
echo " "
set -x

#     execute program unpmgrb1 to unpack GRIB data into
#     floating point numbers and arrange output files into
#     grid-ordered sets.  A complete set contains 8 grids
#     of type 37-44.
#                
export pgm=unpmgrb1
. prep_step

#  Input Parameter List
export XLFUNIT_11="$PARMgrib/grib_listbul.uk125"
#
#  GRIB Bulletin Input Files 
export XLFUNIT_12="ukgribf00"
export XLFUNIT_13="ukgribf06"
export XLFUNIT_14="ukgribf12"
export XLFUNIT_15="ukgribf18"
export XLFUNIT_16="ukgribf24"
export XLFUNIT_17="ukgribf30"
export XLFUNIT_18="ukgribf36"
export XLFUNIT_19="ukgribf42"
export XLFUNIT_20="ukgribf48"
export XLFUNIT_21="ukgribf60"
export XLFUNIT_22="ukgribf72"
#
#  Unpacked GRIB Output Files,  Set 1 - Winds Excluded
export XLFUNIT_51="unpukg00"
export XLFUNIT_52="unpukwg00"
export XLFUNIT_53="unpukg06"
export XLFUNIT_54="unpukwg06"
export XLFUNIT_55="unpukg12"
export XLFUNIT_56="unpukwg12"
export XLFUNIT_57="unpukg18"
export XLFUNIT_58="unpukwg18"
export XLFUNIT_59="unpukg24"
export XLFUNIT_60="unpukwg24"
export XLFUNIT_61="unpukg30"
export XLFUNIT_62="unpukwg30"
export XLFUNIT_63="unpukg36"
export XLFUNIT_64="unpukwg36"
export XLFUNIT_65="unpukg42"
export XLFUNIT_66="unpukwg42"
export XLFUNIT_67="unpukg48"
export XLFUNIT_68="unpukwg48"
export XLFUNIT_69="unpukg60"
export XLFUNIT_70="unpukwg60"
export XLFUNIT_71="unpukg72"
export XLFUNIT_72="unpukwg72"
#
#  Unpacked GRIB Output Files,  Set 2 - Winds
export XLFUNIT_80="ukjcdata" # Output file

startmsg
$EXECgrib/unpmgrb1 >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr
 
if test "$bullerr" -ne '0'
then
   set +x
   echo "No bulletins found on input file"
   set -x
   err=$bullerr

   msg="NO UKMET 1.25 DATA FOUND IN INPUT FILE"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi
 
set +x
echo " "
echo "####################################################"
echo " Read, combine and thicken 1.25 (non-wind) grids    "
echo "####################################################"
echo " "
set -x

#     Execute program mk125fls to read sets of 8 thinned
#     grids, combine into a global thinned grid, then
#     thicken into a 288 x 145 1.25 degree global grid.
#     Output is a GRIB 45 grid (1.25 grid).
#                
export pgm=mk125fls
. prep_step

#  JCDATA Input File  
export XLFUNIT_11="ukjcdata"
#
#  Unpacked GRIB Input Files
export XLFUNIT_12="unpukg00"
export XLFUNIT_13="unpukg06"
export XLFUNIT_14="unpukg12"
export XLFUNIT_15="unpukg18"
export XLFUNIT_16="unpukg24"
export XLFUNIT_17="unpukg30"
export XLFUNIT_18="unpukg36"
export XLFUNIT_19="unpukg42"
export XLFUNIT_20="unpukg48"
export XLFUNIT_21="unpukg60"
export XLFUNIT_22="unpukg72"
#
#  Output Files of Grid map 45, 1.25 Degree fields
export XLFUNIT_51="uk45f00"
export XLFUNIT_52="uk45f06"
export XLFUNIT_53="uk45f12"
export XLFUNIT_54="uk45f18"
export XLFUNIT_55="uk45f24"
export XLFUNIT_56="uk45f30"
export XLFUNIT_57="uk45f36"
export XLFUNIT_58="uk45f42"
export XLFUNIT_59="uk45f48"
export XLFUNIT_60="uk45f60"
export XLFUNIT_61="uk45f72"

startmsg
$EXECgrib/mk125fls >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr
 
if test "$bullerr" -ne '0'
then
   set +x
   echo "No bulletins found on input file"
   set -x

   msg="NO UKMET 1.25 DATA FOUND IN INPUT FILE"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi

set +x
echo " "
echo "####################################################"
echo " Read, combine and thicken 1.25 (u/v wind) grids    "
echo "####################################################"
echo " "
set -x

#     Execute program mk125flw to read sets of 8 thinned
#     U/V grids, combine into a global thinned grid, then
#     thicken into a 288 x 145 1.25 degree global grid.
#     Output is a GRIB 45 grid (1.25 grid).
#                
export pgm=mk125flw
. prep_step

#  JCDATA Input File  
export XLFUNIT_11="ukjcdata"
#
#  Unpacked GRIB Input Files
export XLFUNIT_12="unpukwg00"
export XLFUNIT_13="unpukwg06"
export XLFUNIT_14="unpukwg12"
export XLFUNIT_15="unpukwg18"
export XLFUNIT_16="unpukwg24"
export XLFUNIT_17="unpukwg30"
export XLFUNIT_18="unpukwg36"
export XLFUNIT_19="unpukwg42"
export XLFUNIT_20="unpukwg48"
export XLFUNIT_21="unpukwg60"
export XLFUNIT_22="unpukwg72"
#
#  Output Files of Grid map 45, 1.25 Degree fields
export XLFUNIT_51="uk45wf00"
export XLFUNIT_52="uk45wf06"
export XLFUNIT_53="uk45wf12"
export XLFUNIT_54="uk45wf18"
export XLFUNIT_55="uk45wf24"
export XLFUNIT_56="uk45wf30"
export XLFUNIT_57="uk45wf36"
export XLFUNIT_58="uk45wf42"
export XLFUNIT_59="uk45wf48"
export XLFUNIT_60="uk45wf60"
export XLFUNIT_61="uk45wf72"

startmsg
$EXECgrib/mk125flw >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr
 
if test "$bullerr" -ne '0'
then
   set +x
   echo "No bulletins found on input file"
   set -x

   msg="NO UKMET 1.25 WIND DATA FOUND IN INPUT FILE"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi

###############################################################
#  Good run, now concatenate the uk45wf to uk45f files,
#  run COPYGB to convert 00-72hr 1.25 degree to 2.5 degree files
#  make index files and copy the files to /com
###############################################################
ftimes='00 06 12 18 24 30 36 42 48 60 72'

for ft in $ftimes
do
 cat uk45f$ft uk45wf$ft > ukmet.${cycle}.ukmet$ft

 $EXECgribprod/grbindex ukmet.${cycle}.ukmet$ft \
  ukmet.${cycle}.ukmeti$ft

#############################################
# Convert to grib2 format
#############################################

 /nwprod/util/exec/cnvgrib -g12 -p40 ukmet.${cycle}.ukmet$ft ukmet.${cycle}.ukmet${ft}.grib2
 /nwprod/util/exec/wgrib2 -s ukmet.${cycle}.ukmet$ft.grib2 > ukmet.${cycle}.ukmet${ft}.grib2.idx

  if test $SENDCOM = 'YES'
  then
    cp ukmet.${cycle}.ukmet$ft $COMOUT
    cp ukmet.${cycle}.ukmeti$ft $COMOUT

    cp ukmet.${cycle}.ukmet${ft}.grib2 $COMOUT
    cp ukmet.${cycle}.ukmet${ft}.grib2.idx $COMOUT
  fi

  $EXECgribprod/copygb -g2 ukmet.${cycle}.ukmet$ft \
  ukmet.${cycle}.ukmeti$ft ukmet.${cycle}.ukm25f$ft
  $EXECgribprod/grbindex ukmet.${cycle}.ukm25f$ft \
  ukmet.${cycle}.ukm25if$ft

  if test $SENDCOM = 'YES'
  then
    cp ukmet.${cycle}.ukm25f$ft $COMOUT
    cp ukmet.${cycle}.ukm25if$ft $COMOUT
  fi
done

##############################################################
#
#     Process the 2.5 degree UKMET bulletins (96-144hr)
#
##############################################################


# Clean up work files from 1.25 degree processing

rm hdrfile hdrinv bullfile grdbulls
 
set +x
echo " "
echo "#############################################"
echo " Initialize UKMET run control file (bullproi)"
echo "#############################################"
echo " "
set -x
#
#  Get Run Date and GRIB processing file name/path name
#
rm RDATE

RDY=$PDY
RDYm1=$PDYm1

grbfile_id=uk25
if test "$grbfile_id" != 'grdbul'
then
   Filedate=$PDY
   RCYCL=$cyc
else
   Filedate=$RDYm1
   RCYCL=12
fi

GRBFILE=${dcom}/${Filedate}/wgrbbul/${grbfile_id}_${RCYCL}
#GRBFILE=$COMOUT/ukmet.${cycle}.${grbfile_id}
echo "DATE  ${Filedate}${RCYCL}WASHINGTON">RDATE

rm hdrfile*
rm hdrinv* 

export pgm=bullproi
. prep_step

export XLFUNIT_11="RDATE"
export XLFUNIT_12="$PARMgrib/grib_listbul.uk25"
export XLFUNIT_51="hdrfile"
export XLFUNIT_52="hdrinv"

startmsg
msg="pgm=BULLPROI has BEGUN"
postmsg "$jlogfile" "$msg"

$EXECgrib/bullproi >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr

###############################################################
# Initialize None-Restricted UKMET run control file (bullproi)
###############################################################

export XLFUNIT_11="RDATE"
export XLFUNIT_12="$PARMgrib/grib_NRlistbul.uk25"
export XLFUNIT_51="hdrfile2"
export XLFUNIT_52="hdrinv2"

$EXECgrib/bullproi >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr

if test "$bullerr" -ne '0'
then
   set +x
   echo "Something wrong with input cards"
   set -x

   msg="SOMETHING WRONG WITH UKMET 2.5 INPUT CARDS"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi
 
#  Copy GRIB Bulletin File to the Working Directory for pgm BULL2SEQ 

cp ${GRBFILE} grbulls

set +x
echo " "
echo "#########################################"
echo " Extract UKMET data from /dcom (bull2seq)"
echo "#########################################"
echo " "
set -x

#     execute program bull2seq to extract 2.5 GRIB/GRID bulletins 
#     from the incoming bulletin holding files and create
#     a file from which they may be accessed for processing.
#                
export pgm=bull2seq
. prep_step

# Input Files
export XLFUNIT_11="hdrfile"
export XLFUNIT_12="grbulls"
# Output Files
export XLFUNIT_51="hdrinv"
export XLFUNIT_61="ukmgrbfxx"

startmsg
$EXECgrib/bull2seq >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr

###############################################################
# Process None-Restricted UKMET
###############################################################
 
# Input Files
export XLFUNIT_11="hdrfile2"
export XLFUNIT_12="grbulls"
# Output Files
export XLFUNIT_51="hdrinv2"
export XLFUNIT_61="nrukmgrbfxx"

startmsg
$EXECgrib/bull2seq >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr

if test "$bullerr" -ne '0'
then
   set +x
   echo "No UKMET 2.5 data found in /dcom"
   set -x

   msg="NO UKMET 2.5 DATA FOUND IN /DCOM"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi
 
set +x
echo " "
echo "######################################"
echo " Process UKMET data (mkgrb25)"
echo "######################################"
echo " "
set -x

#     Reads sets of 4 grids, combines into one global set.
#     Output is made in GRIB format under grid 2 specs.

export pgm=mkgrb25
. prep_step

export XLFUNIT_11="hdrfile" # JCDATA Input File
export XLFUNIT_12="hdrinv"
export XLFUNIT_13="ukmgrbfxx" # Unpacked GRIB Input
export XLFUNIT_51="ukm25fxx" # Output Grid 2, 2.5 Degree fields

startmsg
$EXECgrib/mkgrb25 >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr

###############################################################
# Process None-Restricted UKMET data (mkgrb25)
###############################################################
 
export XLFUNIT_11="hdrfile2" # JCDATA Input File
export XLFUNIT_12="hdrinv2"
export XLFUNIT_13="nrukmgrbfxx" # Unpacked GRIB Input
export XLFUNIT_51="nrukmet.${cycle}.ukm25" # Output Grid 2, 2.5 Degree fields

startmsg
$EXECgrib/mkgrb25 >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr

if test "$bullerr" -ne '0'
then
   set +x
   echo "mkgrb25 failed making UKMET 2.5 degree grids"
   set -x

   msg="MKGRB25 FAILED MAKING UKMET 2.5 DEGREE GRIDS"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi
 
#    Append the fxx file to the f72 hr file and reindex
#    the ukm25f72 file

cat ukm25fxx >> ukmet.${cycle}.ukm25f72

$EXECgribprod/grbindex ukmet.${cycle}.ukm25f72 \
 ukmet.${cycle}.ukm25if72

$EXECgribprod/grbindex nrukmet.${cycle}.ukm25 \
 nrukmet.${cycle}.ukm25i

set +x
echo " "
echo "######################################"
echo " Enhance it with ukauxfld"
echo "######################################"
echo " "
set -x

#     Read in basic ecmwf grib fields and compute 500 mb 
#     geostrophic vorticity and 1000 mb height

export pgm=ukauxfld
. prep_step

#
#  Output Files of Grid map 2, 2.5 Degree fields
export XLFUNIT_11="ukmet.${cycle}.ukm25f72"
export XLFUNIT_31="ukmet.${cycle}.ukm25if72"
export XLFUNIT_51="ukauxfld"

startmsg
$EXECgrib/ukauxfld >> $pgmout 2> errfile
export bullerr=$?;export err=$bullerr
 
if test "$bullerr" -ne '0'
then
   set +x
   echo "ukauxfld failed making 2.5 degree auxiliary grids"
   set -x

   msg="UKAUXFLD FAILED MAKING UKMET 2.5 DEGREE AUXILIARY GRIDS"
   postmsg "$jlogfile" "$msg"

   tot_err=`expr $tot_err + $err`
fi
 
cat ukauxfld >> ukmet.${cycle}.ukm25f72
$EXECgribprod/grbindex ukmet.${cycle}.ukm25f72 \
 ukmet.${cycle}.ukm25if72

##############################
# Convert to grib2 format
##############################
/nwprod/util/exec/cnvgrib  -g12 -p40 nrukmet.${cycle}.ukm25 nrukmet.${cycle}.ukm25.grib2
/nwprod/util/exec/wgrib2 nrukmet.${cycle}.ukm25.grib2 -s >nrukmet.${cycle}.ukm25.grib2.idx

if test $SENDCOM = 'YES'
then
  cp ukmet.${cycle}.ukm25f72  $COMOUT
  cp ukmet.${cycle}.ukm25if72 $COMOUT
  cp nrukmet.${cycle}.ukm25   $COMOUT
  cp nrukmet.${cycle}.ukm25i  $COMOUT

  cp nrukmet.${cycle}.ukm25.grib2 $COMOUT
  cp nrukmet.${cycle}.ukm25.grib2.idx $COMOUT
fi

set +x
echo " "
echo "####################################################"
echo " Process D+3 average field and departure from clim  "
echo "####################################################"
echo " "
set -x

export pgm=ukmetdp3
. prep_step

file1=ukmet.$cycle.ukm25f

for fhr in 48 72
do
  cat $file1$fhr >> ukmet.dp3.$cycle
done

$EXECgribprod/grbindex ukmet.dp3.$cycle ukmet.dp3.$cycle.index

export XLFUNIT_30="ukmet.dp3.$cycle"
export XLFUNIT_31="ukmet.dp3.$cycle.index"
export XLFUNIT_49="RDATE"
export XLFUNIT_51="D+3.ukmet.$cycle.grib"

startmsg
$EXECgrib/ukmetdp3 
export err=$?;err_chk

set +x
echo " "
echo "####################################################"
echo " Run ANOMGB to make departure from normal file      "
echo "####################################################"
echo " "
set -x

gc=$FIXgrib/grib_zclmgrb
gi=$FIXgrib/grib_zclmgrb.index
t=D+3

$EXECgribprod/anomgb -X -C $gc -c $gi -x $t.ukmet.$cycle.grib $t.z500DN.grib &&\
cat $t.z500DN.grib >>$t.ukmet.$cycle.grib

err=$?;export err;err_chk

$EXECgribprod/grbindex D+3.ukmet.$cycle.grib D+3.ukmet.$cycle.gribi

if test $SENDCOM = 'YES'
then
  cp D+3.ukmet.$cycle.grib $COMOUT
  cp D+3.ukmet.$cycle.gribi $COMOUT
fi

##################################################################
export model=ukmet



set +x
echo " "
echo "#############################################################"
echo " Generate FAX/varian UKMET charts."
echo "#############################################################"
echo " "
set -x

bkgnd=nh4004.pur
cp $FIXgrib/graph_gphbg/$bkgnd .

export pgm=faxmakr
. prep_step

export XLFUNIT_11="ukmet.$cycle.ukm25f72"
export XLFUNIT_12="D+3.ukmet.$cycle.grib"
export XLFUNIT_15="$PARMgraph/graph_faxmakr.${job}"
export XLFUNIT_31="ukmet.$cycle.ukm25if72"
export XLFUNIT_32="D+3.ukmet.$cycle.gribi"
export XLFUNIT_41="$bkgnd"
export XLFUNIT_48="$FIXgraph/graph_awpseed"
export XLFUNIT_49="RDATE"
export XLFUNIT_55="label55"
export XLFUNIT_60="label60"
export XLFUNIT_61="label61"
export XLFUNIT_62="label62"
export XLFUNIT_63="label63"
export XLFUNIT_71="fax.ukmetmap.D+3"
export XLFUNIT_75="LOGMSG"
export XLFUNIT_76="outdgn1"
export XLFUNIT_77="outdgn2"
export XLFUNIT_78="outdgn3"
export XLFUNIT_79="outdgn4"
export XLFUNIT_80="faxext8"
export XLFUNIT_89="pureras"

startmsg
$EXECgrib/faxmakr  >> $pgmout 2> errfile
export err=$?

if test "$SENDCOM" = 'YES'
then
  cp fax.ukmetmap.D+3 $pcom/fax.ukmetmap.D+3.$job

  if test "$SENDDBN" = 'YES'
  then
    SNDKEY=ukmetmap.$cycle
    # JY - turn off printing UKMET chart for HPC 
    # $utilities/snd2forgntbl.sh $SNDKEY fax.ukmetmap.D+3.$job $pcom
  fi
fi

set +x
echo " "
echo "#############################################################"
echo " Send the 2.5 degree UKMET files to OSO (for the server).    "
echo "#############################################################"
echo " "
set -x

if test "$SENDDBN" = 'YES'
then
  $DBNROOT/bin/dbn_alert MODEL NRUKMET25 $job $COMOUT/nrukmet.${cycle}.ukm25

  if [ $SENDDBN_GB2 = YES ]
  then

  $DBNROOT/bin/dbn_alert MODEL NRUKMET25_GB2 $job $COMOUT/nrukmet.${cycle}.ukm25.grib2
  $DBNROOT/bin/dbn_alert MODEL NRUKMET25_GB2_WIDX $job $COMOUT/nrukmet.${cycle}.ukm25.grib2.idx

  fi

  for ft in $ftimes
  do
    $DBNROOT/bin/dbn_alert MODEL UKMET25 $job $COMOUT/ukmet.${cycle}.ukm25f$ft
    $DBNROOT/bin/dbn_alert MODEL UKMET125 $job $COMOUT/ukmet.${cycle}.ukmet$ft

    if [ $SENDDBN_GB2 = YES ]
    then

    $DBNROOT/bin/dbn_alert MODEL UKMET125_GB2 $job $COMOUT/ukmet.${cycle}.ukmet${ft}.grib2
    $DBNROOT/bin/dbn_alert MODEL UKMET125_GB2_WIDX $job $COMOUT/ukmet.${cycle}.ukmet${ft}.grib2.idx

    fi
  done
fi

set +x
echo " "
echo "#############################################################"
echo " Run job to track tropical storms in UKMET GRIB files"
echo "#############################################################"
echo " "
set -x

export cmodel=ukmet
export trkrscript=/nwprod/util/ush

sh $trkrscript/extrkr.sh

if test "$tot_err" -ne '0'
then
   set +x
   echo "There are incomplete data in input ukmet file"
   echo "This UKMET model output comes from the TOC on the TNC feed (line 292)."
   echo "The WMO headers fit the pattern H.[I-P]...  EGRR"
   set -x

   msg="INCOMPLETE UKMET DATA - This UKMET model output comes from the TOC on the TNC feed (line 292).  The WMO headers fit the pattern H.[I-P]...  EGRR"

   postmsg "$jlogfile" "$msg"
   export err=$tot_err;err_chk
fi
 
ls -l
#####################################################################
# GOOD RUN
set +x
echo "**************JOB UKMET COMPLETED NORMALLY ON THE IBM SP"
echo "**************JOB UKMET COMPLETED NORMALLY ON THE IBM SP"
echo "**************JOB UKMET COMPLETED NORMALLY ON THE IBM SP"
set -x
#####################################################################

############## END OF SCRIPT #######################
