#!/bin/bash -l
# ----------------------------------------------------------
# Chuong trinh nay thuc hien toan bo cac buoc chay Operational sau khi da co ket qua chay cac mo hinh RegCM va clWRF
# 1) Remap so lieu mo hinh ve khu vuc VN voi do phan giai 0.25 do
# 2) Thuc hien Bias correction bang ANN, Cli va MLR
# 3) Noi (Combine) so lieu cua thang vua BC voi toan bo so lieu tu 1982 den thang truoc do
# 4) Tinh cac chi so han SPI va SPEI cho toan bo chuoi SL
# 5) Tinh xac suat xuat hien han du bao tren tung o luoi
# 6) Tinh ket qua du bao nhiet do va luong mua va di thuong cua chung
# 7) Ve do thi xac suat han du bao
# 8) Ve do thi nhiet do va luong mua va di thuong du bao

# o1_run_BC_Ope_Current.sh  o2_Combine_for_Ope_from_2024-01.sh  o3_Calc_Dr_IDXs.sh  o4_Calc_Pr_T2_Ano.sh
# ----------------------------------------------------------

#year=${1}          # Year to be run
#mon=${2}           # Month to be run

year=`date +%Y`
mon=`date -d "1 month ago" +%m`

if [ $((mon)) -eq 12 ] ; then
   year=$(( $((year)) - 1 ))
fi

ROOT=/home/tanpv/Progs/Dr_VN_Ope/DATA/PDF_Ope/
NC_DIR=${ROOT}${year}${mon}
file1=${NC_DIR}/Dr_Prob.nc
file2=${NC_DIR}/Forecast_Ope_Pr_T2_and_Anomaly.nc

if [[ -e ${file1} && -e ${file2} ]] ; then
   echo "Running for "${year}${mon}" has been done !"
   exit
fi

timerun=`date +%Y%m%d%H`

# Checking for enough files to run:
DIR=/home/tanpv/Data/562_VNU/0.25_VN/Operational/monthly/${year}/${mon}
MEMS=" 01 02 03 04 05 06 "
MODELS=" clWRF RegCM "
icount=0
for Model in ${MODELS} ; do
   for mem in ${MEMS} ; do
       Fname=${Model}_${mem}.nc
       if [[ -e ${DIR}/${Fname} ]] ; then
          echo "File "${DIR}/${Fname}" exists"
       else
          icount=$((icount + 1))
          echo "File "${DIR}/${Fname}" does not exist"
       fi
   done
done
if [ $((icount)) -ne 0 ] ; then
   echo "Not enough files to run for "${year}-${mon} 
   echo "Number of missing files is "${icount}
   echo "Run at "${timerun} ". Not enough files to run for "${year}-${mon}". Number of missing files is "${icount} | mailx -s "Operational Drought forecast" remoclic@hus.edu.vn
   ./remap_any_case_Ope.sh ${year} ${mon}
   exit
fi
#exit
###################################################################
# Current directory:
Here=`pwd`

# 1) Remap to 0.25 for VN domain:
#cd /home/tanpv/Data/562_VNU/0.25_VN
cd /home/tanpv/Progs/Dr_VN_Ope/
./remap_Ope.sh ${year} ${mon} 

# Move to current directory:
cd ${Here}

# 2) Bias corrections:
cd /home/tanpv/Progs/Dr_VN_Ope/
./o1_run_BC_Ope_Current.sh ${year} ${mon}

# 3) Combine data from current run to previous runs:
cd /home/tanpv/Progs/Dr_VN_Ope/
./o2_Combine_for_Ope_from_2024-01.sh ${year} ${mon}

# 4) Calculate Drought indices:
cd /home/tanpv/Progs/Dr_VN_Ope/
./o3_Calc_Dr_IDXs.sh ${year} ${mon}

# 5) Calculate probability of Drought occurrence
cd /home/tanpv/Progs/Dr_VN_Ope/
./o5_Calc_Dr_Prob.sh ${year} ${mon}

# Calculate the model forecast of rainfall and temperature and their anomaly
cd /home/tanpv/Progs/Dr_VN_Ope/
./o4_Calc_Pr_T2_Ano.sh ${year} ${mon}

# Plot Drought probability using GrADS
cd /home/tanpv/Progs/Dr_VN_Ope/
./o7_Plot_Dr_Prob.sh ${year} ${mon}

# Plot forecast rainfall and temperature and their anomaly using GrADS
cd /home/tanpv/Progs/Dr_VN_Ope/
./o6_Plot_Pr_T2_fcs.sh ${year} ${mon}

# Plot drought indices:
cd /home/tanpv/Progs/Dr_VN_Ope/
./o8_Plot_Dr_IDX.sh ${year} ${mon}

# Move NC file to ${year}${mon} directory
cd /home/tanpv/Progs/Dr_VN_Ope/
./o9_data_organize.sh ${year} ${mon}

