#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         nam_smartinitconusnest2p5_on.sh
# Script description:  runs smartinit code over NDFD conus domain 
#                       and generate 2.5 km output for 00/12z cycles
#
# Author:        Geoff Manikin       Org: NP22         Date: 2011-07-05 
#
# Script history log:
# 2011-07-05  Geoff Manikin     
#

set -x

let pcphr=ffhr+3
let pcphrl=ffhr+3
let pcphr12=pcphr-12
let pcphr6=pcphr-6
let pcphr3=pcphr-3

typeset -Z2 srefcyc
typeset -Z2 gefscyc
typeset -Z2 pcphrl

export FIXruc2=${FIXruc2:-/nwprod/fix}

cd $DATA

if [ $ffhr -gt 0 ]; then
# get the sref precip fields that we need
cp $COMIN_SREF/sref.t${srefcyc}z.pgrb212.prob_3hrly SREFPROB

if [ ! -s SREFPROB ]; then
cp $COMIN_GEFS/${gefscyc}/sref.t${gefscyc}z.pgrb212.prob_3hrly SREFPROB
fi

$utilexec/grbindex SREFPROB SREFPROBI

# 3-hr prob of pcp > 0.01
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 64 64 0 0"| grep "0 1 $pcphr3 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp1

# 3-hr prob of pcp > 0.05
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 20 81 236"| grep "0 1 $pcphr3 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp2

# 3-hr prob of pcp > 0.10
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 40 163 215"| grep "0 1 $pcphr3 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp3

# 3-hr prob of pcp > 0.25
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 101 153 154"| grep "0 1 $pcphr3 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp4

# 3-hr prob of pcp > 0.50
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 203 51 51"| grep "0 1 $pcphr3 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp5

if [ $ffhr -gt 5 ]; then
# 6-hr prob of pcp > 0.01
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 64 64 0 0"| grep "0 1 $pcphr6 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp6

# 6-hr prob of pcp > 0.05
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 20 81 236"| grep "0 1 $pcphr6 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp7

# 6-hr prob of pcp > 0.10
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 40 163 215"| grep "0 1 $pcphr6 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp8

# 6-hr prob of pcp > 0.25
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 101 153 154"| grep "0 1 $pcphr6 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp9

# 6-hr prob of pcp > 0.50
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 203 51 51"| grep "0 1 $pcphr6 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp10
fi

if [ $ffhr -gt 11 ]; then
# 12-hr prob of pcp > 0.01
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 64 64 0 0"| grep "0 1 $pcphr12 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp11

# 12-hr prob of pcp > 0.05
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 20 81 236"| grep "0 1 $pcphr12 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp12

# 12-hr prob of pcp > 0.10
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 40 163 215"| grep "0 1 $pcphr12 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp13

# 12-hr prob of pcp > 0.25
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 101 153 154"| grep "0 1 $pcphr12 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp14

# 12-hr prob of pcp > 0.50
$utilexec/wgrib -PDS10 SREFPROB |grep "2 0 0 0 0 65 203 51 51"| grep "0 1 $pcphr12 $pcphr 4"|$utilexec/wgrib -i -grib -o dump SREFPROB
mv dump srefpcp15
fi

if [ $ffhr -gt 11 ]; then
cat srefpcp1 srefpcp2 srefpcp3 srefpcp4 srefpcp5 srefpcp6 srefpcp7 srefpcp8 srefpcp9 srefpcp10 srefpcp11 srefpcp12 srefpcp13 srefpcp14 srefpcp15 > srefallpcp
elif [ $ffhr -gt 5 ]; then
cat srefpcp1 srefpcp2 srefpcp3 srefpcp4 srefpcp5 srefpcp6 srefpcp7 srefpcp8 srefpcp9 srefpcp10 > srefallpcp
elif [ $ffhr -gt 1 ]; then
cat srefpcp1 srefpcp2 srefpcp3 srefpcp4 srefpcp5 > srefallpcp
fi

grid="255 3 2145 1377 20192 238446 8 265000 2540 2540 0 64 25000 25000"
$utilexec/copygb -g "$grid" -x srefallpcp srefpcpcon_${SREF_PDY}${srefcyc}f0${pcphrl}
$utilexec/grbindex srefpcpcon_${SREF_PDY}${srefcyc}f0${pcphrl} srefpcpconi_${SREF_PDY}${srefcyc}f0${pcphrl}
fi

let ffhr1=ffhr-1
let ffhr2=ffhr-2

if [ $ffhr -gt 0 ]; then
hours="${ffhr2} ${ffhr1} ${ffhr}"
else
hours="00"
fi

cp $FIXruc2/ruc2_ndfd_elevtiles.ndfd2.5.grb TOPONDFD
cp $FIXruc2/ruc2_ndfd_elevtiles.ndfd2.5.grbi TOPONDFDI
cp $FIXruc2/ruc2_ndfd_vegtiles.ndfd2.5.grb VEGNDFD
cp $FIXruc2/ruc2_ndfd_vegtiles.ndfd2.5.grbi VEGNDFDI

for fhr in $hours; do

  let check3=fhr%3
  let check6=fhr%6
  let fhr1=fhr-1
  let fhr2=fhr-2
  let fhr3=fhr-3
  let fhr6=fhr-6
  let fhr9=fhr-9

  typeset -Z2 fhr3
  typeset -Z2 fhr1
  typeset -Z2 fhr2
  typeset -Z2 fhr6
  typeset -Z2 fhr9
  typeset -Z2 fhr

  cp $COMIN/nam.t${cyc}z.conusnest.bsmart${fhr}.tm00 WRFPRS${fhr}.tm00
  $utilexec/grbindex WRFPRS${fhr}.tm00 WRFPRS${fhr}i.tm00
  cp $PARMnam/nam_smartmasterconusnest2p5.ctl master${fhr}.ctl

# all nest files contain 3-hr precip accumulations
#  # if the forecast hour is valid at 00/12Z, we need to make 12-hr
#  accumulations.

if [ $fhr -eq 12 -o $fhr -eq 24 -o $fhr -eq 36 -o $fhr -eq 48 -o \
       $fhr -eq 60 ]; then
  echo 'running addprecip12'
  cp ${COMIN}/nam.t${cyc}z.conusnest.bsmart${fhr3}.tm00 WRFPRS${fhr3}.tm00
  /nwprod/util/exec/grbindex WRFPRS${fhr3}.tm00 WRFPRS${fhr3}i.tm00
  cp ${COMIN}/nam.t${cyc}z.conusnest.bsmart${fhr6}.tm00 WRFPRS${fhr6}.tm00
  /nwprod/util/exec/grbindex WRFPRS${fhr6}.tm00 WRFPRS${fhr6}i.tm00
  cp ${COMIN}/nam.t${cyc}z.conusnest.bsmart${fhr9}.tm00 WRFPRS${fhr9}.tm00
  /nwprod/util/exec/grbindex WRFPRS${fhr9}.tm00 WRFPRS${fhr9}i.tm00

    export XLFUNIT_13="WRFPRS${fhr9}.tm00"
    export XLFUNIT_14="WRFPRS${fhr9}i.tm00"
    export XLFUNIT_15="WRFPRS${fhr6}.tm00"
    export XLFUNIT_16="WRFPRS${fhr6}i.tm00"
    export XLFUNIT_17="WRFPRS${fhr3}.tm00"
    export XLFUNIT_18="WRFPRS${fhr3}i.tm00"
    export XLFUNIT_19="WRFPRS${fhr}.tm00"
    export XLFUNIT_20="WRFPRS${fhr}i.tm00"
    export XLFUNIT_50="12precip.${fhr}"
    export XLFUNIT_51="12cprecip.${fhr}"
    export XLFUNIT_52="12snow.${fhr}"
    $EXECnam/nam_smartaddprecip12_conusnest <<EOF >> addprecip12.out
$fhr9 $fhr6 $fhr3 $fhr
EOF

/nwprod/util/exec/copygb -g "184" -i3 -x 12precip.${fhr} 12precip
/nwprod/util/exec/grbindex 12precip 12precipi
/nwprod/util/exec/copygb -g "184" -i3 -x 12snow.${fhr} 12snow
/nwprod/util/exec/grbindex 12snow 12snowi
fi

# at all 6-hour intervals, we need 6 hour buckets as well
if [ $check6 -eq 0 -a $fhr -ne 0 ] ; then
 echo 'need 6-hr buckets'

 cp ${COMIN}/nam.t${cyc}z.conusnest.bsmart${fhr3}.tm00 WRFPRS${fhr3}.tm00
 /nwprod/util/exec/grbindex WRFPRS${fhr3}.tm00 WRFPRS${fhr3}i.tm00

    export XLFUNIT_13="WRFPRS${fhr3}.tm00"
    export XLFUNIT_14="WRFPRS${fhr3}i.tm00"
    export XLFUNIT_15="WRFPRS${fhr}.tm00"
    export XLFUNIT_16="WRFPRS${fhr}i.tm00"
    export XLFUNIT_50="6precip.${fhr}"
    export XLFUNIT_51="6cprecip.${fhr}"
    export XLFUNIT_52="6snow.${fhr}"
    $EXECnam/nam_smartaddprecip6_conusnest <<EOF >> addprecip6.out
$fhr3 $fhr
EOF

/nwprod/util/exec/copygb -g "184" -i3 -x 6precip.${fhr} 6precip
/nwprod/util/exec/grbindex 6precip 6precipi
/nwprod/util/exec/copygb -g "184" -i3 -x 6snow.${fhr} 6snow
/nwprod/util/exec/grbindex 6snow 6snowi
fi

cat >input${fhr}.prd <<EOF5
WRFPRS${fhr}.tm00
EOF5

  export pgm=nam_prdgen;. prep_step 
  ln -sf master${fhr}.ctl                 fort.10
  ln -sf $FIXnam/nam_wgt_184_conusnest    fort.21
  ln -sf $PARMnam/nam_kwbx.tbl       fort.41
  ln -sf $PARMnam/nam_time.tbl       fort.42
  ln -sf $PARMnam/nam_parm.tbl       fort.43
  ln -sf $PARMnam/nam_grid.tbl       fort.44
  ln -sf $PARMnam/nam_levl.tbl       fort.45
  $EXECnam/nam_prdgen < input${fhr}.prd > prdgen.out${fhr}
   export err=$?;err_chk

    cp /com/date/t${cyc}z DATE
    mv mesocon.NDFD mesocon.NDFDf${fhr}
    $utilexec/grbindex mesocon.NDFDf${fhr} mesocon.NDFDif${fhr}

# at 00/12z valid times, we need to make 12-hr max/min temps
#   we also need 6 and 12-hr buckets; the max/min values from
#   tm6, tm9, and tm12 are in the smartconus files in /com

  if [ $fhr -eq 12 -o $fhr -eq 24 -o $fhr -eq 36 -o $fhr -eq 48 -o \
       $fhr -eq 60 ]; then
    cp srefpcpcon_${SREF_PDY}${srefcyc}f0${pcphrl} SREFPCP
    cp srefpcpconi_${SREF_PDY}${srefcyc}f0${pcphrl} SREFPCPi

    cp MAXMIN${fhr2}.tm00 MAXMIN2
    cp MAXMIN${fhr1}.tm00 MAXMIN1
    cp $COMOUT/nam.t${cyc}z.smartconus2p5${fhr3}.tm00 MAXMIN3
    cp $COMOUT/nam.t${cyc}z.smartconus2p5${fhr6}.tm00 MAXMIN4
    cp $COMOUT/nam.t${cyc}z.smartconus2p5${fhr9}.tm00 MAXMIN5

    /nwprod/util/exec/grbindex MAXMIN1 MAXMIN1i
    /nwprod/util/exec/grbindex MAXMIN2 MAXMIN2i
    /nwprod/util/exec/grbindex MAXMIN3 MAXMIN3i
    /nwprod/util/exec/grbindex MAXMIN4 MAXMIN4i
    /nwprod/util/exec/grbindex MAXMIN5 MAXMIN5i

    export pgm=nam_smartinit_conusnest2p5;. prep_step
    export XLFUNIT_11="mesocon.NDFDf${fhr}"
    export XLFUNIT_12="mesocon.NDFDif${fhr}"
    export XLFUNIT_13="SREFPCP"
    export XLFUNIT_14="SREFPCPi"
    export XLFUNIT_15="6precip"
    export XLFUNIT_16="6precipi"
    export XLFUNIT_17="12precip"
    export XLFUNIT_18="12precipi"
    export XLFUNIT_19="6snow"
    export XLFUNIT_20="6snowi"
    export XLFUNIT_21="12snow"
    export XLFUNIT_22="12snowi" 
    export XLFUNIT_23="MAXMIN1"
    export XLFUNIT_24="MAXMIN2"
    export XLFUNIT_25="MAXMIN3"
    export XLFUNIT_26="MAXMIN4"
    export XLFUNIT_27="MAXMIN5"
    export XLFUNIT_28="MAXMIN1i"
    export XLFUNIT_29="MAXMIN2i"
    export XLFUNIT_30="MAXMIN3i"
    export XLFUNIT_31="MAXMIN4i"
    export XLFUNIT_32="MAXMIN5i"
    export XLFUNIT_46="TOPONDFD"
    export XLFUNIT_47="TOPONDFDI"
    export XLFUNIT_48="VEGNDFD"
    export XLFUNIT_49="VEGNDFDI"
    $EXECnam/nam_smartinit_conusnest2p5 <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
export err=$?;err_chk

# add processing for conversion to grib2 and awips files
$utilexec/cnvgrib -g12 -p40 MESOCS${fhr}.tm00 nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
# Processing grids for AWIPS
pgm=tocgrib2
export pgm;. prep_step
startmsg

export XLFUNIT_11=nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
export XLFUNIT_31=" "
export XLFUNIT_51=grib2.t${cyc}z.smartconus2p5f${fhr}

$utilexec/tocgrib2 <$UTILparm/grib2_awpnamdngconusonf${fhr}.184 >> $pgmout 2> errfile
echo " error from tocgrib=",$err 

mv MESOCS${fhr}.tm00 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00
mv nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2 
mv grib2.t${cyc}z.smartconus2p5f${fhr} $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
if [ $SENDDBN = YES ]
 then
  $DBNROOT/bin/dbn_alert NTC_LOW SMARTCONUS2P5 $job $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
fi

if [ $SENDDBN_GB2 = YES ]
 then
  $DBNROOT/bin/dbn_alert MODEL NAM_SMARTCONUS2P5_GB2_PARA $job $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2 
fi

  # for all forecast hours divisible by 3 except for (3,15,27....), we need
  #    3-hr buckets and max/min temp data for the previous 2 hours

  elif [ $check3 -eq 0 -a $fhr -ne 00 -a $fhr -ne 03 -a $fhr -ne 15 -a $fhr -ne 27 -a \
       $fhr -ne 39 -a $fhr -ne 51 ] ; then

    cp srefpcpcon_${SREF_PDY}${srefcyc}f0${pcphrl} SREFPCP
    cp srefpcpconi_${SREF_PDY}${srefcyc}f0${pcphrl} SREFPCPi
    cp MAXMIN${fhr2}.tm00 MAXMIN2
    cp MAXMIN${fhr1}.tm00 MAXMIN1

    $utilexec/grbindex MAXMIN2 MAXMIN2i
    $utilexec/grbindex MAXMIN1 MAXMIN1i

    export pgm=nam_smartinit_conusnest2p5;. prep_step
    export XLFUNIT_11="mesocon.NDFDf${fhr}"
    export XLFUNIT_12="mesocon.NDFDif${fhr}"
    export XLFUNIT_13="SREFPCP"
    export XLFUNIT_14="SREFPCPi"
    export XLFUNIT_15="6precip"
    export XLFUNIT_16="6precipi"
    export XLFUNIT_17="6snow"
    export XLFUNIT_18="6snowi"
    export XLFUNIT_19="MAXMIN2"
    export XLFUNIT_20="MAXMIN1"
    export XLFUNIT_21="MAXMIN2i"
    export XLFUNIT_22="MAXMIN1i"
    export XLFUNIT_46="TOPONDFD"
    export XLFUNIT_47="TOPONDFDI"
    export XLFUNIT_48="VEGNDFD"
    export XLFUNIT_49="VEGNDFDI"
    $EXECnam/nam_smartinit_conusnest2p5 <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
export err=$?;err_chk

#add processing for conversion to grib2 and awips files
$utilexec/cnvgrib -g12 -p40 MESOCS${fhr}.tm00 nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
# Processing grids for AWIPS
pgm=tocgrib2
export pgm;. prep_step
startmsg

export XLFUNIT_11=nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
export XLFUNIT_31=" "
export XLFUNIT_51=grib2.t${cyc}z.smartconus2p5f${fhr}

$utilexec/tocgrib2 <$UTILparm/grib2_awpnamdngconusonf${fhr}.184 >> $pgmout 2> errfile
echo " error from tocgrib=",$err

mv MESOCS${fhr}.tm00 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00
mv nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
mv grib2.t${cyc}z.smartconus2p5f${fhr} $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
if [ $SENDDBN = YES ] 
 then
  $DBNROOT/bin/dbn_alert NTC_LOW SMARTCONUS2P5 $job $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
fi

if [ $SENDDBN_GB2 = YES ]
 then
  $DBNROOT/bin/dbn_alert MODEL NAM_SMARTCONUS2P5_GB2_PARA $job $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
fi

#  for forecast hours divisible by 3, we need only max/min temp
#   data for the previous 2 hours

  elif [ $check3 -eq 0 -a ${fhr} -ne 0 ]; then
    cp srefpcpcon_${SREF_PDY}${srefcyc}f0${pcphrl} SREFPCP
    cp srefpcpconi_${SREF_PDY}${srefcyc}f0${pcphrl} SREFPCPi
    cp MAXMIN${fhr2}.tm00 MAXMIN2
    cp MAXMIN${fhr1}.tm00 MAXMIN1

    $utilexec/grbindex MAXMIN2 MAXMIN2i
    $utilexec/grbindex MAXMIN1 MAXMIN1i

    export pgm=nam_smartinit_conusnest2p5;. prep_step 
    export XLFUNIT_11="mesocon.NDFDf${fhr}"
    export XLFUNIT_12="mesocon.NDFDif${fhr}"
    export XLFUNIT_13="SREFPCP"
    export XLFUNIT_14="SREFPCPi"
    export XLFUNIT_15="MAXMIN2"
    export XLFUNIT_16="MAXMIN1"
    export XLFUNIT_17="MAXMIN2i"
    export XLFUNIT_18="MAXMIN1i"
    export XLFUNIT_46="TOPONDFD"
    export XLFUNIT_47="TOPONDFDI"
    export XLFUNIT_48="VEGNDFD"
    export XLFUNIT_49="VEGNDFDI"
    $EXECnam/nam_smartinit_conusnest2p5 <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
  export err=$?;err_chk
  
# add processing for conversion to grib2 and awips files
$utilexec/cnvgrib -g12 -p40 MESOCS${fhr}.tm00 nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
# Processing grids for AWIPS
pgm=tocgrib2
export pgm;. prep_step
startmsg

export XLFUNIT_11=nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
export XLFUNIT_31=" "
export XLFUNIT_51=grib2.t${cyc}z.smartconus2p5f${fhr}

$utilexec/tocgrib2 <$UTILparm/grib2_awpnamdngconusonf${fhr}.184 >> $pgmout 2> errfile
echo " error from tocgrib=",$err

mv MESOCS${fhr}.tm00 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00
mv nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
mv grib2.t${cyc}z.smartconus2p5f${fhr} $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
if [ $SENDDBN = YES ] 
 then
  $DBNROOT/bin/dbn_alert NTC_LOW SMARTCONUS2P5 $job $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
fi

if [ $SENDDBN_GB2 = YES ]
 then
  $DBNROOT/bin/dbn_alert MODEL NAM_SMARTCONUS2P5_GB2_PARA $job $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
fi

  #  for all "in-between" forecast hours (13,14,16....), we don't need
  #    any special data
  else 
    export pgm=nam_smartinit_conusnest2p5;. prep_step
    export XLFUNIT_11="mesocon.NDFDf${fhr}"
    export XLFUNIT_12="mesocon.NDFDif${fhr}"
    export XLFUNIT_46="TOPONDFD"
    export XLFUNIT_47="TOPONDFDI"
    export XLFUNIT_48="VEGNDFD"
    export XLFUNIT_49="VEGNDFDI"
    $EXECnam/nam_smartinit_conusnest2p5 <<EOF >> smartinit.out${fhr}
$fhr
$cyc
EOF
export err=$?;err_chk
  if [ $fhr -eq 0 ] ; then
# add processing for conversion to grib2 and awips files
   $utilexec/cnvgrib -g12 -p40 MESOCS${fhr}.tm00 nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
# Processing grids for AWIPS
  pgm=tocgrib2
  export pgm;. prep_step
  startmsg

  export XLFUNIT_11=nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
  export XLFUNIT_31=" "
  export XLFUNIT_51=grib2.t${cyc}z.smartconus2p5f${fhr}

  $utilexec/tocgrib2 <$UTILparm/grib2_awpnamdngconusonf${fhr}.184 >> $pgmout 2> errfile
  echo " error from tocgrib=",$err

   mv MESOCS${fhr}.tm00 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00
   mv nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2 $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
   mv grib2.t${cyc}z.smartconus2p5f${fhr} $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
   if [ $SENDDBN = YES ] 
    then
     $DBNROOT/bin/dbn_alert NTC_LOW SMARTCONUS2P5 $job $pcom/grib2.awpnamsmart.conus2p5${fhr}_awips_f${fhr}_${cyc}
   fi

   if [ $SENDDBN_GB2 = YES ]
    then
     $DBNROOT/bin/dbn_alert MODEL NAM_SMARTCONUS2P5_GB2_PARA $job $COMOUT/nam.t${cyc}z.smartconus2p5${fhr}.tm00.grib2
   fi
  fi
  fi
done
exit
