#!/bin/sh

cd $DATA

########################################################################
#
# Processing to pull out available UKMET WAFS data into a global grid for
# for use by NAWIPS early look UKMET jobs
#
#########################################################################

set -x

##############################################################
#     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

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"

################################################################
   $SMSBIN/smsmsg "*** JOB ${job} BULLPROI FAILED RC=$err ***"
################################################################
   exit 1
fi
 
err_chk


#  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"

   exit 1
fi
 
err_chk


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 "Missing or no bulletins in input file"
   set -x
   err=$bullerr

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

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 "MISSING or No bulletins found on input file"
   set -x

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

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 "MISSING or No bulletins found on input file"
   set -x

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

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/copygb -x -g2 ukmet.${cycle}.ukmet$ft \
  ukmet.${cycle}.ukm25f$ft
 
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}
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

#  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

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

#    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

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
 
cat ukauxfld >> ukmet.${cycle}.ukm25f72

ls -l uk*

exit

