#!/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 RHARM data sample, for T(2 m) - T(850 hPa)
#
# The following need to be given as arguments:
#
#   - 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 notes:
#
# 1) It is assumed that all the files are in ODB format. However,
#    their relevant parts are converted to text format for processing.
# 2) The rank histogram bootstrap MSD values must be available
#    for the same length of simulation as given as arguments to this script.
# 
# Execution (e.g): ./produce_standard_plot_all_stations 2010 2010
#
# ORIGINAL:
# Jouni Räisänen, July 2023
# MODIFIED:
# Alexander Mahura, Sep-Oct-Nov 2023
#
##################################################################
#
# Arguments:
#
# The Variable is always the same
#
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}"
#
# 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/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
# module load odb_api/0.18.1-cpeCray-23.03.lua
# module load python-climatedt/3.11.3-cpeCray-23.03.lua
#
#
##################################################################
#
# ----- Directory names. Hard-coded, at least this far -----
#
# 1) Raw data from observations
echo "   Directory with raw data from observations (pre-computed) ..."
#obs_dir=RHARM_vertical_differences_odb
obs_dir=/scratch/project_465000454/ama/STATDATARADSOUND/RHARM_vertical_differences_odb
echo "   $obs_dir"

# 2) Raw data from model simulation (this far, mimicked by observations)
echo "   Directory with data from model simulations (this far, mimicked by observatios) ..."
#sim_dir=RHARM_vertical_differences_odb
sim_dir=/scratch/project_465000454/ama/STATDATARADSOUND/RHARM_vertical_differences_odb
echo "   $sim_dir"

# 3) Pre-computed quantiles
echo "   Directory for pre-computed quantiles ..."
#quant_dir=quantiles
quant_dir=/scratch/project_465000454/ama/STATDATARADSOUND/quantiles
echo "     $quant_dir"

# 3) Pre-computed rank histogram data
#rh_dir=rank_histograms/${variable}_${year1}${month1}-${year2}${month2}
echo "   Directory for computed rank histogram data ..."
rh_dir=rank_histograms/${variable}_${year1}${month1}-${year2}${month2}
echo "     $rh_dir"

# 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
#
#echo "   (Note) List includes all stations with obs/data points ..."
#station_list=list_of_all_radsound_stations.txt
echo "   (Note) List includes stations with at least 1000 obs/data points ..."
station_list=list_of_valid_radsound_stations.txt
echo "          stationID, longitude, latitude, obs00UTC, obs12UTC"
echo "   List of radiosounding 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 n00 n12 < input.txt
echo "   **********"
echo "   Radiosounding stationID, longitude, latitude: ${station} ${longitude} ${latitude}"
#
sim_file=${sim_dir}/station_${station}_var_2_diff_H_2_p_85000.odb
quant_file=${quant_dir}/quantiles_${station}_T2-T850.odb
rh_file=${rh_dir}/rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}.odb
#
################################################################
# Select the simulation data for the station (mimicked this far by observations!)
echo "   Selecting the simulation data (mimicked this far by observations) for radiosounding station: ${station}"
#
odb_command="odb sql 'select year,month,day,hour,value where (year>=${year1} and year<=${year2})' -i ${sim_file} -o sim_data"
eval ${odb_command}

################################################################
# Select the rank histogram data for the station
echo "   Selecting rank histogram data for radiosounding 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
# 00 and 12 UTC are picked separately, since
# select \* does not allow for parentheses (very strange)
echo "   Selecting the quantiles for radiosounding station: ${station}"
#
rm quantile_selection
odb sql select \* -i ${quant_file} -o quantile_selection
#rm quantile_selection_*
#odb sql select \* where hour=0 and station=${station} -i ${quant_file} -o quantile_selection_00
#odb sql select \* where hour=12 and station=${station} -i ${quant_file} -o quantile_selection_12
#cat quantile_selection_* > quantile_selection
#
################################################################
#
# Run the fortran progam that prepares the plotting
#
###############################################################
#
./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=.true.
 &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}.png
EOF

################################################################
#
echo "   Calling python to plot quantiles rank histogram for radiosounding station: ${station}"
python3 python/plot_quantiles_rankhist_00_12.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 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 rank_histogram.ctl
rm time_series.ctl
rm quantile_selection_*
rm msd_and_p-val*
rm plots_for_one_station
rm coordinates
