#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
echo "Python version"
which python
##########################################################################
#
# Produce the following plots:
#
#  (a) individual data values (from a model simulation or a real station)
#      against the background of quantiles from the observed distribution 
#      for the same station
#  (b) rank histogram for the same station
#
# for all stations in the FMI open data sample
#
# 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) List of stations
# 2) Raw simulation data at station coordinates
#    (but here, the station observations are used as a surrogate of this file)
# 3) File including quantiles as a function of time of year
# 4) File including the rank histogram bootstrap MSD values
#
# Important note:
#
# 1) It is assumed that all the files are in ODB format. However,
#    their relevant parts are converted to text format for processing.
# 
# Execution (e.g): ./produce_standard_plot_all_stations 39 2020 2020 1 12
# ORIGINAL:
# Jouni Räisänen, August 2023 
# MODIFIED:
# Alexander Mahura, Sep-Oct 2023 
#
##################################################################
#
# Arguments:
#
# 1. 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}"
#
# Find the length of the simulation period.
# It this is 12 months or more, it is rounded to the nearest integer
# number of years. Otherwise, the number of months is recorded.
#
let nmonths=12*nyears+month2-month1-11
echo 'Number of months' $nmonths
#
n=$( echo "$nmonths / 12" | bc -l )
nyears_round=$( echo "($n + 0.5) / 1" | bc )
echo 'Number of years rounded' ${nyears_round}
#
if [ $nmonths -ge 12 ]
then
  period=${nyears_round}yr
else
  period=${nmonths}mon
fi
echo $period 
#
# 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 (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
# it has been activated in .bash_profile with --- module load python-climatedt/3.11.3-cpeCray-22.08
# On Lumi
# to make in /project/project_465000454/devaraju/modules/LUMI/23.03/C
# module use /project/project_465000454/devaraju/modules/LUMI/23.03/C
#
##################################################################
#
# ----- File names. Hard-coded, at least this far -----
#
# 1) Raw data from model simulation (this far, mimicked by observations)
echo "   Raw data from model simulation (this far, mimicked by observations) ..."
sim_dir="/scratch/project_465000454/ama/open_data_ODB"
sim_file=${sim_dir}/FMI_open_data_2010-2021.odb
echo "      $sim_file"

# 2) Pre-computed quantiles
echo "   Directory for pre-computed quantiles ..."
#quant_dir=quantiles
quant_dir=/scratch/project_465000454/ama/STATDATASYNOP/quantiles
echo "     $quant_dir"
quant_file=${quant_dir}/quant_all_stations_${variable}_4.odb

# 3) Pre-computed rank histogram data
echo "   Directory for pre-computed rank histogram data ..."
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.odb 

# 4) Directory for figures
echo "   Directory for figures ..."
figure_dir=figures/standard_plots_${variable}_${year1}${month1}-${year2}${month2}
echo "     $figure_dir"
if test -d "$figure_dir"; then
  echo $figure_dir exists
else
  echo $figure_dir does not exist
  echo "   Creating directory: $figure_dir"
  mkdir -p $figure_dir
fi

#
##################################################################
#
# It is assumed that the list of stations has been pre-produced
# and is available as a .txt file.
#
station_list=list_of_stations_${variable}.txt
echo "   List of synop stations (with geographical coordinates): $station_list"
#
#
################################################################
#
echo "   Compiling the Fortran program needed for creating the figures ..."
echo "      fortran-programs/plots_for_one_station.f95"

gfortran fortran-programs/plots_for_one_station.f95 -o plots_for_one_station

#################################################################
#
# Count the number of lines in the station list
#
n_lines=$(cat ${station_list} | wc | awk NF=1)
#
# Skip the first line which contains no station_ID
#
line_number=2
#
# Loop over all stations
#
while [ ${line_number} -le `expr $n_lines` ]
do
  head -`expr ${line_number}` ${station_list} | tail -1 > input.txt
  read station longitude latitude < input.txt
echo "   **********"
echo "   Synop station ID, longitude, latitude: ${station} ${longitude} ${latitude}"
#
################################################################
# Select the simulation data for the station (mimicked this far by observations!)
# When simulation data are available, a means to retrieve the correct data points from it must be designed.
echo "   Selecting the simulation data (mimicked this far by observations) for synop station: ${station}"
#
odb_command="odb sql 'select year,month,day,hour,value where station=${station} and (year>=${year1} and year<=${year2}) and (hour=0 or hour=6 or hour=12 or hour=18) and variable=${variable}' -i ${sim_file} -o sim_data"
eval ${odb_command}

################################################################
# Select the rank histogram data for the station
echo "   Selecting rank histogram data for synop station: ${station}"
#
odb sql select \* where station=${station} -i ${rh_file} -o rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}
#
###############################################################
# Select the quantiles for the station
echo "   Selecting the quantiles for synop station: ${station}"
#
# 00, 06, 12 and 18 UTC are picked separately, since
# select \* does not allow for parentheses (very strange)
#
rm quantile_selection_*
odb sql select \* where hour=0 and station=${station} -i ${quant_file} -o quantile_selection_00
odb sql select \* where hour=6 and station=${station} -i ${quant_file} -o quantile_selection_06
odb sql select \* where hour=12 and station=${station} -i ${quant_file} -o quantile_selection_12
odb sql select \* where hour=18 and station=${station} -i ${quant_file} -o quantile_selection_18
cat quantile_selection_* > quantile_selection
#
##############################################################
#
# Produce the plots for one station
#  1) Run the fortran program that produces the required text files
#  2) Run the python script that produces the actual plot
#  
#  NB: the fortran program also produces some files for generation of the same plots
#      in GrADS, but plotting in GrADS is omitted in this script.
#
###############################################################
./plots_for_one_station <<EOF
 &PARAM
  sim_file='sim_data'
  year1=${year1},year2=${year2},month1=${month1},month2=${month2}
  quantile_file='quantile_selection'
  rank_histogram_file='rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}'
  grads_data_time_series='time_series_${variable}_${station}_${year1}${month1}-${year2}${month2}.grads'
  grads_data_rank_histogram='rank_histogram_${variable}_${station}_${year1}${month1}-${year2}${month2}.grads'
  txt_data_time_series='time_series_${variable}_${station}_${year1}${month1}-${year2}${month2}.txt'
  txt_data_rank_histogram='rank_histogram_${variable}_${station}_${year1}${month1}-${year2}${month2}.txt'
  txt_data_quantiles='quantiles_${variable}_${station}.txt'
  l_code_in_char=.false.,l_round_6h=.false.
 &END
EOF

################################################################
# Produce the figure in Python
#
cat <<EOF > standard_plot.cfg
[data]
variable=${variable}
station_id=${station}
longitude=${longitude}
latitude=${latitude}
year_beg=${year1}
year_end=${year2}
month_beg=${month1}
month_end=${month2}

[input]
sim_data=time_series_${variable}_${station}_${year1}${month1}-${year2}${month2}.txt
quantile_sel=quantiles_${variable}_${station}.txt
rank_hists=rank_histogram_${variable}_${station}_${year1}${month1}-${year2}${month2}.txt

[output]
fig_name=${figure_dir}/standard_plot_${variable}_${station}_${year1}${month1}-${year2}${month2}_python.png
EOF

################################################################
#
echo "   Calling python to plot quantiles rank histogram for synop station: ${station}" 
python3 python/plot_quantiles_rankhist.py standard_plot.cfg

################################################################
# Remove unnecessary files
echo "   Removing unnecessary temporary files ..."

rm input.txt
rm vrange_*
rm msd_and_p-value
rm quantile_selection
rm rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}
rm sim_data
###rm msd_bootstrap_selection
###rm standard_plot_one_station
rm time_series_commands
rm time_series_${variable}_${station}_${year1}${month1}-${year2}${month2}.grads
rm rank_histogram_${variable}_${station}_${year1}${month1}-${year2}${month2}.grads
rm standard_plot.cfg
rm time_series_${variable}_${station}_${year1}${month1}-${year2}${month2}.txt
rm quantiles_${variable}_${station}.txt
rm rank_histogram_${variable}_${station}_${year1}${month1}-${year2}${month2}.txt
#
((line_number++))
done

################################################################
# The Fortran executable is not needed any more:
#
rm plots_for_one_station
rm quantile_selection_*
rm msd_and_p-value_*
rm coordinates
