#!/bin/ksh  

datai=20200802
dataf=20210131

var=TMAX

exp1=ERA
exp2=BAN


scr=`pwd`
dir=$scr/${exp1}x${exp2}/${var}
out=$scr/${exp1}x${exp2}/${var}

fig=$dir/FIG
TXT=$dir/TXT
BIN=$dir/BIN

mkdir -p $TXT $BIN $fig

# ANUAL


dd=$datai

CTL=$(date -u -d "${datai} 12:00 00 hours " +"%H"Z"%d%b%Y" | tr [a-z] [A-Z])

if [ ${exp1}x${exp2} == ERAxGFS ] ; then
 cat  /share/ioper/tempo/CRNG/users/ROZANTE/AVAL_GFS_WRF_ERA5/ERAxGFSxWRF/F24/$var/completo_ERAxGFSxWRF.txt|grep -v 9.99e+08 | awk '{print $1" "$2" "$3" "$4" "$5" "$6" "$8}' >  $dir/completo_${exp1}x${exp2}.txt
fi

if [ ${exp1}x${exp2} == ERAxWRF ] ; then
 cat  /share/ioper/tempo/CRNG/users/ROZANTE/AVAL_GFS_WRF_ERA5/ERAxGFSxWRF/F24/$var/completo_ERAxGFSxWRF.txt |grep -v 9.99e+08| awk '{print $1" "$2" "$3" "$4" "$5" "$7" "$8}' >  $dir/completo_${exp1}x${exp2}.txt
fi

if [ ${exp1}x${exp2} == ERAxBAN ] ; then
 cat  /share/ioper/tempo/CRNG/users/ROZANTE/AVAL_GFS_WRF_ERA5/ERAxGFSxBAN/F24/$var/completo_ERAxGFSxBAN.txt |grep -v 9.99e+08| awk '{print $1" "$2" "$3" "$4" "$5" "$7" "$8}' >  $dir/completo_${exp1}x${exp2}.txt
fi


for REG in $(echo "B1  B2 B3 B4"); do

rm -f $TXT/RMSE_ME_r_${REG}.txt

datai=$dd
cd $dir

while [ $datai -le $dataf ] ; do

   DCTL=$datai
   echo $datai"  "${REG}
   nd=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |wc -l)

   if [ $nd != 0 ] ; then   
    OB=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk 'BEGIN {R=0; c=0}   {R+=$4;c++} END {print R/c}')
    RT=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk 'BEGIN {R=0; c=0}   {R+=$5;c++} END {print R/c}')
    EL=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk 'BEGIN {R=0; c=0}   {R+=$6;c++} END {print R/c}')  

    ViesRT=$(grep -i $DCTL  completo_${exp1}x${exp2}.txt  |grep $REG |awk 'BEGIN {R=0; c=0}    {R+=$5-$4;c++}   END {print R/c}')
    ViesEL=$(grep -i $DCTL  completo_${exp1}x${exp2}.txt  |grep $REG |awk 'BEGIN {R=0; c=0}    {R+=$6-$4;c++}   END {print R/c}')

    RmseRT=$(grep -i $DCTL completo_${exp1}x${exp2}.txt |grep $REG |awk 'BEGIN {R=0; c=0}  {R+=($5-$4)*($5-$4);c++}   END {print sqrt(R/c)}')
    RmseEL=$(grep -i $DCTL completo_${exp1}x${exp2}.txt |grep $REG |awk 'BEGIN {R=0; c=0}  {R+=($6-$4)*($6-$4);c++}   END {print sqrt(R/c)}')

     numRT=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk  -v mO=${OB}  -v mRT=${RT} 'BEGIN {X=0}   {X+=($4-mO)*($5-mRT) } END {print X}' |sed "s/e/*10^/g"|sed "s/+//g")
    XdenRT=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk  -v mO=${OB}               'BEGIN {X=0}   {X+=($4-mO)*($4-mO)}   END {print  sqrt(X)}'|sed "s/e/*10^/g"|sed "s/+//g")
    YdenRT=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk  -v mRT=${RT}              'BEGIN {X=0}   {X+=($5-mRT)*($5-mRT)} END {print sqrt(X)}'|sed "s/e/*10^/g"|sed "s/+//g")

    numEL=$(grep -i   $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk   -v mO=${OB}  -v mEL=${EL} 'BEGIN {X=0}   {X+=($4-mO)*($6-mEL) } END {print X}' |sed "s/e/*10^/g"|sed "s/+//g")
    XdenEL=$XdenRT
    YdenEL=$(grep -i  $DCTL  completo_${exp1}x${exp2}.txt |grep $REG |awk  -v mEL=${EL}               'BEGIN {X=0}   {X+=($6-mEL)*($6-mEL)} END {print sqrt(X)}'|sed "s/e/*10^/g"|sed "s/+//g")
    decide=$(echo "$XdenEL*$YdenEL*$XdenRT*$YdenRT" |bc -l )

    if [ X$decide = X0 ] ; then
      rEL=-9999.9
      rRT=-9999.9
    else
     rEL=$(echo "(($numEL)/($XdenEL*$YdenEL))"| bc -l)       #"
     rRT=$(echo "(($numRT)/($XdenRT*$YdenRT))"| bc -l)           #"
    fi
   else
        nd=-9999.9
        OB=-9999.9
        RT=-9999.9
        EL=-9999.9
    ViesRT=-9999.9
    ViesEL=-9999.9
    RmseRT=-9999.9
    RmseEL=-9999.9
       rEL=-9999.9
       rRT=-9999.9    
   fi 
   echo $nd"  " $OB"  "$RT"  "$EL"  "$ViesRT"   "$ViesEL"  "$RmseRT"   "$RmseEL"   "$rRT"  "$rEL >> $TXT/RMSE_ME_r_${REG}.txt
   datai=`date -d "${datai:0:8} 00:00 24 hours" +"%Y%m%d"`
done
nl=$(wc -l $TXT/RMSE_ME_r_${REG}.txt | awk '{print $1}') 
$scr/bina_RMSE_ME_r.exe $TXT/RMSE_ME_r_${REG}.txt $BIN/RMSE_ME_r_${REG}.bin ${nl}
cat  $scr/RMSE_ME_r_TEMPLATE.ctl | sed "s@EXP@${REG}@g" | sed "s@_NN@${nl}@g" | sed "s@_CTL@${CTL}@g"> $BIN/RMSE_ME_r_${REG}.ctl
grads -blc "run $scr/plota_RMSE_ME_r_EXPERIMENTOS.gs $BIN/RMSE_ME_r_${REG}.ctl ${REG} $nl $exp1  $exp2 $fig"
done
rm -f $dir/completo_${exp1}x${exp2}.txt
exit
