set -xa
date

#####################################################################
echo "------------------------------------------------"
echo "JRADAR_$cyc the national radar summary postprocessing"
echo "------------------------------------------------"
#################################################################
#
# ABSTRACT : This portion of the script produces the national radar
#            summary chart from NAWIPS creating a GIF,TIFF and two
#            red book graphics files (the first one containing radar
#            reflectivity contours and the second one with all the
#            legends containing cell top, cell speed and direction,
#            watch boxes etc ) 
# Author  :  V. Krishna Kumar
#            System Integration Branch (SIB) NCEP Central Operations
#            May 2006
#
#################################################################

########################################################################
#
#  FLOW OF CONTROL
#  1) Create the gif product.
#  2) Create the radar summary legends product for awips
#  3) Create the radar summary contours product for awips
#########################################################################
#
# Convert the rcmedit_grib file into a GEMPAK grid file
#
# Copy the local grib1 table from /nwprod/gempak/fix to the $DATA
#
cp /nw${envir}/gempak/fix/radar_summary.tbl ./. 

nagrib << EOF
 GBFILE   = rcmedit_grib
 INDXFL   =
 GDOUTF   = dummy.grd
 PROJ     =
 GRDAREA  =
 KXKY     =
 MAXGRD   = 100
 CPYFIL   = gds
 GAREA    = grid
 OUTPUT   = file/rcmedit_grib.out
 GBTBLS   = radar_summary.tbl
 GBDIAG   =

r

EOF

mv dummy.grd rcmedit_grib.grd
#-----------------------------------------------
# Convert asci files of various data types
# to gempak sfc files
#
#  Produce namelist file for the gempak sffdsf.
#  npgiv is the number of parameters to be plotted.
#  The single parameter is rdrp
#
cat <<\gemEOF >gem_namlst
 &INPARM npgiv=1/
gemEOF

date=${PDY}
hour=${cyc}'00'
cpdy=`echo $date | cut -c3-8`
hhmm=$hour

#
# Cleanup the old sffdsf_input file prior to creating
# the new input file
#
/bin/rm -f sffdsf_input* > /dev/null 2>&1

###--------------------------------------------------- 1st for loop begins
files=`ls n*file*.dat`
for file in $files
do

####
#### Conversion gempak program from asci data to gempak extension surface
####
export pgm;. prep_step
export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default name
export XLFUNIT_9="gem_namlst"

cat << EOF >> sffdsf_input 
 SFEFIL   = $file
 SFOUTF   = $file.sfc
 AREA     = -90;-180;90;180
 SFPRMF   = radar_summary.pack
 DATTIM   = $cpdy/$hhmm
 TIMSTN   = 1/3999
 FORMAT   = mklein
r

EOF

done

echo ex >> sffdsf_input

sffdsf < sffdsf_input >> out.log

###--------------------------------------------------- 1st for loop ends

#
# Make the GEMPAK radar summary file using the rcmedit_grib.grd 
# file and gempak .sfc files for various devices
#

###--------------------------------------------------- 2nd for loop
#
# Cleanup the old input files prior to creating
# the new input files
#
/bin/rm -f sfmap_input* > /dev/null 2>&1
/bin/rm -f gpanot_input* > /dev/null 2>&1
#
#
n_device='1 2 3 4'
for ndf in $n_device
do
if [ $ndf -eq 1 ];then
   garea_spec="21.0;-121;40.5;-57"
   proj_spec="STR/90;-105;0"
   gfunc_spec="slt(rspc,7)"
   cint_spec="1;3;5"
   fint_spec="1;3;5"
   ctype_spec="C/F"
   n_dev="gif"
   c_dev="gif|radar.fax$cyc.hourly.gif|896;716"
   l_spec="25;21;16/1;1;1/1;1;1/0/0/0"
   f_spec="0;25;16;0"
   t_spec="0.857/11/////sw"
   g_text1=0.9/11/1/11///sw
   g_text2=0.7/11////l/sw
   g_text5=1.2/21////l/sw
   g_textn=1.0/11////l/sw
   noaa_l="NOAA | 1.4 | C"
   nws_l="NWS | 1.35 | C"
elif [ $ndf -eq 2 ]; then
   garea_spec="21.0;-121;40.5;-57"
   proj_spec="STR/90;-105;0"
   gfunc_spec="slt(rspc,7)"
   cint_spec="1;3;5"
   fint_spec="1;3;5"
   ctype_spec="C/F"
   n_dev="tiff"
   c_dev="tiff|QAUA00"
   l_spec="1;1;1/1;1;1/1;1;1/0/0/0"
   f_spec="0;1;1;0/3;4;5"
   t_spec="0.857/21/////hw"
   g_text1=0.9/11/1/11///hw
   g_text2=0.7/11////l/hw
   g_text5=1.2/22////l/hw
   g_textn=1.0/11////l/hw
   noaa_l="NOAA | 1.4 | M"
   nws_l="NWS | 1.35 | M"
elif [ $ndf -eq 3 ]; then
   garea_spec="awna"
   proj_spec=" "
   gfunc_spec="sm5s(slt(rspc,7))"
   cint_spec="1;3;5"
   fint_spec=" "
   ctype_spec="C"
   n_dev="rbk1"
   c_dev="rbk|90R||C"
   l_spec="3;4;2//3;4;5/0/4/2"
   f_spec=" "
   t_spec="0.857/21/////hw"
   g_text1=0.9/11/1/11///hw
   g_text2=0.7/11////l/hw
   g_text5=1.2/22////l/hw
   g_textn=1.0/11////l/hw
   noaa_l="NOAA | 1.4 | C"
   nws_l="NWS | 1.35 | C"
elif [ $ndf -eq 4 ]; then
   garea_spec="awna"
   proj_spec=" "
   gfunc_spec="sm5s(slt(rspc,7))"
   cint_spec="1;3;5"
   fint_spec="1;3;5"
   ctype_spec="C/F"
   n_dev="rbk2"
   c_dev="rbk|90S||C"
   l_spec="1;1;1/1;1;1/1;1;1/0/0/0"
   f_spec="0;0;0;0/3;4;5"
   t_spec="0.857/21/////hw"
   g_text1=0.9/11/1/11///hw
   g_text2=0.7/11////l/hw
   g_text5=1.2/22////l/hw
   g_textn=1.0/11////l/hw
   noaa_l="NOAA | 1.4 | C"
   nws_l="NWS | 1.35 | C"
fi

# Don't make the contours for "rbk2"
###------------------------ "rbk2" if loop begins here

if [ "$n_dev" = "gif" -o "$n_dev" = "tiff" -o "$n_dev" = "rbk1" ]
then

gdcntr << EOF >> gdcntr.record
 GDATTIM  = LAST
 GLEVEL   = 0
 GVCORD   = none
 GFUNC    = $gfunc_spec
 GDFILE   = rcmedit_grib.grd
 CINT     = $cint_spec
!
!        colr1;..;colrn/type1;..;typen/width1;..;widthn/labl1;..;labln/smth/fltr
!      For example:
!                LINE = 1;2 / 7;8 / 4;5 / 2 / 2
!      assigns colors 1 and 2 to alternate lines, dashing patterns 7 and 8 to
!      alternate lines, line widths 4 and 5 to alternate lines, and labels
!      every other line.  Smoothing level 2 is used on all lines.
!
 LINE     = $l_spec
 MAP      =  1/1/1
 DEVICE   = $c_dev
 SATFIL   =
 RADFIL   =
 PROJ      = $proj_spec
 GAREA    = $garea_spec
 IJSKIP   =
 CLEAR    = no
 PANEL    = 0
 TEXT     = $t_spec
 TITLE    = 0
 SCALE    = 0
 LATLON   = 1/10/1/2/5;5
 HILO     =
 HLSYM    =
 CLRBAR   =
 CONTUR   = 0
 SKIP     = 0
 FINT     = $fint_spec
 FLINE    = $f_spec
 CTYPE    = $ctype_spec
 LUTFIL   =
 STNPLT   =
 MSCALE   = 0
 FILTER   = 1
r

EOF

fi 
###------------------------ "rbk2" if loop ends here

################################################
# Plot gempak sfc files of various data types
# to gif images
################################################

# Don't make the legends for "rbk1"
###------------------------ "rbk1" if loop begins here
if [ "$n_dev" = "gif" -o "$n_dev" = "tiff" -o "$n_dev" = "rbk2" ]
then
#
###----------------------------------------- 2nd for loop begins here
files=`ls n*file*sfc`
for file in $files
do

if [ $file = "na_file.dat.sfc" ]; then
   colorval=2
   sfparmval=stid
#
# Text specifications
#   text size / font / width / border /
#   rotation / justification / hw flag
#
   if [ "$n_dev" = "gif" ]; then
      textval=1/11/2/11/s/c/sw
   else
      textval=1/11/2/11/s/c/hw
   fi

elif [ $file = "ne_file.dat.sfc" ]; then
   colorval=2
   sfparmval=stid

   if [ "$n_dev" = "gif" ]; then
      textval=1/11/2/11/s/c/sw
   else
      textval=1/11/2/11/s/c/sw
   fi

elif [ $file = "nc_file.dat.sfc" ]; then
   colorval=23
   sfparmval=rdrp

   if [ "$n_dev" = "gif" ]; then
      textval=0.8/11/1/215/s/c/sw
   else
      textval=0.8/11/1/215/s/c/hw
   fi

elif [ $file = "nd_file.dat.sfc" ]; then
   colorval=29
   sfparmval=rdrp

   if [ "$n_dev" = "gif" ]; then
      textval=0.8/11/2/11/s/c/sw
   else
      textval=0.8/11/2/11/s/c/hw
   fi

elif [ $file = "nw_file.dat.sfc" ]; then
   colorval=2
   sfparmval=stid

   if [ "$n_dev" = "gif" ]; then
      textval=0.8/11/1/11/s/c/sw
   else
      textval=0.8/11/1/11/s/c/hw
   fi

elif [ $file = "ns_file.dat.sfc" ]; then
   colorval=30
   sfparmval=stid

   if [ "$n_dev" = "gif" ]; then
      textval=0.8/11/1/211/s/c/sw
   else
      textval=0.8/11/1/211/s/c/hw
   fi

elif [ $file = "nb_file.dat.sfc" ]; then
   colorval=15
   sfparmval=stid

   if [ "$n_dev" = "gif" ]; then
      textval=0.8/11/1/211/s/c/sw
   else
      textval=0.8/11/1/211/s/c/hw
   fi

elif [ $file = "nt_file.dat.sfc" ]; then
   colorval="2;0"
   sfparmval="mark:19:0.5:0.5:0.5;stid"

   if [ "$n_dev" = "gif" ]; then
      textval=0.7/11/1/11/s/c/sw
   else
      textval=0.7/11/1/11/s/c/hw
   fi

fi

cat << EOF >> sfmap_input_$ndf
 GAREA    = $garea_spec
 SATFIL   =
 RADFIL   =
!mark:15 (dot)
 SFPARM = $sfparmval
 DATTIM   = $cpdy/$hhmm
 SFFILE   = $file
 COLORS = $colorval
 LATLON = 0
 TITLE    = 0
 CLEAR    = no
 DEVICE   = $c_dev 
 PROJ     = $proj_spec
 FILTER   = 0
 TEXT     = $textval
 CLRBAR   =
r

EOF

done

echo ex >> sfmap_input_$ndf

sfmap < sfmap_input_$ndf >> out.log

###----------------------------------------- 2nd for loop ends here

#
# Draw the lines from cell top to the cell values
# Draw the line from the line bottom to the SLD
#
###----------------------------------------- 3rd for loop begins here
#
files=`ls conn*file*dat`
for file in $files
do

if [ $file = "conn_nltb_file.dat" ]; then
   lineval=30/1/4
   shp=line
   sinfo=1
elif [ $file = "conn_nlbs_file.dat" ]; then
   lineval=30/1/1
   shp=line
   sinfo=1
elif [ $file = "conn_nltn_file.dat" ]; then
   lineval=23/1/1
   shp=line
   sinfo=1
elif [ $file = "conn_wbox_file.dat" ]; then
   lineval=15/1/3
   shp=line
   sinfo=1
elif [ $file = "conn_wbln_file.dat" ]; then
   lineval=15/1/1
   shp=line
   sinfo=1
elif [ $file = "conn_nspd_file.dat" ]; then
   lineval=29/1/2
   shp=arrow
fi

while read gfile
do
first=`echo $gfile`
echo $first
if [ $file = "conn_nspd_file.dat" ]; then
   dirc=`echo $first | cut -c1-3`
#
#Arrow Specification
#line typ/arrw typ/arrw siz/head siz/head ang mult/rotation
#
   sinfo=1/1/15/15/1/$dirc
   firstc=`echo $first | cut -c4-50`
else
   firstc=$first
fi

cat << EOF >> gpanot_input_$ndf
 GDFILE   =
 DEVICE   = $c_dev
 CLEAR    = no
 SHAPE    = $shp
 INFO     = $sinfo
 LOCI     = $firstc
 LINE     = $lineval
 CTYPE    = c
r

EOF

done < $file

done

echo ex >> gpanot_input_$ndf

gpanot < gpanot_input_$ndf >> out.log

#
###----------------------------------------- 3rd for loop ends here
#
# Write text with gpanot to indicate date and time
#

gptext <<EOF > plot.gptext.record1
PANEL    =
COLORS = 4
TEXT     = $g_text1
DEVICE   = $c_dev
CLEAR    = no
TXTFIL   = radar_summary1_text
TXTLOC   =  #25 ; -118
COLUMN   = 1
r

TEXT     = $g_text2
TXTFIL   = radar_summary2_text
TXTLOC   =  #24 ; -119
r

TXTFIL   = radar_summary3_text
TXTLOC   =  #25 ; -105
r

TXTFIL   = radar_summary6_text
TXTLOC   =  #23.9 ; -105.2
r

TXTFIL   = radar_summary4_text
TXTLOC   =  #24 ; -88
r

TEXT     = $g_text5
TXTFIL   = radar_summary5_text
TXTLOC   =  #20.5 ; -105
r

TXTFIL   = radar_summary5_text
TXTLOC   =  #51.0 ; -132
r

TEXT     = $g_textn
TXTFIL = $noaa_l
TXTLOC = #25.5 ; -94
r

TXTFIL = $nws_l
TXTLOC = #24.8 ; -91
r

EOF

fi
###------------------------ "rbk1" if loop ends here

gpend

done

#
# Transfer the gif, tiff and red book graphics files to
# different destinations like TOC, NCDC, AWIPS etc
#
#
# FOR NCDC route - NCEP ftp
#
export outname=radar.fax$cyc.hourly.gif
  if test "$SENDCOM" = 'YES'
  then
    cp ${outname} ${COMOUT}
      if test "$SENDDBN" = 'YES'
      then
        $DBNROOT/bin/dbn_alert MODEL NCDCGIF ${job} ${COMOUT}/$outname
      fi
  fi
#
# First move the original GEMPAK tiff output to a name similar to gif
#

cp QAUA00.tiff radar.fax$cyc.hourly.tiff

#
# FOR TOC route - from TIFF file
# for adding a WMO header to a TIFF file
#
   export outname=radar.fax$cyc.hourly.tiff
   INPATH=$DATA/$outname
   SUB=IMAG2000
   WMO=QAUA00
   ORIG=KWBC
   PDYHH=$PDY$cyc
   OUTPATH=radar.fax$cyc.hourly.ntc
   $USHutil/make_NTC_file.pl $WMO $ORIG $PDYHH $SUB $INPATH $OUTPATH
# 
# Send it to TOC with the header
#
  if test "$SENDCOM" = 'YES'
  then
    cp ${OUTPATH} ${pcom}
      if test "$SENDDBN" = 'YES'
      then
        $DBNROOT/bin/dbn_alert GRIB_LOW HRLY FAX ${pcom}/$OUTPATH
      fi
  fi

#
# First move the original GEMPAK first red book graphics output (contour)
# (90R) to a name similar to gif
#

cp NMCGPH90R 90R.redbook.PAWO82.radar_${cyc}

#
# FOR TOC route - from RBG files
# for adding a WMO header to a RGB file
#
   export outname=90R.redbook.PAWO82.radar_${cyc}
   INPATH=$DATA/$outname
   SUB=NONE
   WMO=PAWO82
   ORIG=KWBC
   PDYHH=$PDY$cyc
   OUTPATH=$outname
   $USHutil/make_ntc_bull.pl redb $SUB $ORIG $SUB $INPATH $OUTPATH 
#  /nwtest/util/ush/make_NTC_file.pl $WMO $ORIG $PDYHH $SUB $INPATH $OUTPATH
#
# Send it to TOC with the header
#
  if test "$SENDCOM" = 'YES'
  then
    cp ${OUTPATH} ${pcom}
      if test "$SENDDBN" = 'YES'
      then
        $DBNROOT/bin/dbn_alert GRIB_LOW HRLY FAX ${pcom}/$OUTPATH
      fi
  fi
#

#
# First move the original GEMPAK second red book graphics output (legends)
# (90S) to a name similar to gif
#

cp NMCGPH90S 90S.redbook.PAWO83.radar_${cyc}

#
# FOR TOC route - from RBG files
# for adding a WMO header to a RGB file
#
   export outname=90S.redbook.PAWO83.radar_${cyc}
   INPATH=$DATA/$outname
   SUB=NONE
   WMO=PAWO83
   ORIG=KWBC
   PDYHH=$PDY$cyc
   OUTPATH=$outname
   $USHutil/make_ntc_bull.pl redb $SUB $ORIG $SUB $INPATH $OUTPATH 
#   /nwtest/util/ush/make_NTC_file.pl $WMO $ORIG $PDYHH $SUB $INPATH $OUTPATH
#
# Send it to TOC with the header
#
  if test "$SENDCOM" = 'YES'
  then
    cp ${OUTPATH} ${pcom}
      if test "$SENDDBN" = 'YES'
      then
        $DBNROOT/bin/dbn_alert GRIB_LOW HRLY FAX ${pcom}/$OUTPATH
      fi
  fi

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

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

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

