#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
########################################################################
# Author:  Alexander Mahura - 2023-08-03
# UPDATED:
#        2023-08; 2023-09; 
########################################################################
#
# NEEDED MODULES LOADED ... 
#  module load LUMI/23.03
#  module load partition/C
#  module load PrgEnv-gnu
#  module load odb_api/0.18.1-cpeCray-23.03.lua
#  module load python-climatedt/3.11.3-cpeCray-23.03.lua
# NOTE:
#  these are part of /appl/AS/AUTOSUBMIT_DATA/expid/proj/workflow/lib/LUMI/config.sh
#
#########################################################################
# PRE-SETUP (TESTING FOR LIMITED ODB DATASET & LIMITED GEOGRAPHICAL AREA)
# FOR AIR TEMPERATURE AT 2 METRE
# ANY OTHER METEOROLOGICAL PARAMETER (FROM SYNOP LISTED) CAN BE ADDED

echo "===================================================================="
echo "       DestinE Digital Twin --- OBSALL Apps for SYNOP OBS DATA:"
echo "          TESTING/ RUNING FOR: "
echo "          -- LIMITED SYNOP OBS DATA ODB DATASET"
echo "          -- LIMITED GEOGRAPHICAL AREA"
echo "          -- FOR AIR TEMPERATURE AT 2 METRE"
echo "===================================================================="
echo " "

varMETnum="39"
varMETstr="2t"
lon_val_min="20" #20
lon_val_max="25" #25
lat_val_min="60" #60
lat_val_max="65" #65
echo "varMETnum varMETstr lon_val_min lon_val_max lat_val_min lat_val_max:"
echo "$varMETnum $varMETstr $lon_val_min $lon_val_max $lat_val_min $lat_val_max"


echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "    STEP #0 : PRE-CHEKING EXISTANCE OF NECESSARY DIRECTORIES ..."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
pwd
echo "   Directory for pre-processing mod data extracted with gsv ..."
inp_dir_gsvmoddata="GSVMODDATA"
echo "$inp_dir_gsvmoddata"
if test -d "$inp_dir_gsvmoddata"; then
  echo $inp_dir_gsvmoddata exists
else
  echo $inp_dir_gsvmoddata does not exist
  echo "   Creating directory: $inp_dir_gsvmoddata"
  mkdir $inp_dir_gsvmoddata
fi

echo "   Directory for processing observation data ..."
inp_dir_dataobs="DATAOBS"
echo "$inp_dir_dataobs"
if test -d "$inp_dir_dataobs"; then
  echo $inp_dir_dataobs exists
else
  echo $inp_dir_dataobs does not exist
  echo "   Creating directory: $inp_dir_dataobs"
  mkdir $inp_dir_dataobs
fi

echo "   Directory for processing modeled data ..."
inp_dir_datamod="DATAMOD"
echo "$inp_dir_datamod"
if test -d "$inp_dir_datamod"; then
  echo $inp_dir_datamod exists
else
  echo $inp_dir_datamod does not exist
  echo "   Creating directory: $inp_dir_datamod"
  mkdir $inp_dir_datamod
fi

echo "   Directory for saving graphical output ..."
inp_dir_graphs="GRAPHS"
echo "$inp_dir_graphs"
if test -d "$inp_dir_graphs"; then
  echo $inp_dir_graphs exists
else
  echo $inp_dir_graphs does not exist
  echo "   Creating directory: $inp_dir_graphs"
  mkdir $inp_dir_graphs
fi

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "               ..... STEP 0 - COMPLETED ....."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "   "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "  STEP #1 : PRE-PROCESSING MOD DATA EXTRACTED WITH GSV_INTERFACE ..."
echo "  SCRIPT --- gsv_mod_data.sh"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

./gsv_mod_data.sh

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "               ..... STEP 1 - COMPLETED ....."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "===================================================================="
echo "         SYNOP : START LOOP OVER ALL AVAILABLE TIME-SLICES "
echo "===================================================================="

# List all available splitted files (by time-slices) in dir GSVMODDATA/
# Define YYYY MM DD HH to start calculations for time-slices

tmp_file_tstamps="file_with_tstamps.txt"
#ls GSVMODDATA/ > $tmp_file_tstamps
ls $inp_dir_gsvmoddata > $tmp_file_tstamps
tail -n 1 "$tmp_file_tstamps" | tee >(wc -c | xargs -I {} truncate "$tmp_file_tstamps" -s -{})

nrec_file=$( sed -n '$=' $tmp_file_tstamps)
echo "nrec_file: $nrec_file"
for (( nnrec=1; nnrec<=$nrec_file; nnrec++ ))
do
  b_yy=$( head -n 1 $tmp_file_tstamps | cut -c 1-4 )
  b_mm=$( head -n 1 $tmp_file_tstamps | cut -c 5-6 )
  b_dd=$( head -n 1 $tmp_file_tstamps | cut -c 7-8 )
  b_hh=$( head -n 1 $tmp_file_tstamps | cut -c 9-10 )
  #echo "b_yy, b_mm, b_dd, b_hh : $b_yy $b_mm $b_dd $b_hh"
  #tail -n +2 "$tmp_file_tstamps" > "$tmp_file_tstamps.tmp" && mv "$tmp_file_tstamps.tmp" "$tmp_file_tstamps"
  # see line above (it is added at the bottom of the script)
  echo "===================================================================="
  echo "     START CALCULATIONS FOR TIME-SLICE: $b_yy $b_mm $b_dd $b_hh "
  echo "===================================================================="

echo " "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "           STEP #2 : EXTRACTING SYNOP OBS DATA FROM ODB ..."
echo "  SCRIPT --- synop_obs.sh"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
#./synop_obs.sh 2020 01 20 00 00 00 39 21 24 61 64
#./synop_obs.sh 2020 01 20 15 00 00 $varMETnum $lon_val_min $lon_val_max $lat_val_min $lat_val_max
b_min="00"
b_sec="00"
./synop_obs.sh $b_yy $b_mm $b_dd $b_hh $b_min $b_sec $varMETnum $lon_val_min $lon_val_max $lat_val_min $lat_val_max

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "               ..... STEP 2 - COMPLETED ....."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "  STEP #3 : EXTRACTING AND INTERPOLATING MOD DATA TO SYNOP STATIONS "
echo "            GEOGRAPHICAL COORDINATES/POSITIONS"
echo "            AND ADDING SUCH INTERPOLATED MOD DATA TO ODB ..."
echo "  SCRIPT --- synop_mod.sh"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
#./synop_mod.sh 2020012000_2t_r360x180.nc 2020012000 2t
#./synop_mod.sh 2020012015_2t_r360x180.nc 2020012015 $varMETstr
b_2t="_2t_r360x180.nc"
b_yyyymmddhh=$b_yy$b_mm$b_dd$b_hh
echo "b_2t, b_yyyymmddhh = $b_2t $b_yyyymmddhh"
b_fname_yyyymmddhh_2t=$b_yyyymmddhh$b_2t
echo "b_fname_yyyymmddhh_2t = $b_fname_yyyymmddhh_2t"
./synop_mod.sh $b_fname_yyyymmddhh_2t $b_yyyymmddhh $varMETstr

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "               ..... STEP 3 - COMPLETED ....."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "     STEP #4 : SELF-CONTROL BY PLOTTING DIFFEENCE OBS VS MOD ..."
echo " SCRIPT --- graph_mod_obs.py"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
#python graph_mod_obs.py 2020012000 2t
#python graph_mod_obs.py 2020012015 $varMETstr
python graph_mod_obs.py $b_yyyymmddhh $varMETstr

mv *.png GRAPHS/

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "               ..... STEP 4 - COMPLETED ....."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "===================================================================="
echo "      END CALCULATIONS FOR TIME-SLICE: $b_yy $b_mm $b_dd $b_hh "
echo "===================================================================="

# Removing record with completed timestamp from file ... 
# in order to start next time-slice calculations
tail -n +2 "$tmp_file_tstamps" > "$tmp_file_tstamps.tmp" && mv "$tmp_file_tstamps.tmp" "$tmp_file_tstamps"

echo " "
echo "===================================================================="
echo "    Checking if STATS should be run ..."
echo "    STATS - PRODUCING QUANTILE RANK HISTOGRAM STATISTICS AND PLOTS"
echo "    SCRIPT --- synop_stats.sh"
echo "    STATS is run on a monthly basis - i.e., done on 2nd day of a month"
echo "    ... BUT NOW : $b_yy $b_mm $b_dd $b_hh"

##if [ $b_dd == "02" ] && [ $b_hh == "00" ]; then
if [ $b_dd == "21" ] && [ $b_hh == "01" ]; then 
  b_mm_start="1"
  b_mm_end="12"
  echo "    ... CALCULATING NOW : $b_yy $b_mm $b_dd $b_hh"
  echo "===================================================================="
  echo "   b_yy, b_mm, b_dd, b_hh, b_mm_start, b_mm_end : $b_yy $b_mm $b_dd $b_hh $b_mm_start $b_mm_end"
  echo "   varMETnum, b_yy, b_yy, b_mm_start, b_mm_end : $varMETnum, $b_yy, $b_yy, $b_mm_start, $b_mm_end"
./synop_stats.sh $varMETnum $b_yy $b_yy $b_mm_start $b_mm_end 
fi

echo "   Checking size of file with time stamps: $tmp_file_tstamps ..."

file_actualsize=$(wc -c <"$tmp_file_tstamps")
if [ $file_actualsize == "0" ]; then
    echo size is equal to zero bytes
    echo "===================================================================="
    echo "          SYNOP : END LOOP OVER ALL AVAILABLE TIME-SLICES "
    echo "===================================================================="
    rm $tmp_file_tstamps
    exit 1
else
    echo size is over zero bytes
fi

done

exit 1
