#!/bin/ksh
set -x

################################################################################
# Script: ngac_post.sh
# Purpose: run NCEP Post-processor to generate NGAC products
# Auther: Sarah Lu
#
# Revision Log
#    2011-12-19 Sarah Lu,  Initial script 
#    2011-12-22 Sarah Lu,  Modified to have 2d and 3d options
#    2012-01-16 Sarah Lu,  Modified to read single parm file
#    2012-01-19 Sarah Lu,  Modified to import FH only
#    2012-01-30 Jun Wang,  Add option to run grib2
#    2012-02-21 Sarah LU,  Add POST_NCEPGRB2TBL
#    2012-03-08 Sarah LU,  Add OPT_LUMP
#    2012-06-18 Sarah LU,  Modified history file names
################################################################################

if [ $# -lt 1 ] ; then
        $DATA/err_exit "`date` $0: argument error"
fi

#######################################################
# Incoming parameters
#######################################################

FH=${1:-$FH}

#  Set environment.
export MP_LABELIO=yes

###  check NGAC history files
if eval test -f $SIGO${FH} -a -f $AERO${FH}
then
  msg="Post Starting with NGAC SIG/AER Files"
  postmsg "$jlogfile" "$msg"
else
  msg="NGAC SIG/AER Files Not Available/Rerun JNGAC_FCST"
  postmsg "$jlogfile" "$msg"
  export err=1; err_chk
fi

###  determine time stamp
export NEWDATE=`$ndate ${FH} $CDATE`
export YY=`echo $NEWDATE | cut -c1-4`
export MM=`echo $NEWDATE | cut -c5-6`
export DD=`echo $NEWDATE | cut -c7-8`
export HH=`echo $NEWDATE | cut -c9-10`

cd $DATA

cat > itag <<EOF
$SIGO${FH}
binarynemsio
${POST_GRIBVERSION}
${YY}-${MM}-${DD}_${HH}:00:00
GFS
$FLXO${FH}
$D3DO${FH}
 &NAMPGB
 fileNameAER='$AERO${FH}',
/
EOF

## Link parm files
if [ $POST_GRIBVERSION = grib1 ]; then
  ln -sf ${POSTCTLFILE} fort.14
elif [ $POST_GRIBVERSION = grib2 ]; then
  ln -sf ${POST_PARM}        postcntrl.xml
  ln -sf ${POST_AVBLFLDSXML} post_avblflds.xml
  ln -sf ${POST_NCEPGRB2TBL} params_grib2_tbl_new
fi
ln -sf griddef.out fort.110
cp /nwprod/parm/nam_micro_lookup.dat ./eta_micro_lookup.dat

## run ncep post
$POSTEXEC  < itag > outpost_nems_${NEWDATE}
export err=$?; err_chk



