#####################################################################
echo "------------------------------------------------"
echo "JFNOC -  FNOC postprocessing"
echo "------------------------------------------------"
echo "History: JUNE 1997 - First implementation of this new script  "
echo "                     for processing 1.25 degree GRIB bulletins" 
echo "                     from Navy FNOC.                          "
echo "         AUG 1998 - modified to make GRIB processing Y2K"
echo "                    compliant.  -- W. Bostelman"
echo "         MAR 2000 - Converted to IBM-SP."
#####################################################################

cd $DATA

########################################################################
#
# START FLOW OF CONTROL
#
#  1) Initialize the run control file for the FNOC 1.25 processing.
#  2) Extract FNOC 1.25 degree data from /dcom holding tank.
#  3) Form GRIB Grid 45 (1.25) fields from input 1.25 degree GRIB fields.
#########################################################################


########################################
set -x
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

if test "$cycle" = 't09z'
then
  cycfnoc=t00z
else
  cycfnoc=t12z
fi

set +x
echo " "
echo PDY="$PDY"
echo PDYm1="$PDYm1"
echo " "
set -x

##############################################################
#
#     Process Navy FNOC bulletins
#
##############################################################


set +x
echo " "
echo "#############################################"
echo " Initialize FNOC run control file (bullproi)"
echo "#############################################"
echo " "
set -x

#
#  Get Run Date and GRIB processing file name/path name
#
#
grbfile_id=mtybul
Filedate=$PDY
RCYCL=`echo "$cycfnoc"|cut -c2-3`
GRBFILE=/dcom/us007003/${Filedate}/wgrbbul/${grbfile_id}_${RCYCL}
echo GRBFILE="$GRBFILE"
echo "`cut -c1-6 ncepdate`${Filedate}${RCYCL}`cut -c17-30 ncepdate`">RDATE
#

rm hdrfile
rm hdrinv 

export pgm=bullproi
. prep_step

export XLFUNIT_11="RDATE"
export XLFUNIT_12="$PARMutil/grib_listbul.fnoc"
export XLFUNIT_51="hdrfile"
export XLFUNIT_52="hdrinv"

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

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

   exit
fi

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

cp ${GRBFILE} grbulls

set +x
echo " "
echo "#########################################"
echo " Extract FNOC 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

export XLFUNIT_11="hdrfile"
export XLFUNIT_51="hdrinv"
export XLFUNIT_12="grbulls"
export XLFUNIT_61="fnocgrib"

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

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

   exit
fi
 

set +x
echo " "
echo "####################################################"
echo " Unpack and order FNOC 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="$PARMutil/grib_listbul.fnoc"
#
#  GRIB Bulletin Input Files 
export XLFUNIT_12="fnocgrib"
#
#  Unpacked GRIB Output Files,  Set 1 - Winds Excluded
export XLFUNIT_51="unpfnocg"
#
#  Unpacked GRIB Output Files,  Set 2 - Winds
export XLFUNIT_52="unpfnocwg"
#
#   JCDATA Output file
export XLFUNIT_80="fnocjcdata"

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

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

   exit
fi
 
set +x
echo " "
echo "####################################################"
echo " Read, combine and thicken 1.25 (non-wind) grids    "
echo "####################################################"
echo " "
set -x

#     Execute program mkm125fls 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="fnocjcdata"
#
#  Unpacked GRIB Input Files
export XLFUNIT_12="unpfnocg"
#
#  Output Files of Grid map 45, 1.25 Degree fields
export XLFUNIT_51="fnoc45"

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

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

   exit
fi
 

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

#     Execute program mkm125flw 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="fnocjcdata"
#
#  Unpacked GRIB Input Files
export XLFUNIT_12="unpfnocwg"
#
#  Output Files of Grid map 45, 1.25 Degree fields
export XLFUNIT_51="fnoc45w"

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

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

   exit
fi
 

#  Good run, now concatenate the fnoc45w to fnoc45 files, 
#  copy the files to /com and make and index files

cat fnoc45 fnoc45w > ${COMOUT}/fnoc.${cycfnoc}

$EXECutil/grbindex ${COMOUT}/fnoc.${cycfnoc} ${COMOUT}/fnoci.${cycfnoc}

#####################################################################
# GOOD RUN
set +x
echo "**************JOB exfnoc.sh COMPLETED NORMALLY"
echo "**************JOB exfnoc.sh COMPLETED NORMALLY"
echo "**************JOB exfnoc.sh COMPLETED NORMALLY"
set -x
#####################################################################
