#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         nam_prdgen1.sh
# Script description:  Run nam product generator jobs
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Abstract: This script runs the Nam PRDGEN jobs
#
# Script history log:
# 1999-06-23  Eric Rogers
# 1999-08-25  Brent Gordon  Pulled the PRDGEN here file out of the post
#                           jobs into this script.
# 2001-10-23  Eric Rogers   Changes for nam-12
#                           - Use one master control file only 
#                           - Split up prdgen into 4 separate jobs for
#                             timely nam-12 output. This job creates
#                             files AWP207 (#207), AWP211 (#211), 
#                             AWP217 (#217), AWIP32 (#221), and 221 tiles 
# 2003-03-18  Eric Rogers   Changed script to process special hourly
#                           output if variable hswitch is not set to zero 
# 2010-03-03  Eric Rogers   Version for nested NAM output
#

set -x

export CNVGRIB=/nwprod/util/exec/cnvgrib
export WGRIB2=/nwprod/util/exec/wgrib2
export WGRIB=/nwprod/util/exec/wgrib

export fhr=$1
export hswitch=$2
export PS4='PRDGEN${fhr}_T$SECONDS + '

DOMAIN=`echo $domain | tr '[a-z]' '[A-Z]'`

cd $DATA/prdgen_${domain}_${fhr}
sh $utilscript/setup.sh

if [ $domain = firewx ] 
then

if [ $firewx_location = alaska ]
then
 regtag=ALASKA
else
 regtag=CONUS
fi

cat > itagfw <<EOF
$regtag
EOF

export pgm=nam_firewx_gridspecs
. prep_step

export XLFUNIT_11=$COMOUT/nam.t${cyc}z.latlons_corners_firewxnest.txt.f${fhr}

startmsg
$EXECnam/nam_firewx_gridspecs < itagfw >> $pgmout 2>errfile
export err=$?;err_chk

gridspecs=`head copygb_gridnavfw.txt`

COPYGB=${COPYGB:-$utilexec/copygb}
$COPYGB -g "${gridspecs}" -i2 -x $DATA/${domain}.BGDAWP${fhr}.tm00 nam.HIRESF${fhr}

mv copygb_gridnavfw.txt $COMOUT/nam.t${cyc}z.copygb_gridnavfw.txt

else

cat >input_${domain}_${fhr}.prd <<EOF5
$DATA/${domain}.BGDAWP${fhr}.tm00
EOF5

export pgm=nam_prdgen;. prep_step
export XLFUNIT_10="master1.${fhr}.ctl"
export XLFUNIT_21="$FIXnam/nam_wgt_${domain}nest"
export XLFUNIT_41="$PARMnam/nam_kwbx.tbl"
export XLFUNIT_42="$PARMnam/nam_time.tbl"
export XLFUNIT_43="$PARMnam/nam_parm.tbl"
export XLFUNIT_44="$PARMnam/nam_grid.tbl"
export XLFUNIT_45="$PARMnam/nam_levl.tbl"
$EXECnam/nam_prdgen < input_${domain}_${fhr}.prd > $DATA/prdgen_${domain}.out${fhr}
export err=$?;err_chk

#end domain test
fi

if test $SENDCOM = 'YES'
then

 $utilexec/grbindex nam.HIRESF${fhr} HIRESFi${fhr}

 $CNVGRIB -g12 -p40 nam.HIRESF${fhr} nam.HIRESF${fhr}.grib2
 $WGRIB2 nam.HIRESF${fhr}.grib2 -s >nam.HIRESF${fhr}.grib2.idx
 mv nam.HIRESF${fhr}.grib2 $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00.grib2
 mv nam.HIRESF${fhr}.grib2.idx $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00.grib2.idx

 if [ $domain = conus ]
 then
  $WGRIB -s nam.HIRESF${fhr} | grep -f $utilfix_nam/nam_conusnest.parmlist_nos|$WGRIB -i -grib -o nam.HIRESF${fhr}_nos nam.HIRESF${fhr}
  $CNVGRIB -g12 -p40 nam.HIRESF${fhr}_nos nam.HIRESF${fhr}.grib2_nos
  mv nam.HIRESF${fhr}_nos $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00_nos
  mv nam.HIRESF${fhr}.grib2_nos $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00.grib2_nos
 fi

 mv nam.HIRESF${fhr} $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00
 mv HIRESFi${fhr} $COMOUT/${RUN}.${cycle}.${domain}nest.hiresfi${fhr}

 if [ $domain = firewx ] 
 then
   $USHnam/nam_nawips_firewx.sh ${fhr}
 fi

 if test $SENDDBN = 'YES'
 then
  #$DBNROOT/bin/dbn_alert MODEL NAM_${DOMAIN}NEST_HIRES $job $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00
  #$DBNROOT/bin/dbn_alert MODEL NAM_${DOMAIN}NEST_HIRESI $job $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00
  $DBNROOT/bin/dbn_alert MODEL NAM_${DOMAIN}NEST_GB2 $job $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00.grib2
  $DBNROOT/bin/dbn_alert MODEL NAM_${DOMAIN}NEST_GB2_WIDX $job $COMOUT/${RUN}.${cycle}.${domain}nest.hiresf${fhr}.tm00.grib2.idx
 fi

fi

if test "$SENDSMS" = 'YES'
then
  $SMSBIN/setev post_${domain}_complete
fi

echo EXITING $0
exit
