#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
########################################################################
# DestinE Climate DT Project
#
# Author:
# ORIGINAL:
#   Alexander Mahura: 2023-09
# UPDATED/ADDED:
#  2023-09-25
#  Converting/adding extracted and interpolated radsound mod data into odb
#  2023-09-29
#  Creating unstructured grid based on number of radiosounding stations with obs
#  Calculating weights for unstructured grid
########################################################################
#
#   STEP 3 - EXTRACTING AND INTERPOLATING MOD DATA TO RADIOSOUNDING STATIONS
#            GEOGRAPHICAL COORDINATES/POSITIONS
#            AND ADDING SUCH INTERPOLATED MOD DATA TO ODB
#
#   Reading extracted over global domain gsv modelled data
#   for selected time-slots (00 & 12 UTCs) for "variable" 
#   as alerady calculated difference between air temperature at 2 m and 850 hPa 
#   & interpolating to geographical positions of radiosounding stations
#   where observations are available for selected time-slot
# ---------------------------------------------------------------------
# Execution (fx.): 
#   ./radsound_mod_read.run 2020012000_2t_r360x180.nc 2020012000 2t
#   ./radsound_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/RADSOUND/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/RADSOUND/DATAOBS/"
inp_path_obs="DATAOBS/"
echo "      $inp_path_obs"

echo "   Output path to location of modelled data interpolated to coordinates of radiosounding stations:"
#out_path_mod="/projappl/project_465000454/ama/DEOBS/RADSOUND/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

## (!) Commented, because no need in obs.data at tme-slices ...
##input radsound file: fx. obs_radsound_2020012102.dat
##var1a="obs_radsound_"; var1b=".dat"
##infile=$var1a$timefilename$var1b
##echo "   Name of input file with radsound obs data: $infile"
##infile=$inp_path_obs$infile

#######cp list_of_all_radsound_stations.txt list_of_radsound_stations.txt
infile="list_of_radsound_stations.txt"

echo "  Name of file with fixed geographical coordinates of radiosounding staions:"
echo "  $infile"

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

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

##############################################################
# Creating unstructured grid based on number of radiosounding stations

echo "   Path and Name of input file with coordinates of radiosounding stations:"
echo "      $infile"

echo "   Counting number of records in file with geographical coordinates of radiosounding stations ..."
numrec=0
while read
do
  ((numrec=$numrec+1))
done < $infile
echo "   Number of records (numrec) in 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 radiosounding stations ..."

unstr_grid_file="grid_unstructured_radsound.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 astation alongitude alatitude aUTC00obscounts aUTC12obscounts
do
#t echo "astation alongitude alatiude: $astation $alongitude $alatitude" 
echo "$alongitude" >> $unstr_grid_file             # adding longitude values (along x)
done < $infile
echo "yvals =" >> $unstr_grid_file
while read astation alongitude alatitude aUTC00obscounts aUTC12obscounts
do
#t echo "astation alongitude alatiude: $astation $alongitude $alatitude"
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_radsound.nc
cdo genbil,$unstr_grid_file -selname,$varMETstr $ncinfile weights_radsound.nc
cdo remap,$unstr_grid_file,weights_radsound.nc $ncinfile tmpfile_radsound_new.nc

echo "   IMPORTNAT NOTE (!!!): "
echo "   in radiosounding observations for variable air temperature the code is 2"
variable="2"

#while IFS=' ' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11
while IFS=' ' read -r f1 f2 f3 f4 f5
do
   station=$f1
   longitude=$f2
   latitude=$f3
   tUTC00=$((f4))
   tUTC12=$((f5))

   #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 mod data to geographical coordinates of radiosounding stations ..."
   echo "     $station $longitude $latitude $variable"
   # 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_radsound_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_radsound_new.nc weights_radsound.nc
rm -rf grid_unstructured_radsound.txt 


###############################################################################
# Converting radsound mod data file into odb  (with import command) 
# Opt1: saving/adding records to ODB (for mod data at radsound stations locations) 
# Opt2: saving adding records to ODB (for odb already having obs radsound 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 radsound mod data into ODB ..."
# ..............................................................................
# RADSOUND data ................................................................
# Defining header for adding mod radsound data to odb file ...
header_mod_radsound_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_radsound_YYYYMMDDHH.dat file ...
sed  -i "1i ${header_mod_radsound_data}" $outfile

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

odb import -d ' ' $outfile tmp_mod_radsound_odb.odb
cat tmp_mod_radsound_odb.odb >> $mod_radsound_odb
rm -rf tmp_mod_radsound_odb.odb

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

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

exit
