#!/bin/bash

# -------------------------------------------------------------------
# RPC Project :
# Run RPC_visu_spectro_4Bz : visualization of 4 Bz from 4 SP.rff file
# P. Robert, LPP, 2012-06-22
# -------------------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36` 
Narg=13
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 Bz from 4 Spectrogram RFF file"
  echo "$appli   require $Narg argument(s), ex:"
  echo "$appli   SP1.rff SP2.rff SP3.rff SP4.rff datiso1 datiso2 f1 f2 pmin pmax fi1 fi2"
  echo "    SP1-4.rff       : names of 4 spectro RFF files"
  echo "    datiso1 datiso2 : iso date/time first and end"
  echo "    f1 f2           : frequency bounds to plot"
  echo "    pmin pmax       : min max power for dynamic colors"
  echo "    fi1, fi2        : frequency bounds for integrated power"
  echo "    y/n             : for position and FGM plot"
  echo " "   
  echo "$appli SP1.rff SP2.rff SP3.rff SP4.rff \\"
  echo "           2001-09-23T09:00:00.000Z 2001-09-23T11:00:00.000Z  0. 5. 0. 0. 0.2 10."
  echo "$appli SP1.rff SP2.rff SP3.rff SP4.rff  0 0  0. 0. 0. 0. 0.2 0."
  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`
 
SP1=$1
SP2=$2
SP3=$3
SP4=$4
T1=$5
T2=$6
f1=$7
f2=$8
arg1="$*"
shift 8
p1=$1
p2=$2
fi1=$3
fi2=$4
info_posmag=$5

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

# -----------------------------------------------------------
# test suffixe

suffix=`echo $SP1 | cut -f2 -d.`
if test $suffix != rff 
   then echo "$SP1 file has suffix .$suffix, not .rff"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 2
fi

suffix=`echo $SP2 | cut -f2 -d.`
if test $suffix != rff 
   then echo "$SP2 file has suffix .$suffix, not .rff"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 3
fi

suffix=`echo $SP3 | cut -f2 -d.`
if test $suffix != rff 
   then echo "$SP3 file has suffix .$suffix, not .rff"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 4
fi

suffix=`echo $SP4 | cut -f2 -d.`
if test $suffix != rff 
   then echo "$SP4 file has suffix .$suffix, not .rff"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 5
fi

# test existence

if test -f $SP1
   then echo "content of $SP1 file will be visualize"
   else echo "$SP1 file does not exists"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 6
fi

if test -f $SP2
   then echo "content of $SP2 file will be visualize"
   else echo "$SP2 file does not exists"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 7
fi

if test -f $SP3
   then echo "content of $SP3 file will be visualize"
   else echo "$SP3 file does not exists"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 8
fi

if test -f $SP4
   then echo "content of $SP4 file will be visualize"
   else echo "$SP4 file does not exists"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 9
fi

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

info=`RPC_info_rff $SP1 s`
proj=`echo $info | cut -d ' ' -f2 | cut -c1-3`
expe=`echo $info | cut -d ' ' -f4 | cut -c1-3`
mode=`echo $info | cut -d ' ' -f5 | cut -c1-3`
syst=`echo $info | cut -d ' ' -f6 | cut -c1-3`
yy=`echo $info | cut -d ' ' -f7 | cut -c1-4`
mm=`echo $info | cut -d ' ' -f8 | cut -c1-2`
dd=`echo $info | cut -d ' ' -f9 | cut -c1-2`

PS=$proj"_"$expe"_"$mode"_"$syst"_"$yy$mm$dd"_4Bz.ps"

echo " PS file=$PS"

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

# 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


# --------------------------------------------------------------
if test  $info_posmag = y 
   then

pos_ind='no'
pos_mag='no'

echo
echo " search POS and MAG data for $proj/$expe - $yy $mm $dd"

if test $proj = CLU
   then 
       RPC_get_indexed_data_CLUPOS 3 $yy $mm $dd       2> /dev/null
       pos_ind=`head -1 vectime_to_indexed_data.out`
       RPC_get_indexed_data_CLUFGM 1 $yy $mm $dd SPIN  2> /dev/null
       mag_ind1=`head -1 vectime_to_indexed_data.out`
       RPC_get_indexed_data_CLUFGM 2 $yy $mm $dd SPIN  2> /dev/null
       mag_ind2=`head -1 vectime_to_indexed_data.out`
       RPC_get_indexed_data_CLUFGM 3 $yy $mm $dd SPIN  2> /dev/null
       mag_ind3=`head -1 vectime_to_indexed_data.out`
       RPC_get_indexed_data_CLUFGM 4 $yy $mm $dd SPIN  2> /dev/null
       mag_ind4=`head -1 vectime_to_indexed_data.out`
fi

# --------------------------------------------------------------
# fin test $info_posmag
fi
# --------------------------------------------------------------


echo $SP1       > $appli.in
echo $SP2      >> $appli.in
echo $SP3      >> $appli.in
echo $SP4      >> $appli.in
echo $T1       >> $appli.in
echo $T2       >> $appli.in
echo $f1 $f2   >> $appli.in
echo $p1 $p2   >> $appli.in
echo $fi1 $fi2 >> $appli.in
echo $info_posmag >> $appli.in

if test  $info_posmag = y 
   then
   echo $pos_ind  >> $appli.in
   echo $mag_ind1 >> $appli.in
   echo $mag_ind2 >> $appli.in
   echo $mag_ind3 >> $appli.in
   echo $mag_ind4 >> $appli.in
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 " -------------------------------------------------------------------"
