source: BAORadio/AmasNancay/onoffmaker.sh @ 519

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

give the status of ON-Off analysis (jec)

  • Property svn:executable set to *
File size: 14.9 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; shift;;
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
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
102scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
103
104#get the daq current irod status
105tag=`${DATE} +%F`
106OUT1=${publicpath}/baodaqstatus-${tag}.txt
107
108if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
109    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
110    $RM -f ${tmppublicpath}/statusdaq.finished
111    ${scriptpath}/statusdaq.sh > ${OUT1}
112    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
113        sleep 30
114    done
115    $RM ${tmppublicpath}/statusdaq.finished
116#protect against remove/rewriting   
117    $CHMOD -v 444 ${OUT1} 
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  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
139  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
140
141  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
142  $LS -lrt
143
144
145#Look if the date has changed since beginning of the job!
146  curtag=`${DATE} +%F`
147  if [ "<${curtag}>" != "<${tag}>" ]; then
148      tag=${curtag}
149#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
150      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
151      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
152      $RM -f ${tmppublicpath}/statusdaq.finished
153      ${scriptpath}/statusdaq.sh > ${OUT1}
154      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
155          sleep 30
156      done
157      $RM ${tmppublicpath}/statusdaq.finished
158#protect against remove/rewriting   
159      $CHMOD -v 444 ${OUT1} 
160  fi
161#
162# look if the gain & calibration files exist
163#
164  gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
165  if [ ! ${gainDone} -gt 0 ]; then
166      $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
167      continue
168  fi
169
170  calibDone=`ils ${i} 2>&1 | $GREP -i "calib_.*\.txt" | $WC -l`
171  if [ ! ${calibDone} -gt 0 ]; then
172      $ECHO "FATAL (${scriptName}): calibration files DO NOT exist skip `basename $i` "
173      continue
174  fi
175#
176  somethingTodo=1
177  input4dataOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` 
178  input4dataOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` 
179#
180  if [ "<${input4dataOff}>" != "<>" -a "<${input4dataOn}>" != "<>"  ]; then
181#look at the content of the datacycle directory as may be the previous batch job has crashed
182      tableauOff=( `$ECHO ${input4dataOff}` )
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 ${input4dataOn}` )
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 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 ANALOFF"
224      $RM -f ${tmppublicpath}/proc_script.finished
225      if [ ${simulationMode} = "On" ]; then
226          $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF"
227      else
228          ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF
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 OFF"
237      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ON"
238      $RM ${tmppublicpath}/proc_script.finished
239     
240      if [ ${simulationMode} = "On" ]; then
241          $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON"
242      else
243          ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON
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 ON"
254######
255  else
256######
257      $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
258      #go to the .../source/date-sourcelowercase directory
259#download Off/datacycle<> and On/datacycle<> 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} data fits files"
267      inFileDirectory="./${mode}"
268      $MKDIR -p ${inFileDirectory}
269      listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
270      IFS='
271'
272      listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
273      IFS=$DefaultIFS
274
275      firstCycleOff=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
276      nCyclesOff=`expr ${#listOfDataCycle[*]} - 1`
277
278      lastCycleOff=`$ECHO ${listOfDataCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
279
280      listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
281
282     
283      for j in ${listOfDataCycle[@]}
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} data fits files"
298#
299      mode="On"
300      $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
301      inFileDirectory="./${mode}"
302      $MKDIR -p ${inFileDirectory}
303
304      listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
305      IFS='
306'
307      listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
308      IFS=$DefaultIFS
309
310      firstCycleOn=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
311      nCyclesOn=`expr ${#listOfDataCycle[*]} - 1`
312
313      lastCycleOn=`$ECHO ${listOfDataCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
314
315      listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
316
317
318#
319#Check if there is the same number of cycles in Off and On mode
320#     
321#first cycle = max(firstCycleOn, firstCycleOff)
322#last  cycle = min(lastCycleOn,lastCycleOff)
323
324      if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
325          firstCycle=${firstCycleOff}
326          lastCycle=${lastCycleOff}
327      else
328          $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
329          $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
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
338      fi
339      $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
340     
341
342      for j in ${listOfDataCycle[@]}
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} data 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
384#Get the calibration files
385      OUT=./getCalib.$$
386      iget -f -K -v ${i}/calib_*.txt ${aboveOnOffDir}> ${OUT} 2>&1
387      igetStatus=`$GREP "^ERROR" ${OUT}`
388      if [ "<$igetStatus>" != "<>" ]; then
389          $ECHO "FATAL (${scriptName}): error while iget calib files:"
390          $ECHO $igetStatus
391          exit 1
392      fi
393      $RM -f ${OUT}
394     
395#prepare job submission
396      cd ${aboveOnOffDir}
397#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
398
399      logFile="analyse_DATA_${sourceRadio}_${dateDAQ}.log"
400      $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
401      if [ ${simulationMode} = "On" ]; then
402          $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}"
403          cat > ${logFile} <<EOF
404          Ok calibration finished
405EOF
406      else
407          ${scriptpath}/Objs/analyse -act dataOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debuglev 1 -freqBAOCalib ${freqBAOCalib} >& ${logFile}
408      fi
409      $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
410      rcstatus=`$GREP -i "Ok dataOnOff finished" ${logFile} | $WC -l`
411      if [ ${rcstatus} -eq 0 ]; then
412          $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
413          $ECHO "                      START logfile"
414          $CAT ${logFile}
415          $ECHO "                      END   logFile"
416#clean and leave
417          cd ${aboveOnOffDir}
418          $LS | $XARGS -i $RM -rf {}
419          continue
420      fi
421#Save into Irods
422      $ECHO "DEBUG (${scriptName}): START save output files"
423      $LS ${aboveOnOffDir}
424#use -f option for iput ONLY to force override
425      if [ ${simulationMode} = "On" ]; then
426          $ECHO "SIMUL: here we look for calib file and put them in Irods"
427      else
428          $LS -lrth
429          $FIND . -name "dataRaw_*" -print | $XARGS -i  iput  -v -K {} ${i}
430          $FIND . -name "dataBAOCalib_*" -print | $XARGS -i  iput  -v -K {} ${i}
431          $FIND . -name "dataBAORTCalib_*" -print | $XARGS -i  iput  -v -K {} ${i}
432          $FIND . -name "diffOnOff_*" -print | $XARGS -i  iput  -v -K {} ${i}
433      fi
434#save analysis logfile
435      $LS -l ${tmppublicpath} > /dev/null
436      $CP ${logFile} ${tmppublicpath}
437      $ECHO "DEBUG (${scriptName}): END save output files"
438#clean up to avoid scratch SIZE EXCEED LIMIT
439      cd ${aboveOnOffDir}
440      $LS | $XARGS -i $RM -rf {}
441  fi
442done
443
444exit 0
Note: See TracBrowser for help on using the repository browser.