#!/bin/bash

# -----------------------------------------------------------
# RPC Project :
# Run RPC_visu_spectro_add_mag_pos : visualization of a SP.rff file
# P. Robert, LPP, 2012-06-22
# -----------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36` 
Narg=11
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 Spectrogram RFF file"
  echo "$appli   require $Narg argument(s), ex:"
  echo "$appli   SP.rff datiso1 datiso2 f1 f2 pmin pmax XY fi1 fi2"
  echo "    SP.rff          : name of a spectro RFF file"
  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 "    XY or LR        : for XYZ or Left Right Z"
  echo "    fi1, fi2        : frequency bounds for integrated power"
  echo "    info POS & MAG  : y/n "
  echo " "   
  echo "$appli   toto_SP.rff  2001-09-23T09:00:00.000Z 2001-09-23T11:00:00.000Z  0. 5. 0. 0. XY 0.2 10. y"
  echo "$appli   toto_SP.rff  0 0  0. 200. -9.6 -3. LR 0. 200. y"
  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 " -------------------------------------------------------------------"
echo " Run of $appli :"
echo " $appli $*"

SP=$1
T1=$2
T2=$3
f1=$4
f2=$5
p1=$6
p2=$7
XY=`echo $8 | cut -c1-2`
shift 8
fi1=$1
fi2=$2
info_posmag=$3

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

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

# test existence

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

# --------------------------------------------------------------
# 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 BLOCK_FIRST_INDEX du fichier rff
      T1=`grep BLOCK_FIRST_INDEX $SP | sed 's/.*STR): //'`
      T2=`grep BLOCK_LAST_INDEX  $SP | sed 's/.*STR): //'`
      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"

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

# possible plot of POS and MAG data

miss=`grep MISSION_NAME       $SP | sed 's/.*STR): //'`
expm=`grep EXPERIMENT_NAME    $SP | sed 's/.*STR): //'`
isat=`grep OBSERVATORY_NUMBER $SP  | sed 's/.*INT): //'`
dat1=`grep BLOCK_FIRST_INDEX  $SP | sed 's/.*STR): //'`

# cas mission sation mobile
set `echo $miss`
miss=$1

set `RPC_decode_datiso $dat1`
    
yy=$1
mm=$2
dd=$3

pos_ind='no'
mag_ind='no'

echo
echo " search POS and MAG data for $miss/$isat/$expm - $yy $mm $dd"

if test $miss = CLUSTER 
   then 
       RPC_get_indexed_data_CLUPOS $isat $yy $mm $dd 2> /dev/null
       if test -f vectime_to_indexed_data.out
          then
          pos_ind=`head -1 vectime_to_indexed_data.out`
       fi
       RPC_get_indexed_data_CLUFGM $isat $yy $mm $dd SPIN 2> /dev/null
       if test -f vectime_to_indexed_data.out
          then
          mag_ind=`head -1 vectime_to_indexed_data.out`
       fi
fi

if test $miss = GEOS 
   then 
       RPC_get_indexed_data_GEOPOS $isat $yy $mm $dd 2> /dev/null
       if test -f vectime_to_indexed_data.out
          then
          pos_ind=`head -1 vectime_to_indexed_data.out`
       fi
       RPC_get_indexed_data_GEOMAG $isat $yy $mm $dd 2> /dev/null
       if test -f vectime_to_indexed_data.out
          then
          mag_ind=`head -1 vectime_to_indexed_data.out`
       fi
fi
# --------------------------------------------------------------

if test $pos_ind = no
   then info_posmag='n'
   echo "no pos data found..."
fi
if test $mag_ind = no 
   then info_posmag='n'
   echo "no mag data found..."
fi

# fin test $info_posmag
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

echo $SP        > $appli.in
echo $T1       >> $appli.in
echo $T2       >> $appli.in
echo $f1 $f2   >> $appli.in
echo $p1 $p2   >> $appli.in
echo $XY       >> $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_ind >> $appli.in
fi

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

PS=`basename $SP .rff`".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

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