# UTILITY SCRIPT NAME : mkwindalftf.sh
#              AUTHOR : Mary Jacobs
#                       
# ABSTRACT : NOW PRODUCE UPPER AIR WIND PLOTS
#
#   SCRIPT TO RUN WNDANFTF HDS REPLACEMENT
#   The script runs a bufr extraction
#   program to extract soundings from
#   an gfs PREPBUFR file and
#   write them as a FORMATTED HUMAN
#   READABLE REPORT.  (rsonde.x).  It
#   then runs an NCAR graphics program to
#   plot wind barbs and temperatures
#   (wndanftf.x).  
#   This generates a cgm metafile which
#   is rasterized (raster2bit.x),
#   prefixed with a CNTR standard header
#   (glue.head.x), and packed to
#    the NOAA bedient sixbit format
#   demanded by OSO.  (sixbitb.x)
#   Author:    George VandenBerghe Cray Research.
#   HISTORY:   1/23/97 first written.
#   HISTORY:   9/12/05 Remove snd2forgn
#   HISTORY:  10/05/05 Converted fax graphics to T4
#                      format and stopped writing to
#                      the status file.
#
set -x

###########################################################
#  Extract significant Height observations
###########################################################
cp $COMIN/gfs.${cycle}.prepbufr gfs.${cycle}.prepbufr

export pgm=rsonde
. prep_step

export XLFUNIT_11=gfs.${cycle}.prepbufr 
export XLFUNIT_51=f51Z                    

startmsg
$EXECgraph/rsonde < $PARMgraph/graph_rsonde.ft05.Z >> $pgmout 2> errfile
export err=$?;err_chk

###########################################################
#  Extract significant pressure observations
###########################################################

export pgm=rsonde
. prep_step

export XLFUNIT_11=gfs.${cycle}.prepbufr    
export XLFUNIT_51=f51P                       

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

###########################################################
# cat the oservation files together
###########################################################

cat f51Z f51P >formatted.soundings

###########################################################
# draw ncar graphics map
###########################################################

export pgm=wndanftf
. prep_step

export XLFUNIT_20=formatted.soundings 

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

###########################################################
# rasterize the map
###########################################################

ictrans -d xwd  -fdn 2 -resolution 1728x2440  -e 'zoom 0.08 0.0 0.79  1.0' -e 'plot 1' gmeta >f8
###########################################################
# pack 8 bit pixels into one bit bits 
###########################################################

export pgm=ras2bit
. prep_step

export XLFUNIT_11=f8     
export XLFUNIT_59=f59  

fssize=`cat f8 | wc -c `
echo $fssize  >fin
echo 1728 >>fin
echo 2440 >>fin

startmsg

$EXECgraph/ras2bit <fin  >> $pgmout 2> errfile
export err=$?;err_chk
###########################################################
# Glue the header onto the image file by getting rid of HGLUE
###########################################################

cat $FIXgraph/graph_wndanftf.header f59 > mapback.pur
cp mapback.pur image002.pur
cp mapback.pur image001.pur

###########################################################
# now we have bit image background field (in mapback.pur)  which
# actually contains the ENTIRE MAP and we
# merely need "cntr" to do the six bit bedient
# packing.
# A second image, (duplicate but for alternative
# disposition with a different subset number )
# is in image002.pur. 
###########################################################

export pgm=sixbitb
. prep_step
 
jobn=$job
FAXOUT=bkwndalf.${cyc}

export XLFUNIT_11="mapback.pur"               
export XLFUNIT_12="mapback.pur"               
export XLFUNIT_15=$FIXgraph/graph_sixbitb.generic.f15      
export XLFUNIT_18=$FIXgraph/graph_sixbitb.wndanftf.$cycle 
export XLFUNIT_52=x6b             
export XLFUNIT_55=putlab.55      
export XLFUNIT_60=f60                   
export XLFUNIT_61=f61                  
export XLFUNIT_62=f62                 
export XLFUNIT_63=f63                
export XLFUNIT_71=ras               
export XLFUNIT_72=rs2              
# the 6 bit output is assigned here
export XLFUNIT_81="bkwndalf.${cyc}"             

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


 for KEYW in WNDAFT WNDAFT_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

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

 done

exit

