source: BAORadio/AmasNancay/calibmaker.sh @ 542

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

forbid INTERACTIVE Mode

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