#!/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
# RHARM 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:
#
# 0) It is assumed that all the files are in ODB format. However,
#    their relevant parts are converted to text format for processing.
# 1) The list of station coordinates can be retrieved from the raw
#    ODB file, but this part is commented here. 
# 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 2010 2010 1 12
#
# Jouni Räisänen, July 2023 
#
##################################################################
#
# Arguments:
#
# 0. Variable 
#
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
#
##################################################################
###module load grads
#
# Add odb_api to $PATH
#
# On Puhti
#PATH="${PATH}":/projappl/project_2001011/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 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 with pre-computed quantiles ..."
#quant_dir="quantiles"
quant_dir=/scratch/project_465000454/ama/STATDATARADSOUND/quantiles
echo "   $quant_dir"

# 4) 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/STATDATARADSOUND/bootstrap_statistics/${variable}_${period}
echo "   $bootstrap_dir"

# 5) Directory for results
echo "    Directory for output results ..."
outdir=rank_histograms/${variable}_${year1}${month1}-${year2}${month2}

#[ ! -d ${outdir} ] && mkdir ${outdir}
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
#
#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 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

#################################################################
#
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
  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}"

#
# File names
#
obs_file=${obs_dir}/station_${station}_var_2_diff_H_2_p_85000.odb
sim_file=${sim_dir}/station_${station}_var_2_diff_H_2_p_85000.odb
quant_file=${quant_dir}/quantiles_${station}_T2-T850.odb
msd_bootstrap_file=${bootstrap_dir}/MSD_bootstrap_T2-T850_${station}_${period}.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 station=${station} and (year>=${year1} and year<=${year2}) and (hour=21 or hour=22 or hour=23 or hour=0 or hour=1 or hour=2 or hour=9 or hour=10 or hour=11 or hour=12 or hour=13 or hour=14)' -i ${sim_file} -o sim_data"
eval ${odb_command}
#
###############################################################
# Select the quantiles for the nearest station.
echo "   Selecting the quantiles for radiosounding station: ${station}"
#
rm quantile_selection
odb sql select \* -i ${quant_file} -o 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 \* -i ${msd_bootstrap_file} -o msd_bootstrap_selection
#
###############################################################
# Produce the rank histogram for one station.
# Include data for 00 and 12 aUTC.
echo "   Producing the rank histogram for 00 and 12 UTCs for one radiosounding 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=12,hour_step=12
  year1=${year1},year2=${year2},month1=${month1},month2=${month2}
  grads_out=.false.
  l_code_in_char=.false.,l_round_hour=.true.
  &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
