#!/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 (*** only implemented in GrADS this far ***)
#
# 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, only the following is needed:
#
# 1) Rank histogram statistics files for all stations in a single file
#    (for the time range specified by the script arguments)
# 
# Execution (e.g): ./summary_rank_histograms_all_stations 39 2020 2020 1 12
# ORIGINAL:
# Jouni Räisänen, August 2023 
# MODIFIED:
# Alexander Mahura, Sep-Oct 2023
#
##################################################################
#
# Arguments:
#
# 1. Meteorological variable code
#
variable=$1
echo "   Meteorological variable code = $1"
#
# The following variables are included:
#
# 91 'total amount of clouds'
# 108 'sea level pressure'
# 80 '1-hour precipitation'
# 58 'relative humidity'
# 999 '10-minute precipitation intensity'
# 71 'snow depth'
# 39 '2m temperature'
# 40 'dew point temperature'
# 62 'visibility'
# 111 'wind direction'
# 261 'maximum wind gust in last 10 minutes'
# 221 'surface wind speed'
#
# 2.-3: First and last year
#
year1=$2
year2=$3
let nyears=year2-year1+1
#
# 4.-5: First and last month
#
month1="${4:-1}"
month2="${5:-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/jouni/to_alexander_220823/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

# Define the python environment
# On Puhti
#export PATH="/projappl/project_2001011/madeleine/python_envs/post-pp/bin:$PATH"
# On Lumi
# module use /project/project_465000454/devaraju/modules/LUMI/23.03/
#
##################################################################
#
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 -----
#
# 1) Rank histogram directory and input name file name without extension
echo "   Directory for rank histogram ..."
rh_dir=rank_histograms/${variable}_${year1}${month1}-${year2}${month2}
echo "     $rh_dir"
rh_file=${rh_dir}/rank_histograms_${variable}_${year1}${month1}-${year2}${month2}_all_stations
rh_summary_file=${rh_dir}/rank_histograms_${variable}_${year1}${month1}-${year2}${month2}_summary

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

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

##################################################################
#
# Convert the all-station ODB format rank histogram file to txt
# 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=6
  grads_out=.true.
 &END
EOF
rm rank_histogram_summary_statistics

##############################################################
#
# Read the number of stations and all-UTC p-value frequencies from
# the '${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
tail -3 ${rh_summary_file}_text_output | head -1 > line_1
read v1 v2 v3 v4 v5 v6 p01 < line_1
tail -2 ${rh_summary_file}_text_output | head -1 > line_1
read v1 v2 v3 v4 v5 v6 p1 < line_1
tail -1 ${rh_summary_file}_text_output > line_1
read v1 v2 v3 v4 v5 v6 p5 < 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
#
######################################################################
#
# Plotting in Python:
#     Madeleine converted Jouni's grads-scripts to python 
# 1 - Madeleine Ekblom, 2023-04-06 
#     python/plot_p_values_map.py
# 2 - Madeleine Ekblom, 2023-09-12
#     python/plot_rank_hist_sum_all_stations.py
#
######################################################################
#
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 ..."
#
#####################################################################

python_script=python/plot_rank_hist_sum_all_stations.py

python3 ${python_script} ${rh_summary_file} ${number_of_stations} ${p01} ${p1} ${p5} ${max_freq_p} ${max_freq_q} ${figname}

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

######################################################################
#
# Delete files that are not needed any more/ 
# ! NOT NEEDED as converted to usage of python instead of grads
#
#####################################################################
#rm summary_plot_in_grads
#rm summary_statistics.ctl
