| 1 | #!/bin/sh
 | 
|---|
| 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
 | 
|---|
| 34 | tmppublicpath=${TMPPUBLICPATH}
 | 
|---|
| 35 | $LS -l ${tmppublicpath} > /dev/null
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #Path where the job will do temporary IO
 | 
|---|
| 38 | if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
 | 
|---|
| 39 |     iojobpath="/sps/baoradio/AmasNancay/JEC"
 | 
|---|
| 40 |     $MKDIR -p $iojobpath
 | 
|---|
| 41 | elif [ ${ENVIRONMENT} = "BATCH" ] ; then
 | 
|---|
| 42 |     iojobpath=${TMPBATCH}
 | 
|---|
| 43 | else
 | 
|---|
| 44 |     $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
 | 
|---|
| 45 |     touch ${tmppublicpath}/sca.finished
 | 
|---|
| 46 |     exit 1
 | 
|---|
| 47 | fi
 | 
|---|
| 48 | cd ${iojobpath}
 | 
|---|
| 49 | 
 | 
|---|
| 50 | 
 | 
|---|
| 51 | if [ ! $# = 1 ]
 | 
|---|
| 52 |     then
 | 
|---|
| 53 |     $ECHO "FATAL  (${scriptName}): usage: ./sca.sh <file>"
 | 
|---|
| 54 |     touch ${tmppublicpath}/sca.finished
 | 
|---|
| 55 |     exit 1
 | 
|---|
| 56 | fi
 | 
|---|
| 57 | 
 | 
|---|
| 58 | 
 | 
|---|
| 59 | fullfile=$1
 | 
|---|
| 60 | if [ ! -e $fullfile ] 
 | 
|---|
| 61 |     then
 | 
|---|
| 62 |     $ECHO "FATAL (${scriptName}): $fullfile not found: FATAL"
 | 
|---|
| 63 |     touch ${tmppublicpath}/sca.finished
 | 
|---|
| 64 |     exit 1
 | 
|---|
| 65 | fi
 | 
|---|
| 66 | 
 | 
|---|
| 67 | #The output file would be in "fullprocfile"
 | 
|---|
| 68 | filename=$(basename $fullfile)
 | 
|---|
| 69 | #extension=${filename##*.}
 | 
|---|
| 70 | filename=${filename%.*}
 | 
|---|
| 71 | extension=".sum"
 | 
|---|
| 72 | fullprocfile=${filename}${extension}
 | 
|---|
| 73 | #create empty output file
 | 
|---|
| 74 | $RM -f $fullprocfile
 | 
|---|
| 75 | $TOUCH $fullprocfile
 | 
|---|
| 76 | 
 | 
|---|
| 77 | 
 | 
|---|
| 78 | #extract the PERI lines valid for DAB and DATA
 | 
|---|
| 79 | OUT1=./tmp1.$$
 | 
|---|
| 80 | $AWK '
 | 
|---|
| 81 | $1=="PERI" {
 | 
|---|
| 82 |    print "CYCLE",$6,$2,$3,$7,$8
 | 
|---|
| 83 |   }
 | 
|---|
| 84 | ' $fullfile > $OUT1
 | 
|---|
| 85 | 
 | 
|---|
| 86 | 
 | 
|---|
| 87 | #Sequence of the cycle numbers (perform a unique filtering)
 | 
|---|
| 88 | tableau=( `cat $OUT1 | $AWK '{print $2}'` )
 | 
|---|
| 89 | IFS='
 | 
|---|
| 90 | '
 | 
|---|
| 91 | tableau=( $( printf "%s\n" "${tableau[@]}" | awk 'x[$0]++ == 0' ) )
 | 
|---|
| 92 | IFS=$DefaultIFS
 | 
|---|
| 93 | 
 | 
|---|
| 94 | #scan each cycle
 | 
|---|
| 95 | OUT2=./tmp2.$$
 | 
|---|
| 96 | for i in ${tableau[@]}
 | 
|---|
| 97 | do
 | 
|---|
| 98 | #extract the DAB calibration phases. Hypothesis: 2 identical sequences 
 | 
|---|
| 99 |   CALIB=./tmp_cycle_${i}_calib.$$
 | 
|---|
| 100 |   $AWK -v cycle=$i '$2==cycle && $5=="-1" {print $0} ' $OUT1 > $CALIB
 | 
|---|
| 101 |   nlines=`$WC $CALIB | $AWK '{print $1}'`
 | 
|---|
| 102 |   start1=1
 | 
|---|
| 103 |   let "end1=nlines/2"
 | 
|---|
| 104 |   let "start2=end1+1"
 | 
|---|
| 105 |   end2=$nlines
 | 
|---|
| 106 | 
 | 
|---|
| 107 | #date Start/End of DAB1 
 | 
|---|
| 108 |   $SED -e "${start1}!d" $CALIB | $AWK -v cycle=$i 'BEGIN {ORS=","} {
 | 
|---|
| 109 |     split($4,b,".");   
 | 
|---|
| 110 |     print cycle "," $3,b[1]}' >> ${OUT2}
 | 
|---|
| 111 |   $SED -e "${end1}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
 | 
|---|
| 112 |     split($4,b,".");   
 | 
|---|
| 113 |     print $3,b[1]}' >> ${OUT2}
 | 
|---|
| 114 | 
 | 
|---|
| 115 | 
 | 
|---|
| 116 | #date Start/End of DATA1: use date system function to add the duration to
 | 
|---|
| 117 | #compute the End,cf. End = Start + duration 
 | 
|---|
| 118 |   $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="1" ) {
 | 
|---|
| 119 |     split($3,a,"/"); 
 | 
|---|
| 120 |     split($4,b,".");   
 | 
|---|
| 121 |     dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
 | 
|---|
| 122 |     duree=$6 " sec";
 | 
|---|
| 123 |     cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
 | 
|---|
| 124 |    (cmd | getline dateEnd);
 | 
|---|
| 125 |     close(cmd);  
 | 
|---|
| 126 |     ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
 | 
|---|
| 127 |     ORS=","; print dateEnd;
 | 
|---|
| 128 |   } ' $OUT1 >> ${OUT2}
 | 
|---|
| 129 |  
 | 
|---|
| 130 | #same as DAB1 for DAB2
 | 
|---|
| 131 |   $SED -e "${start2}!d" $CALIB | $AWK 'BEGIN {ORS=","} {
 | 
|---|
| 132 |     split($4,b,".");   
 | 
|---|
| 133 |     print $3,b[1]}' >> ${OUT2}
 | 
|---|
| 134 |   $SED -e "${end2}!d"   $CALIB | $AWK 'BEGIN {ORS=","} {
 | 
|---|
| 135 |      split($4,b,".");   
 | 
|---|
| 136 |      print $3,b[1]}' >> ${OUT2}
 | 
|---|
| 137 | #same as DATA1 for DATA2
 | 
|---|
| 138 |   $AWK -v datesys=$DATE -v cycle=$i '$2==cycle && ( $5=="2" ) {
 | 
|---|
| 139 |     split($3,a,"/"); 
 | 
|---|
| 140 |     split($4,b,".")   
 | 
|---|
| 141 |     dateStart=a[2]"/"a[1]"/"a[3] " " b[1];
 | 
|---|
| 142 |     duree=$6 " sec";
 | 
|---|
| 143 |     cmd=sprintf("%s --date \"%s %s\"  \"+%%d/%%m/%%y %%T\" ",datesys,dateStart,duree);
 | 
|---|
| 144 |    (cmd | getline dateEnd);
 | 
|---|
| 145 |     close(cmd);  
 | 
|---|
| 146 |     ORS=","; print a[1]"/"a[2]"/"a[3] " " b[1];
 | 
|---|
| 147 |     ORS="\n"; print  dateEnd;
 | 
|---|
| 148 |   } ' $OUT1 >> ${OUT2}
 | 
|---|
| 149 | #clean
 | 
|---|
| 150 |   $RM $CALIB
 | 
|---|
| 151 | done
 | 
|---|
| 152 | 
 | 
|---|
| 153 | #transform all the date/time into %Y-%m-%d %T
 | 
|---|
| 154 | $AWK -v datesys=$DATE ' 
 | 
|---|
| 155 | BEGIN { FS=","; OFS="," }
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   for (i=2;i<=NF;i++) {
 | 
|---|
| 158 |     split($i,a,"/");
 | 
|---|
| 159 |     newdate=a[2]"/"a[1]"/"a[3];
 | 
|---|
| 160 |     cmd=sprintf("%s --date \"%s\" \"+%%Y-%%m-%%d %%T\" ",datesys,newdate);
 | 
|---|
| 161 |     (cmd | getline newdate);
 | 
|---|
| 162 |     close(cmd);
 | 
|---|
| 163 |     $i=newdate;
 | 
|---|
| 164 |   } 
 | 
|---|
| 165 |   print $0;
 | 
|---|
| 166 | }' ${OUT2} > ${fullprocfile}
 | 
|---|
| 167 | 
 | 
|---|
| 168 | 
 | 
|---|
| 169 | #clean
 | 
|---|
| 170 | $RM $OUT1
 | 
|---|
| 171 | $RM $OUT2
 | 
|---|
| 172 | 
 | 
|---|
| 173 | touch ${tmppublicpath}/sca.finished
 | 
|---|
| 174 | exit 0
 | 
|---|
| 175 | 
 | 
|---|
| 176 | 
 | 
|---|
| 177 | 
 | 
|---|