#!/bin/sh

# call from main script or command line
# nos_glofs_reformat.sh '2008072806' '2008072812'  
#                       beginingtime  endingtime  
 
startime=$1
endtime=$2


#grab enough nam data for interpolation
forstartime=`$NDATE -1 $startime`
forendtime=`$NDATE +3 $endtime`

NHRS=`$NHOUR $endtime $forstartime`

yr=`echo $forstartime |cut -c1-4`
mn=`echo $forstartime |cut -c5-6`
dy=`echo $forstartime |cut -c7-8`
hhr=`echo $forstartime |cut -c9-10`

if [ $fcstsys == 'leofs' ]
then
IM=81;JM=24
LONS='-84'
LONE='-78'
LATS='40'
LATE='44'
GRIDFILE=nos_leofs_lat_lon.dat
elif [ $fcstsys == 'lmofs' ]
then
IM=53;JM=102
LONS='-89'
LONE='-83'
LATS='41'
LATE='48'
GRIDFILE=nos_lmofs_lat_lon.dat
elif [ $fcstsys == 'loofs' ]
then
IM=61;JM=25
LONS='-80'
LONE='-75'
LATS='43'
LATE='45'
GRIDFILE=nos_loofs_lat_lon.dat
elif [ $fcstsys == 'lhofs' ]
then
IM=81;JM=75
LONS='-86'
LONE='-79'
LATS='42'
LATE='47'
GRIDFILE=nos_lhofs_lat_lon.dat
elif [ $fcstsys == 'lsofs' ]
then
IM=61;JM=30
LONS='-93'
LONE='-84'
LATS='45'
LATE='49'
GRIDFILE=nos_lsofs_lat_lon.dat
fi

typeset -Z2 hr counthr fcounthr
counthr=0

fortime=`$NDATE -5 $forstartime`

yyr=`echo $fortime |cut -c1-4`
mmn=`echo $fortime |cut -c5-6`
ddy=`echo $fortime |cut -c7-8`
#forcyc=`echo $fortime |cut -c9-10`

countime=$forstartime

typeset -Z2 GU
typeset -Z2 IGU 
GU=10
IGU=11

#search for last good files for this run 
 ymdh=$forstartime
 ymdht=`$NDATE -1 $ymdh`
 ymdhe=`$NDATE -13 $ymdh`

  while [ $ymdht -ge $ymdhe ]
  do

 ymdht=`$NDATE -1 $ymdht` 
 ymd=`echo $ymdht | cut -c1-8`
 forcyc=`echo $ymdht | cut -c9-10`

 echo 'ext' $ext

if [ -f $COMNAM/nam.$ymd/nam.t${forcyc}z.awip1200.tm00.grib2 ]
then

echo 'GRIB FILE FOUND!'

while [ $countime -lt $forendtime ]
do

countime=`$NDATE $counthr $forstartime`

#echo 'countime= ' $countime
#echo 'counthr= ' $counthr

fcounthr=`expr $counthr + 3`
#echo 'fcounthr= ' $fcounthr

   GRIBINPUTFILE=$COMNAM/nam.$yyr$mmn$ddy/nam.t${forcyc}z.awip12${fcounthr}.tm00.grib2
   $WGRIB2INDEX $GRIBINPUTFILE "GRIBINDEXFILE$IGU"

# export variables to be called by fortran program
   export 'GRB2FNM_'$GU="$GRIBINPUTFILE"
#   echo 'GU= ' $GU
   let GU=GU+1   

    export 'GRB2IFN_'$IGU="GRIBINDEXFILE$IGU"
#   echo 'IGU= ' $IGU   

   let IGU=IGU+1
   let counthr=counthr+3

echo 'grib file read into interpolation code' $GRIBINPUTFILE

echo 'grib index file read into interpolation code' "GRIBINDEXFILE$IGU"
done
fi
done

# variable to allocate fortran arrays (numtimes)
export TGRB2=`expr $GU - 10`

echo 'total grib files read in= ' $TGRB2
echo 'total hours for nam interpolation= ' $NHRS

DT=1

echo 'interpolation code output (DT)' $DT

echo $IM $JM 'IM,JM of model grid' > 'nos_'$fcstsys'_reformatfor.in' 
echo $GRIDFILE >> 'nos_'$fcstsys'_reformatfor.in' 
echo $LONS $LONE $LATS $LATE >> 'nos_'$fcstsys'_reformatfor.in' 
echo $yr $mn $dy $hhr >> 'nos_'$fcstsys'_reformatfor.in'
echo $NHRS >> 'nos_'$fcstsys'_reformatfor.in' 
echo $DT >> 'nos_'$fcstsys'_reformatfor.in'

export pgm=nos_glofs_reformatfor_${fcstsys}_nowcast
. ./prep_step

./startmsg

$EXECnos/nos_nam_reformatfor < 'nos_'$fcstsys'_reformatfor.in'>>${fcstsys}.out
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, critical failure!"
  msg="$pgm did not complete normally, critical failure!"
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi
