#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
########################################################################
# DestinE Climate DT Project
#
# Author:
# ORIGINAL:
#   Alexander Mahura: 2023-08, 2023-09
# UPDATED/ADDED:
#  2023-08-14
#  Converting/adding extracted and interpolated synop mod data into odb
#  2023-09-19
#  Creating unstructured grid based on number of synop stations with obs
#  Calculating weights for unstructured grid
########################################################################
#
#   STEP 3 - EXTRACTING AND INTERPOLATING MOD DATA TO SYNOP STATIONS
#            GEOGRAPHICAL COORDINATES/POSITIONS
#            AND ADDING SUCH INTERPOLATED MOD DATA TO ODB
#
#   Reading extracted over global domain gsv modelled data
#   for selected time-slot for selected variable &
#   interpolating to geographical positions of synop stations
#   where observations are available for selected time-slot
# ---------------------------------------------------------------------
# Execution (fx.): 
#   ./synop_mod_read.run 2020012000_2t_r360x180.nc 2020012000 2t
#   ./synop_mod_read.run 2020012015_2t_r360x180.nc 2020012015 $varMETstr

echo "   Input path to location of files with extracted (using gsv_interface) modelled data:"
#inp_path_mod="/projappl/project_465000454/ama/DEOBS/SYNOP/GSVMODDATA/"
inp_path_mod="GSVMODDATA/"
echo "      $inp_path_mod"

echo "   Input path to location of files with extracted (using sql) observed data:"
#inp_path_obs="/projappl/project_465000454/ama/DEOBS/SYNOP/DATAOBS/"
inp_path_obs="DATAOBS/"
echo "      $inp_path_obs"

echo "   Output path to location of modelled data interpolated to coordinates of synop stations:"
#out_path_mod="/projappl/project_465000454/ama/DEOBS/SYNOP/DATAMOD/"
out_path_mod="DATAMOD/"
echo "      $out_path_mod"

# Input name of nc-file with modelled data extracted with gsv_interface
ncinfile=$1 
ncinfile=$inp_path_mod$ncinfile

# Input time for making filename
timefilename=$2
# Input name for meteo.parameter etracted with gsv
varMETstr=$3

#input synop file: fx. obs_synop_2020012102.dat
var1a="obs_synop_"; var1b=".dat"
infile=$var1a$timefilename$var1b
echo "   Name of input file with synop obs data: $infile"

infile=$inp_path_obs$infile

# Name of output file with interpolated modelled data to positions of synop stations
vardef="_"
outfile="mod_synop$vardef$timefilename$vardef$varMETstr.dat"

echo "   Name of output file for interpolated mod data: $outfile"

##############################################################
# Creating unstructured grid based on number of synop stations with observations

echo "   Path and Name of input file with synop obs data:"
echo "      $infile"

echo "   Counting number of records in obs data file ..."
numrec=0
while read
do
  ((numrec=$numrec+1))
done < $infile
echo "   Number of records (numrec) in obs data file: $numrec"

# example: obs_synop_2020012001.dat
#              2020  1  20  1  0  0  24.956800  60.326698  39  3.500000 100968
#t while read ayear amonth aday ahour aminute asecond alongitude alatitude avariable avalue astation
#t do
#t    echo "$ayear, $amonth, $aday, $ahour, $aminute, $asecond, $alongitude, $alatitude, $avariable, $avalue, $astation"
#t done < $inpfile

echo "   Creating unstructured grid for 2D geographical locations of synop stations ..."

unstr_grid_file="grid_unstructured_synop.txt"
echo "   Name of file with unstructured grid: $unstr_grid_file"

echo "gridtype = unstructured" > $unstr_grid_file  # adding grid type
echo "gridsize = $numrec" >> $unstr_grid_file      # adding number of grids
echo "xvals =" >> $unstr_grid_file
while read ayear amonth aday ahour aminute asecond alongitude alatitude avariable avalue astation
do
echo "$alongitude" >> $unstr_grid_file             # adding longitude values (along x)
done < $infile
echo "yvals =" >> $unstr_grid_file
while read ayear amonth aday ahour aminute asecond alongitude alatitude avariable avalue astation
do
echo "$alatitude" >> $unstr_grid_file              # adding latitude values (along y)
done < $infile

##############################################################
# Calculating weights for unstructured grid ...

echo "   CDO calculating weights for unstructured grid ... "
#cdo genbil,$unstr_grid_file -selname,2t $ncinfile weights_synop.nc
cdo genbil,$unstr_grid_file -selname,$varMETstr $ncinfile weights_synop.nc
cdo remap,$unstr_grid_file,weights_synop.nc $ncinfile tmpfile_synop_new.nc

while IFS=' ' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11
do
   year=$((f1))       # converting integer $((f#))
   month=$((f2))
   day=$((f3))
   hour=$((f4))
   minute=$((f5))
   second=$((f6))
   longitude=$f7
   latitude=$f8 
   variable=$((f9))
   value=$f10
   station=$f11

   longitude=`echo $longitude` # excluding possible empty spaces in names
   latitude=`echo $latitude`
   
   namevar="lon="${longitude}_"lat="${latitude}
   #t echo "namevar: $namevar"
   echo "  CDO interpolating synop mod data to geographical coordinates of observational points ..."
   echo "     $longitude $latitude $variable $value $station"
   # without weights - original ...
   #cdo -s -outputtab,date,time,lon,lat,value -remapnn,"lon=${longitude}_lat=${latitude}" -selvar,${varMETstr} $ncinfile > tmpout_${station}_${variable}.txt
   # with weight - modified
   cdo -s -outputtab,date,time,lon,lat,value -remapnn,"lon=${longitude}_lat=${latitude}" -selvar,${varMETstr} tmpfile_synop_new.nc > tmpout_${station}_${variable}.txt
   cat tmpout_${station}_${variable}.txt | sed '1,1d' > tmpoutrev_${station}_${variable}.txt # deliting header line
   sed -i s/$/" "${variable}/ tmpoutrev_${station}_${variable}.txt  # adding variable ID
   sed -i s/$/" "${station}/ tmpoutrev_${station}_${variable}.txt   # adding station ID
   cat tmpoutrev_${station}_${variable}.txt >> $outfile             # adding record to summary file
   rm -rf tmpout_*.txt tmpoutrev_*.txt                              # cleaning tmp-files

done < $infile

# Fixing time-slice structure from cdo processesed (YYYY-MM-DD HH-MM-SS) to sql-odb (YYYY MM DD HH MM SS)
sed -i 's/-/  /' $outfile
sed -i 's/-/  /' $outfile
sed -i 's/:/  /' $outfile
sed -i 's/:/  /' $outfile

# Removing extra temporary files ...
rm -rf tmpfile_synop_new.nc weights_synop.nc
rm -rf grid_unstructured_synop.txt 


###############################################################################
# Converting synop mod data file into odb  (with import command) 
# Opt1: saving/adding records to ODB (for mod data at synop stations locations) 
# Opt2: saving adding records to ODB (for odb already having obs synop data)
# 
# modify-update (& unify) according to different types of obs data:
#           SYNOP (only at ground), 
#           RADSOUND (at 3D - lat/lon/alt)
#           SATELLITE (at 3D - area within lat/lon/alt)

echo "   Converting/adding/saving records with synop mod data into ODB ..."
# ..............................................................................
# SYNOP data ...................................................................
# Defining header for adding mod synop data to odb file ...
header_mod_synop_data="year@descr:integer month@descr:integer day@descr:integer hour@descr:integer minute@descr:integer second@descr:integer longitude@descr:real latitude@descr:real value@descr:real variable@descr:integer station@descr:string"

# Inserting header line as 1st line into mod_synop_YYYYMMDDHH.dat file ...
sed  -i "1i ${header_mod_synop_data}" $outfile

# Adding data to odb file ...
mod_synop_odb="/scratch/project_465000454/ama/open_data_ODB/synop_mod_data_2010-2021.odb"   # Opt1 - new
##mod_synop_odb="/scratch/project_465000454/ama/open_data_ODB/synop_open_data_2010-2021.odb"  # Opt2 - original
echo "   Path and Name of odb-file with saved records:"
echo "      $mod_synop_odb"

odb import -d ' ' $outfile tmp_mod_synop_odb.odb
cat tmp_mod_synop_odb.odb >> $mod_synop_odb
rm -rf tmp_mod_synop_odb.odb

# Deleting header line as 1st line in mod_synop_YYYYMMDDHH.dat file ... 
sed -i -e "1d" $outfile

# Moving file (with interpolated modelled data) to output dir 
mv $outfile $out_path_mod

# ..............................................................................
# RADSOUND data ................................................................

# Defining header for adding mod radsound data to odb file ...
# header_mod_radsound_data=

# Inserting header line as 1st line into mod_radsound_YYYYMMDDHH.dat file ...

# Adding data to odb file ...

# Deleting header line as 1st line in mod_radsound_YYYYMMDDHH.dat file ...

# ..............................................................................
# SATELLITE data ...............................................................

# Defining header for adding mod satellite data to odb file ...
# header_mod_satellite_data=

# Inserting header line as 1st line into mod_satellite_YYYYMMDDHH.dat file ...

# Adding data to odb file ...

# Deleting header line as 1st line in mod_satellite_YYYYMMDDHH.dat file ...

exit
