#!/bin/bash

# -----------------------------------------------------------------------
# RPC Project :
# Run add_DxDy_to_BxBy : extract Dx & Dy from file 1 and create file 2
# P. Robert, ScientiDev, dec. 2020
# -----------------------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=2
echo " -------------------------------------------------------------------"
echo $appli $*


if( (test $# = 1 ) && (test $1 = -h ) ) ; then hh=1 ; else hh=0 ; fi

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : Add Dx and Dy to Bx and By"
   echo "$appli   require $Narg argument(s)"
   echo "$appli   file1 file2"
   echo "         file1: VTL2 file with 5 components, Bx,By,Bz,Dx,Dy"
   echo "         file2: VTL2 wth 3 components, Bx+Dx, By+Dy,Bz"
   echo "Example:"
   echo "$appli   GEOS1_ULF_VTL2_19771207.rff GEOS1_ULF_VTL2_19771207_DxDy.rff "
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo "$appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

file1=$1
file2=$2

# info RFF et test fichier 5 comp

set `RPC_info_rff $file1 s `

class=$1
mission=$2
satnum=$3
experi=$4
mode=$5
rep=$6
year1=$7
month1=$8
shift 8
day1=$1
hh1=$2
mm1=$3
ss1=$4
year2=$5
month2=$6
day2=$7
hh2=$8
shift 8
mm2=$1
ss2=$2
nbloc=$3
ndim=$4

nbloc=`echo $nbloc | cut -c7-` 
ndim=`echo $ndim | cut -c7-` 

if (test $rep != SRV ) && (test $rep != ISR2)
    then echo "*** coord. = $rep"
         echo " *** input file : Only SRV (GEOS) or ISR2 (CLUSTER) allowed"
         echo "$appli36 : *** ERROR ! Command aborted." >&2
         exit 2
fi

if (test $ndim != 5 ) 
    then echo " *** input file have $ndim components"
         echo " *** input file must have 5 components"
         echo "$appli36 : *** ERROR ! Command aborted." >&2
         exit 3
fi

# remove RPC_ from RPC command

appli=`echo $appli | cut -c5-`
appli32=`echo "$appli                                 " | cut -c1-31`

# creating appli.in from arguments list

if test -f $appli.in ; then \mv $appli.in $appli.in.old ; fi

echo $file1  > $appli.in
echo $file2 >> $appli.in

datim1=`date +%F'  '%H':'%M':'%S `
julsec1=`date +%s`

datiso=`date +%F'T'%H':'%M':'%S `.000Z


echo " -------------------------------------------------------------------"
echo " Run of $appli, please wait..."
echo ""

# run appli.exe 

. run_appli.bash $appli
exit $?

datim2=`date +%F'  '%H':'%M':'%S `
julsec2=`date +%s`
diff=`expr $julsec2 - $julsec1`

diffm=`echo "scale=2; $diff/60." | bc `

echo ""
echo " Starting time $run_appli : $datim1"
echo " Ending   time $run_appli : $datim2"
echo " Duration      $run_appli : $diff sec.  ($diffm mn.)"

echo " -------------------------------------------------------------------"
