#!/bin/ksh 
#
#  extract_onehour.ksh
#   
#  Extract data for hours between 00 to 71 hours, steps 3 hours [This is 00,03,06, ...71]
#  from fileo* and filem* files
#  Execute : extract_onehour.ksh 20130804 BOIA_RS
#  

data=${1}00
name_station=${2}
fileobs=/scratchout/grupos/oceano/rosio.camayo/validation/validate_pesq/d-input/fileo
filemod=/scratchout/grupos/oceano/rosio.camayo/validation/validate_pesq/d-input/fileint
dir_func=/stornext/home/rosio.camayo/ondas_io.v2/FUNCTIONS/
dir_out=/scratchout/grupos/oceano/rosio.camayo/validation/validate_pesq/d-input/d-mod_vs_obs

forecast_hour=0
forecast_hourf=71

mkdir -p ${dir_out}/${name_station}/${data}

while [[ $forecast_hour -le $forecast_hourf ]] do

data_fcst_julian1=$(${dir_func}/caldate.3.0 ${data} + ${forecast_hour}nh 'jjjhh')
a=${data_fcst_julian1:0:3}
b=${data_fcst_julian1:3:2}

# Don't working for all cases
#data1=$(awk "BEGIN {print ${a} + (${b}/24); exit}")
#data2=$(awk "BEGIN {print ${data1} + (1/24); exit}")

# Otherwise
#cat <<EOF> division.f90
#real :: c,d
#c=float($a)+(float($b)/24)
#d=c+1./24.
#write(10,'(f9.5)') c  
#write(10,'(f9.5)') d  
#close(10)
#end
#EOF
#ftn division.f90 -o division
#./division
#data1=$(head -1 fort.10 | awk '{print $1}')
#data2=$(tail -1 fort.10 | awk '{print $1}')
#rm -f fort.10 division.f90 division

# and otherwise

data1=$(echo "scale=3;($a+($b/24))"|bc)
data2=$(echo "scale=3;($data1+(1/24))"|bc)

if (( $forecast_hour < 10 ));then
forecast_hour=0${forecast_hour}
fi

awk 'a<=$1 && $1<b  {print $0}' a=${data1} b=${data2} $fileobs > fileobs
awk 'a<=$1 && $1<b  {print $0}' a=${data1} b=${data2} $filemod > filemod

paste fileobs filemod | awk '{print $1,$2,$4}' > ${dir_out}/${name_station}/${data}/mod_obs_${name_station}_${data}_${forecast_hour}

(( forecast_hour = forecast_hour + 1 ))

done

\rm -f fileobs filemod
