source: BAORadio/AmasNancay/trunk/calibmaker.sh @ 579

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

version adaptee a grid engin (ana)

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