#!/bin/sh

set +x
echo "********************************************************************"
echo "   Script Name: interp.sh                                           "
echo "   Contacts   : Bruce Whistler (bwhistler@nesdis.noaa.gov)          "
echo "                               (301-457-5258)                       "
echo "                Rich Kelley    (rkelley@nesdis.noaa.gov)            "
echo "                               (301-457-5195)                       "
echo "   Purpose    : Process forecast data from NCEP's model data        "
echo "                directories and create 2.5/2.0 degree output        "
echo "                This is a replacement for file                      "
echo "                NMC.PROD.VF06.TXXZ.AVN (HDS)                        "
echo "                NMC.PROD.SHARED.VANL.TXXZ.FNL (HDS)                 "
echo "                The output file 50 is a data file (2.5/2.0 degree grid"
echo "                resol in IBM real format)                           "
echo "                The output file 40 is an index file (in IBM text    "
echo "                format)                                             "
echo "********************************************************************"
set -x

##################################################################
# Define Variables
#    Variables from Arguement List
#       fhr       - Current Forecast Hour
#       resol     - Grid Resolution to Convert
#    Variables Imported from Parent Script
#       COM       - NCEP Model Data Directory
#       PDY       - Current YYYYMMDD
#       cyc       - Current Model Cycle (Numeric: 00)
#       cycle     - Current Model Cycle (Alpha-Numeric: t00z)
#       model     - Current Model
#       fprefix   - NCEP Model Data file prefix
#       SCRATCHdir- Temporary Working Directory
#       EXECdir   - Executables Directory
#       PARMdir   - Parameter Files Directory
#       SCRIPTdir - Script Directory
#    Local Variables
#       outprefix - Prefix for data/index/out files
##################################################################

if test $# -eq 2
then
   fhr=$1
   resol=$2
else
   echo ""
   echo "Usage: interp.sh ForecastHour resol"
   echo "  where ForecastHour: 00,06,12,18..."
   echo "  where resol: 2.0 or 2.5"
   echo ""
   exit
fi

cd $DATA

if test "$resol" = "2.0"
then
   outprefix=${model}${cyc}${fhr}_2
   parm=$PARMutil/nesdis_${model}_interp1t2.0
   EXEC=$EXECutil/nesdis_interp1t2.0
elif test "$resol" = "2.5"
then
   outprefix=${model}${cyc}${fhr}
   parm=$PARMutil/nesdis_${model}_interp1t2.5
   EXEC=$EXECutil/nesdis_interp1t2.5
else
   exit
fi


###########################
# Copy input files
###########################
cp $COMIN/${fprefix}.${cycle}.pgrbf$fhr ${fprefix}.${cycle}.pgrbf$fhr
cp $COMIN/${fprefix}.${cycle}.pgrbif$fhr ${fprefix}.${cycle}.pgrbif$fhr

###########################
# Execute Interpolation
###########################
export XLFRTEOPTS="unit_vars=yes"
export XLFUNIT_11="${fprefix}.${cycle}.pgrbf$fhr"
export XLFUNIT_31="${fprefix}.${cycle}.pgrbif$fhr"
export XLFUNIT_40="${outprefix}.index"
export XLFUNIT_50="${outprefix}.data"

$EXEC < $parm >>$pgmout 2>errfile
err=$?; err_chk 

