| 1 | #!/bin/sh
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #hypothesis: ON = data1 phase, OFF data2 phase
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #Basic tuning of system fuction used.
 | 
|---|
| 6 | AWK=/bin/awk
 | 
|---|
| 7 | SED=/bin/sed
 | 
|---|
| 8 | GREP=/bin/grep
 | 
|---|
| 9 | WC=/usr/bin/wc
 | 
|---|
| 10 | RM=/bin/rm
 | 
|---|
| 11 | CAT=/bin/cat
 | 
|---|
| 12 | TOUCH=/bin/touch
 | 
|---|
| 13 | DATE=/bin/date
 | 
|---|
| 14 | ECHO=/bin/echo
 | 
|---|
| 15 | FIND=/usr/bin/find
 | 
|---|
| 16 | XARGS=/usr/bin/xargs
 | 
|---|
| 17 | SORT=/bin/sort
 | 
|---|
| 18 | MKDIR=/bin/mkdir
 | 
|---|
| 19 | LS=/bin/ls
 | 
|---|
| 20 | DefaultIFS=$' \t\n'
 | 
|---|
| 21 | IFS=$DefaultIFS
 | 
|---|
| 22 | 
 | 
|---|
| 23 | scriptName="`basename $0`"
 | 
|---|
| 24 | $ECHO "Processing script ${scriptName} at `date`"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | if [ ! $# = 3 ]; then
 | 
|---|
| 27 |   $ECHO "usage: ${scriptName} <scaXYZ.sum> <signalXYZDirectory> <type of phase:ON|OFF|CALIBON|CALIBOFF>"
 | 
|---|
| 28 |   exit 0
 | 
|---|
| 29 | fi
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #Path to public backupable path
 | 
|---|
| 32 | publicpath="/afs/in2p3.fr/home/c/campagne/public"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #temporary files to synchronize scripts
 | 
|---|
| 35 | tmppublicpath=${TMPPUBLICPATH}
 | 
|---|
| 36 | $LS -l ${tmppublicpath} > /dev/null
 | 
|---|
| 37 | 
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #Path of the utility scripts
 | 
|---|
| 40 | scriptpath=${SCRIPTPATH}
 | 
|---|
| 41 | 
 | 
|---|
| 42 | 
 | 
|---|
| 43 | OUT1=./selectFits.$$
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #inputs x-chexk sca file
 | 
|---|
| 46 | inScaFile=$1
 | 
|---|
| 47 | if [ ! -e $inScaFile ]; then
 | 
|---|
| 48 |   $ECHO "FATAL (${scriptName}): $inScaFile not found"
 | 
|---|
| 49 |   exit 1
 | 
|---|
| 50 | fi
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #Where to look at
 | 
|---|
| 53 | inFiberDir=$2
 | 
|---|
| 54 | 
 | 
|---|
| 55 | inTypeOfPhase=$3
 | 
|---|
| 56 | case "$inTypeOfPhase" in
 | 
|---|
| 57 |     ON) 
 | 
|---|
| 58 |     $ECHO "INFO (${scriptName}): filter signal files for ON phases";;
 | 
|---|
| 59 |     ONDS) 
 | 
|---|
| 60 |     $ECHO "INFO (${scriptName}): filter signal files for ON Drift Scan phases";;
 | 
|---|
| 61 |     OFF) 
 | 
|---|
| 62 |     $ECHO "INFO (${scriptName}): filter signal files for OFF phases";;
 | 
|---|
| 63 |     MEANON) 
 | 
|---|
| 64 |     $ECHO "INFO (${scriptName}): filter signal files for MEANON phases";;
 | 
|---|
| 65 |     MEANOFF) 
 | 
|---|
| 66 |     $ECHO "INFO (${scriptName}): filter signal files for MEANOFF phases";;
 | 
|---|
| 67 |     CALIBON) 
 | 
|---|
| 68 |     $ECHO "INFO (${scriptName}): filter signal files for CALIBRATION ON phases";;
 | 
|---|
| 69 |     CALIBOFF) 
 | 
|---|
| 70 |     $ECHO "INFO (${scriptName}): filter signal files for CALIBRATION OFF phases";;
 | 
|---|
| 71 |     GAIN) 
 | 
|---|
| 72 |     $ECHO "INFO (${scriptName}): filter signal files for GAIN = CALIB OFF phases";;
 | 
|---|
| 73 |     GAINNF)
 | 
|---|
| 74 |     $ECHO "INFO (${scriptName}): filter signal files for GAINNF = GAIN wo filt. freq.";;
 | 
|---|
| 75 |     GAINDS)
 | 
|---|
| 76 |     $ECHO "INFO (${scriptName}): filter signal files for GAINDS = GAIN for Drift Scan";;
 | 
|---|
| 77 |     *) 
 | 
|---|
| 78 |     $ECHO "FATAL (${scriptName}): use ON|OFF|MEANON|MEANOFF|CALIBON|CALIBOFF phase tags."
 | 
|---|
| 79 |     exit 1
 | 
|---|
| 80 |     ;;
 | 
|---|
| 81 | esac
 | 
|---|
| 82 | 
 | 
|---|
| 83 | 
 | 
|---|
| 84 | #################
 | 
|---|
| 85 | #fill the arrays association header file with start/end date&time 
 | 
|---|
| 86 | # arrays
 | 
|---|
| 87 | declare -a sigStart
 | 
|---|
| 88 | declare -a sigEnd
 | 
|---|
| 89 | declare -a sigFile
 | 
|---|
| 90 | 
 | 
|---|
| 91 | for fullfile in $( $FIND $inFiberDir -name "header*" | $XARGS -i basename {} | $SORT -k1.7n)
 | 
|---|
| 92 |   do
 | 
|---|
| 93 |   filename=$(basename $fullfile)
 | 
|---|
| 94 |   extension=${filename##*.}
 | 
|---|
| 95 |   filename=${filename%.*}
 | 
|---|
| 96 |   ffn=${inFiberDir}/${filename}.${extension}
 | 
|---|
| 97 | #  echo "process $ffn"
 | 
|---|
| 98 |   while read -n80 L
 | 
|---|
| 99 |     do
 | 
|---|
| 100 |     [ "${L::3}" = "END" ] && break
 | 
|---|
| 101 |     echo $L
 | 
|---|
| 102 |   done < $ffn > $OUT1
 | 
|---|
| 103 |   
 | 
|---|
| 104 |   strStart=`$GREP TMSTART $OUT1 | $AWK '{split($0,a,"/"); split(a[1],b,"="); print b[2];}' | $SED 's/^[ \t]*//;s/[ \t]*$//'  | $SED "s/^\([\"']\)\(.*\)\1\$/\2/g" | $AWK '{split($0,a,"T"); print a[1]" "a[2];}' `
 | 
|---|
| 105 |   
 | 
|---|
| 106 |   strEnd=`$GREP TMEND $OUT1 | $AWK '{split($0,a,"/"); split(a[1],b,"="); print b[2];}' |  $SED 's/^[ \t]*//;s/[ \t]*$//'  | $SED "s/^\([\"']\)\(.*\)\1\$/\2/g" | $AWK '{split($0,a,"T"); print a[1]" "a[2];}' `
 | 
|---|
| 107 |   
 | 
|---|
| 108 |   if [[ -n "$strStart"  &&  -n "$strEnd" ]]; then
 | 
|---|
| 109 |       timeStart=`${DATE} +%s -d "${strStart}"`
 | 
|---|
| 110 |       timeEnd=`${DATE} +%s -d "${strEnd}"`
 | 
|---|
| 111 | #      echo "$fullfile start $strStart   <$timeStart>, end  $strEnd <$timeEnd>"
 | 
|---|
| 112 |       sigStart[${#sigStart[*]}]=$timeStart
 | 
|---|
| 113 |       sigEnd[${#sigEnd[*]}]=$timeEnd
 | 
|---|
| 114 |       sigFile[${#sigFile[*]}]=$filename
 | 
|---|
| 115 |   else
 | 
|---|
| 116 |       $ECHO "WARNING (${scriptName}): $ffn PATHOLOGIQUE"
 | 
|---|
| 117 |   fi
 | 
|---|
| 118 | done
 | 
|---|
| 119 | 
 | 
|---|
| 120 | ############
 | 
|---|
| 121 | #scan all the cycles 
 | 
|---|
| 122 | #  calib1start=$2; calib1end=$3; 
 | 
|---|
| 123 | #  data1start=$4; data1end=$5;
 | 
|---|
| 124 | #  calib2start=$6; calib2end=$7; 
 | 
|---|
| 125 | #  data2start=$8; data2end=$9;
 | 
|---|
| 126 | #to pass to awk: use a "," as field separator
 | 
|---|
| 127 | awk_sigStart=$( printf "%s," "${sigStart[@]}" )
 | 
|---|
| 128 | awk_sigEnd=$( printf "%s," "${sigEnd[@]}" )
 | 
|---|
| 129 | awk_sigFile=$( printf "%s," "${sigFile[@]}" )
 | 
|---|
| 130 | 
 | 
|---|
| 131 | #attention a la derniere "," pour le calcul de nfile
 | 
|---|
| 132 | OUT1=./tmpselect1.$$
 | 
|---|
| 133 | #selection d'une seul cycle pour debug 
 | 
|---|
| 134 | $AWK -v datesys=$DATE -v type=$inTypeOfPhase -v str_sigStart="$awk_sigStart"  -v str_sigEnd="$awk_sigEnd"  -v str_sigFile="$awk_sigFile" '
 | 
|---|
| 135 | BEGIN { 
 | 
|---|
| 136 |   FS=",";
 | 
|---|
| 137 |   split(str_sigStart,a_sigStart,",");
 | 
|---|
| 138 |   split(str_sigEnd,a_sigEnd,",");
 | 
|---|
| 139 |   nfile=split(str_sigFile,a_sigFile,",");
 | 
|---|
| 140 |   delete a_sigFile[nfile];
 | 
|---|
| 141 |   delete a_sigStart[nfile];
 | 
|---|
| 142 |   delete a_sigEnd[nfile];
 | 
|---|
| 143 |   nfile--;  
 | 
|---|
| 144 | 
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 |  {
 | 
|---|
| 148 |   cycle=$1; 
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   if ( (type == "ON") || (type == "MEANON" || (type == "ONDS")) ) {
 | 
|---|
| 151 |     phaseStart=$4; phaseEnd=$5;
 | 
|---|
| 152 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
 | 
|---|
| 153 |     (cmd | getline phstart);
 | 
|---|
| 154 |     close(cmd);
 | 
|---|
| 155 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
 | 
|---|
| 156 |     (cmd | getline phend);
 | 
|---|
| 157 |     close(cmd);
 | 
|---|
| 158 |     # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
 | 
|---|
| 159 |     deltaStartSafety=4;
 | 
|---|
| 160 |     deltaEndSafety=-4;
 | 
|---|
| 161 |   }
 | 
|---|
| 162 |   else if ( (type == "OFF") || (type == "MEANOFF") ) {
 | 
|---|
| 163 |     phaseStart=$8; phaseEnd=$9;
 | 
|---|
| 164 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
 | 
|---|
| 165 |     (cmd | getline phstart);
 | 
|---|
| 166 |     close(cmd);
 | 
|---|
| 167 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
 | 
|---|
| 168 |     (cmd | getline phend);
 | 
|---|
| 169 |     close(cmd);
 | 
|---|
| 170 |     # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
 | 
|---|
| 171 |     deltaStartSafety=4;
 | 
|---|
| 172 |     deltaEndSafety=-4;
 | 
|---|
| 173 |   } 
 | 
|---|
| 174 |   else if ( (type == "CALIBON") || (type == "GAINDS") ) {
 | 
|---|
| 175 |     phaseStart=$2; phaseEnd=$3;
 | 
|---|
| 176 |     print "Phase: ",type," cycle: ",cycle,": <",phaseStart,">:<",phaseEnd,">";
 | 
|---|
| 177 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
 | 
|---|
| 178 |     (cmd | getline phstart);
 | 
|---|
| 179 |     close(cmd);
 | 
|---|
| 180 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
 | 
|---|
| 181 |     (cmd | getline phend);
 | 
|---|
| 182 |     close(cmd);
 | 
|---|
| 183 | #    print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
 | 
|---|
| 184 |     deltaStartSafety=-6;
 | 
|---|
| 185 |     deltaEndSafety=6;
 | 
|---|
| 186 |   } 
 | 
|---|
| 187 |   else if ( (type == "CALIBOFF") || (type == "GAIN") || (type == "GAINNF")) {
 | 
|---|
| 188 |     phaseStart=$6; phaseEnd=$7;
 | 
|---|
| 189 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseStart);
 | 
|---|
| 190 |     (cmd | getline phstart);
 | 
|---|
| 191 |     close(cmd);
 | 
|---|
| 192 |     cmd=sprintf("%s +%%s -d \"%s\" ",datesys,phaseEnd);
 | 
|---|
| 193 |     (cmd | getline phend);
 | 
|---|
| 194 |     close(cmd);
 | 
|---|
| 195 |     # print "Phase: ",type," cycle: ",cycle,": <",phstart,">:<",phend,">";
 | 
|---|
| 196 |     deltaStartSafety=-6;
 | 
|---|
| 197 |     deltaEndSafety=6;
 | 
|---|
| 198 |   } 
 | 
|---|
| 199 |   else { 
 | 
|---|
| 200 |     print "FATAL: ",type," NOT YET SUPPORTED"; exit 1;
 | 
|---|
| 201 |   }
 | 
|---|
| 202 | 
 | 
|---|
| 203 |   for (i=1; i<=nfile; i++) {
 | 
|---|
| 204 |     diffstart = a_sigStart[i] - phstart;
 | 
|---|
| 205 |     diffend   = a_sigEnd[i] - phend;
 | 
|---|
| 206 | #     print "file ",i," start ",a_sigStart[i],": diff <" diffstart ">:<" diffend ">";
 | 
|---|
| 207 |     if ( diffstart >= deltaStartSafety && diffend <= deltaEndSafety )
 | 
|---|
| 208 |        print "cycle ",cycle," file ",i-1," accepted ",a_sigFile[i];
 | 
|---|
| 209 |   }  
 | 
|---|
| 210 |      
 | 
|---|
| 211 | 
 | 
|---|
| 212 | }' $inScaFile > ${OUT1}
 | 
|---|
| 213 | 
 | 
|---|
| 214 | OUT2=./tmpselect2.$$
 | 
|---|
| 215 | $AWK '
 | 
|---|
| 216 | BEGIN { max=0; min=999999; }; 
 | 
|---|
| 217 | 
 | 
|---|
| 218 | ($1=="cycle") { table[$2] = table[$2]","$4; if($2>max){max=$2}; if($2<min){min=$2} }; 
 | 
|---|
| 219 | 
 | 
|---|
| 220 | END { for (j=min;j<=max;j++) {print j,table[j];} }
 | 
|---|
| 221 | ' ${OUT1} > ${OUT2}
 | 
|---|
| 222 | 
 | 
|---|
| 223 | $AWK '
 | 
|---|
| 224 | BEGIN { FS=","}
 | 
|---|
| 225 | {
 | 
|---|
| 226 |  print "#"$1,$2","$NF",1";
 | 
|---|
| 227 | }
 | 
|---|
| 228 | ' ${OUT2}
 | 
|---|
| 229 | 
 | 
|---|
| 230 | $RM $OUT1
 | 
|---|
| 231 | $RM $OUT2
 | 
|---|
| 232 | 
 | 
|---|
| 233 | exit 0 | 
|---|