#!/bin/ksh -x
#
#VARIAVEIS

ifct=$1
Fct=$2


GRBDIR=${Eta_run}
WOUTDIR=${Eta_run}

me=$(whoami)
echo "I am $me."

echo 'remove old files!!!'
rm -fv $WOUTDIR/wbin.*
rm -fv wbin.*

dhri=${ifct}


typeset -Z6 dhr
typeset -Z6 dhri Fct

if  [ "${TInitBC}" == "0" ] && [ "${dhri}" != "000000" ]  ; then    
   Run_Date=`${Eta_home1}/util/caldate.3.0 ${Run_Date} - ${HInitBC}hr 'yyyymmddhh'`
   let dhri=${dhri}+${HInitBC}
   let Fct=${Fct}+${HInitBC}
fi
ihour=`echo ${Run_Date} |cut -c 9-10`


while [ $dhri -le ${Fct} ];do
echo "passei"

 if [ "${dhri}" == "000000" ] ; then  
   FInitBC=${CInit}  
 else
    FInitBC=${BCond}   
 fi

#if [ $dhri -ne 0 ];then
#if [ $dhri -le 9 ];then
#dhri=0$dhri
#fi
#fi

dhr=${dhri}

echo '==================================================='
#echo 'file in process '$GRBDIR'/gfs.t'${ihour}'z.pgrb2f'$dhri'.'$Run_Date'.grib2'
echo 'file in process '$GRBDIR'/gfs.t'${ihour}'z.pgrb2.0p25.f'${dhri}'.'${Run_Date}'.grib2'
echo '==================================================='

line=$(${Eta_home}/util/wgrib2 -v $GRBDIR'/gfs.t'${ihour}'z.pgrb2.0p25.f'${dhri}'.'${Run_Date}'.grib2' | grep "HGT" | grep "10 mb")
adate=`echo ${line}|cut -d"=" -f2|cut -c1-10`
echo $adate > ${WOUTDIR}/adate.txt

if [ $adate -eq $Run_Date ] ; then
  echo "The gfs date is $adate"
else
  echo "Data for $Run_Date isn't available"
  exit 99 
fi

rm -f  ${WOUTDIR}/${FInitBC}_${adate}.$dhr
touch  ${WOUTDIR}/${FInitBC}_${adate}.$dhr
echo 'OPEN ' ${WOUTDIR}/${FInitBC}_${adate}.$dhr '  FILE'

for vars in HGT UGRD VGRD TMP RH; do
for levs in 100000 97500 95000 92500 90000 85000 80000 75000 70000 65000 \
             60000 55000 50000 45000 40000 35000 30000 25000 20000 15000 \
             10000  7000  5000  3000  2000  1000;do


#line=$(${Eta_home}/util/wgrib2 -ctl_inv $GRBDIR'/gfs.t'${ihour}'z.pgrb2f'$dhri'.'$Run_Date'.grib2' | grep "$vars 100,$levs ")
line=$(${Eta_home}/util/wgrib2 -ctl_inv $GRBDIR'/gfs.t'${ihour}'z.pgrb2.0p25.f'${dhri}'.'${Run_Date}'.grib2' | grep "$vars 100,$levs ")

if [ -z "$line" ]; then
echo 'MISSING DATA :: VAR = '$vars' LEVEL = '$levs
else

tptp=`expr index "$line" :`
tptp=`expr $tptp - 1`
nrec=${line:0:$tptp}

#${Eta_home}/util/wgrib2 -d $nrec $GRBDIR'/gfs.t'${ihour}'z.pgrb2f'$dhri'.'$Run_Date'.grib2' -append -bin ${WOUTDIR}/${FInitBC}_${adate}.$dhr >> ${WOUTDIR}/log
${Eta_home}/util/wgrib2 -d $nrec $GRBDIR'/gfs.t'${ihour}'z.pgrb2.0p25.f'${dhri}'.'${Run_Date}'.grib2' -append -bin ${WOUTDIR}/${FInitBC}_${adate}.$dhr >> ${WOUTDIR}/log

fi   # if [ -z "$line" ]

done # for levs 
done # for vars 

# SURFACE VARIABLES

for vars in "LAND" "PRMSL" "PRES 1," "HGT 1,"\
            "TSOIL 106,0,0.1"\
            "SOILW 106,0,0.1"\
            "TSOIL 106,0.1,0.4"\
            "SOILW 106,0.1.0.4"\
            "TSOIL 106,0.4,1"\
            "SOILW 106,0.4,1"\
            "TSOIL 106,1,2"\
            "SOILW 106,1,2"\
	    ; do

#line=$(${Eta_home}/util/wgrib2 -ctl_inv $GRBDIR'/gfs.t'${ihour}'z.pgrb2f'$dhri'.'$Run_Date'.grib2' | grep "$vars")
line=$(${Eta_home}/util/wgrib2 -ctl_inv $GRBDIR'/gfs.t'${ihour}'z.pgrb2.0p25.f'${dhri}'.'${Run_Date}'.grib2' | grep "$vars")

if [ -z "$line" ]; then
echo 'NO DATA FOR VAR = '$vars' LEVEL = '$levs'mb'
else

tptp=`expr index "$line" :`
tptp=`expr $tptp - 1`
nrec=${line:0:$tptp}

#${Eta_home}/util/wgrib2 -d $nrec $GRBDIR'/gfs.t'${ihour}'z.pgrb2f'$dhri'.'$Run_Date'.grib2' -append -bin  ${WOUTDIR}/${FInitBC}_${adate}.$dhr >> ${WOUTDIR}/log
${Eta_home}/util/wgrib2 -d $nrec $GRBDIR'/gfs.t'${ihour}'z.pgrb2.0p25.f'${dhri}'.'${Run_Date}'.grib2' -append -bin  ${WOUTDIR}/${FInitBC}_${adate}.$dhr >> ${WOUTDIR}/log

fi   # if [ -z "$line" ]
#echo "PASSOU"
#sleep 50
done # for vars


echo 'DONE WITH ' ${WOUTDIR}/${FInitBC}_${adate}.$dhr '  FILE'



ls -l ${WOUTDIR}/${FInitBC}_${adate}.$dhr

#echo '=========================================='
dhri=`expr $dhri + ${InitBC} `

done # while [ $dhr -le $dend ]

#mv -v ${FInitBC}_${adate}.* $WOUTDIR
#mv -v adate.txt $WOUTDIR
mv -v log $WOUTDIR


exit
