#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
########################################################################
# DestinE Climate DT Project
# Author: Alexander Mahura : 2023-09-21
########################################################################

#   STEP 1 - PRE-PROCESSING MOD DATA EXTRACTED WITH GSV_INTERFACE

#  2020012008_2t_r360x180.nc
# Specify:
# (1) name of gsv extracted files at 2 m & 850 hPa (now at 700 from fake-FDB)
gsv_extr_filename_2t="2t_r360x180.nc"
gsv_extr_filename_t850="t850_r360x180.nc"

# (2) path to dir where such file is placed
#path_gsv_mod_data="/projappl/project_465000454/ama/DEOBS/SYNOP/GSVMODDATA/"
path_gsv_mod_data="GSVMODDATA/"

cd $path_gsv_mod_data; pwd

# --------------------------------------------------------------------------------
echo "   Checking if files with gsv extracted mod data (2t & t850) are available ..."

until [ -f $gsv_extr_filename_2t ]
do
     echo "   File is not available in dir: $path_gsv_mod_data"
     sleep 10
done
echo "   File found: $gsv_extr_filename_2t"

until [ -f $gsv_extr_filename_t850 ]
do
     echo "   File is not available in dir: $path_gsv_mod_data"
     sleep 10
done
echo "   File found: $gsv_extr_filename_t850"


if test -f "$gsv_extr_filename_2t"; then
  echo $gsv_extr_filename_2t exists
else
  echo $gsv_extr_filename_2t does not exist
fi

if test -f "$gsv_extr_filename_t850"; then
  echo $gsv_extr_filename_t850 exists
else
  echo $gsv_extr_filename_t850 does not exist
fi

echo "   Name of input files with extracted mod data using GSV : "
echo "   for 2t   : $gsv_extr_filename_2t"
echo "   for t850 : $gsv_extr_filename_t850"

# --------------------------------------------------------------------------------
echo "   CDO calculating difference between temperatures at 2 m and 850 hPa ..."

dif_gsv_extr_filename="2t_t850_r360x180.nc"

# cdo setname,dif_2t_t850 -sub $gsv_extr_filename_2t $gsv_extr_filename_t850 $dif_gsv_extr_filename
# in dif_gsv_extr_filename-file the difference between 2t & t850 is saved under name "2t"
cdo setname,2t -sub $gsv_extr_filename_2t $gsv_extr_filename_t850 $dif_gsv_extr_filename

echo "   and saving to file: $dif_gsv_extr_filename"

# --------------------------------------------------------------------------------
echo "   CDO extacting timestamps from file : $dif_gsv_extr_filename"

# temporary file for saving time slices
tmp_file_list_timestamp="list_timestamp.txt"

cdo showtimestamp $dif_gsv_extr_filename > $tmp_file_list_timestamp
cdo -splitsel,1 $dif_gsv_extr_filename split_

# --------------------------------------------------------------------------------
echo "   Creating names of output files using timestamps ..."
echo "   Saving individual files by time-slices ..."
for ii in 1 2 3 4 5 6 7 8
do
 sed -i 's/-//' $tmp_file_list_timestamp
done
for jj in 1 2 3 4
do
 sed -i 's/:00:00//' $tmp_file_list_timestamp
 sed -i 's/T//' $tmp_file_list_timestamp
done

filenames=($(cat $tmp_file_list_timestamp))

x=0; 
for f in $(ls split_*.nc);
#do mv $f ${filenames[$x]}_2t_r360x180.nc;
do mv $f ${filenames[$x]}_$dif_gsv_extr_filename;
let x=$x+1; 
done

rm $tmp_file_list_timestamp
###############tmp test
###   rm -rf 20200120*

exit

