#!/bin/bash

# -----------------------------------------------------------
# RPC Project :
# Run RPC_visu_polar : visualization of a copolar.resu file
# P. Robert, LPP, 2014-06-22
# -----------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36` 
Narg=8
version=`RPC_version`

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

if test $# != $Narg || test $hh = 1
  then
  echo "$appli : visualization of a copolar.resu file"
  echo "$appli   require $Narg argument(s), ex:"
  echo "$appli   copolar.resu datiso1 datiso2 -3.1 0.5 10. -6. 0."
  echo "         copolar.resu: name of file created by RPC_spectro_to_polar"
  echo "         datiso1 datiso2 : iso date/time first and end"
  echo "         threshold for visualisation ex: -3.1"
  echo "         f1r f2r: frequency bounds for visualisations"
  echo "         p1 p2 for spectrogram"
  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`

FIL=$1
T1=$2
T2=$3

echo " -------------------------------------------------------------------"
echo " Run of $appli.exe :"

# test existence

if test -f $1
   then echo " content of $1 file will be visualize"
   else echo " $1 file does not exists"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 3
fi
echo " -------------------------------------------------------------------"
# traitement option 0 sur dates

echo
echo " T1,T2 asked : $T1 $T2"

datim=`RPC_decode_datiso $T1 2> /dev/null`
err=$?
if test $err = 0 
   then
      echo " input date OK"
   else
#  argument date pris a zero, on prend la date du premier et dernier bloc du fichier copolar.resu
      T1=`grep ".*T.*Z" $FIL | head -1`
      T2=`grep ".*T.*Z" $FIL | tail -1`
      datim=`RPC_decode_datiso $T1`
      err=$?
      if test $err = 0 
         then
            echo " input date taken in rff file"
         else
         echo " $appli36 : *** ERROR ! Command aborted." >&2
         echo " $appli36 has exit $err during RPC_decode_datiso"
      fi  
fi
echo " T1,T2 taken : $T1 $T2" 

# --------------------------------------------------------------
# remove RPC_ from RPC command

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

# rename possible existing appli.out file

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

# creating appli.in from arguments list

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

echo $1         > $appli.in
echo $T1  $T2  >> $appli.in
echo $4        >> $appli.in
echo $5  $6    >> $appli.in
echo $7  $8    >> $appli.in

echo " -------------------------------------------------------------------"
echo " Run of $appli :"
echo " $appli $*"

PS=`basename $1 .resu`".ps"

if test -f $PS ; then rm $PS ; fi

# run appli.exe 

. run_appli.bash 
err=$?
if test $err != 0
   then exit $err
fi

# creation eventuelle du pdf,png, visu a l'ecran

run_options_visu.bash $PS 
err=$?
if test $err != 0
   then exit $err
fi

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 " -------------------------------------------------------------------"
