#####################################################################
echo "------------------------------------------------"
echo "Storm Surge Forecast"
echo "------------------------------------------------"
echo "History: JULY 97 - First implementation of this new script."
echo "         MAY 98  - modify to correct an error in call to"
echo "                   snd2forgntbl.sh ."
echo "  31 AUG 2005 - Converted graphics from 6bit format to T4 format and"
echo "                rmoved snd2forgn processing."
echo "                         "
#####################################################################

cd $DATA
export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default name

######################
# Set up Here Files.
######################

##########################################
#
# START FLOW OF CONTROL
#
# 1) Make COM dirs.
#
# 2) GENDATA - reads hourly dumps, extracts synoptic, buoy, ship
#    and METAR data. Creates an AFOS plotfile over the tropics and
#    midlatitudes between 50N and 50S.
#
# 3) REDSAT - extracts low level satellite winds from the GFS
#    prepbufr  file and creates a file to be used by program TRPSFCMV.
#
# 4) TRPSFCMV - Plots analyzed wind barbs and temperatures. Contours
#    the 100mb streamfunctions and plots gridded winds.  It uses
#    NCAR graphics to produce a metafile which is rasterized in 
#    subsequent program execution.
#
# 5) RAS2BITY - packs 8-bit color map pizels into 1-bit black and
#    white pixels of the second slice 40S - 40N.
#
# 6) SIXBITB2 - Reads station pixel coordinates and station wind,
#    temperture, dewpoint, cloud, weather, sky cover and barometer
#    data and plots standard plots on a generic bitmap background.
#
#########################################


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

set +x
echo " "
echo "######################################"
echo " Load SYNOP DATA "
echo "######################################"
echo " "
set -x

export FORM=$PDY$cyc
export TIME=$PDY
DUMP=/nwprod/ush/dumpjb

$DUMP $PDY$cyc 1.5  synop
export err=$?
if [ "$err" -ne 0 ]
then
   echo "###  No synop data for synop.${PDY}${cyc}! ###"
   echo "###  Stoping execution of GENDATA          ###"
    err_chk
fi

for TYPE in metar ships lcman mbuoy dbuoy
do
   cp /com/hourly/prod/hourly.$PDY/$TYPE.$PDY$cyc.bufr $TYPE.$PDY$cyc

   if [ ! -f /com/hourly/prod/hourly.$PDY/$TYPE.$PDY$cyc.bufr ]
   then
      $DUMP $PDY$cyc 0.5 $TYPE
   fi
done


export pgm=gendata
 . prep_step

export XLFUNIT_11="synop.$PDY$cyc"
export XLFUNIT_12="metar.$PDY$cyc"
export XLFUNIT_13="ships.$PDY$cyc"
export XLFUNIT_14="lcman.$PDY$cyc"
export XLFUNIT_15="mbuoy.$PDY$cyc"
export XLFUNIT_16="dbuoy.$PDY$cyc"
export XLFUNIT_17="$FIXgraph/graph_pillist1"
export XLFUNIT_52="NHPLOT"

 startmsg
$EXECgraph/gendata >> $pgmout <<EOF 2>errfile
$PDY$cyc
  50 -50  00 360 006 006
EOF
 export err=$?;err_chk

#########################################################
# Obtain satellite winds
#########################################################
cp $COMIN/gdas1.$cycle.prepbufr gdas1.$cycle.prepbufr

export pgm=redsat
 . prep_step

export XLFUNIT_11="gdas1.$cycle.prepbufr"
export XLFUNIT_78="satwinds"

 startmsg
$EXECgraph/redsat >> $pgmout 2> errfile
 export err=$?;err_chk

cp $COMIN/gdas1.$cycle.pgrbf00   gdas1.$cycle.tmppgrbf00
#cp $COMIN/gdas1.$cycle.pgrbif00  gdas1.$cycle.pgrbif00
cp $COMIN/gdas1.$cycle.syndata.tcvitals.tm00 tcvitals

$EXECutil/copygb -xg3 gdas1.$cycle.tmppgrbf00 gdas1.$cycle.pgrbf00
$EXECutil/grbindex gdas1.$cycle.pgrbf00 gdas1.$cycle.pgrbif00

export pgm=trpsfcmv
 . prep_step

### input files ###
export XLFUNIT_11="gdas1.${cycle}.pgrbf00"
export XLFUNIT_12="gdas1.${cycle}.pgrbif00"
export XLFUNIT_38="satwinds"
export XLFUNIT_31="tcvitals"
export XLFUNIT_43="NHPLOT"

### output files unit ### i

export XLFUNIT_44="afosplot"
export XLFUNIT_74="HBULL"
export XLFUNIT_87="afosplot"
export XLFUNIT_88="f88"
export XLFUNIT_89="f89"

filesize=`cat NHPLOT | wc -c`
echo $filesize >fsize_in

 startmsg
$EXECgraph/gdas_trpsfcmv <fsize_in >> $pgmout 2> errfile
 export err=$?; err_chk


###########################################################
# Rasterize the map of the second slice 40S-40N
###########################################################
ictrans -d xwd  -fdn 2 -resolution 6912x1728 -e ' zoom   0.0 0.3755555 1.0 0.6255556   ' -e 'plot 1 ' gmeta >f11

###########################################################
# Pack 8 bit pixels into one bits of the first slice
###########################################################
export pgm=ras2bity
 . prep_step

export XLFUNIT_11="f11"
export XLFUNIT_59="f59"

fssize=`cat f11 | wc -c `
echo $fssize  >fin
echo 6912 >>fin
echo 1728 >>fin

 startmsg
$EXECgraph/ras2bity <fin >> $pgmout 2> errfile
 export err=$?;err_chk
#
###############################################################
# Set up the first input image with the header glued at the top
###############################################################
cat $FIXgraph/graph_ras2bity.header f59 > f59_ras2bity1

cp f59_ras2bity1 image001.pur
cp image001.pur mapback.pur
cp mapback.pur image002.pur

###########################################################
# Make NCEP sixbitb map
###########################################################
export pgm=sixbitb2
 . prep_step

FAXOUT=tropic${cycle}"Z."${cyc}

export DTIME=`cat $DATA/fort.79`
cp f88 fort.13
cat $FIXgraph/graph_sixbitb.trpsfcmv.all >> fort.13
cat >>fort.13 <<EOF
SHIFT 000020034600000
GULFT 0150001400 TROPICAL SURFACE ANAL. AND OBS.
GULFT  1520 1400 VALID   $DTIME
GULFT  1540 1400 STREAM FUNCTION FROM GFS  ANAL.
GULFT  1500 2400 TROPICAL SURFACE ANAL. AND OBS.
GULFT  1520 2400 VALID  $DTIME
GULFT  1540 2400 STREAM FUNCTION FROM GFS  ANAL.
GULFT  1500 6300 TROPICAL SURFACE ANAL. AND OBS.
GULFT  1520 6300 VALID   $DTIME
GULFT  1540 6300 STREAM FUNCTION FROM GFS  ANAL.
PUTLA 00520 4201 01.0 90.0 060 1 0 0  TROPICAL CYCLONE INFORMATION $DTIME
PUTLA 00620 6411 01.0 90.0 060 1 0 0  TROPICAL CYCLONE INFORMATION $DTIME
EOF
cat HBULL >> fort.13

################
#input files
################
export XLFUNIT_12="mapback.pur"
export XLFUNIT_13="fort.13"
export XLFUNIT_15="$FIXgraph/graph_sixbitb.generic.f15"
export XLFUNIT_18="$FIXgraph/graph_sixbitb.trpsfcmv.$cycle"
export XLFUNIT_39="f89"
################
#scratch files
################
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_52="x6b"
export XLFUNIT_55="putlab.55"
################
#output file(s)
################
export XLFUNIT_81="tropic${cycle}"Z."${cyc}"

 startmsg
$EXECgraph/sixbitb2 >> $pgmout 2>errfile
 export err=$?;err_chk

jobn=${model}_fax_${cyc}

 KEYW=GDTROP_g

 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

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


#####################################################################
# GOOD RUN
set +x
echo "**************JOB $job COMPLETED NORMALLY ON THE IBM SP"
echo "**************JOB $job COMPLETED NORMALLY ON THE IBM SP"
echo "**************JOB $job COMPLETED NORMALLY ON THE IBM SP"
set -x
#####################################################################

msg="HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"
############## END OF SCRIPT #######################
