#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
##################################################################
#
# Calculation of quantile space rank histograms and their
# summary statistics (MSD + p-value) 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 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_rank_histograms_all_stations 39 2020 2020 1 12
#
# ORIGINAL:
# Jouni Räisänen, Aug 2023
# MODIFIED:
# Alexander Mahura, Sep-Oct-Nov 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
#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
#
##################################################################
#
# ----- 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) 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 with 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 bootstrap MSD statistics
echo "   Directory with pre-computed bootstrap MSD statistics ..." 
#bootstrap_dir=bootstrap_statistics/${variable}_${period}
bootstrap_dir=/scratch/project_465000454/ama/STATDATASYNOP/bootstrap_statistics/${variable}_${period}
echo "     $bootstrap_dir"
msd_bootstrap_file=${bootstrap_dir}/MSD_bootstrap_${variable}_${period}_all_stations.odb

# 4) Directory for results
echo "   Directory for results ..."
outdir=rank_histograms/${variable}_${year1}${month1}-${year2}${month2}
echo "     $outdir"
if test -d "$outdir"; then
  echo $outdir exists
else
  echo $outdir does not exist
  echo "   Creating directory: $outdir"
  mkdir -p $outdir
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"
#
#
################################################################
#
# Compile the Fortran program that calculates the rank histograms
#
echo "   Compiling fortran-program to calculate rank histograms ..."
echo "      fortran-programs/rank_histograms_one_station.f95"

gfortran fortran-programs/rank_histograms_one_station.f95 -o rank_histograms_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}"

#echo ${station}
#echo ${longitude}
#echo ${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 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
#
################################################################
#
# Get the distribution of pre-computed Mean Square Deviations from
# the msd_bootstrap_file. Note that these values depend on the length
# of the time period (included in the name of msd_bootstrap_file)
# and (to a smaller extent) the selected station.
#
odb sql select \* where station=${station} -i ${msd_bootstrap_file} -o msd_bootstrap_selection
#
###############################################################
# Produce the rank histogram for one station.
# Include data for 00, 06, 12 and 18 UTC.
echo "   Producing the rank histogram for one synop station: ${station}"
#
##############################################################
#
echo "   Checking existence of file: rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2} ..."
outfile=${outdir}/rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}

echo "     $outfile"
if test -f "$outfile"; then
  echo $outfile exists
else
  echo $outfile does not exist
  echo "   Creating file: $outfile"
  touch $outfile
fi


#
./rank_histograms_one_station <<EOF
 &PARAM
  infile='sim_data'
  quantile_file='quantile_selection'
  msd_bootstrap_file='msd_bootstrap_selection'  
  outfile='${outdir}/rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}'
  hour1=0,hour2=18,hour_step=6
  year1=${year1},year2=${year2},month1=${month1},month2=${month2}
  grads_out=.false.
  outfile_grads='rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}.grads'
  l_code_in_char=.false.,l_round_hour=.false.
  &END
EOF
##############################################################
#
echo "   Converting the rank histogram file to ODB format ..."
#
#
odb import -d ' ' ${outdir}/rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2} ${outdir}/rank_histograms_${variable}_${station}_${year1}${month1}-${year2}${month2}.odb
#
# Remove unnecessary files
#
rm input.txt
rm msd_bootstrap_selection
rm quantile_selection*
rm sim_data
#
((line_number++))
done
######################################################################
echo "   Combining the rank histogram data to all stations in a single ODB file ..."
#
cat ${outdir}/rank_histograms_${variable}_*_${year1}${month1}-${year2}${month2}.odb > ${outdir}/rank_histograms_${variable}_${year1}${month1}-${year2}${month2}_all_stations.odb 
######################################################################
#
# Delete the fortran executable
#
rm rank_histograms_one_station
