source: BAORadio/AmasNancay/v4/anacalibmaker.sh@ 639

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

u_irods_baoradio pour BQS (jec)

  • Property svn:executable set to *
File size: 10.5 KB
Line 
1#!/bin/sh -xvf
2#do calibration
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
34bwBAOCalib=6.25
35#use -sim option to simulate processing (debug the script as if...)
36simulationMode=Off
37while [ $# -gt 0 ]
38 do
39 case "$1" in
40 -src) sourceRadio=$2; shift;;
41 -date) dateSelected=$2; shift;;
42 -freq) freqBAOCalib=$2; shift;;
43 -bwd) bwBAOCalib=$2; shift;;
44 -sim) simulationMode=On;;
45 -h)
46 echo >&2 \
47 "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-bwd bandwidth in MHz (default ${bwBAOCalib})]"
48 exit 1;;
49 *) break;; # terminate while loop
50 esac
51 shift
52done
53
54if [ ${simulationMode} = "On" ]; then
55 $ECHO "INFO ${scriptName} running in SIMUL mode"
56fi
57
58if [ "<${sourceRadio}>" = "<>" ]; then
59 $ECHO "FATAL: You have forgotten to select the source option (-src)"
60 exit 1
61fi
62
63case ${sourceRadio} in
64 Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
65 Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
66 Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
67 *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
68 exit 1;;
69esac
70
71srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
72
73#Path to public backupable path
74publicpath="/afs/in2p3.fr/home/c/campagne/public"
75
76
77#temporary files to synchronize scripts
78tmppublicpath=${TMPPUBLICPATH}
79#wakeup the NFS disk
80$LS -l ${tmppublicpath} > /dev/null
81#clean previous spurious files
82$RM -f ${tmppublicpath}/*.finished
83
84#Path where the job will do temporary IO
85if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
86 iojobpath="/sps/baoradio/AmasNancay/JEC"
87 $MKDIR -p $iojobpath
88 $ECHO "Usage in INTERACTIVE may be destructive... To be used with great care !!!!"
89 exit 0;
90elif [ ${ENVIRONMENT} = "BATCH" ] ; then
91 iojobpath=${TMPBATCH}
92else
93 $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
94 exit 1
95fi
96cd ${iojobpath}
97
98localpath="${sourceRadio}"
99$MKDIR -p ./${localpath}
100cd ./${localpath}
101
102#save the top directory
103topDir=`pwd`
104
105#Path of the utility scripts
106scriptpath=${SCRIPTPATH}
107
108#JEC 1/10/11 Use generic baodaqstatus name
109OUT1=${publicpath}/baodaqstatus-current.txt
110#
111#get the daq current irod status
112#tag=`${DATE} +%F`
113#OUT1=${publicpath}/baodaqstatus-${tag}.txt
114#
115if [ ! -e ${OUT1} -o ! -r ${OUT1} ]; then
116 $ECHO "FATAL (${scriptName}): ${OUT1} has a problem"
117 exit 1
118fi
119
120
121#Nb: if we want to implement a loop on sources we have to take care of srclower...
122
123$ECHO "You have selected sourceRadio = $sourceRadio[date = ${dateSelected}]"
124tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
125IFS='
126'
127tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
128IFS=$DefaultIFS
129
130
131for i in ${tableau[@]}
132 do
133#
134# start a "fresh" session
135 cd ${topDir}
136 $LS | $XARGS -i $RM -rf {}
137
138
139 $ECHO "DEBUG: (${scriptName}) irods root dir $i"
140 dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
141
142 $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
143 $LS -lrt
144
145
146# alreadyDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
147# if [ ${alreadyDone} -gt 0 ]; then
148# $ECHO "INFO (${scriptName}): calibration coef files already exist"
149# continue
150# fi
151
152 gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
153 if [ ! ${gainDone} -gt 0 ]; then
154 $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
155 continue
156 fi
157
158######
159 $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
160#go to the .../source/date-sourcelowercase directory
161#download Off/calibcycle<> and On/calibcycle<> directories
162 $MKDIR -p ${topDir}/${dateDAQ}${srclower}
163 cd ${topDir}/${dateDAQ}${srclower}
164 aboveOnOffDir=`pwd`
165 $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
166#
167 mode="Off"
168 $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
169 inFileDirectory="./${mode}"
170 $MKDIR -p ${inFileDirectory}
171 listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
172 IFS='
173'
174 listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
175 IFS=$DefaultIFS
176
177 firstCycleOff=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
178 nCyclesOff=`expr ${#listOfCalibCycle[*]} - 1`
179
180 lastCycleOff=`$ECHO ${listOfCalibCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
181
182 listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
183
184
185 for j in ${listOfCalibCycle[@]}
186 do
187 irodsDownDir="${j}"
188 OUT=./getInFits.$$
189 iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
190 igetStatus=`$GREP "^ERROR" ${OUT}`
191 if [ "<$igetStatus>" != "<>" ]; then
192 $ECHO "FATAL (${scriptName}): error while iget fits files:"
193 $ECHO $igetStatus
194 $ECHO " ==> skip this run $sourceRadio $dateDAQ"
195 continue
196 fi
197 $RM -f ${OUT}
198 done
199 $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
200#
201 mode="On"
202 $ECHO "DEBUG (${scriptName}): START download the ${mode} calib fits files"
203 inFileDirectory="./${mode}"
204 $MKDIR -p ${inFileDirectory}
205
206 listOfCalibCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "calibcycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.11n` )
207 IFS='
208 '
209 listOfCalibCycle=( $( $PRINTF "%s\n" "${listOfCalibCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
210 IFS=$DefaultIFS
211
212 firstCycleOn=`$ECHO ${listOfCalibCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
213 nCyclesOn=`expr ${#listOfCalibCycle[*]} - 1`
214
215 lastCycleOn=`$ECHO ${listOfCalibCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
216
217 listOfCalibCycle=( "${listOfCalibCycle[@]/#/${i}/${mode}/}" )
218
219
220#
221#Check if there is the same number of cycles in Off and On mode
222#
223#first cycle = max(firstCycleOn, firstCycleOff)
224#last cycle = min(lastCycleOn,lastCycleOff)
225
226 if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
227 firstCycle=${firstCycleOff}
228 lastCycle=${lastCycleOff}
229 else
230 $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
231 $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
232 firstCycle=${firstCycleOff}
233 if [ ${firstCycleOn} -gt ${firstCycle} ]; then
234 firstCycle=${firstCycleOn}
235 fi
236 lastCycle=${lastCycleOff}
237 if [ ${lastCycleOn} -lt ${lastCycle} ]; then
238 lastCycle=${lastCycleOn}
239 fi
240 fi
241 $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
242
243
244 for j in ${listOfCalibCycle[@]}
245 do
246 irodsDownDir="${j}"
247 OUT=./getInFits.$$
248 iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
249 igetStatus=`$GREP "^ERROR" ${OUT}`
250 if [ "<$igetStatus>" != "<>" ]; then
251 $ECHO "FATAL (${scriptName}): error while iget fits files:"
252 $ECHO $igetStatus
253 $ECHO " ==> skip this run $sourceRadio $dateDAQ"
254 continue
255 fi
256 $RM -f ${OUT}
257 done
258 $ECHO "DEBUG (${scriptName}): END download the ${mode} calib fits files"
259#Get sca file
260 $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
261 OUT1=./getScaStatus.$$
262 $RM -f ${tmppublicpath}/getscafiles.finished
263 ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
264 while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
265 $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
266 date +%T
267 sleep 30
268 done
269 $RM ${tmppublicpath}/getscafiles.finished
270 $ECHO "DEBUG (${scriptName}): END"
271#
272 getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
273#
274 if [ "<${getScaStatus}>" != "<>" ]; then
275 $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
276 $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
277 $CAT ${OUT1}
278 $ECHO "DEBUG (${scriptName}): END"
279 continue
280 fi
281 $RM -f ${OUT1}
282
283 scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
284
285#prepare job submission
286 cd ${aboveOnOffDir}
287#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
288
289 logFile="analyse_CALIB_${sourceRadio}_${dateDAQ}.log"
290 $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
291 if [ ${simulationMode} = "On" ]; then
292 $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}"
293 cat > ${logFile} <<EOF
294 Ok calibration finished
295EOF
296#WARNING: the EOF must start at 1st column overwise the rest of the script is part of the file cat...
297 else
298 ${scriptpath}/Objs/analyse -act calib -inPath ${iojobpath} -source ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir calibcycle -numcycle ${firstCycle},${lastCycle} -calibopt mean -debug 1 -freqBAOCalib ${freqBAOCalib} -bwBAOCalib ${bwBAOCalib} >& ${logFile}
299 fi
300#
301 $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
302 rcstatus=`$GREP -i "Ok calibration finished" ${logFile} | $WC -l`
303 if [ ${rcstatus} -eq 0 ]; then
304 $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
305 $ECHO " START logfile"
306 $CAT ${logFile}
307 $ECHO " END logFile"
308#clean and leave
309 cd ${aboveOnOffDir}
310 $LS | $XARGS -i $RM -rf {}
311 continue
312 fi
313#Save into Irods
314 $ECHO "DEBUG (${scriptName}): START save output files"
315 $LS ${aboveOnOffDir}
316#use -f option for iput ONLY to force override
317 if [ ${simulationMode} = "On" ]; then
318 $ECHO "SIMUL: here we look for calib file and put them in Irods"
319 else
320 $LS -lrth
321 $FIND . -name "calib_*" -print | $XARGS -i iput -f -v -K {} ${i}
322 fi
323#save analysis logfile
324 $LS -l ${tmppublicpath} > /dev/null
325 $CP ${logFile} ${tmppublicpath}
326 $ECHO "DEBUG (${scriptName}): END save output files"
327#clean up to avoid scratch SIZE EXCEED LIMIT
328 cd ${aboveOnOffDir}
329 $LS | $XARGS -i $RM -rf {}
330
331done
332
333exit 0
Note: See TracBrowser for help on using the repository browser.