source: BAORadio/AmasNancay/calibmaker.sh @ 507

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

New import

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