#!/bin/sh

# UTILITY SCRIPT NAME : exwafsg3.sh 
#              AUTHOR : Larry Sager
#
# ABSTRACT : This utility script converts WAFS graphics from       
#            6-bit format to g3.
#
# INPUT: 6 arguments are input to this script.
#         1st argument - file    - 6-bit file
#         2nd argument - hour1   - Fcst hour of 1st grib file
#         3nd argument - hour2   - Fcst hour of 2nd grib file
#         4th argument - type    - Type of chart to create
#
# TYPES: a - Western Hemisphere Mercator
#        b1- Atlantic Mercator 
#        f - Create the Pacific Mercator (500 400 300 250 200mb)
#        f1- Create the Pacific Mercator (850 700 150  70mb)
#        h - Polar Stereographic North Atlantic
#        i - Polar Stereographic North Pacific
#        j - Polar Stereographic Anarctic
#        c - Mercator Africa/Europe      
#        d - Mercator Indian Ocean        
#        e - Mercator Indian Ocean/Australia
#        g - Polar Stereographic Anarctic
#        m - Mercator North Pacific       
#

set +x
out=$1
hour1=$2 
hour2=$3
type=$4
num=$#

if test "$num" -eq 4  
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECgraph=${EXECgraph:-/nwprod/util/exec}
   export FIXgraph=${FIXgraph:-/nwprod/util/fix}
   export envir=${envir:-prod}
   export jlogfile=${jlogfile:-jlogfile}
   export NET=${NET:-gfs}
   export RUN=${RUN:-gfs}
   export cyc=${cyc:-00}
   export cycle=${cycle:-t${cyc}z}
   export SENDCOM=${SENDCOM:-NO}
   export SENDDBN=${SENDDBN:-NO}
   if [ -z "$DATA" ]
   then
      export DATA=`pwd`
      cd $DATA
      /nwprod/util/ush/setup.sh
      /nwprod/util/ush/setpdy.sh
      . PDY
   fi
   export COMIN=${COMIN:-/com/$NET/$envir/$NET.$PDY}
   export pcom=${pcom:-/pcom/$NET}
   export job=${job:-interactive}
   export pgmout=${pgmout:-OUTPUT.$$}
else
   echo ""
   echo "Usage: exwafsg3.sh file hour1 hour2 type "
   echo ""
   exit 16
fi

dayhr=`echo $PDY | cut -c 7-8`$cyc     
echo $dayhr > DAYHR

set +x
echo "########################################################"
echo "#  Entering Utility  exwafsg3.sh"
echo "########################################################"
set -x


##############################
# Convert to g3 format
##############################
export PATH=$PATH:/usrx/local/ncar401/bin:/usrx/local/imajik/bin:.
export LIBPATH="$LIBPATH":/usrx/local/imajik/lib
export DELEGATE_PATH=/usrx/local/imajik/share/ImageMagick

width=1728
height=1950
echo P4 > header
echo # NCEPtest >> header
echo $width '   '  $height >> header

JOB=`echo $job | cut -c1-4`
grep ${type}.${JOB} $FIXgraph/wafs_bulletin.list > a
files=`cat a | cut -f 2 -d"-"`

for file in $files
do
   parmsg="ssno=$file"

#  extract the raster

   export pgm=fxcompoz    
   . prep_step

   export XLFUNIT_66="rasfile.$file"       # in cards
   export XLFUNIT_67="rascrd"            # out card
   export XLFUNIT_75="rasmes"            # message file

   $EXECutil/fxcompoz $out  parm=$parmsg >> $pgmout 2> errfile
   export err=$?;err_chk


   cat DAYHR > DAYHRSIZE
   cat header rasfile.$file > image.$file
   convert  PBM:image.$file  FAX:wafs.g3.$file
   wc -m wafs.g3.$file | cut -c1-8 >> DAYHRSIZE
   echo "Number of Header to make=01" > label
   grep $file $FIXgraph/wafs_bulletin.list > xx
   cut -c9-19 xx >> label

   export pgm=tocheder    
   . prep_step

   export XLFUNIT_11="label"
   export XLFUNIT_51="header2"
   export XLFUNIT_61="trailer"
   export XLFUNIT_62="trailer2"

   $EXECutil/tocheder < DAYHRSIZE >> $pgmout 2> errfile
   export err=$?;err_chk 

   out2=g3.wafs${hour1}${type}.${job}
   cat header2 wafs.g3.$file  trailer >> ${out2}
   rm trailer
done

cat trailer2 >> ${out2}

##############################
# Post Files to PCOM - 
##############################
if test "$SENDCOM" = 'YES'
then
   cp ${out2} $pcom/${out2}
   ##############################
   # Distribute Data --                        
   ##############################
   if test "$SENDDBN" = 'YES'
   then
     $DBNROOT/bin/dbn_alert GRIB  $job WAFS_GRIB $pcom/${out2}
     echo at send
   else
     msg="${out}.${job} file not posted to db_net."
     postmsg "$jlogfile" "$msg"
   fi
fi

msg="exwafsg3 processing for type=$type completed normally"
postmsg "$jlogfile" "$msg"

msg="exwafsg3.sh completed normally"
postmsg "$jlogfile" "$msg"

exit
