#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
echo "Python version"
which python
##################################################################
#
# Calculation and plotting of summary statistics for quantile
# space rank histograms:
#
#   - a map of p-values
#   - bar plot of p-value distribution + rank histogram
#     averaged over all stations
#
# The following need to be given as arguments:
#
#   - variable
#   - first year of raw data
#   - last year of raw data
#   - first month of raw data (default 1)
#   - last month of raw data (default 12)
#
# As the input for this script, the following files are needed:
#
# 1) Rank histogram statistics files for all available stations
#    (for the time range specified by the script arguments)
# 
# Execution (e.g): ./summary_rank_histograms_all_stations 2010 2010 1 12

# ORIGINAL:
# Jouni Räisänen, Aug 2023
# MODIFIED:
# Alexander Mahura, Sep-Oct-Nov 2023
#
##################################################################
#
# Arguments:
#
# 0. Variable code (not needed: always T2-T850)
#
variable=T2-T850
echo "   Variable: $variable"
#
# 1.-2: First and last year
#
year1=$1
year2=$2
let nyears=year2-year1+1
#
# 3.-4: First and last month
#
month1="${3:-1}"
month2="${4:-12}"
#
# Add zero to ahead of $month1 and $month2 for naming of files if needed?
#
if [ $month1 -le 9 ]
then
  month1='0'${month1}
fi
if [ $month2 -le 9 ]
then
  month2='0'${month2}
fi
#
##################################################################
#
# Add odb_api to $PATH
#
# On Puhti
#PATH="${PATH}":/projappl/project_2001011/odb_api/bin
# On Lumi
#export PATH="/projappl/project_465000454/ama/software/odb_api/bin:$PATH"

# On Lumi
#PATH="${PATH}":/projappl/project_465000454/ama/software/odb_api/bin
#
# On Lumi (asked Devaraju Narayanappa, CSC)
# to make in /project/project_465000454/devaraju/modules/LUMI/23.03/C
# module use /project/project_465000454/devaraju/modules/LUMI/23.03/C
# module load odb_api/0.18.1-cpeCray-23.03.lua
# module load python-climatedt/3.11.3-cpeCray-23.03.lua
#
##################################################################
#
echo "   Compiling the Fortran program that produces the rank histogram summary statistics ..."
echo "      fortran-programs/rank_histogram_summary_statistics.f95"
#
gfortran fortran-programs/rank_histogram_summary_statistics.f95 -o rank_histogram_summary_statistics

##################################################################
# ----- File names. Hard-coded, at least this far -----
#
# NB: it would be more efficient to have ODB files for individual
# stations instead for the ones that include everything for all stations!
#
# 1) Rank histogram directory and input name file name without extension
echo "   Directory for rank histograms ..."
#rh_dir=/scratch/project_2001011/RHARM_to_Alexander_120923/rank_histograms/${variable}_${year1}${month1}-${year2}${month2}
rh_dir=rank_histograms/${variable}_${year1}${month1}-${year2}${month2}
echo "     $rh_dir"
echo "   Input file with rank histograms for all stations ..."
rh_file=${rh_dir}/rank_histograms_${variable}_${year1}${month1}-${year2}${month2}_all_stations
echo "     $rh_file"
echo "   Input file with sammy rank histograms ..."
rh_summary_file=${rh_dir}/rank_histograms_${variable}_${year1}${month1}-${year2}${month2}_summary
echo "     $rh_summary_file"

# 2) Name of output file(s) without extension
echo "   Name of output file without extension ..."
out_file=${rh_dir}/rh_summary_${variable}_${year1}${month1}-${year2}${month2}
echo "     $out_file"

# 3) Directory for figures
echo "   Directory for figures ..."
figure_dir=figures
echo "     $figure_dir"

##################################################################
echo "   Converting the all-station ODB format rank histogram file to txt ..."
echo "   (it is format for reading in Fortran)"
#
odb sql select \* -i ${rh_file}.odb -o ${rh_file}.txt
#
##################################################################
echo "   Calculating the rank histogram summary statistics ..."
#
./rank_histogram_summary_statistics <<EOF
 &PARAM
  infile='${rh_file}.txt'
  outfile_p_freq='${rh_summary_file}_p-freq.txt'
  outfile_p_values='${rh_summary_file}_p-values.txt'  
  outfile_q='${rh_summary_file}_q-freq.txt'  
  outfile_grads='${rh_summary_file}.grads'
  text_output='${rh_summary_file}_text_output'
  nbins_p=20
  hour1=0,hour2=24,hour_step=12
  grads_out=.true.
  l_code_in_char=.false.
 &END
EOF
##############################################################
echo "   Reading the number of stations and all-UTC p-value frequencies ..."
# from '${rh_summary_file}_text_output' file
#
head -1 ${rh_summary_file}_text_output > line_1
read v1 v2 v3 number_of_stations < line_1
head -2 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 v2 v3 v4 number_of_p_bins < line_1
head -3 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 max_freq_p < line_1
head -4 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 max_freq_q < line_1
head -5 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 v2 v3 v4 v5 v6 p01_00 < line_1
head -6 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 v2 v3 v4 v5 v6 p1_00 < line_1
head -7 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 v2 v3 v4 v5 v6 p5_00 < line_1
head -8 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 v2 v3 v4 v5 v6 p01_12 < line_1
head -9 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 v2 v3 v4 v5 v6 p1_12 < line_1
head -10 ${rh_summary_file}_text_output | tail -1 > line_1
read v1 v2 v3 v4 v5 v6 p5_12 < line_1
numbers_of_p_bins_up=$( echo "(${number_of_p_bins} + 0.5)" | bc )
echo $( echo "(${number_of_p_bins} + 0.5)" | bc ) > line_1
read number_of_p_bins_up < line_1
rm line_1

######################################################################
#
echo "   (1) Plotting - P-Values Summary on 2D map ..."
#
######################################################################

python_script=python/plot_p_values_map.py

python3 ${python_script} ${rh_summary_file}_p-values.txt ${year1}${month1}-${year2}${month2}
mv p_values.png ${figure_dir}/p-value_map_${variable}_${year1}${month1}-${year2}${month2}.png
echo "        Output file: ${figure_dir}/p-value_map_${variable}_${year1}${month1}-${year2}${month2}.png"

#####################################################################
#
echo "   (2) Plotting - Rank Histogram Summary Statistics for 00 and 12 UTCs ..."
#
#####################################################################
utc_term="UTC00"
echo "       for 00 UTC - variable : ${variable}"
python_script=python/plot_rank_hist_sum_all_stations.py
echo "        number_of_stations : ${number_of_stations}"
echo "        p01_00, p1_00, p5_00, max_freq_p, max_freq_q : ${p01_00} ${p1_00} ${p5_00} ${max_freq_p} ${max_freq_q}"

python3 ${python_script} ${rh_summary_file} ${number_of_stations} ${p01_00} ${p1_00} ${p5_00} ${max_freq_p} ${max_freq_q} ${utc_term} ${figname}

mv rank_hist_sumstats.png ${figure_dir}/rank-hist-00utc-sumstats_${variable}_${year1}${month1}-${year2}${month2}.png
echo "        Output file for 00 UTC: ${figure_dir}/rank-hist-00utc-sumstats_${variable}_${year1}${month1}-${year2}${month2}.png"

#####################################################################
utc_term="UTC12"
echo "       for 12 UTC - variable : ${variable}" 
python_script=python/plot_rank_hist_sum_all_stations.py
echo "        number_of_stations : ${number_of_stations}"
echo "        p01_12, p1_12, p5_12, max_freq_p, max_freq_q : ${p01_12} ${p1_12} ${p5_12} ${max_freq_p} ${max_freq_q}"

python3 ${python_script} ${rh_summary_file} ${number_of_stations} ${p01_12} ${p1_12} ${p5_12} ${max_freq_p} ${max_freq_q} ${utc_term} ${figname}

mv rank_hist_sumstats.png ${figure_dir}/rank-hist-12utc-sumstats_${variable}_${year1}${month1}-${year2}${month2}.png
echo "        Output file for 12 UTC: ${figure_dir}/rank-hist-12utc-sumstats_${variable}_${year1}${month1}-${year2}${month2}.png"

######################################################################
# Delete files that are not needed any more

rm rank_histogram_summary_statistics

