#!/bin/bash

# --------------------------------------------------------------
# RPC Project :
# Run RPC_VTL2_to_cef : transform 2 RFF VTL2 (NBR & HBR) into a cef CWF file
#
# P. Robert & R. Piberne, LPP, 2012-10-03
# 
# Updates:
# R. Katra, Feb 2016:   Commented lines about calibration caveat
#                       Section indicated by tags:
#                        CALIBRATION_CAVEATS_COMMENT_BEGIN
#                        CALIBRATION_CAVEATS_COMMENT_END
#                       except for :
#                        mv toto4.tmp toto5.tmp
#
# --------------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : tranform a RFF VTL2 into a cef CWF file"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   toto_VTL2_NBR.rff toto_VTL2_HBR.rff"
   echo "    toto_VT.rff : name of an input RFF VTL2 file"
   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`

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

# check existing files

if ! test -f $1
   then echo "non existing file $1" 
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 2
fi

if ! test -f $2
   then echo "non existing file $2" 
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 3
fi

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

# remove RPC_ from RPC command

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

# creating appli.in from arguments list

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

#for i in $*
#    do
#    echo $i >> $appli.in
#done
echo "rff_header_HBR.rff" >> $appli.in
echo "#$1        >> $appli.in
echo "#$2        >> $appli.in

# rename possible existing appli.out file

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

# set elements for cef header from RFF header(R. Piberne)

short_nbr_filename=`basename $1`
short_hbr_filename=`basename $2`

echo " -------------------------------------------------------------------"
echo "  $appli36 on $short_nbr_filename "
echo "                                  & $short_hbr_filename" 

debut_HBR=`grep -n "START INDEXED_DATA" $2`
debut_HBR=${debut_HBR%%:*} #retire le ":START INDEXED_DATA" pour ne donner que le numero de ligne

fin_HBR=`grep -n "END INDEXED_DATA" $2`
fin_HBR=${fin_HBR%%:*} #retire le ":END INDEXED_DATA" pour ne donner que le numero de ligne

fin_HBR_fichier=`wc -l $2`
fin_HBR_fichier=${fin_HBR_fichier%% *}

sed -n -e "1,$debut_HBR p" -e "$fin_HBR,$fin_HBR_fichier p" $2 > rff_header_HBR.rff

# run appli.exe 
# -------------

echo " -------------------------------------------------------------------"
path=`dirname $0`/../bin/$appli.exe
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 " Please wait..."
echo ""

 $path < $appli.in > $appli.out
#echo rff_header_HBR.rff | $RPC_DIR/bin/vectime_L2_to_cef.exe >  $appli.out
echo " ==> $appli.out text file available"

# complete cef header by VTL2 data (R. Piberne)
# ================================

# generic cefname and caveat name

sat=${short_nbr_filename:3:1}

frame=${short_nbr_filename:16:4}

echo frame=$frame

if test $frame = "GSE_"
then
        frame="GSE"
        date=${short_nbr_filename:24:8}
else
        date=${short_nbr_filename:25:8}
fi

#cefname=`tail -1 $appli.out`
cefname="C"$sat"_CP_STA_CWF_"$frame"__"$date"*.cef"
caveat="C"$sat"_CQ_STA_CALIBRATION_CAVEATS__"$date"*.cef"

echo ""
echo " cef file : $cefname"
echo " header created !"

valid=`echo $cefname | cut -c3-10`
if test "$valid" != "_CP_STA_"
   then 
       echo "file $cefname "
       echo "is not a valid name"
       echo ""
       echo " $appli36 : *** ERROR ! Command aborted." >&2
       exit 4
fi

# process NBR data
# ----------------

echo
echo " Process NBR data, please wait..."
#Recherche début des données
debut_NBR=`grep -n "START INDEXED_DATA" $1`
debut_NBR=${debut_NBR%%:*} #retire le ":START INDEXED_DATA" pour ne donner que le numero de ligne

#Recherche fin des données
fin_NBR=`grep -n "END INDEXED_DATA" $1`
fin_NBR=${fin_NBR%%:*} #retire le ":END INDEXED_DATA" pour ne donner que le numero de ligne

#Recherche fin du fichier
fin_NBR_fichier=`wc -l $1`
fin_NBR_fichier=${fin_NBR_fichier%% *}

#Suppression du header et de la queue
sed -e "1,$debut_NBR d" -e "$fin_NBR,$fin_NBR_fichier d" $1 > toto_NBR.tmp

#Ajout du interval
if test $sat="1"
then
#        time_interval=0.019999 #0.019999533
        time_interval=0.039999 #0.039999067

fi
if test $sat="2"
then
#        time_interval=0.020000 #0.019999711
        time_interval=0.039999 #0.039999422
fi
if test $sat="3"
then
#        time_interval=0.020000 #0.019999867
        time_interval=0.040000 #0.039999733
fi
if test $sat="4"
then
#        time_interval=0.020000 #0.019999867
        time_interval=0.040000 #0.039999733
fi
sed "s/Z,/Z,$time_interval,/g" toto_NBR.tmp > toto_NBR2.tmp
echo " Done..."

# process HBR data
# ----------------

echo
echo " Process HBR data, please wait..."
debut_HBR=`grep -n "START INDEXED_DATA" $2`
debut_HBR=${debut_HBR%%:*} #retire le ":START INDEXED_DATA" pour ne donner que le numero de ligne

fin_HBR=`grep -n "END INDEXED_DATA" $2`
fin_HBR=${fin_HBR%%:*} #retire le ":END INDEXED_DATA" pour ne donner que le numero de ligne

fin_HBR_fichier=`wc -l $2`
fin_HBR_fichier=${fin_HBR_fichier%% *}

sed -n -e "1,$debut_HBR p" -e "$fin_HBR,$fin_HBR_fichier p" $2 > rff_header_HBR.rff
sed -e "1,$debut_HBR d" -e "$fin_HBR,$fin_HBR_fichier d" $2 > toto_HBR.tmp

if test $sat="1"
then
        time_interval=0.002222 #0.00222217
fi
if test $sat="2"
then
        time_interval=0.002222 #0.00222219
fi
if test $sat="3"
then
        time_interval=0.002222 #0,002222207
fi
if test $sat="4"
then
        time_interval=0.002222 #0,002222207
fi
sed "s/Z,/Z,$time_interval,/g" toto_HBR.tmp > toto_HBR2.tmp
echo " Done..."

# files concatenation of NBR & HBR data and time sort data
# --------------------------------------------------------

echo
echo " Sort NBR and HBR data, please wait..."
cat toto_NBR2.tmp toto_HBR2.tmp > toto3.tmp

#Tri du fichier 
if ((test -s toto_NBR2.tmp ) && (test -s toto_HBR2.tmp ))
then
        echo "      Both NBR and HBR files contains data, so sort data..."
        sort -d toto3.tmp > toto4.tmp
else
        echo "      At least one file is empty, so no sort..."
        mv toto3.tmp toto4.tmp
fi
echo " Done..."


mv toto4.tmp toto5.tmp

# add data to cef header
# ---------------------

echo
echo " Add NBR & HBR data to cef header, please wait..."

cef_fichier=`ls $cefname`
echo 'DATA_UNTIL = "END_OF_FILE"' >> $cef_fichier
cat $cef_fichier toto5.tmp > toto6.tmp
mv toto6.tmp $cef_fichier

#Ajout de END_OF_FILE
echo "END_OF_FILE" >> $cef_fichier

echo " Done..."
echo ""
echo " $appli36 : *** ERROR ! Command aborted." >&2

# cleaning useless files
# ----------------------

rm -f toto*.tmp
rm -f test_cal*.tmp
if test -s cal_stat2.tmp
   then
   rm -f *CWF*CAVEAT*.cef
fi
rm -f cal_stat*.tmp
rm -f rff_header_HBR.rff
   
# 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 " ==================================================================="

