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