#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# RPC_get_data_GEOPOS : get VTL2 data in RFF database
#                            search also .gz files
# P. Robert, LPP, 2014-11-14, rev Nov 2020
# -------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : get CLUSTER/POS"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   SatNum year month day"
   echo "    ex: $appli 3 2001 09 23"
   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`

SatNum=$1
year=$2
month=$3
day=$4

# Spacecraft test
# ---------------

if ( (test $SatNum -lt 1 ) || (test $SatNum -gt 4 ) )
   then 
        echo "$appli : S/C # must be >=1 and =< 4"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 2
fi

# get data

RPC_get_data_GEOPOS $*
err=$?
if test $err != 0
   then echo "*** no POS rff file"
        echo " $appli36 : ***BAD TERMINATION " >&2
        exit 1
   else pos_rff=`head -1 get_data_GEOPOS.out`
        pos_ind=`basename $pos_rff .rff`.ind
        RPC_vectime_to_indexed_data $pos_rff $pos_ind
		echo $pos_ind > vectime_to_indexed_data.out
fi

# end of command
# --------------

datim2=`date +%F'  '%H':'%M':'%S `
julsec2=`date +%s`
diff=`expr $julsec2 - $julsec1`
diffm=`echo "scale=2; $diff/60." | bc `

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

echo 
echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2

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

