#!/bin/bash

#################################################
#    Program used to creat LAGRANTO-file from   #
#    python output file                         #
#                                               #
#    by Lukas Langhamer                         #    
#    Innsbruck, Feb. 2017                       # 
#                                               #
#################################################


\rm -f reformat.param
\rm -f trainfo.param
\rm -f cp*

filetype=$1  # reference file

newdata=$2   # file to convert to LAGRANTO file
timestep=$3  # forward or backward calculation time interval in minutes


outfile=${filetype}_$4

prog1=${LAGRANTO}/goodies/reformat
prog2=${LAGRANTO}/goodies/trainfo.sh

refdate=`${prog2} ${filetype} refdate` 
timerange=`${prog2} ${filetype} timerange`

ntim=`${prog2} ${filetype} ntim` 
ntra=`${prog2} ${filetype} ntra` 
ncol=`${prog2} ${newdata} ncol`  

ntim=`expr ${ntim} - 1` # new file is shorter
timerange=`expr ${timerange} - ${timestep}` # timerange is shorter

echo "Reference date" ${refdate}" /Time range  " ${timerange} "min"  > cp_file   # Header
echo " " >> cp_file                                                              # blank
tail -n +1 -q $newdata >> cp_file                                                # data
sed '3 a\\' cp_file >> cp_file2 
sed '3 a\\' cp_file2 >> cp_file3                                                 # adding 2 lines -> nessecary for <reformat>



\rm -f reformat.param
echo '"cp_file3"'  > reformat.param
echo \"${outfile}\"  >> reformat.param
echo $ntra $ntim $ncol  >> reformat.param

${prog1}

\rm -f reformat.param
\rm -f trainfo.param
\rm -f cp*

exit 0