#!/bin/ksh
#
set -x
#--------------------------------------------------------
# Updates:
#
# August 2005: Hui-Ya Chuang, NCEP: This script uses 
# NCEP's Unipost to post processes WRF native model 
# output, and uses copygb to horizontally interpolate posted 
# output from native A-E to a regular projection grid. 
#
# July 2006: Meral Demirtas, NCAR/DTC: Added new "copygb" 
# options and revised some parts for clarity. 
#
# April 2015: Modified to run NMM-B/NEMS, KRF(DTC)
#
# January 2019: Modified to remove NMMB/NMM and add FV3, TH (DTC)
#--------------------------------------------------------
# This script performs 2 jobs:
#
# 1. Run Unipost
# 2. Run copygb to horizontally interpolate output from 
#    native A-E to a user-defined projection grid (optional)
#--------------------------------------------------------

#----------------------------------------------------------------------------------
#--- USER EDIT DESCIPTIONS --------------------------------------------------------
# See UPP User's Guide for more information
# http://www.dtcenter.org/upp/users/docs/user_guide/V4/upp_users_guide.pdf 
#----------------------------------------------------------------------------------
# TOP_DIR       : Top level directory for source codes (UPPV4.0)
# DOMAINPATH    : Working directory for this run.
# UNIPOST_HOME  : Where the UPP build directory located
# POSTEXEC      : Where the UPP executables are located
# SCRIPTS       : Where the UPP scripts directory is (i.e. UPPV4.0/scripts/)
# modelDataPath : Where are the model data files to be processed located
#                 e.g. "wrfprd/" for WRF-based runs
#                 FV3 model filename examples (may need to alter in script) 
#                 inFileName=dynf012.nemsio (default) or gfs.t00z.atmf012.nemsio
#                 flxFileName=phyf012.nemsio (default) or gfs.t00z.sfcf012.nemsio
# paramFile     : Name and location of cntrl.parm file (wrf_cntrl.parm)
#                 Text file lists desired fields for grib1 output. Template in UPPV4.0/parm/
# txtCntrlFile  : Name and location of post flat file (postxconfig-NT.txt) for grib2
#                 Text file listing desired fields to be generated by the user before running UPP.
#                 Step 1: Edit postcntrl.xml to include desired fields (template in UPPV4.0/parm)
#                 Step 2: Validate postcntrl.xml and post_avblflds.xml
#                 Step 3: Type 'make' in parm directory to generate the post flat file
# dyncore       : What model is used ARW (WRF) or FV3 (GFS)
# inFormat      : Format of the model data 
#                 arw - "netcdf"
#                 fv3 - "binarynemsio"
# outFormat     : Format of output from UPP 
#                 grib (WRF only)
#                 grib2 
# startdate     : Forecast start date (YYYYMMDDHH)
# fhr           : First forecast hour to be post-processed
# lastfhr       : Last forecast hour to be post-processed
# incrementhr   : Increment (in hours) between forecast files
#                 * Do not set to 0 or the script will loop continuously *
# domain_list   : List of domains for run
# RUN_COMMAND   : System run command for serial or parallel runs, examples below.
# copygb_opt    : Copygb grid specfication option to regrid (optional)
#                 "awips"   = Use a predefined awips grid, e.g. 212
#                             ** Uncomment "export awips_id= " and add desired grid number.
#                 "custom"  = Specify your own grid  
#                             ** Uncomment "export custom_gds= " and add grid description.
#
#----------------------------------------------------------------------------------
#--- BEGIN USER EDIT HERE ---------------------------------------------------------
#----------------------------------------------------------------------------------

# Set relevant paths and data information
# This script assumes you created a directory $DOMAINPATH/postprd
# and that your model output is in $DOMAINPATH/wrfprd
# as recommended in the users guide where UPP will output.
export TOP_DIR=/home/username
export DOMAINPATH=${TOP_DIR}/DOMAINS/test_case
export UNIPOST_HOME=${TOP_DIR}/UPPV4.0
export POSTEXEC=${UNIPOST_HOME}/bin
export SCRIPTS=${UNIPOST_HOME}/scripts
export modelDataPath=${DOMAINPATH}/wrfprd
export paramFile=${DOMAINPATH}/parm/wrf_cntrl.parm # grib1 (WRF only)
export txtCntrlFile=${DOMAINPATH}/parm/postxconfig-NT-WRF.txt # grib2 (or postxconfig-NT-GFS.txt)

# Specify Dyn Core (ARW or FV3 in upper case)
export dyncore="ARW"

# Set input format from model and ouput format from unipost
export inFormat="netcdf"
export outFormat="grib2"

# Set date/time information
export startdate=2014020412
export fhr=00
export lastfhr=06
export incrementhr=03

# Set domain lists
export domain_list="d01"

# Set run command: 

# Serial command example
export RUN_COMMAND="${POSTEXEC}/unipost.exe "

# Parallel command examples:
#export RUN_COMMAND="mpirun -np 1 ${POSTEXEC}/unipost.exe "
#export RUN_COMMAND="mpirun.lsf ${POSTEXEC}/unipost.exe "
#export RUN_COMMAND="mpiexec_mpt ${POSTEXEC}/unipost.exe "

# DEBUG command example found further below, search "DEBUG" 

# Optional: Specify COPYGB grid spec for regridding
# (options are awips or custom)
#export copygb_opt="custom"

# If using awips grid, uncomment awips_id and modify to desired grid
# If familiar with copygb and selecting custom, uncomment custom_gds
# and modify to fit your needs. Use at own risk!
#export awips_id="212" 
#export custom_gds="255 3 109 91 37748 -77613 8 -71000 10379 9900 0 64 42000 42000"

# Shouldn't need to edit these.
# tmmark is an variable used as the file extention of the output
# filename .GrbF is used if this variable is not set
# COMSP is a variable used as the initial string of the output filename
export tmmark=tm00
export MP_SHARED_MEMORY=yes
export MP_LABELIO=yes

#----------------------------------------------------------------------
#--- END USER EDIT ----------------------------------------------------
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# Shouldn't need to edit below unless something goes wrong or debugging
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# Do some checks for directory/executable existence, user input, etc.
#----------------------------------------------------------------------
if [ ! -d ${POSTEXEC} ]; then
  echo "ERROR: POSTEXEC, '${POSTEXEC}', does not exist"
  exit 1
fi

if [ ! -x ${POSTEXEC}/unipost.exe ]; then
  echo "ERROR: unipost.exe, '${POSTEXEC}/unipost.exe', does not exist or is not executable."
  exit 1
fi
if [ ! -x ${POSTEXEC}/copygb.exe ]; then
  echo "ERROR: copygb.exe, '${POSTEXEC}/copygb.exe', does not exist or is not executable."
  exit 1
fi
if [ ! -x ${POSTEXEC}/ndate.exe ]; then
  echo "ERROR: ndate.exe, '${POSTEXEC}/ndate.exe', does not exist or is not executable."
  exit 1
fi

# Set tag based on user defined $dyncore (ARW or FV3 in upper case)
if [ $dyncore = "ARW" ]; then
   export tag=NCAR
elif [ $dyncore = "FV3" ]; then
   export tag=GFS
else
    echo "${dyncore} is not supported. Edit script to choose ARW or FV3 dyncore."
    exit
fi

if [[ ${dyncore} == "ARW" ]]; then
   if [[ ${inFormat} != "netcdf" ]]; then
      echo "ERROR: 'inFormat' must be 'netcdf' for ARW model output. Exiting... "
      exit 1
   fi 
elif [ ${dyncore} == "FV3" ]; then
   if [[ ${inFormat} != "binarynemsio" ]]; then
      echo "ERROR: 'inFormat' must be 'binarynemsio' for FV3 model output. Exiting... "
      exit 1
   fi
fi

if [[ ${outFormat} == "grib" ]]; then
   if [ ! -e ${paramFile} ]; then
      echo "ERROR: 'paramFile' not found in '${paramFile}'.  Exiting... "
      exit 1
   fi
elif [[ ${outFormat} == "grib2" ]]; then
   if [ ! -e ${txtCntrlFile} ]; then
      echo "ERROR: 'txtCntrlFile' not found in '${txtCntrlFile}'.  Exiting... "
      exit 1
   fi
fi
 
if [ ! -d ${DOMAINPATH}/postprd ]; then
  echo "ERROR: DOMAINPATH/postprd, '${DOMAINPATH}/postprd', does not exist. Exiting..."
  exit 1
fi

if [ ${incrementhr} -eq 0 ]; then
  echo "ERROR: increment hour (incrementhr) cannot be zero. Inifinite loop will result. Please modify. Exiting..."
  exit 1
fi

if [ ${copygb_opt} == 'awips' ]; then
   if [ -z ${awips_id} ]; then
      echo "ERROR: copygb_opt = '${copygb_opt}', must uncomment and set 'awips_id'. Exiting..."
      exit 1
   fi
fi

if [ ${copygb_opt} == 'custom' ]; then
   if [ -z ${custom_gds} ]; then
      echo "ERROR: copygb_opt = '${copygb_opt}', must uncomment and set 'custom_gds'. Exiting..."
      exit 1
   fi
fi

#----------------------------------------------------------------------
# End checks of user input
#----------------------------------------------------------------------

#----------------------------------------------------------------------
#  Begin work
#----------------------------------------------------------------------

# cd to working directory
cd ${DOMAINPATH}/postprd
err1=$?
if test "$err1" -ne 0; then
    echo "ERROR: Could not 'cd' to working directory. Did you create directory: '${DOMAINPATH}/postprd'?  \
    Does '${DOMAINPATH}' exist?  Exiting... "
    exit 1
fi

# Get local copy of parm file
# For GRIB1 the code uses wrf_cntrl.parm to select variables for output
#   the available fields are set at compilation
if [[ ${outFormat} == "grib" ]]; then
    if [[ ${dyncore} == "ARW" ]]; then
	ln -fs ${paramFile} wrf_cntrl.parm
    elif [[ ${dyncore} == "FV3" ]]; then
	echo "ERROR: FV3 not available for grib1 output. Use GRIB2 output. Exiting..."
	exit 1
    fi
elif [[ ${outFormat} == "grib2" ]]; then
# For GRIB2 the code reads postxconfig-NT.txt to select variables for output
#   the available fields are defined in post_avlbflds.xml -- while we
#   set a link to this file for reading during runtime it is not typical
#   for one to update this file, therefore the link goes back to the
#   program directory - this is true for params_grib2_tbl_new also - a
#   file which defines the GRIB2 table values
ln -fs ${txtCntrlFile} postxconfig-NT.txt
ln -fs ${UNIPOST_HOME}/parm/post_avblflds.xml post_avblflds.xml
ln -fs ${UNIPOST_HOME}/src/lib/g2tmpl/params_grib2_tbl_new params_grib2_tbl_new
fi

# Link microphysics tables - code will use based on mp_physics option
# found in data
ln -fs ${UNIPOST_HOME}/parm/nam_micro_lookup.dat .
ln -fs ${UNIPOST_HOME}/parm/hires_micro_lookup.dat .

# link coefficients for crtm2 (simulated synthetic satellites)
CRTMDIR=${UNIPOST_HOME}/src/lib/crtm2/src/fix
ln -fs $CRTMDIR/EmisCoeff/IR_Water/Big_Endian/Nalli.IRwater.EmisCoeff.bin           ./
ln -fs $CRTMDIR/EmisCoeff/MW_Water/Big_Endian/FASTEM4.MWwater.EmisCoeff.bin           ./
ln -fs $CRTMDIR/EmisCoeff/MW_Water/Big_Endian/FASTEM5.MWwater.EmisCoeff.bin           ./
ln -fs $CRTMDIR/EmisCoeff/MW_Water/Big_Endian/FASTEM6.MWwater.EmisCoeff.bin           ./
ln -fs $CRTMDIR/EmisCoeff/IR_Land/SEcategory/Big_Endian/NPOESS.IRland.EmisCoeff.bin           ./
ln -fs $CRTMDIR/EmisCoeff/IR_Snow/SEcategory/Big_Endian/NPOESS.IRsnow.EmisCoeff.bin           ./
ln -fs $CRTMDIR/EmisCoeff/IR_Ice/SEcategory/Big_Endian/NPOESS.IRice.EmisCoeff.bin           ./
ln -fs $CRTMDIR/AerosolCoeff/Big_Endian/AerosolCoeff.bin     ./
ln -fs $CRTMDIR/CloudCoeff/Big_Endian/CloudCoeff.bin         ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/imgr_g11.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/imgr_g11.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/imgr_g12.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/imgr_g12.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/imgr_g13.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/imgr_g13.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/imgr_g15.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/imgr_g15.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/imgr_mt1r.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/imgr_mt1r.TauCoeff.bin    
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/imgr_mt2.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/imgr_mt2.TauCoeff.bin    
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/imgr_insat3d.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/imgr_insat3d.TauCoeff.bin    
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/amsre_aqua.SpcCoeff.bin  ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/amsre_aqua.TauCoeff.bin  ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/tmi_trmm.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/tmi_trmm.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmi_f13.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmi_f13.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmi_f14.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmi_f14.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmi_f15.SpcCoeff.bin    ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmi_f15.TauCoeff.bin    ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmis_f16.SpcCoeff.bin   ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmis_f16.TauCoeff.bin   ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmis_f17.SpcCoeff.bin   ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmis_f17.TauCoeff.bin   ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmis_f18.SpcCoeff.bin   ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmis_f18.TauCoeff.bin   ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmis_f19.SpcCoeff.bin   ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmis_f19.TauCoeff.bin   ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/ssmis_f20.SpcCoeff.bin   ./
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/ssmis_f20.TauCoeff.bin   ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/seviri_m10.SpcCoeff.bin   ./   
ln -fs $CRTMDIR/TauCoeff/ODPS/Big_Endian/seviri_m10.TauCoeff.bin   ./
ln -fs $CRTMDIR/SpcCoeff/Big_Endian/v.seviri_m10.SpcCoeff.bin   ./   

#######################################################
# 1. Run Unipost
#
# The Unipost is used to read native WRF model 
# output and put out isobaric state fields and derived fields.
#######################################################

export NEWDATE=$startdate

while [ $((10#${fhr})) -le $((10#${lastfhr})) ]; do

# Formatted fhr for filenames
fhr=`printf "%02i" ${fhr}`
fhour=`printf "%03i" ${fhr}`

NEWDATE=`${POSTEXEC}/ndate.exe +$((10#${fhr})) $startdate`

YY=`echo $NEWDATE | cut -c1-4`
MM=`echo $NEWDATE | cut -c5-6`
DD=`echo $NEWDATE | cut -c7-8`
HH=`echo $NEWDATE | cut -c9-10`
iHH=`echo $startdate | cut -c9-10`

echo 'NEWDATE' $NEWDATE
echo 'YY' $YY

# Begin looping through domains list
for domain in ${domain_list}; do

# Create model file name (inFileName)
dom_id=`echo "${domain}" | cut -d 'd' -f 2`
if [[ ${dyncore} == "ARW" ]]; then
    inFileName=${modelDataPath}/wrfout_d${dom_id}_${YY}-${MM}-${DD}_${HH}:00:00
elif [ ${dyncore} == "FV3" ]; then
    inFileName=${modelDataPath}/dynf${fhour}.nemsio
    flxFileName=${modelDataPath}/phyf${fhour}.nemsio
#    inFileName=${modelDataPath}/gfs.t${iHH}z.atmf${fhour}.nemsio
#    flxFileName=${modelDataPath}/gfs.t${iHH}z.sfcf${fhour}.nemsio
fi

# Check if the files exist
if [[ ! -e ${inFileName} ]]; then
  echo "ERROR: Can't find 'inFileName': ${inFileName}. Directory or file does not exist.  Exiting..."
  echo "ERROR: Check if 'modelDataPath': ${modelDataPath} exists."
  if [[ ${dyncore} == "ARW" ]]; then
     echo "ERROR: Check if file: 'wrfout_d${dom_id}_${YY}-${MM}-${DD}_${HH}:00:00' exists in modelDataPath."
  elif [ ${dyncore} == "FV3" ]; then
     echo "ERROR: Check if file: 'dynf${fhour}.nemsio' exists in modelDataPath."
#     echo "ERROR: Check if file: 'gfs.t${iHH}z.sfcf${fhour}.nemsio' exists in modelDataPath."
  fi 
  exit 1
fi

# Check if that flux file exists for FV3
if [ ${dyncore} == "FV3" ]; then
    if [[ ! -e ${flxFileName} ]]; then
        echo "ERROR: Can't find 'flxFileName': ${flxFileName}. Directory or file does not exist.  Exiting..."
        echo "ERROR: Check if 'modelDataPath': ${modelDataPath} exists."
        echo "ERROR: Check if file: 'phyf${fhour}.nemsio' exists in modelDataPath."
#        echo "ERROR: Check if file: 'gfs.t${iHH}z.sfcf${fhour}.nemsio' exists in modelDataPath."
        exit 1
    fi
fi

# Create itag based on user provided info. 
# Output format now set by user so if-block below uses this
# to generate the correct itag. 

if [[ ${outFormat} == "grib" ]]; then

cat > itag <<EOF
${inFileName}
${inFormat}
${YY}-${MM}-${DD}_${HH}:00:00
${tag}
EOF

elif [[ ${outFormat} == "grib2" ]]; then

    if [[ ${dyncore} == "ARW" ]]; then

cat > itag <<EOF
${inFileName}
${inFormat}
${outFormat}
${YY}-${MM}-${DD}_${HH}:00:00
${tag}
EOF

    elif [[ ${dyncore} == "FV3" ]]; then

cat > itag <<EOF
${inFileName}
${inFormat}
${outFormat}
${YY}-${MM}-${DD}_${HH}:00:00
${tag}
${flxFileName}
EOF
    fi

else
echo "ERROR: output format 'outFormat=${outFormat}' not supported, must choose 'grib' or 'grib2'. Exiting..."
exit 1
fi

#-----------------------------------------------------------------------
#   Run unipost.
#-----------------------------------------------------------------------
rm fort.*

ln -sf ${paramFile} fort.14

#----------------------------------------------------------------------
# There are two environment variables tmmark and COMSP
# RUN the unipost.exe executable. 
#----------------------------------------------------------------------

if [[ ${dyncore} == "ARW" ]]; then
    ${RUN_COMMAND} > unipost_${domain}.${fhr}.out 2>&1
elif [ ${dyncore} == "FV3" ]; then
     ${RUN_COMMAND} > unipost.${fhr}.out 2>&1
fi

#----------------------------------------------------------------------
# DEBUG Example, uncomment below and comment ${RUN_COMMAND} line above.

# debugger runs - enter your debugger and hour of error
#if [[ $((10#${fhr})) -eq 3 ]]; then
#  mpirun.dbg.totalview -progname ${POSTEXEC}/unipost.exe > unipost_${domain}.${fhr}.out 2>&1
#else
#  mpirun -np 1 ${POSTEXEC}/unipost.exe > unipost_${domain}.${fhr}.out 2>&1
#fi
#----------------------------------------------------------------------

# This prefix was given in the wrf_cntl.parm file (GRIB1)
# or postcntrl.xml(GRIB2)

if [[ ${dyncore} == "ARW" ]]; then
   mv WRFPRS${fhr}.${tmmark} WRFPRS_${domain}.${fhr}
elif [ ${dyncore} == "FV3" ]; then
   mv GFSPRS.GrbF${fhr} GFSPRS.${fhr}
fi

#
#----------------------------------------------------------------------
#   End of unipost job
#----------------------------------------------------------------------

# check to make sure UPP was successful and script linked the file
if [[ ${dyncore} == "ARW" ]]; then
    ls -l WRFPRS_${domain}.${fhr}
    err1=$?
elif [ ${dyncore} == "FV3" ]; then
    ls -l GFSPRS.${fhr}
    err1=$? 
fi

if test "$err1" -ne 0; then
    echo 'UNIPOST FAILED, EXITTING'
    exit
fi

#######################################################################
# EXAMPLES of running copygb
#######################################################################
# 
# Copygb interpolates Unipost output from its native 
# grid to a regular projection grid. The package copygb 
# is used to horizontally interpolate from one domain 
# to another, but this is not necessary for WRF-ARW.
#
# Copygb can be run in 2 ways as explained below. 
#
# NOTE: This section is providied as examples of the various ways to
# run copygb. The script runs copygb based on user input above
# (copygb_opt).The user will need to uncomment running copygb below
# in order to do so.
#
#----------------------------------------------------------------------
#
# Option 1: 
# Copygb is run with a pre-defined AWIPS grid 
# (variable $awips_id, see above in user edit section). Specify
# the grid to interpolate the forecast onto. To use standard AWIPS
# grids (list in  http://www.nco.ncep.noaa.gov/pmb/products/nam/ 
# or http://www.nco.ncep.noaa.gov/pmb/docs/on388/tableb.html),
# set the number of the grid in variable awips_id which is then
# passed to $nav variable for processing.
#
# ${POSTEXEC}/copygb.exe -xg"212" WRFPRS_${domain}.${fhr} wrfprs_${domain}.${fhr}
#
#----------------------------------------------------------------------
#
# Option 2: 
# Copygb ingests a custom kgds definition. Grid definition is set above in the
# User Edit section (custom_gds) and then passed to the $nav variable
# for processing.
#
# ${POSTEXEC}/copygb.exe -xg"255 3 109 91 37748 -77613 8 -71000 10379 9900 0 64 42000 42000" WRFPRS_${domain}.${fhr} wrfprs_${domain}.${fhr}
#
#----------------------------------------------------------------------
#
# (For more info on "copygb" see UPP User's Guide - link at top of script)
#
#######################################################################
# End EXAMPLES Section
#######################################################################

#######################################################################
# To run copygb, uncomment sections below
#######################################################################

#if [[ ${outFormat} == "grib" ]]; then

##-------------------------------------------------------------------------------------
## 2. Run copygb.
##-------------------------------------------------------------------------------------
##  Set grid specs file to read based on user specifications (copygb_opt)
##  in User Edit Section at top of script.

#if [[ ${copygb_opt} == "awips" ]]; then
#   export nav=${awips_id}
#   echo "copygb_opt = ${copygb_opt}, using awips_id=${awips_id} to run copygb."
#   echo $nav
#elif [[ ${copygb_opt} == "custom" ]]; then
#   echo ${custom_gds} > 'copygb_custom.txt'
#   read nav < 'copygb_custom.txt' 
#   export nav
#   echo "copygb_opt = ${copygb_opt}, using custom_gds=${custom_gds} to run copygb."
#   echo $nav
#fi

## Execute copygb with grid specs read into ${nav} and check if successful
#if [[ ${dyncore} == "ARW" ]]; then
#   ${POSTEXEC}/copygb.exe -xg"${nav}" WRFPRS_${domain}.${fhr} wrfprs_${domain}.${fhr}
#   ls -l wrfprs_${domain}.${fhr}
#   err1=$?
#fi

#if test "$err1" -ne 0; then
#    echo 'copygb FAILED, EXITTING'
#    exit
#fi  # End test block for file

#elif [[ ${outFormat} == "grib2" ]]; then
#    echo "Warning: nothing done for grib2 output using copygb, see wgrib2 for regridding grib2 output"

#fi # End if-block for if grib or grib2

#----------------------------------------------------------------------
#   End of copygb job
#----------------------------------------------------------------------

done 

fhr=$((10#${fhr}+$((${incrementhr}))))

NEWDATE=`${POSTEXEC}/ndate.exe +$((10#${fhr})) $startdate`

done

date
echo "End of Output Job"
exit