#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
########################################################################
# DestinE Climate DT Project
#
# Author: 
# ORIGINAL:
#    Jouni Räisänen: 2022-11
# UPDATED
#   Alexander Mahura: 2023-04-20, 2023-05, 2023-08
########################################################################
#
#          STEP 2 - EXTRACTING SYNOP OBS DATA FROM ODB
#
#   Prototype for finding surface observations from openly available
#   ground-based (SYNOP) data for a given hour
#   using files converted to ODB format
#
# ---------------------------------------------------------------------
# old - Execution (e.g.): ./odb-filename 20190728 11 39 20 25 60 65
# new - Execution (e.g.): ./odb-filename 2019 07 28 11 00 00 39 20 25 60 65
# ---------------------------------------------------------------------
# Arguments:
# ---------------------------------------------------------------------
# 1. Time in format: year, month, day
year=$1  
month=$2
day=$3
#t echo "   year, month, day = $year $month $day"
yyyymmdd=$1$2$3

# 2. Time in format: hour, minute, second
hour=$4
minute=$5
second=$6
#t echo "   hour, minute, second = $hour $minute $second"
hhmmss=$4$5$6

echo "   year, month, day, hour, minute, second = $year $month $day $hour $minute $second"

# 3. Meteorological variable code (see list below)
variable=$7

echo "   Meteorological variable code = $variable"

# List of included meteorological variables:
#   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'

# 4.-5. Minimum and maximum longitude
#       If minimum > maximum, search for stations outside the interval maximum ... -minimum
lon_min="${8:--180}"
lon_max="${9:-180}"

# 6.-7. Minimum and maximum latitude
lat_min="${10:--90}"
lat_max="${11:-90}"

# Note: if you do not give values for the last three arguments,
#       all stations will be included in the output,
#       regardless of their coordinates.
#
################################################################
#
# Increase lon_max by 360 degrees if it is smaller than lon_min
#
if [ "$(echo "$lon_max < $lon_min" | bc)" = 1 ]; then
  lon_max=$((${lon_max}+360))
fi

# --- Year-month-day part of "date" in sql access / old
#t echo "   year, monh, day    (yyyymmdd=):" $yyyymmdd
# --- Hour-minute-second-part of "time" in sql access / old
#t echo "   hour, minute, second (hhmmss=):" $hhmmss 

##################################################################
#
# ----- File names
#
##################################################################
# On Lumi (ODB as a single file)

echo "   Path to dir where ODB file is placed:"
in_dir=/scratch/project_465000454/ama/open_data_ODB
echo "      $in_dir"

echo "   Name of ODB file containing all synop obs data:"
in_file=synop_open_data_2010-2021.odb
echo "      $in_file"

echo "   Name of file for extracted synop obs data at geographical coordinates of stations:"
out_file=obs_synop_${yyyymmdd}${hour}
echo "      $out_file"
rm -f ${out_file}

##################################################################
#
# 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
#
##################################################################
#
# Input file name(s): 

in_file=${in_dir}/${in_file} 
#  echo $in_file
  if test -f "$in_file"; then
    echo $in_file exists
#
# This would show the file headers
# 
#    odb header ${in_file}
#
# This would retrieve all the contents of the input file to the output file
#
#    odb sql select \* -i ${in_file} -o ${out_file}
#
# This retrieves data from the desired date, time and variable, for all stations
# within a given area. The treatment of the longitude bounds is slightly
# complicated because there are two possibilities. 
#
# The 'eval' structure is required because odb_api does not directly recognize the bash variables.

echo "   Extracting (with odb sql) synop obs data at geographical coordinates of stations ..."
# ---------------------------------------
# On Puhti 
# ---------------------------------------
# echo "   date, time: $date $time"
#
#      odb_command="odb sql 'select date,time,longitude,latitude,variable,value where time=${hhmmss} and variable=${variable} and ((longitude>=${lon_min} and longitude<=${lon_max}) or (longitude+360>=${lon_min} and longitude+360<=${lon_max})) and latitude>=${lat_min} and latitude<=${lat_max}' -i ${in_file} -o ${out_file}"
#     eval ${odb_command}
#

# ---------------------------------------
# On Lumi
# ---------------------------------------
# Note, in overall combined .odb-file "date" became as year,month,day & "time" became as hour,minute,second
#
echo "      year, month, day, hour, minute, second : $year $month $day $hour $minute $second"

      odb_command="odb sql 'select year,month,day,hour,minute,second,longitude,latitude,variable,value,station where year=${year} and month=${month} and day=${day} and hour=${hour} and minute=${minute} and second=${second} and variable=${variable} and ((longitude>=${lon_min} and longitude<=${lon_max}) or (longitude+360>=${lon_min} and longitude+360<=${lon_max})) and latitude>=${lat_min} and latitude<=${lat_max}' -i ${in_file} -o ${out_file}"
     eval ${odb_command}

# Deleting header line (example below) in a newly created output file ...
#     year@hdr       month@hdr         day@hdr        hour@hdr      minute@hdr      second@hdr   longitude@hdr    latitude@hdr   variable@body      value@body     station@hdr
#         2020               1              20               0               0               0       24.956800       60.326698              39        3.500000          100968

echo "   Cleaning and moving extracted from odb synop obs data into separate folder DATAOBS ..."
cat ${out_file} | sed '1,1d' > ${out_file}.dat
rm -rf ${out_file}
pwd
mv ${out_file}.dat DATAOBS/

#  Some more 'odb sql' examples:
#
#       https://confluence.ecmwf.int/display/ODBAPI/Examples
#
  else
    echo $in_file does not exist
  fi

exit
