#####################################################################
echo "------------------------------------------------"
echo "JECMF - ECMWF postprocessing"
echo "------------------------------------------------"
echo "History: JUL 1997 - ECMWF processing removed from job 936   "
echo "                    and placed in this new job which runs   "
echo "                    when the data is available ~ 23:30Z.    "
echo "         MAR 1998 - Add lines to kick off script to track "
echo "                    tropical storms in ECMWF GRIB files -- Marchok "
echo "         APR 1998 - modified to use afstoawp.sh to convert"
echo "                    AFOS maps E06 and E17 into awips format-"
echo "                    Peter Henrichsen. "
echo "         AUG 1998 - modified to make GRIB processing Y2K"
echo "                    compliant.  -- W. Bostelman"
echo "         OCT 1998 - modified to run FAXMAKR90 & AFOSMAKR90 making"
echo "                    scripts Y2K compliant. -- Krishna Kumar"
echo "         DEC 1999 - modified to run on IBMSP"
echo "         FEB 2002 - modified to create a non-restricted ECMWF "
echo "                    grids to post on the ftp server           "
echo "         APR 2004 - modified to run on 0000 UTC ECMWF data"
echo "         APR 2005 - removed all code creating AFOS/AWIPS graphics "
#####################################################################

cd $DATA

##########################################
#
# START FLOW OF CONTROL
#
# 1) Get the Date from /com/date
# 2) Initialize the run control file for the ECMWF processing.
# 3) Extract ECMWF data from /dcom holding tank.
# 4) Form GRIB Grid 2 fields from input 2.5degree GRIB fields.
# 5) Make auxiliary fields for 1000mb Height and 500mb Vorticity.
# 6) Make D+3 z500 field and the anomaly from climatology
#    Make D+8 z500 field and the anomaly from climatology
#### if CYCLE = 12Z
# 7) FAXMAKR90 - generates ECMWF charts - sent to WWB only
#### endif CYCLE = 12Z
# 8) Kick off script to track tropical storms in ECMWF GRIB files.
#
#########################################

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

set +x
echo " "
echo "##########################################"
echo "  GET DATE FROM THE IBM"
echo "##########################################"
echo " "
set -x

#  Get Run Date and GRIB processing file name/path name
#
grbfile_id=grdbul

GRBFILE=${dcom}/${PDATE}/wgrbbul/${grbfile_id}_${cyc}
echo GRBFILE="$GRBFILE"
echo "`cut -c1-6 ncepdate`${PDATE}${cyc}`cut -c17-30 ncepdate`">RDATE

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

export pgm=bullproi
. prep_step

export XLFUNIT_11="RDATE"
export XLFUNIT_12="$PARMgrib/grib_listbul.ecmf"
export XLFUNIT_51="hdrlist"
export XLFUNIT_52="hdrinv"

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

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

export XLFUNIT_11="RDATE"
export XLFUNIT_12="$PARMgrib/grib_NRlistbul.ecmf"
export XLFUNIT_51="hdrlist2"
export XLFUNIT_52="hdrinv2"

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

if test "$bullerr" -ne '0'
then
   set +x
   echo "No ECMWF data found on input file"
   set -x
   err=$bullerr
   export err;

   msg="NO ECMWF DATA TO DECODE AT THIS TIME"
   postmsg "$jlogfile" "$msg"

##################################################################
   $SMSBIN/smsmsg "*** JOB ${jobid} NO DATA TO DECODE RC=$err ***"
##############################################################
   err_chk
   exit
fi
 
err_chk

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

cp $GRBFILE grbulls

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

#     execute program bull2seq to extract GRIB/GRID Bulletins from
#     the incoming bulletin holding files and create
#     a file from which they may be accessed for processing.
#                
#            file ecmgrib1 contains all bulletins except 700 mb
#            file ecmgrib2 contains only 700 mb bulletins
#
export pgm=bull2seq
. prep_step

#   GRIB Input File
export XLFUNIT_11="hdrlist"
export XLFUNIT_12="grbulls"
#   GRIB Output Files
export XLFUNIT_51="hdrinv"
export XLFUNIT_61="ecmgrib1"
export XLFUNIT_62="ecmgrib2"

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

###############################################################
# Process None-Restricted ECMWF
###############################################################

#   GRIB Input File
export XLFUNIT_11="hdrlist2"
export XLFUNIT_12="grbulls"
#   GRIB Output Files
export XLFUNIT_51="hdrinv2"
export XLFUNIT_61="nrecmgrib1"
export XLFUNIT_62="nrecmgrib2"

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

if test "$bullerr" -ne '0'
then
   set +x
   echo "No ECMWF data found in /dcom"
   set -x
   err=$bullerr
   export err;

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

##################################################################
   $SMSBIN/smsmsg "*** JOB ${jobid} NO DATA TO DECODE RC=$err ***"
##############################################################

   err_chk
   exit
fi
 
err_chk

set +x
echo " "
echo "######################################"
echo " Process ECMWF data (mkgrb25)"
echo "######################################"
echo " "
set -x

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

export pgm=mkgrb25
. prep_step

export XLFUNIT_11="hdrlist" # JCDATA Input File
export XLFUNIT_12="hdrinv"
#  JCDATA Input File  
#
#  Unpacked GRIB Input Files
export XLFUNIT_13="ecmgrib1"
export XLFUNIT_14="ecmgrib2"
#  Output Files of Grid map 2, 2.5 Degree fields
export XLFUNIT_51="ecmgrb25"
export XLFUNIT_52="ecm7grb25"

startmsg
$EXECgrib/mkgrb25 >> $pgmout 2> errfile
bullerr=$?;export bullerr;err=$bullerr;export err
 
###############################################################
# Process None-Restricted ECMWF data (mkgrb25)
###############################################################

export XLFUNIT_11="hdrlist2" # JCDATA Input File
export XLFUNIT_12="hdrinv2"
#  JCDATA Input File
#
#  Unpacked GRIB Input Files
export XLFUNIT_13="nrecmgrib1"
export XLFUNIT_14="nrecmgrib2"
#  Output Files of Grid map 2, 2.5 Degree fields
export XLFUNIT_51="nrecmgrb25"
export XLFUNIT_52="nrecm7grb25"

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

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

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

##################################################################
   $SMSBIN/smsmsg "*** JOB ${jobid} MKGRB25 FAILED RC=$err ***"
##############################################################

   err_chk
   exit
fi
 
err_chk

$EXECgrib/grbindex ecmgrb25 ecmgrbi25
$EXECgrib/grbindex nrecmgrb25 nrecmgrbi25

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

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

export pgm=ecauxflv
. prep_step

#  Output Files of Grid map 2, 2.5 Degree fields
export XLFUNIT_11="ecmgrb25"
export XLFUNIT_31="ecmgrbi25"
export XLFUNIT_51="ecauxflv"

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

   msg="ECAUXFLV FAILED MAKING ECMWF 2.5 DEGREE AUXILIARY GRIDS"
   postmsg "$jlogfile" "$msg"

##################################################################
   $SMSBIN/smsmsg "*** JOB ${jobid} ECAUXFLV FAILED RC=$err ***"
##############################################################

   err_chk
   exit
fi
 
err_chk

set +x
echo " "
echo "######################################"
echo " Make D+3 z500 field and anomaly from climatology "
echo " Make D+8 z500 field and anomaly from climatology "
echo "######################################"
echo " "
set -x

#     Read in basic ecmwf grib fields and compute D+3
#     5-day average

export pgm=ecmwfdp3
. prep_step

#  Output Files of Grid map 2, 2.5 Degree fields
export XLFUNIT_30="ecmgrb25"
export XLFUNIT_35="ecmgrbi25"
export XLFUNIT_51="D+3.ecmwf.z500.grib"

startmsg
$EXECgrib/ecmwfdp3 ${PDATE}${cyc} >> $pgmout 2> errfile
bullerr=$?;export bullerr;err=$bullerr;export err

if test "$bullerr" -ne '0'
then
   set +x
   echo "ecmwfdp3 failed making D+3 averages "
   set -x
   err=$bullerr
   export err;

   msg="ECMWFDP3 FAILED MAKING ECMWF D+3 AVERAGE FIELD"
   postmsg "$jlogfile" "$msg"

##################################################################
   $SMSBIN/smsmsg "*** JOB ${jobid} ECMWFDP3 FAILED RC=$err ***"
##############################################################

   err_chk
   exit
fi
 
err_chk

#################################################################
#  
# Make climate anomalies
#################################################################
gc=$FIXgrib/grib_zclmgrb
gi=$FIXgrib/grib_zclmgrb.index
t=D+3
$EXECgrib/anomgb -X -C $gc -c $gi -x $t.ecmwf.z500.grib $t.z500DN.grib &&\
cat $t.z500DN.grib >>$t.ecmwf.z500.grib

#################################################################
#  cat the ecauxflv and ecm7grb25 to the ecmgrb25 
##################################################################

cat ecauxflv >> ecmgrb25
cat ecm7grb25 >> ecmgrb25

#################################################################
# Make D+8 fields
#################################################################
gc=$FIXgrib/grib_zclmgrb
gi=$FIXgrib/grib_zclmgrb.index
t=D+8
k="4*-1,7,100,500,5*-1,1,168,240,3"
$EXECgrib/copygb -k$k ecmgrb25 ecmgrbi25 $t.ecmwf.z500.grib
$EXECgrib/anomgb -X -C $gc -c $gi -x $t.ecmwf.z500.grib $t.z500DN.grib &&\
cat $t.z500DN.grib >>$t.ecmwf.z500.grib

#  Good run, now cat the files together and make and index file

#############################
# Convert to grib2 format
#############################
/nwprod/util/exec/cnvgrib -g12 -p40 nrecmgrb25 nrecmgrb25.grib2
/nwprod/util/exec/wgrib2 nrecmgrb25.grib2 -s >nrecmgrb25.grib2.idx

cp nrecmgrb25.grib2 ${COMOUT}/nrecmgrb25.${cycle}.grib2
cp nrecmgrb25.grib2.idx ${COMOUT}/nrecmgrb25.${cycle}.grib2.idx

/nwprod/util/exec/cnvgrib -g12 -p40 ecmgrb25 ecmgrb25.grib2
/nwprod/util/exec/wgrib2 ecmgrb25.grib2 -s >ecmgrb25.grib2.idx

cp ecmgrb25.grib2  ${COMOUT}/ecmgrb25.${cycle}.grib2
cp ecmgrb25.grib2.idx ${COMOUT}/ecmgrb25.${cycle}.grib2.idx

cp  ecmgrb25  ${COMOUT}/ecmgrb25.${cycle}
cp  nrecmgrb25  ${COMOUT}/nrecmgrb25.${cycle}
cp  D+3.ecmwf.z500.grib ${COMOUT}/D+3.ecmwf.${cycle}.z500.grib
cp  D+8.ecmwf.z500.grib ${COMOUT}/D+8.ecmwf.${cycle}.z500.grib
if [ $cyc -eq 12 ]
then
   cp  D+3.ecmwf.z500.grib ${COMOUT}/D+3.ecmwf.z500.grib
   cp  D+8.ecmwf.z500.grib ${COMOUT}/D+8.ecmwf.z500.grib
fi

$EXECgrib/grbindex ${COMOUT}/ecmgrb25.${cycle} \
 ${COMOUT}/ecmgrbi25.${cycle}
export err=$?;err_chk

$EXECgrib/grbindex ${COMOUT}/nrecmgrb25.${cycle} \
 ${COMOUT}/nrecmgrbi25.${cycle}
export err=$?;err_chk
 
#########################
# Comment out the err_chk(s) below
# if failing then J947 will fail later.
#########################
$EXECgrib/grbindex ${COMOUT}/D+3.ecmwf.${cycle}.z500.grib \
 ${COMOUT}/D+3.ecmwf.${cycle}.z500.gribi
export err=$?;err_chk

$EXECgrib/grbindex ${COMOUT}/D+8.ecmwf.${cycle}.z500.grib \
 ${COMOUT}/D+8.ecmwf.${cycle}.z500.gribi
  export err=$?;err_chk

if test "$SENDDBN" = 'YES'
then
   $DBNROOT/bin/dbn_alert MODEL ECMWF_GRB25 $job ${COMOUT}/ecmgrb25.${cycle}
   $DBNROOT/bin/dbn_alert MODEL ECMWF_GRBI25 $job ${COMOUT}/ecmgrbi25.${cycle}

   $DBNROOT/bin/dbn_alert MODEL NRECMWF_GRB25 $job ${COMOUT}/nrecmgrb25.${cycle}
   $DBNROOT/bin/dbn_alert MODEL NRECMWF_GRBI25 $job ${COMOUT}/nrecmgrbi25.${cycle}

   if [ $SENDDBN_GB2 = YES ]
   then

   $DBNROOT/bin/dbn_alert MODEL ECMWF_GRB25_GB2 $job ${COMOUT}/ecmgrb25.${cycle}.grib2
   $DBNROOT/bin/dbn_alert MODEL ECMWF_GRB25_GB2_WIDX $job ${COMOUT}/ecmgrb25.${cycle}.grib2.idx

   $DBNROOT/bin/dbn_alert MODEL NRECMWF_GRB25_GB2 $job ${COMOUT}/nrecmgrb25.${cycle}.grib2
   $DBNROOT/bin/dbn_alert MODEL NRECMWF_GRB25_GB2_WIDX $job ${COMOUT}/nrecmgrb25.${cycle}.grib2.idx

   fi
fi

#
#   Only continue to produce graphics if it is the 12Z cycle
#
if [ $cyc -eq 12 ]
then

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

 cp  ${COMOUT}/ecmgrb25.${cycle} ecmgrb25
 cp  ${COMOUT}/ecmgrbi25.${cycle} ecmgrbi25

 cp $FIXutil/graph_gphbg/nh4004.pur nh4004.pur

 export pgm=faxmakrx;. prep_step
  
 export XLFUNIT_15="$PARMutil/graph_ecmwfax.jecmwf"
 export XLFUNIT_48="$FIXutil/graph_awpseed"
 export XLFUNIT_49="ncepdate"
 export XLFUNIT_11="ecmgrb25"
 export XLFUNIT_31="ecmgrbi25"
 export XLFUNIT_55="label55"
 export XLFUNIT_60="label60"
 export XLFUNIT_61="label61"
 export XLFUNIT_62="label62"
 export XLFUNIT_63="label63"
 export XLFUNIT_71="fax.ecmwfmp1"
 export XLFUNIT_76="outdgn1"
 export XLFUNIT_77="outdgn2"
 export XLFUNIT_78="outdgn3"
 export XLFUNIT_80="faxext8"
 export XLFUNIT_89="pureras"
 # faxet8 is a  temp output file
 # outdgn1 &2 are output design files.
  
 msg="$pgm start"
 postmsg "$jlogfile" "$msg"
  
 $EXECutil/faxmakrx >> $pgmout 2>errfile
 err=$?
 if [ $err -eq 1 ] ; then
   postmsg "$jlogfile" "One or more ECMWF grids missing or incomplete"
   err=0
 fi
 export err; err_chk

 if test "$SENDCOM" = 'YES'
 then
    cp fax.ecmwfmp1 $pcom/fax.ecmwfmp1.$job
 
    if test "$SENDDBN" = 'YES'
    then
      SNDKEY=fax.ecmwfmp1
      $utilscript/snd2forgntbl.sh $SNDKEY fax.ecmwfmp1.$job $pcom
    fi

 fi
fi
#####################################################################
# GOOD RUN
set +x
echo "**************job EMCWF COMPLETED NORMALLY ON THE IBM"
echo "**************job ECMWF COMPLETED NORMALLY ON THE IBM"
echo "**************job ECMWF COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################


msg="HAS COMPLETED NORMALLY!"
echo $msg
postmsg "$jlogfile" "$msg"

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