source: BAORadio/AmasNancay/v3/onoffmaker.sh @ 564

Last change on this file since 564 was 547, checked in by campagne, 13 years ago

move to trunk the previous HEAD (jec)

  • Property svn:executable set to *
File size: 15.5 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
83if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
84    iojobpath="/sps/baoradio/AmasNancay/JEC"
85    $MKDIR -p $iojobpath
86    $ECHO "Usage in INTERACTIVE may be destructive... To be used with great care !!!!"
87    exit 0;
88elif [ ${ENVIRONMENT} = "BATCH" ] ; then
89    iojobpath=${TMPBATCH}
90else
91    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
92    exit 1
93fi
94cd ${iojobpath}
95
96localpath="${sourceRadio}"
97$MKDIR -p ./${localpath}
98cd ./${localpath}
99
100#save the top directory
101topDir=`pwd`
102
103#Path of the utility scripts
104scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
105
106#JEC 1/10/11 Use generic baodaqstatus name
107#get the daq current irod status
108#tag=`${DATE} +%F`
109#OUT1=${publicpath}/baodaqstatus-${tag}.txt
110OUT1=${publicpath}/baodaqstatus-current.txt
111
112if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
113    $ECHO "FATAL (${scriptName}): ${OUT1} has a problem"
114    exit 1
115#    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
116#    $RM -f ${tmppublicpath}/statusdaq.finished
117#    ${scriptpath}/statusdaq.sh > ${OUT1}
118#    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
119#       sleep 30
120#    done
121#    $RM ${tmppublicpath}/statusdaq.finished
122#protect against remove/rewriting   
123#    $CHMOD -v 444 ${OUT1}
124fi
125
126
127#Nb: if we want to implement a loop on sources we have to take care of srclower...
128
129$ECHO "You have selected sourceRadio = ${sourceRadio} [date = ${dateSelected}]"
130tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
131IFS='
132' 
133tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
134IFS=$DefaultIFS
135
136
137for i in ${tableau[@]}
138  do
139#
140# start a "fresh" session 
141  cd ${topDir}
142  $LS | $XARGS -i $RM -rf {}
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 gain & calibration files exist
170#
171  gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
172  if [ ! ${gainDone} -gt 0 ]; then
173      $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
174      continue
175  fi
176
177  calibDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
178  if [ ! ${calibDone} -gt 0 ]; then
179      $ECHO "FATAL (${scriptName}): calibration files DO NOT exist skip `basename $i` "
180      continue
181  fi
182#
183  somethingTodo=1
184  input4dataOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` 
185  input4dataOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` 
186#
187  if [ "<${input4dataOff}>" != "<>" -a "<${input4dataOn}>" != "<>"  ]; then
188#look at the content of the datacycle directory as may be the previous batch job has crashed
189      tableauOff=( `$ECHO ${input4dataOff}` )
190      IFS='
191'
192      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
193      IFS=$DefaultIFS
194#
195      nTotFilesOff=0
196      for j in ${tableauOff[@]}
197        do
198        nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
199        nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
200      done
201#
202      tableauOn=( `$ECHO ${input4dataOn}` )
203      IFS='
204      '
205      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
206      IFS=$DefaultIFS
207#
208      nTotFilesOn=0
209      for j in ${tableauOn[@]}
210        do
211        nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
212        nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
213      done
214      diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
215      if [ ${diffOnOff} -lt 0 ]; then
216          zero=0
217          diffOnOff=`expr ${zero} - ${diffOnOff}` 
218      fi
219#allow files differences
220      if [  ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0  ]; then
221          somethingTodo=0
222          $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
223      else
224          $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
225      fi
226#
227  fi 
228  if [ ${somethingTodo} -eq 1 ]; then
229#
230      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ANALOFF"
231      $RM -f ${tmppublicpath}/proc_script.finished
232      if [ ${simulationMode} = "On" ]; then
233          $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF"
234      else
235          ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF
236          while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
237              $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
238              date +%T
239              sleep 60
240          done
241      fi
242      $ECHO "INFO (${scriptName}): proc_script has finished"
243      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ OFF"
244      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ON"
245      $RM ${tmppublicpath}/proc_script.finished
246     
247      if [ ${simulationMode} = "On" ]; then
248          $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON"
249      else
250          ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON
251          while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
252              $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
253              date +%T
254              sleep 60
255          done
256      fi
257      $ECHO "INFO (${scriptName}): proc_script has finished"
258      $RM ${tmppublicpath}/proc_script.finished
259#Todo see if there is errors in the log file before copy
260      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ ON"
261######
262  else
263######
264      $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
265      #go to the .../source/date-sourcelowercase directory
266#download Off/datacycle<> and On/datacycle<> directories
267      $MKDIR -p ${topDir}/${dateDAQ}${srclower}
268      cd ${topDir}/${dateDAQ}${srclower}
269      aboveOnOffDir=`pwd`
270      $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
271#
272      mode="Off"
273      $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
274      inFileDirectory="./${mode}"
275      $MKDIR -p ${inFileDirectory}
276      listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
277      IFS='
278'
279      listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
280      IFS=$DefaultIFS
281
282      firstCycleOff=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
283      nCyclesOff=`expr ${#listOfDataCycle[*]} - 1`
284
285      lastCycleOff=`$ECHO ${listOfDataCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
286
287      listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
288
289     
290      for j in ${listOfDataCycle[@]}
291        do
292        irodsDownDir="${j}"
293        OUT=./getInFits.$$
294        iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
295        igetStatus=`$GREP "^ERROR" ${OUT}`
296        if [ "<$igetStatus>" != "<>" ]; then
297            $ECHO "FATAL (${scriptName}): error while iget fits files:"
298            $ECHO $igetStatus
299            $ECHO " ==> skip this run $sourceRadio $dateDAQ"
300            continue
301        fi
302        $RM -f ${OUT}
303      done
304      $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
305#
306      mode="On"
307      $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
308      inFileDirectory="./${mode}"
309      $MKDIR -p ${inFileDirectory}
310
311      listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
312      IFS='
313'
314      listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
315      IFS=$DefaultIFS
316
317      firstCycleOn=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
318      nCyclesOn=`expr ${#listOfDataCycle[*]} - 1`
319
320      lastCycleOn=`$ECHO ${listOfDataCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
321
322      listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
323
324
325#
326#Check if there is the same number of cycles in Off and On mode
327#     
328#first cycle = max(firstCycleOn, firstCycleOff)
329#last  cycle = min(lastCycleOn,lastCycleOff)
330
331      if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
332          firstCycle=${firstCycleOff}
333          lastCycle=${lastCycleOff}
334      else
335          $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
336          $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
337          firstCycle=${firstCycleOff}
338          if [ ${firstCycleOn} -gt ${firstCycle} ]; then
339              firstCycle=${firstCycleOn}
340          fi
341          lastCycle=${lastCycleOff}
342          if [ ${lastCycleOn} -lt ${lastCycle} ]; then
343              lastCycle=${lastCycleOn}
344          fi
345      fi
346      $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
347     
348
349      for j in ${listOfDataCycle[@]}
350        do
351        irodsDownDir="${j}"
352        OUT=./getInFits.$$
353        iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
354        igetStatus=`$GREP "^ERROR" ${OUT}`
355        if [ "<$igetStatus>" != "<>" ]; then
356            $ECHO "FATAL (${scriptName}): error while iget fits files:"
357            $ECHO $igetStatus
358            $ECHO " ==> skip this run $sourceRadio $dateDAQ"
359            continue
360        fi
361        $RM -f ${OUT}
362      done
363      $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
364#Get sca file
365      $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
366      OUT1=./getScaStatus.$$
367      $RM -f ${tmppublicpath}/getscafiles.finished
368      ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
369      while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
370          $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
371          date +%T
372          sleep 30
373      done
374      $RM ${tmppublicpath}/getscafiles.finished
375      $ECHO "DEBUG (${scriptName}): END"
376#
377      getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
378#       
379      if [ "<${getScaStatus}>" != "<>" ]; then
380          $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
381          $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
382          $CAT ${OUT1}
383          $ECHO "DEBUG (${scriptName}): END"
384          continue
385      fi
386      $RM -f ${OUT1}
387
388      scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
389
390
391#Get the calibration files
392      OUT2=./calibFiles.$$
393      ils ${i} | ${GREP} -i "calib.*\.txt" > ${OUT2}
394      listeOfCalib=( `$CAT ${OUT2}` )
395      IFS='
396'
397      listeOfCalib=( $( $PRINTF "%s\n" "${listeOfCalib[@]}" | $AWK 'x[$0]++ == 0' ) )
398      IFS=$DefaultIFS
399      $RM $OUT2
400      for fcal in ${listeOfCalib[@]}
401      do
402        OUT3=./getCalib.$$
403        iget -f -K -v ${i}/${fcal} ${aboveOnOffDir}> ${OUT3} 2>&1
404        igetStatus=`$GREP "^ERROR" ${OUT3}`
405        if [ "<$igetStatus>" != "<>" ]; then
406            $ECHO "FATAL (${scriptName}): error while iget calib files:"
407            $ECHO $igetStatus
408            exit 1
409        fi
410        $RM -f ${OUT3}
411      done
412     
413#prepare job submission
414      cd ${aboveOnOffDir}
415#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
416
417      logFile="analyse_DATA_${sourceRadio}_${dateDAQ}.log"
418      $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
419      if [ ${simulationMode} = "On" ]; then
420          $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}"
421          cat > ${logFile} <<EOF
422          Ok calibration finished
423EOF
424      else
425          ${scriptpath}/Objs/analyse -act dataOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debuglev 1 -freqBAOCalib ${freqBAOCalib} >& ${logFile}
426      fi
427      $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
428      rcstatus=`$GREP -i "Ok dataOnOff finished" ${logFile} | $WC -l`
429      if [ ${rcstatus} -eq 0 ]; then
430          $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
431          $ECHO "                      START logfile"
432          $CAT ${logFile}
433          $ECHO "                      END   logFile"
434#clean and leave
435          cd ${aboveOnOffDir}
436          $LS | $XARGS -i $RM -rf {}
437          continue
438      fi
439#Save into Irods
440      $ECHO "DEBUG (${scriptName}): START save output files"
441      $LS ${aboveOnOffDir}
442#use -f option for iput ONLY to force override
443      if [ ${simulationMode} = "On" ]; then
444          $ECHO "SIMUL: here we look for calib file and put them in Irods"
445      else
446          $LS -lrth
447          $FIND . -name "dataRaw_*" -print | $XARGS -i  iput  -v -K {} ${i}
448          $FIND . -name "dataBAOCalib_*" -print | $XARGS -i  iput  -v -K {} ${i}
449          $FIND . -name "dataBAORTCalib_*" -print | $XARGS -i  iput  -v -K {} ${i}
450          $FIND . -name "diffOnOff_*" -print | $XARGS -i  iput  -v -K {} ${i}
451      fi
452#save analysis logfile
453      $LS -l ${tmppublicpath} > /dev/null
454      $CP ${logFile} ${tmppublicpath}
455      $ECHO "DEBUG (${scriptName}): END save output files"
456#clean up to avoid scratch SIZE EXCEED LIMIT
457      cd ${aboveOnOffDir}
458      $LS | $XARGS -i $RM -rf {}
459  fi
460done
461
462exit 0
Note: See TracBrowser for help on using the repository browser.