#!/bin/sh

rm wspd.txt
rm wdir.txt
rm fstamp.txt
rm atmp.txt
rm ll.dat
rm wdata.dat
rm mask.dat

set -x

startime=$1
endtime=$2

#grab enough nam data for interpolation
forstartime=`$NDATE -1 $startime`

NHRSmodel=`$NHOUR $endtime $forstartime`

forendtime=`$NDATE +3 $endtime`

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

NHRSndfd=`expr $NHRSmodel \/ 3`
NHRSndfd1=`expr $NHRSndfd + 2`

echo "NHRSndfd $NHRSndfd"

if [ $fcstsys == 'leofs' ]
then
IM=81;JM=24
LONS='-84'
LONE='-78'
LATS='41'
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

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 $hr >> 'nos_'$fcstsys'_reformatfor.in'
echo $NHRSmodel >> 'nos_'$fcstsys'_reformatfor.in'
echo $DT >> 'nos_'$fcstsys'_reformatfor.in'

  ymdh=$forstartime
  echo 'ymdh' $ymdh

  ymdht=`$NDATE -1 $ymdh`
  ymdhe=`$NDATE -4 $ymdh`
  ndfdOK='no'

  while [ "$ndfdOK" = 'no' ] && [ $ymdht -ge $ymdhe ]
  do

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

    if [ -f $NDFDDIR/$ymd/wgrbbul/ndfd_gl_${ext}.grib2 ]
    then
      ndfd_file=ndfd_gl_${ext}.grib2

      cp $NDFDDIR/$ymd/wgrbbul/$ndfd_file . > getndfd.out 2> getndfd.err
      err=$?

      if [ "$err" != '0' ]
      then
        cat getndfd.out
        cat getndfd.err
        echo ' '
        echo '****************************************'
        echo '*** FATAL ERROR IN copying grib data ***'
        echo '****************************************'
        echo 'EXITTING NDFD SCRIPT'
        exit 
      fi

      rm -f getnam.out getnam.err

# 1b. Extract grib data
#make subsetted grib file
      $WGRIB2 $ndfd_file -small_grib ${LONS}:${LONE} ${LATS}:${LATE} ndfd_file_sub > tmp.log

      $WGRIB2 ndfd_file_sub -match WIND -spread wspd.txt > tmp.txt
      $WGRIB2 ndfd_file_sub -match WDIR -spread wdir.txt > tmp2.txt
      $WGRIB2 ndfd_file_sub -match TMP -spread atmp.txt > tmp3.txt

      nc1=`wc -l tmp.txt | awk '{print $1}'`
      nc2=`wc -l tmp2.txt | awk '{print $1}'`
      nc3=`wc -l tmp3.txt | awk '{print $1}'`

      sed 's/d=/ /' tmp.txt | awk '{ print $2}' | cut -f 1,4 -d : | sed 's/:/ /' > stamp.txt

      if [ "$nc1" -ge '30' ] && [ "$nc2" -ge '30' ] && [ "$nc3" -ge '30' ]
      then
        ndfdOK='yes'
        echo 'ndfdOK' $ndfdOK
      fi
    fi

done

# --------------------------------------------------------------------------- #
#Convert to glofs format

  if [ "$ndfdOK" = 'yes' ]
  then

#reformat stamp.txt

rm fstamp.txt

awk '{ print $1 $2}' stamp.txt >junk

for i in $(cat junk)
do
yyyymmddhh=`echo $i |cut -c1-10`
ph=`echo $i |cut -c11-13`
ct=`$EXECutil/ndate +$ph $yyyymmddhh`
y=`echo $ct |cut -c1-4`
m=`echo $ct |cut -c5-6`
d=`echo $ct |cut -c7-8`
h=`echo $ct |cut -c9-10`
echo $y $m $d $h >> fstamp.txt
done

#cp fstamp.txt $COMOUT/${RUN}.t${cyc}z.${fcstsys}_fstamp.txt

echo 'ndfd_file_sub' >> 'nos_'$fcstsys'_reformatfor.in'
echo $NHRSndfd >> 'nos_'$fcstsys'_reformatfor.in'


export pgm=nos_ndfd_reformatfor_${fcstsys}_forecast
. ./prep_step

./startmsg

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

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, will use nam"
  msg="$pgm did not complete normally, will use nam"
  ./postmsg "$jlogfile" "$msg"
   ndfdOK='no'
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

fi


if [ -s $ndfd_file -a "$ndfdOK" = 'yes'  ]
then
   beginhours=`awk '{print $1$2$3$4}' fstamp.txt | head -1`
   endhours=`awk '{print $1$2$3$4}' fstamp.txt | tail -1`
   diffhours=`$NHOUR $beginhours $startime`
   diffhours_end=`$NHOUR $endhours $startime`
   echo "WIND WFO_START " $diffhours " hours teta(1)-tstart " >> $FCSTSYSLOG
   echo "WIND WFO_END " $diffhours_end " hours teta(end)-tstart " >> $FCSTSYSLOG
   echo 'NDFD' > forforcing
   #greys out corms flag for NAM REFORMAT
   cat $FIXnos/corms_raw_for_nam.txt >> $FCSTSYSLOG
else
   echo 'NAM' > forforcing
fi
