source: BAORadio/AmasNancay/calibmaker.sh@ 540

Last change on this file since 540 was 540, checked in by campagne, 14 years ago

avoid the link to a specif bao daq date (jec)

File size: 14.5 KB
RevLine 
[507]1#!/bin/sh -xvf
2#download SCA file, proceed to introspection, and upload results
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=
32freqBAOCalib=1346
33bwBAOCalib=6.25
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 -freq) freqBAOCalib=$2; shift;;
41 -bwd) bwBAOCalib=$2; shift;;
[529]42 -sim) simulationMode=On;;
[507]43 -h)
44 echo >&2 \
45 "usage: $0 -src souce [-freq freq in MHz (default ${freqBAOCalib})] [-bwd bandwidth in MHz (default ${bwBAOCalib})]"
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
[516]76tmppublicpath=${TMPPUBLICPATH}
[507]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
83if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
84 iojobpath="/sps/baoradio/AmasNancay/JEC"
85 $MKDIR -p $iojobpath
86elif [ ${ENVIRONMENT} = "BATCH" ] ; then
87 iojobpath=${TMPBATCH}
88else
89 $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
90 exit 1
91fi
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
[510]102scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
[507]103
[540]104#JEC 1/10/11 Use generic baodaqstatus name
105OUT1=${publicpath}/baodaqstatus-current.txt
106#
[507]107#get the daq current irod status
[540]108#tag=`${DATE} +%F`
109#OUT1=${publicpath}/baodaqstatus-${tag}.txt
110#
[507]111if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
[540]112 $ECHO "FATAL (${scriptName}): ${OUT1} has a problem"
113 exit 1
114# $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
115# $RM -f ${tmppublicpath}/statusdaq.finished
116# ${scriptpath}/statusdaq.sh > ${OUT1}
117# while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
118# sleep 30
119# done
120# $RM ${tmppublicpath}/statusdaq.finished
[507]121#protect against remove/rewriting
[540]122# $CHMOD -v 444 ${OUT1}
[507]123fi
124
125
126#Nb: if we want to implement a loop on sources we have to take care of srclower...
127
128$ECHO "You have selected sourceRadio = $sourceRadio"
129tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
130IFS='
131'
132tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
133IFS=$DefaultIFS
134
135
136for i in ${tableau[@]}
137 do
138#
139# start a "fresh" session
140 cd ${topDir}
141 $LS | $XARGS -i $RM -rf {}
142
143 $ECHO "DEBUG: (${scriptName}) irods root dir $i"
144 dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
145
146 $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
147 $LS -lrt
148
149
[540]150#JEC 1/10/11 Use generic baodaqstatus name
[507]151#Look if the date has changed since beginning of the job!
[540]152# curtag=`${DATE} +%F`
153# if [ "<${curtag}>" != "<${tag}>" ]; then
154# tag=${curtag}
[507]155#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
[540]156# OUT1=${publicpath}/baodaqstatus-${curtag}.txt
157# $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
158# $RM -f ${tmppublicpath}/statusdaq.finished
159# ${scriptpath}/statusdaq.sh > ${OUT1}
160# while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
161# sleep 30
162# done
163# $RM ${tmppublicpath}/statusdaq.finished
[507]164#protect against remove/rewriting
[540]165# $CHMOD -v 444 ${OUT1}
166# fi
[507]167#
168# look if the calibration files already exist
169#
170 alreadyDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
171 if [ ${alreadyDone} -gt 0 ]; then
172 $ECHO "INFO (${scriptName}): calibration coef files already exist"
173 continue
174 fi
175#
176 somethingTodo=1
177 input4calibOff=`ils ${i}/Off 2>&1 | $GREP -i "calibcycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n`
178 input4calibOn=`ils ${i}/On 2>&1 | $GREP -i "calibcycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n`
179#
180 if [ "<${input4calibOff}>" != "<>" -a "<${input4calibOn}>" != "<>" ]; then
181#look at the content of the calibcycle directory as may be the previous batch job has crashed
182 tableauOff=( `$ECHO ${input4calibOff}` )
183 IFS='
184'
185 tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
186 IFS=$DefaultIFS
187#
188 nTotFilesOff=0
189 for j in ${tableauOff[@]}
190 do
191 nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
192 nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
193 done
194#
195 tableauOn=( `$ECHO ${input4calibOn}` )
196 IFS='
197 '
198 tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
199 IFS=$DefaultIFS
200#
201 nTotFilesOn=0
202 for j in ${tableauOn[@]}
203 do
204 nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
205 nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
206 done
207 diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
208 if [ ${diffOnOff} -lt 0 ]; then
209 zero=0
210 diffOnOff=`expr ${zero} - ${diffOnOff}`
211 fi
212#allow up to 5 files differences
213 if [ ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0 ]; then
214 somethingTodo=0
215 $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
216 else
217 $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
218 fi
219#
220 fi
221 if [ ${somethingTodo} -eq 1 ]; then
222#
223 $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ CALIBOFF"
224 $RM -f ${tmppublicpath}/proc_script.finished
225 if [ ${simulationMode} = "On" ]; then
226 $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBOFF"
227 else
228 ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBOFF
229 while [ ! -e "${tmppublicpath}/proc_script.finished" ]; do
230 $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
231 date +%T
232 sleep 60
233 done
234 fi
235 $ECHO "INFO (${scriptName}): proc_script has finished"
236 $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ CALIBOFF"
237 $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ CALIBON"
238 $RM ${tmppublicpath}/proc_script.finished
239
240 if [ ${simulationMode} = "On" ]; then
241 $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBON"
242 else
243 ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ CALIBON
244 while [ ! -e "${tmppublicpath}/proc_script.finished" ]; do
245 $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
246 date +%T
247 sleep 60
248 done
249 fi
250 $ECHO "INFO (${scriptName}): proc_script has finished"
251 $RM ${tmppublicpath}/proc_script.finished
252#Todo see if there is errors in the log file before copy
253 $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ CALIBON"
254######
255 else
256######
257 $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
258 #go to the .../source/date-sourcelowercase directory
259#download Off/calibcycle<> and On/calibcycle<> directories
260 $MKDIR -p ${topDir}/${dateDAQ}${srclower}
261 cd ${topDir}/${dateDAQ}${srclower}
262 aboveOnOffDir=`pwd`
263 $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
264#
265 mode="Off"
266 $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
267 inFileDirectory="./${mode}"
268 $MKDIR -p ${inFileDirectory}
269 listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
270 IFS='
271'
272 listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
273 IFS=$DefaultIFS
274
275 firstCycleOff=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
276 nCyclesOff=`expr ${#listOfCalibCycle[*]} - 1`
277
278 lastCycleOff=`$ECHO ${listOfCalibCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
279
280 listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
281
282
283 for j in ${listOfCalibCycle[@]}
284 do
285 irodsDownDir="${j}"
286 OUT=./getInFits.$$
287 iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
288 igetStatus=`$GREP "^ERROR" ${OUT}`
289 if [ "<$igetStatus>" != "<>" ]; then
290 $ECHO "FATAL (${scriptName}): error while iget fits files:"
291 $ECHO $igetStatus
292 $ECHO " ==> skip this run $sourceRadio $dateDAQ"
293 continue
294 fi
295 $RM -f ${OUT}
296 done
297 $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
298#
299 mode="On"
300 $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
301 inFileDirectory="./${mode}"
302 $MKDIR -p ${inFileDirectory}
303
304 listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
305 IFS='
306'
307 listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
308 IFS=$DefaultIFS
309
310 firstCycleOn=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
311 nCyclesOn=`expr ${#listOfCalibCycle[*]} - 1`
312
313 lastCycleOn=`$ECHO ${listOfCalibCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
314
315 listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
316
317
318#
319#Check if there is the same number of cycles in Off and On mode
320#
[511]321#first cycle = max(firstCycleOn, firstCycleOff)
322#last cycle = min(lastCycleOn,lastCycleOff)
323
[507]324 if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
325 firstCycle=${firstCycleOff}
326 lastCycle=${lastCycleOff}
327 else
[511]328 $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
[507]329 $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
[511]330 firstCycle=${firstCycleOff}
331 if [ ${firstCycleOn} -gt ${firstCycle} ]; then
332 firstCycle=${firstCycleOn}
333 fi
334 lastCycle=${lastCycleOff}
335 if [ ${lastCycleOn} -lt ${lastCycle} ]; then
336 lastCycle=${lastCycleOn}
337 fi
[507]338 fi
[511]339 $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
340
[507]341
342 for j in ${listOfCalibCycle[@]}
343 do
344 irodsDownDir="${j}"
345 OUT=./getInFits.$$
346 iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
347 igetStatus=`$GREP "^ERROR" ${OUT}`
348 if [ "<$igetStatus>" != "<>" ]; then
349 $ECHO "FATAL (${scriptName}): error while iget fits files:"
350 $ECHO $igetStatus
351 $ECHO " ==> skip this run $sourceRadio $dateDAQ"
352 continue
353 fi
354 $RM -f ${OUT}
355 done
356 $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
357#Get sca file
358 $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
359 OUT1=./getScaStatus.$$
360 $RM -f ${tmppublicpath}/getscafiles.finished
361 ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
362 while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
363 $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
364 date +%T
365 sleep 30
366 done
367 $RM ${tmppublicpath}/getscafiles.finished
368 $ECHO "DEBUG (${scriptName}): END"
369#
370 getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
371#
372 if [ "<${getScaStatus}>" != "<>" ]; then
373 $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
374 $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
375 $CAT ${OUT1}
376 $ECHO "DEBUG (${scriptName}): END"
377 continue
378 fi
379 $RM -f ${OUT1}
380
381 scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
382
383#prepare job submission
384 cd ${aboveOnOffDir}
385#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
386
387 logFile="analyse_CALIB_${sourceRadio}_${dateDAQ}.log"
388 $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
389 if [ ${simulationMode} = "On" ]; then
390 $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act calib -inPath ${iojobpath} -source ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir calibcycle -numcycle ${firstCycle},${lastCycle} -calibopt mean -debuglev 1 -freqBAOCalib ${freqBAOCalib} -bwBAOCalib ${bwBAOCalib} >& ${logFile}"
391 cat > ${logFile} <<EOF
392 Ok calibration finished
393EOF
394 else
395 ${scriptpath}/Objs/analyse -act calib -inPath ${iojobpath} -source ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir calibcycle -numcycle ${firstCycle},${lastCycle} -calibopt mean -debuglev 1 -freqBAOCalib ${freqBAOCalib} -bwBAOCalib ${bwBAOCalib} >& ${logFile}
396 fi
397 $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
398 rcstatus=`$GREP -i "Ok calibration finished" ${logFile} | $WC -l`
399 if [ ${rcstatus} -eq 0 ]; then
400 $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
401 $ECHO " START logfile"
402 $CAT ${logFile}
403 $ECHO " END logFile"
404#clean and leave
405 cd ${aboveOnOffDir}
406 $LS | $XARGS -i $RM -rf {}
407 continue
408 fi
409#Save into Irods
410 $ECHO "DEBUG (${scriptName}): START save output files"
411 $LS ${aboveOnOffDir}
412#use -f option for iput ONLY to force override
413 if [ ${simulationMode} = "On" ]; then
414 $ECHO "SIMUL: here we look for calib file and put them in Irods"
415 else
416 $FIND . -name "calib_*" -print | $XARGS -i iput -v -K {} ${i}
417 fi
418#save analysis logfile
419 $LS -l ${tmppublicpath} > /dev/null
420 $CP ${logFile} ${tmppublicpath}
421 $ECHO "DEBUG (${scriptName}): END save output files"
422#clean up to avoid scratch SIZE EXCEED LIMIT
423 cd ${aboveOnOffDir}
424 $LS | $XARGS -i $RM -rf {}
425 fi
426done
427
428exit 0
Note: See TracBrowser for help on using the repository browser.