#!/bin/ksh 
#
#  Run : interp_mod2obs.v2.ksh 20130803 BOIA_RS
#   
#  Find forecasts of model WW3 to time of buoys. 
#  Version 2: 
#  - For one station
#  - For one forecast hour. Choose one between 00 to 72 hours. 
#  Usage:

date=${1}00
name_station=$2
dirdata=/scratchout/grupos/oceano/rosio.camayo/validation/validate_pesq/d-input/
fileobs=${dirdata}/d-boias/${name_station}_ONDA_${date:4:2}${date:0:4}_A
filemod=${dirdata}/d-ww3/ww3_points_dir_${date}
dir_func=/stornext/home/rosio.camayo/ondas_io.v2/FUNCTIONS/

if [ ! -f $fileobs ];
then
   echo "ATTENTION: $fileobs does not exists to compare with model data!"
   exit 1
fi

 echo "Interpolating $date ...."

#  Capture data of one station and initial and final forecast hour from model file

grep ${name_station} $filemod > filem

dateini=$(head -1 filem | awk '{print $1}')
datefin=$(tail -2 filem | head -1 | awk '{print $1}') 

#  Capture observations between dateini and datefin from model data

cat -n $fileobs | sort -nr | cut -c 8- > temp 
##cp $fileobs temp

rm -f fileo

dateact=$dateini

while [[ $dateact -le $datefin ]] ; do

awk '$1==y && $2==m && $3==d {print $0}' y=${dateact:0:4} m=${dateact:4:2} d=${dateact:6:2} temp >> fileo

dateact=$(${dir_func}/caldate.3.0 ${dateact}00 + 24nh 'yyyymmdd')

done

# Converting date from model to julian day

rm -f dayspart1 
mv filem filetemp

for datei in $(awk '{print $1}' filetemp)
do
   ${dir_func}/caldate.3.0 ${datei} + 0d 'jjj' >> dayspart1
done

paste dayspart1 filetemp | awk '{a=$1; b=substr($3,1,2); c=substr($3,3,2); d=substr($3,5,2); e=a+b/24+c/1440+d/86400; printf ("%s %s\n", e,$6 )}' > filem

# Converting date from observations to julian day 

rm -f dayspart1

if [ ! -s fileo ];
then
   echo "ATTENTION: Fileo is empty, then non data to compare with model!"
   exit 2
fi

mv fileo filetemp

for datei in $(awk '{printf "%s %02d %02d\n", $1, $2, $3}' filetemp | awk '{print $1 $2 $3}')
do
   /stornext/home/rosio.camayo/ondas_io.v2/FUNCTIONS/caldate.3.0 ${datei} + 0d 'jjj' >> dayspart1
done

paste dayspart1 filetemp | awk '{e=$1+$5/24+$6/1440+$7/86400; printf ("%s %s\n", e,$8 )}' > fileo

rm -f dayspart1 filetemp

rm -f temp


# Interpolating model data to observations points

\rm -f fileint

cat <<EOF> interplinear.f90

    OPEN(10, FILE='fileo', STATUS='UNKNOWN', FORM='FORMATTED' )
    OPEN(11, FILE='filem', STATUS='UNKNOWN', FORM='FORMATTED' )
    OPEN(12, FILE='fileint', STATUS='UNKNOWN', FORM='FORMATTED' )

    i=1
    DO 
    READ(10,*,END=50) xo,yo

    IF(i.eq.1) THEN 
    READ(11,*,END=60) x1,y1 
    READ(11,*,END=60) x2,y2 
    i=2 
    ENDIF

    DO 
    IF( xo.ge.x2) THEN
      x1=x2 
      y1=y2
      READ(11,*,END=60) x2,y2
    ELSE
      EXIT
    ENDIF
    END DO
   
    yo=(y2-y1)*(xo-x1)/(x2-x1)+y1
    
!    write(*,*) yo,y1,y2
    write(12,*) xo,yo

    END DO
   
50  CLOSE(10)
60  CLOSE(11)
    CLOSE(12)
    END
EOF

ftn interplinear.f90 -o interplinear
./interplinear
rm -f interplinear.f90 interplinear 
mv fileo filem fileint ${dirdata}/.

