#!/bin/sh
#
#Script sh d'extraction de fichier SCA des sequances Diodes a Bruit (DAB) 
#et Data des cycles ON/OFF
#Bash script that extract from SCA files the DAB/DATA phases in ON/OFF cycles
#It is supposed that the sequence is: DAB1 DATA1 DAB2 DATA2
#and that the DAB1 and DAB2 have equal number of subsequences
# Auteur: JE Campagne (LAL Orsay)
# Creation: 11 Avril 2011
# Revision: 12 Avril 2011
#  o 12/04/11: mv date format to %Y-%m-%d %T for further treatment by 'date' 
#              system function 

#Basic tuning of system fuction used.
AWK=/bin/awk
SED=/bin/sed
GREP=/bin/grep
WC=/usr/bin/wc
RM=/bin/rm
CAT=/bin/cat
TOUCH=/bin/touch
DATE=/bin/date
ECHO=/bin/echo
TMP="/tmp"

if [ ! $# = 1 ]
    then
    $ECHO "usage: ./sca.sh <file>"
    exit 1
fi


fullfile=$1
if [ ! -e $fullfile ] 
    then
    $ECHO "$fullfile not found: FATAL"
    exit 1
fi

#The output file would be in "fullprocfile"
filename=$(basename $fullfile)
#extension=${filename##*.}
filename=${filename%.*}
extension=".sum"
fullprocfile=${filename}${extension}
#create empty output file
if [ -e $fullprocfile ] 
    then
    $RM $fullprocfile
fi
$TOUCH $fullprocfile


#extract the PERI lines valid for DAB and DATA
OUT1=${TMP}/tmp1.$$
$AWK '
$1=="PERI" {
   print "CYCLE",$6,$2,$3,$7,$8
  }
' $fullfile > $OUT1


#Sequence of the cycle numbers (perform a unique filtering)
tableau=( `cat $OUT1 | $AWK '{print $2}'` )
IFS='
'
tableau=( $( printf "%s\n" "${tableau[@]}" | awk 'x[$0]++ == 0' ) )

#scan each cycle
OUT2=${TMP}/tmp2.$$
for i in ${tableau[@]}
do
#extract the DAB calibration phases. Hypothesis: 2 identical sequences 
  CALIB=${TMP}/tmp_cycle_${i}_calib.$$
  $AWK -v cycle=$i '$2==cycle && $5=="-1" {print $0} ' $OUT1 > $CALIB
  nlines=`$WC $CALIB | $AWK '{print $1}'`
  start1=1
  let "end1=nlines/2"
  let "start2=end1+1"
  end2=$nlines

#date Start/End of DAB1 
  $SED -e "${start1}!d" $CALIB | $AWK -v cycle=$i 'BEGIN {ORS=","} {
    split($4,b,".");   
    print cycle "," $3,b[1]}' >> ${OUT2}
  $SED -e "${end1}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
    split($4,b,".");   
    print $3,b[1]}' >> ${OUT2}


#date Start/End of DATA1: use date system function to add the duration to
#compute the End,cf. End = Start + duration 
  $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="1" ) {
    split($3,a,"/"); 
    split($4,b,".");   
    dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
    duree=$6 " sec";
    cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
   (cmd | getline dateEnd);
    close(cmd);  
    ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
    ORS=","; print dateEnd;
  } ' $OUT1 >> ${OUT2}
 
#same as DAB1 for DAB2
  $SED -e "${start2}!d" $CALIB | $AWK 'BEGIN {ORS=","} {
    split($4,b,".");   
    print $3,b[1]}' >> ${OUT2}
  $SED -e "${end2}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
     split($4,b,".");   
     print $3,b[1]}' >> ${OUT2}
#same as DATA1 for DATA2
  $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="2" ) {
    split($3,a,"/"); 
    split($4,b,".")   
    dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
    duree=$6 " sec";
    cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
   (cmd | getline dateEnd);
    close(cmd);  
    ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
    ORS="\n"; print  dateEnd;
  } ' $OUT1 >> ${OUT2}
done

#transform all the date/time into %Y-%m-%d %T
$AWK -v datesys=$DATE ' 
BEGIN { FS=","; OFS="," }
{
  for (i=2;i<=NF;i++) {
    split($i,a,"/");
    newdate=a[2]"/"a[1]"/"a[3];
    cmd=sprintf("%s --date \"%s\" \"+%%Y-%%m-%%d %%T\" ",datesys,newdate);
    (cmd | getline newdate);
    close(cmd);
    $i=newdate;
  } 
  print $0;
}' ${OUT2} > ${fullprocfile}


