#####################################################################
echo "------------------------------------------------"
echo "GFS Analysis postprocessing"
echo "------------------------------------------------"
echo "History: JAN 1997  - First implementation of this new script."
echo "         Apr 2003  - Remove Unneeded FAX processing."
echo "         Apr 2004  - Rename AVN to GFS              "
echo "         Sept 2005 - Remove SND2FORN logic              "
echo "         Sept 2005 - Remove generation of the 250MB plot and analysis 6-bit format"
echo "                     Replace with GEMPAK verison             "
echo "                     Converted fax graphics to T4 format and stopped writing to the"
echo "                     stat file."
#####################################################################

cd $DATA

##########################################
# 1) Execute UPAPREP 
# 2) Execute PLOTVPAP
# 3) Execute TRPANL 
# 7) Execute WNDALFTF - This utility consists of 5 steps.
# 8) Execute WNDALFTV - generate 4 AFOS charts of winds aloft.
# 9) Execute GETFLDS to retrieve selected fields from the analysis. 
########################################

export XLFRTEOPTS="unit_vars=yes"
########################################
set -x
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################
set +x
echo " "
echo "###################################################"
echo " Execute the UPAPREP for mercator tropical analysis"
echo "###################################################"
echo " "
set -x

cp ${COMIN}/gfs.$cycle.prepbufr gfs.$cycle.prepbufr
err=$?
if test "$err" -ne 0
then
  msg="File not yet available in com"
  postmsg "$jlogfile" "$msg"
  err_exit
fi

export pgm=upaprep
. prep_step

echo ${PDY}${cyc} > upaprep.input

export XLFUNIT_9=$PARMgraph/graph_upaprep.ft9.mrcontrl
export XLFUNIT_10=gfs.$cycle.prepbufr
export XLFUNIT_22=$PARMgraph/graph_upaprep.ft22.aircft
export XLFUNIT_23=$PARMgraph/graph_upaprep.ft23.satwnd
export XLFUNIT_24=$PARMgraph/graph_upaprep.ft24.tiros
export XLFUNIT_55=gfs.${PDY}${cyc}.upaprep
export XLFUNIT_56=gfs.${PDY}${cyc}.aircft
export XLFUNIT_57=gfs.${PDY}${cyc}.satwnd
export XLFUNIT_58=gfs.${PDY}${cyc}.satell
export XLFUNIT_75=LOGMSG

startmsg
$EXECgraph/upaprep < upaprep.input >> $pgmout 2> errfile
export err=$?;err_chk
  
set +x
echo " "
echo "###################################################"
echo " Execute PLOTVPAP "
echo "###################################################"
echo " "
set -x

export pgm=plotvpap
. prep_step

export XLFUNIT_15="$PARMgraph/graph_plotvpap.gfs.anl"
export XLFUNIT_26="$PARMgraph/graph_plotvpap.plotmlty.ft26"
export XLFUNIT_41="gfs.${PDY}${cyc}.upaprep"
export XLFUNIT_42="gfs.${PDY}${cyc}.aircft"
export XLFUNIT_43="gfs.${PDY}${cyc}.satwnd"
export XLFUNIT_44="gfs.${PDY}${cyc}.satell"
export XLFUNIT_55="OLDlabel55"
export XLFUNIT_60="label60"
export XLFUNIT_61="label61"
export XLFUNIT_62="label62"
export XLFUNIT_63="label63"
export XLFUNIT_75="LOGMSG"
  
startmsg
$EXECgraph/plotvpap
export err=$?;err_chk

set +x
echo " "
echo "###################################################"
echo " Execute TRPANL "
echo "###################################################"
echo " "
set -x

FAXOUT=trpismer.${cyc}

cp ${COMIN}/gfs.$cycle.pgrbanl gfs.$cycle.pgrbanl
err1=$?
cp ${COMIN}/gfs.$cycle.pgrbianl gfs.$cycle.pgrbianl
err2=$?

tot=`expr $err1 + $err2`
if test "$tot" -ne 0
then
  msg="File not yet available in com"
  postmsg "$jlogfile" "$msg"
  err_exit
fi

cp ${FIXgraph}/graph_gphbg/mr4002.pur mr4002.pur

export pgm=trpanl
. prep_step
export XLFUNIT_8="$PARMgraph/graph_trpanl.ft8.gfs_${cycle}.anl"
export XLFUNIT_12="gfs.$cycle.pgrbanl"
export XLFUNIT_22="gfs.$cycle.pgrbianl"
export XLFUNIT_48="$FIXgraph/graph_awpseed"
export XLFUNIT_52="x6b"
export XLFUNIT_54="OLDlabel55"
export XLFUNIT_55="f55"
export XLFUNIT_60="f60"
export XLFUNIT_61="f61"
export XLFUNIT_62="f62"
export XLFUNIT_63="f63"
export XLFUNIT_71="ras"
export XLFUNIT_72="rs2"
export XLFUNIT_81="trpismer.${cyc}"

startmsg
$EXECgraph/trpanl >> $pgmout < $PARMgraph/graph_trpanl.ft5.gfs_${cycle}.anl 2>errfile
#export err=$?; err_chk 

if [ ${cyc} -eq 00 -o ${cyc} -eq 12 ];
then

jobn=$job

 for KEYW in TRP850_g TRP850V TRP700A TRP700_g TRP500A TRP500_g TRP250A TRP250_g
 do

 grep $KEYW $FIXutil/identifyfax.tbl | read Keyword sub00 sub06 sub12 sub18 gif toc prt lprt name

 if [ ${cyc} = '00' ]; then submn=$sub00; fi
 if [ ${cyc} = '12' ]; then submn=$sub12; fi

 export FAXOUT submn name Keyword gif toc prt jobn lprt
 $utilscript/mk_graphics.sh

 done
fi

if [ ${cyc} -eq 06 -o ${cyc} -eq 18 ];
then

 jobn=$job

 for KEYW in  TRP250A TRP250_g
 do

 grep $KEYW $FIXutil/identifyfax.tbl | read Keyword sub00 sub06 sub12 sub18 gif toc prt lprt name

 if [ ${cyc} = '06' ]; then submn=$sub06; fi
 if [ ${cyc} = '18' ]; then submn=$sub18; fi

 export FAXOUT submn name Keyword gif toc prt jobn lprt
 $utilscript/mk_graphics.sh

 done
fi
 
if test ${cycle} = 't00z' -o ${cycle} = 't12z'
then
  set +x
  echo " "
  echo "###################################################"
  echo " Execute WNDALFTF                                  "
  echo "###################################################"
  echo " "
  set -x

  $USHutil/mkwindalftf.sh

  set +x
  echo " "
  echo "###################################################"
  echo " Execute WNDALFTV (first get prepbufr bufr data)     "
  echo "###################################################"
  echo " "
  echo "###################################################"
  echo "#  Extract significant height observations         "
  echo "###################################################"
  set -x

  export pgm=rsondplt
  . prep_step
  
  export XLFUNIT_10="gfs.${cycle}.prepbufr"
  export XLFUNIT_51="f51Z"
  
  startmsg
  $EXECgraph/rsondplt <$PARMgraph/graph_rsondplt.ft05.Z >> $pgmout 2>errfile
  export err=$?;err_chk

  set +x
  echo " "
  echo "###################################################"
  echo "#  Extract significant pressure observations       "
  echo "###################################################"
  set -x
  
  export pgm=rsondplt
  . prep_step

  export XLFUNIT_10="gfs.${cycle}.prepbufr"
  export XLFUNIT_51="f51P"

  startmsg
  $EXECgraph/rsondplt <$PARMgraph/graph_rsondplt.ft05.P >>$pgmout 2>errfile
  export err=$?;err_chk

  set +x
  echo " "
  echo "###################################################"
  echo "# cat the observation files together               "
  echo "###################################################"
  set -x

  cat f51Z f51P > formatted.soundings


fi

exit
