| [625] | 1 | #!/bin/sh
 | 
|---|
| [507] | 2 | #
 | 
|---|
 | 3 | #Script sh d'extraction de fichier SCA des sequances Diodes a Bruit (DAB) 
 | 
|---|
 | 4 | #et Data des cycles ON/OFF
 | 
|---|
 | 5 | #Bash script that extract from SCA files the DAB/DATA phases in ON/OFF cycles
 | 
|---|
 | 6 | #It is supposed that the sequence is: DAB1 DATA1 DAB2 DATA2
 | 
|---|
 | 7 | #and that the DAB1 and DAB2 have equal number of subsequences
 | 
|---|
 | 8 | # Auteur: JE Campagne (LAL Orsay)
 | 
|---|
 | 9 | # Creation: 11 Avril 2011
 | 
|---|
 | 10 | # Revision: 12 Avril 2011
 | 
|---|
 | 11 | #  o 12/04/11: mv date format to %Y-%m-%d %T for further treatment by 'date' 
 | 
|---|
 | 12 | #              system function 
 | 
|---|
 | 13 | 
 | 
|---|
 | 14 | #Basic tuning of system fuction used.
 | 
|---|
 | 15 | AWK=/bin/awk
 | 
|---|
 | 16 | SED=/bin/sed
 | 
|---|
 | 17 | GREP=/bin/grep
 | 
|---|
 | 18 | WC=/usr/bin/wc 
 | 
|---|
 | 19 | RM=/bin/rm
 | 
|---|
 | 20 | CAT=/bin/cat
 | 
|---|
 | 21 | TOUCH=/bin/touch
 | 
|---|
 | 22 | DATE=/bin/date
 | 
|---|
 | 23 | ECHO=/bin/echo
 | 
|---|
 | 24 | LS=/bin/ls
 | 
|---|
 | 25 | MKDIR=/bin/mkdir
 | 
|---|
 | 26 | DefaultIFS=$' \t\n'
 | 
|---|
 | 27 | IFS=$DefaultIFS
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | scriptName="`basename $0`"
 | 
|---|
 | 31 | $ECHO "Processing script ${scriptName} at `date`"
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | #temporary files to synchronize scripts
 | 
|---|
| [516] | 34 | tmppublicpath=${TMPPUBLICPATH}
 | 
|---|
| [507] | 35 | $LS -l ${tmppublicpath} > /dev/null
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | #Path where the job will do temporary IO
 | 
|---|
| [579] | 38 | . ${SCRIPTPATH}/set_iojobpath.sh
 | 
|---|
 | 39 | iojobpath=$(set_iojobpath)
 | 
|---|
| [507] | 40 | cd ${iojobpath}
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | if [ ! $# = 1 ]
 | 
|---|
 | 44 |     then
 | 
|---|
 | 45 |     $ECHO "FATAL  (${scriptName}): usage: ./sca.sh <file>"
 | 
|---|
 | 46 |     exit 1
 | 
|---|
 | 47 | fi
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | fullfile=$1
 | 
|---|
 | 51 | if [ ! -e $fullfile ] 
 | 
|---|
 | 52 |     then
 | 
|---|
| [625] | 53 |     $ECHO "FATAL (${scriptName}): $fullfile not found: FATAL"
 | 
|---|
| [507] | 54 |     exit 1
 | 
|---|
 | 55 | fi
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | #The output file would be in "fullprocfile"
 | 
|---|
 | 58 | filename=$(basename $fullfile)
 | 
|---|
 | 59 | #extension=${filename##*.}
 | 
|---|
 | 60 | filename=${filename%.*}
 | 
|---|
 | 61 | extension=".sum"
 | 
|---|
 | 62 | fullprocfile=${filename}${extension}
 | 
|---|
 | 63 | #create empty output file
 | 
|---|
 | 64 | $RM -f $fullprocfile
 | 
|---|
 | 65 | $TOUCH $fullprocfile
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | #extract the PERI lines valid for DAB and DATA
 | 
|---|
 | 69 | OUT1=./tmp1.$$
 | 
|---|
 | 70 | $AWK '
 | 
|---|
 | 71 | $1=="PERI" {
 | 
|---|
 | 72 |    print "CYCLE",$6,$2,$3,$7,$8
 | 
|---|
 | 73 |   }
 | 
|---|
 | 74 | ' $fullfile > $OUT1
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | #Sequence of the cycle numbers (perform a unique filtering)
 | 
|---|
 | 78 | tableau=( `cat $OUT1 | $AWK '{print $2}'` )
 | 
|---|
 | 79 | IFS='
 | 
|---|
 | 80 | '
 | 
|---|
 | 81 | tableau=( $( printf "%s\n" "${tableau[@]}" | awk 'x[$0]++ == 0' ) )
 | 
|---|
 | 82 | IFS=$DefaultIFS
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | #scan each cycle
 | 
|---|
 | 85 | OUT2=./tmp2.$$
 | 
|---|
 | 86 | for i in ${tableau[@]}
 | 
|---|
 | 87 | do
 | 
|---|
 | 88 | #extract the DAB calibration phases. Hypothesis: 2 identical sequences 
 | 
|---|
 | 89 |   CALIB=./tmp_cycle_${i}_calib.$$
 | 
|---|
 | 90 |   $AWK -v cycle=$i '$2==cycle && $5=="-1" {print $0} ' $OUT1 > $CALIB
 | 
|---|
 | 91 |   nlines=`$WC $CALIB | $AWK '{print $1}'`
 | 
|---|
 | 92 |   start1=1
 | 
|---|
 | 93 |   let "end1=nlines/2"
 | 
|---|
 | 94 |   let "start2=end1+1"
 | 
|---|
 | 95 |   end2=$nlines
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | #date Start/End of DAB1 
 | 
|---|
 | 98 |   $SED -e "${start1}!d" $CALIB | $AWK -v cycle=$i 'BEGIN {ORS=","} {
 | 
|---|
 | 99 |     split($4,b,".");   
 | 
|---|
 | 100 |     print cycle "," $3,b[1]}' >> ${OUT2}
 | 
|---|
 | 101 |   $SED -e "${end1}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
 | 
|---|
 | 102 |     split($4,b,".");   
 | 
|---|
 | 103 |     print $3,b[1]}' >> ${OUT2}
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | #date Start/End of DATA1: use date system function to add the duration to
 | 
|---|
 | 107 | #compute the End,cf. End = Start + duration 
 | 
|---|
 | 108 |   $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="1" ) {
 | 
|---|
 | 109 |     split($3,a,"/"); 
 | 
|---|
 | 110 |     split($4,b,".");   
 | 
|---|
 | 111 |     dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
 | 
|---|
 | 112 |     duree=$6 " sec";
 | 
|---|
 | 113 |     cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
 | 
|---|
 | 114 |    (cmd | getline dateEnd);
 | 
|---|
 | 115 |     close(cmd);  
 | 
|---|
 | 116 |     ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
 | 
|---|
 | 117 |     ORS=","; print dateEnd;
 | 
|---|
 | 118 |   } ' $OUT1 >> ${OUT2}
 | 
|---|
 | 119 |  
 | 
|---|
 | 120 | #same as DAB1 for DAB2
 | 
|---|
 | 121 |   $SED -e "${start2}!d" $CALIB | $AWK 'BEGIN {ORS=","} {
 | 
|---|
 | 122 |     split($4,b,".");   
 | 
|---|
 | 123 |     print $3,b[1]}' >> ${OUT2}
 | 
|---|
 | 124 |   $SED -e "${end2}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
 | 
|---|
 | 125 |      split($4,b,".");   
 | 
|---|
 | 126 |      print $3,b[1]}' >> ${OUT2}
 | 
|---|
 | 127 | #same as DATA1 for DATA2
 | 
|---|
 | 128 |   $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="2" ) {
 | 
|---|
 | 129 |     split($3,a,"/"); 
 | 
|---|
 | 130 |     split($4,b,".")   
 | 
|---|
 | 131 |     dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
 | 
|---|
 | 132 |     duree=$6 " sec";
 | 
|---|
 | 133 |     cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
 | 
|---|
 | 134 |    (cmd | getline dateEnd);
 | 
|---|
 | 135 |     close(cmd);  
 | 
|---|
 | 136 |     ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
 | 
|---|
 | 137 |     ORS="\n"; print  dateEnd;
 | 
|---|
 | 138 |   } ' $OUT1 >> ${OUT2}
 | 
|---|
 | 139 | #clean
 | 
|---|
 | 140 |   $RM $CALIB
 | 
|---|
 | 141 | done
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | #transform all the date/time into %Y-%m-%d %T
 | 
|---|
 | 144 | $AWK -v datesys=$DATE ' 
 | 
|---|
 | 145 | BEGIN { FS=","; OFS="," }
 | 
|---|
 | 146 | {
 | 
|---|
 | 147 |   for (i=2;i<=NF;i++) {
 | 
|---|
 | 148 |     split($i,a,"/");
 | 
|---|
 | 149 |     newdate=a[2]"/"a[1]"/"a[3];
 | 
|---|
 | 150 |     cmd=sprintf("%s --date \"%s\" \"+%%Y-%%m-%%d %%T\" ",datesys,newdate);
 | 
|---|
 | 151 |     (cmd | getline newdate);
 | 
|---|
 | 152 |     close(cmd);
 | 
|---|
 | 153 |     $i=newdate;
 | 
|---|
 | 154 |   } 
 | 
|---|
 | 155 |   print $0;
 | 
|---|
 | 156 | }' ${OUT2} > ${fullprocfile}
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | #clean
 | 
|---|
 | 160 | $RM $OUT1
 | 
|---|
 | 161 | $RM $OUT2
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 | exit 0
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 | 
 | 
|---|