source: BAORadio/AmasNancay/v5/onoffmaker.sh@ 618

Last change on this file since 618 was 579, checked in by torrento, 14 years ago

version adaptee a grid engin (ana)

  • Property svn:executable set to *
File size: 15.4 KB
Line 
1#!/bin/sh -xvf
2#Perform the ON-OFF analysis
3DATE=/bin/date
4GREP=/bin/grep
5AWK=/bin/awk
6ECHO=/bin/echo
7WC=/usr/bin/wc
8CAT=/bin/cat
9PRINTF=/usr/bin/printf
10FIND=/usr/bin/find
11MKDIR=/bin/mkdir
12XARGS=/usr/bin/xargs
13SORT=/bin/sort
14RM=/bin/rm
15TR=/usr/bin/tr
16CP=/bin/cp
17LS=/bin/ls
18CHMOD=/bin/chmod
19
20DefaultIFS=$' \t\n'
21IFS=$DefaultIFS
22
23#set Irods environment
24. /usr/local/shared/bin/irods_env.sh -noverbose
25
26scriptName="`basename $0`"
27$ECHO "Processing script ${scriptName} at `date`"
28#which source to analyse
29
30#Process cmd line args: the -src option is mandatory (source name as Abell85)
31sourceRadio=
32dateSelected=
33freqBAOCalib=1346
34#use -sim option to simulate processing (debug the script as if...)
35simulationMode=Off
36while [ $# -gt 0 ]
37do
38 case "$1" in
39 -src) sourceRadio=$2; shift;;
40 -date) dateSelected=$2; shift;;
41 -freq) freqBAOCalib=$2; shift;;
42 -sim) simulationMode=On;;
43 -h)
44 echo >&2 \
45 "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-sim to trig simulation mode]"
46 exit 1;;
47 *) break;; # terminate while loop
48 esac
49 shift
50done
51
52if [ ${simulationMode} = "On" ]; then
53 $ECHO "INFO ${scriptName} running in SIMUL mode"
54fi
55
56if [ "<${sourceRadio}>" = "<>" ]; then
57 $ECHO "FATAL: You have forgotten to select the source option (-src)"
58 exit 1
59fi
60
61case ${sourceRadio} in
62 Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
63 Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
64 Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
65 *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
66 exit 1;;
67esac
68
69srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
70
71#Path to public backupable path
72publicpath="/afs/in2p3.fr/home/c/campagne/public"
73
74
75#temporary files to synchronize scripts
76tmppublicpath=${TMPPUBLICPATH}
77#wakeup the NFS disk
78$LS -l ${tmppublicpath} > /dev/null
79#clean previous spurious files
80$RM -f ${tmppublicpath}/*.finished
81
82#Path where the job will do temporary IO
83. ${SCRIPTPATH}/isInteractive.sh
84isInteractive=$(isInteractive)
85if [ ${isInteractive} == "1" ]; then
86 $ECHO "Usage in INTERACTIVE may be destructive... To be used with great care !!!!"
87 exit 0;
88fi
89
90. ${SCRIPTPATH}/set_iojobpath.sh
91iojobpath=$(set_iojobpath)
92cd ${iojobpath}
93
94localpath="${sourceRadio}"
95$MKDIR -p ./${localpath}
96cd ./${localpath}
97
98#save the top directory
99topDir=`pwd`
100
101#Path of the utility scripts
102scriptpath=${SCRIPTPATH}
103
104#JEC 1/10/11 Use generic baodaqstatus name
105#get the daq current irod status
106#tag=`${DATE} +%F`
107#OUT1=${publicpath}/baodaqstatus-${tag}.txt
108OUT1=${publicpath}/baodaqstatus-current.txt
109
110if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
111 $ECHO "FATAL (${scriptName}): ${OUT1} has a problem"
112 exit 1
113# $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
114# $RM -f ${tmppublicpath}/statusdaq.finished
115# ${scriptpath}/statusdaq.sh > ${OUT1}
116# while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
117# sleep 30
118# done
119# $RM ${tmppublicpath}/statusdaq.finished
120#protect against remove/rewriting
121# $CHMOD -v 444 ${OUT1}
122fi
123
124
125#Nb: if we want to implement a loop on sources we have to take care of srclower...
126
127$ECHO "You have selected sourceRadio = ${sourceRadio} [date = ${dateSelected}]"
128tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
129IFS='
130'
131tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
132IFS=$DefaultIFS
133
134
135for i in ${tableau[@]}
136 do
137#
138# start a "fresh" session
139 cd ${topDir}
140 $LS | $XARGS -i $RM -rf {}
141
142 $ECHO "DEBUG: (${scriptName}) irods root dir $i"
143 dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
144
145 $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
146 $LS -lrt
147
148
149#JEC 1/10/11 Use generic baodaqstatus name
150#Look if the date has changed since beginning of the job!
151# curtag=`${DATE} +%F`
152# if [ "<${curtag}>" != "<${tag}>" ]; then
153# tag=${curtag}
154#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
155# OUT1=${publicpath}/baodaqstatus-${curtag}.txt
156# $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
157# $RM -f ${tmppublicpath}/statusdaq.finished
158# ${scriptpath}/statusdaq.sh > ${OUT1}
159# while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
160# sleep 30
161# done
162# $RM ${tmppublicpath}/statusdaq.finished
163#protect against remove/rewriting
164# $CHMOD -v 444 ${OUT1}
165# fi
166#
167# look if the gain & calibration files exist
168#
169 gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
170 if [ ! ${gainDone} -gt 0 ]; then
171 $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
172 continue
173 fi
174
175 calibDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
176 if [ ! ${calibDone} -gt 0 ]; then
177 $ECHO "FATAL (${scriptName}): calibration files DO NOT exist skip `basename $i` "
178 continue
179 fi
180#
181 somethingTodo=1
182 input4dataOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n`
183 input4dataOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n`
184#
185 if [ "<${input4dataOff}>" != "<>" -a "<${input4dataOn}>" != "<>" ]; then
186#look at the content of the datacycle directory as may be the previous batch job has crashed
187 tableauOff=( `$ECHO ${input4dataOff}` )
188 IFS='
189'
190 tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
191 IFS=$DefaultIFS
192#
193 nTotFilesOff=0
194 for j in ${tableauOff[@]}
195 do
196 nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
197 nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
198 done
199#
200 tableauOn=( `$ECHO ${input4dataOn}` )
201 IFS='
202 '
203 tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
204 IFS=$DefaultIFS
205#
206 nTotFilesOn=0
207 for j in ${tableauOn[@]}
208 do
209 nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
210 nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
211 done
212 diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
213 if [ ${diffOnOff} -lt 0 ]; then
214 zero=0
215 diffOnOff=`expr ${zero} - ${diffOnOff}`
216 fi
217#allow files differences
218 if [ ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0 ]; then
219 somethingTodo=0
220 $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
221 else
222 $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
223 fi
224#
225 fi
226 if [ ${somethingTodo} -eq 1 ]; then
227#
228 $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ANALOFF"
229 $RM -f ${tmppublicpath}/proc_script.finished
230 if [ ${simulationMode} = "On" ]; then
231 $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF"
232 else
233 ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF
234 while [ ! -e "${tmppublicpath}/proc_script.finished" ]; do
235 $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
236 date +%T
237 sleep 60
238 done
239 fi
240 $ECHO "INFO (${scriptName}): proc_script has finished"
241 $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ OFF"
242 $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ON"
243 $RM ${tmppublicpath}/proc_script.finished
244
245 if [ ${simulationMode} = "On" ]; then
246 $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON"
247 else
248 ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON
249 while [ ! -e "${tmppublicpath}/proc_script.finished" ]; do
250 $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
251 date +%T
252 sleep 60
253 done
254 fi
255 $ECHO "INFO (${scriptName}): proc_script has finished"
256 $RM ${tmppublicpath}/proc_script.finished
257#Todo see if there is errors in the log file before copy
258 $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ ON"
259######
260 else
261######
262 $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
263 #go to the .../source/date-sourcelowercase directory
264#download Off/datacycle<> and On/datacycle<> directories
265 $MKDIR -p ${topDir}/${dateDAQ}${srclower}
266 cd ${topDir}/${dateDAQ}${srclower}
267 aboveOnOffDir=`pwd`
268 $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
269#
270 mode="Off"
271 $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
272 inFileDirectory="./${mode}"
273 $MKDIR -p ${inFileDirectory}
274 listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
275 IFS='
276'
277 listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
278 IFS=$DefaultIFS
279
280 firstCycleOff=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
281 nCyclesOff=`expr ${#listOfDataCycle[*]} - 1`
282
283 lastCycleOff=`$ECHO ${listOfDataCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
284
285 listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
286
287
288 for j in ${listOfDataCycle[@]}
289 do
290 irodsDownDir="${j}"
291 OUT=./getInFits.$$
292 iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
293 igetStatus=`$GREP "^ERROR" ${OUT}`
294 if [ "<$igetStatus>" != "<>" ]; then
295 $ECHO "FATAL (${scriptName}): error while iget fits files:"
296 $ECHO $igetStatus
297 $ECHO " ==> skip this run $sourceRadio $dateDAQ"
298 continue
299 fi
300 $RM -f ${OUT}
301 done
302 $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
303#
304 mode="On"
305 $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
306 inFileDirectory="./${mode}"
307 $MKDIR -p ${inFileDirectory}
308
309 listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
310 IFS='
311'
312 listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
313 IFS=$DefaultIFS
314
315 firstCycleOn=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
316 nCyclesOn=`expr ${#listOfDataCycle[*]} - 1`
317
318 lastCycleOn=`$ECHO ${listOfDataCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
319
320 listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
321
322
323#
324#Check if there is the same number of cycles in Off and On mode
325#
326#first cycle = max(firstCycleOn, firstCycleOff)
327#last cycle = min(lastCycleOn,lastCycleOff)
328
329 if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
330 firstCycle=${firstCycleOff}
331 lastCycle=${lastCycleOff}
332 else
333 $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
334 $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
335 firstCycle=${firstCycleOff}
336 if [ ${firstCycleOn} -gt ${firstCycle} ]; then
337 firstCycle=${firstCycleOn}
338 fi
339 lastCycle=${lastCycleOff}
340 if [ ${lastCycleOn} -lt ${lastCycle} ]; then
341 lastCycle=${lastCycleOn}
342 fi
343 fi
344 $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
345
346
347 for j in ${listOfDataCycle[@]}
348 do
349 irodsDownDir="${j}"
350 OUT=./getInFits.$$
351 iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
352 igetStatus=`$GREP "^ERROR" ${OUT}`
353 if [ "<$igetStatus>" != "<>" ]; then
354 $ECHO "FATAL (${scriptName}): error while iget fits files:"
355 $ECHO $igetStatus
356 $ECHO " ==> skip this run $sourceRadio $dateDAQ"
357 continue
358 fi
359 $RM -f ${OUT}
360 done
361 $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
362#Get sca file
363 $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
364 OUT1=./getScaStatus.$$
365 $RM -f ${tmppublicpath}/getscafiles.finished
366 ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
367 while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
368 $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
369 date +%T
370 sleep 30
371 done
372 $RM ${tmppublicpath}/getscafiles.finished
373 $ECHO "DEBUG (${scriptName}): END"
374#
375 getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
376#
377 if [ "<${getScaStatus}>" != "<>" ]; then
378 $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
379 $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
380 $CAT ${OUT1}
381 $ECHO "DEBUG (${scriptName}): END"
382 continue
383 fi
384 $RM -f ${OUT1}
385
386 scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
387
388
389#Get the calibration files
390 OUT2=./calibFiles.$$
391 ils ${i} | ${GREP} -i "calib.*\.txt" > ${OUT2}
392 listeOfCalib=( `$CAT ${OUT2}` )
393 IFS='
394'
395 listeOfCalib=( $( $PRINTF "%s\n" "${listeOfCalib[@]}" | $AWK 'x[$0]++ == 0' ) )
396 IFS=$DefaultIFS
397 $RM $OUT2
398 for fcal in ${listeOfCalib[@]}
399 do
400 OUT3=./getCalib.$$
401 iget -f -K -v ${i}/${fcal} ${aboveOnOffDir}> ${OUT3} 2>&1
402 igetStatus=`$GREP "^ERROR" ${OUT3}`
403 if [ "<$igetStatus>" != "<>" ]; then
404 $ECHO "FATAL (${scriptName}): error while iget calib files:"
405 $ECHO $igetStatus
406 exit 1
407 fi
408 $RM -f ${OUT3}
409 done
410
411#prepare job submission
412 cd ${aboveOnOffDir}
413#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
414
415 logFile="analyse_DATA_${sourceRadio}_${dateDAQ}.log"
416 $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
417 if [ ${simulationMode} = "On" ]; then
418 $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act dataOnOff -inPath ${iojobpath} -source ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debuglev 1 -freqBAOCalib ${freqBAOCalib} >& ${logFile}"
419 cat > ${logFile} <<EOF
420 Ok calibration finished
421EOF
422 else
423 ${scriptpath}/Objs/analyse -act dataOnOff -inPath ${iojobpath} -source ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debuglev 1 -freqBAOCalib ${freqBAOCalib} >& ${logFile}
424 fi
425 $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
426 rcstatus=`$GREP -i "Ok dataOnOff finished" ${logFile} | $WC -l`
427 if [ ${rcstatus} -eq 0 ]; then
428 $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
429 $ECHO " START logfile"
430 $CAT ${logFile}
431 $ECHO " END logFile"
432#clean and leave
433 cd ${aboveOnOffDir}
434 $LS | $XARGS -i $RM -rf {}
435 continue
436 fi
437#Save into Irods
438 $ECHO "DEBUG (${scriptName}): START save output files"
439 $LS ${aboveOnOffDir}
440#use -f option for iput ONLY to force override
441 if [ ${simulationMode} = "On" ]; then
442 $ECHO "SIMUL: here we look for calib file and put them in Irods"
443 else
444 $LS -lrth
445 $FIND . -name "dataRaw_*" -print | $XARGS -i iput -v -K {} ${i}
446 $FIND . -name "dataBAOCalib_*" -print | $XARGS -i iput -v -K {} ${i}
447 $FIND . -name "dataBAORTCalib_*" -print | $XARGS -i iput -v -K {} ${i}
448 $FIND . -name "diffOnOff_*" -print | $XARGS -i iput -v -K {} ${i}
449 fi
450#save analysis logfile
451 $LS -l ${tmppublicpath} > /dev/null
452 $CP ${logFile} ${tmppublicpath}
453 $ECHO "DEBUG (${scriptName}): END save output files"
454#clean up to avoid scratch SIZE EXCEED LIMIT
455 cd ${aboveOnOffDir}
456 $LS | $XARGS -i $RM -rf {}
457 fi
458done
459
460exit 0
Note: See TracBrowser for help on using the repository browser.