#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# Run RPC_vectime_to_mfa: transform a GSE vectime into MFA system
# P. Robert, LPP, 2015-02-05; re. March 2018
# -------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=3
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 : transform a vectime file in GSE  into the MFA coordinates"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   toto1.rff  toto2.rff toto3.rff"
   echo "    toto1.rff : name of  input ULF VTL2 RFF file in GSE"
   echo "    toto2.rff : name of output ULF VTL2 RFF file in MFA"
   echo "    toto3.rff : name of a MAG RFF file in GSE"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo "$appli36 : *** ERROR ! Command aborted !" >&2
   exit 1
fi

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

VTL2_ULF=$1
VTL2_MFA=$2
VTL2_MAG=$3

if test ! -f $VTL2_ULF ; then echo "*** no file $VTL2_ULF" ; echo "$appli36 : *** ERROR ! Command aborted !" >&2 ; exit 1 ; fi
if test ! -f $VTL2_MAG ; then echo "*** no file $VTL2_MAG" ; echo "$appli36 : *** ERROR ! Command aborted !" >&2 ; exit 1 ; fi

# remove RPC_ from RPC command

appli=`echo $appli | cut -c5-`

echo " -------------------------------------------------------------------"
echo " Run of $appli.exe :"
echo "     - arguments are taken from $appli.in text file"
echo "     - execution report will be available in $appli.out text file"
echo
echo " VTL2 input  file= $1"
echo " VTL2 output file= $2"
echo " MAG DC file      =$3"
echo " Please wait..."
echo ""


# creating appli.in from arguments list

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

echo $VTL2_ULF  > $appli.in
echo $VTL2_MFA >> $appli.in
echo $VTL2_MAG >> $appli.in

# run appli.exe 

. run_appli.bash $appli

err=$?
exit $err
