source: BAORadio/AmasNancay/anarawonoffmaker.sh @ 541

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

avoid the link to a specif bao daq date (jec)

  • Property svn:executable set to *
File size: 11.2 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=
33#use -sim option to simulate processing (debug the script as if...)
34simulationMode=Off
35while [ $# -gt 0 ]
36  do
37  case "$1" in
38      -src)  sourceRadio=$2;    shift;;
39      -date) dateSelected=$2;     shift;;
40      -sim)  simulationMode=On;;
41      -h)
42      echo >&2 \
43          "usage: $0 -src souce -date YYYYMMDD [-freq freq in MHz (default ${freqBAOCalib})] [-sim to trig simulation mode]"
44      exit 1;;
45      *)  break;;       # terminate while loop
46  esac
47  shift
48done
49
50if [ ${simulationMode} = "On" ]; then
51    $ECHO "INFO ${scriptName} running in SIMUL mode"
52fi
53
54if [ "<${sourceRadio}>" = "<>" ]; then
55    $ECHO "FATAL: You have forgotten to select the source option (-src)"
56    exit 1
57fi
58
59case ${sourceRadio} in
60    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
61    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
62    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
63    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
64    exit 1;;
65esac
66
67srclower=`${ECHO} ${sourceRadio} | ${TR} "[:upper:]" "[:lower:]" `
68
69#Path to public backupable path
70publicpath="/afs/in2p3.fr/home/c/campagne/public"
71
72
73#temporary files to synchronize scripts
74tmppublicpath=${TMPPUBLICPATH}
75#wakeup the NFS disk
76$LS -l ${tmppublicpath} > /dev/null
77#clean previous spurious files
78$RM -f ${tmppublicpath}/*.finished
79
80#Path where the job will do temporary IO
81if [ ${ENVIRONMENT} = "INTERACTIVE" ]; then
82    iojobpath="/sps/baoradio/AmasNancay/JEC"
83    $MKDIR -p $iojobpath
84elif [ ${ENVIRONMENT} = "BATCH" ] ; then
85    iojobpath=${TMPBATCH}
86else
87    $ECHO "FATAL (${scriptName}): environment is ${ENVIRONMENT} not allowed"
88    exit 1
89fi
90cd ${iojobpath}
91
92localpath="${sourceRadio}"
93$MKDIR -p ./${localpath}
94cd ./${localpath}
95
96#save the top directory
97topDir=`pwd`
98
99#Path of the utility scripts
100scriptpath="/afs/in2p3.fr/home/c/campagne/private/work/AmasNancay"
101
102#JEC 1/10/11 Use generic baodaqstatus name
103#get the daq current irod status
104#tag=`${DATE} +%F`
105#OUT1=${publicpath}/baodaqstatus-${tag}.txt
106OUT1=${publicpath}/baodaqstatus-current.txt
107
108if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
109    $ECHO "FATAL (${scriptName}): ${OUT1} has a problem"
110    exit 1
111#    $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
112#    $RM -f ${tmppublicpath}/statusdaq.finished
113#    ${scriptpath}/statusdaq.sh > ${OUT1}
114#    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
115#       sleep 30
116#    done
117#    $RM ${tmppublicpath}/statusdaq.finished
118#protect against remove/rewriting   
119#    $CHMOD -v 444 ${OUT1}
120fi
121
122
123
124$ECHO "You have selected sourceRadio = ${sourceRadio} [date = ${dateSelected}]"
125tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
126IFS='
127' 
128tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
129IFS=$DefaultIFS
130
131
132for i in ${tableau[@]}
133  do
134#
135# start a "fresh" session 
136  cd ${topDir}
137  $LS | $XARGS -i $RM -rf {}
138 
139  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
140  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
141
142  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
143  $LS -lrt
144
145
146#JEC 1/10/11 Use generic baodaqstatus name
147#Look if the date has changed since beginning of the job!
148#  curtag=`${DATE} +%F`
149#  if [ "<${curtag}>" != "<${tag}>" ]; then
150#      tag=${curtag}
151#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
152#      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
153#      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
154#      $RM -f ${tmppublicpath}/statusdaq.finished
155#      ${scriptpath}/statusdaq.sh > ${OUT1}
156#     while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
157#         sleep 30
158#      done
159#      $RM ${tmppublicpath}/statusdaq.finished
160#protect against remove/rewriting   
161#      $CHMOD -v 444 ${OUT1}
162#  fi
163#
164# look if the gain & calibration files exist
165#
166  gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
167  if [ ! ${gainDone} -gt 0 ]; then
168      $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
169      continue
170  fi
171
172######
173  $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
174#go to the .../source/date-sourcelowercase directory
175#download Off/datacycle<> and On/datacycle<> directories
176  $MKDIR -p ${topDir}/${dateDAQ}${srclower}
177  cd ${topDir}/${dateDAQ}${srclower}
178  aboveOnOffDir=`pwd`
179  $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
180#
181  mode="Off"
182  $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
183  inFileDirectory="./${mode}"
184  $MKDIR -p ${inFileDirectory}
185  listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
186  IFS='
187'
188  listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
189  IFS=$DefaultIFS
190
191  firstCycleOff=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
192  nCyclesOff=`expr ${#listOfDataCycle[*]} - 1`
193
194  if [ ${nCyclesOff} -eq -1 ]; then
195      continue
196  fi
197
198  lastCycleOff=`$ECHO ${listOfDataCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
199
200  listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
201
202 
203  for j in ${listOfDataCycle[@]}
204    do
205    irodsDownDir="${j}"
206    OUT=./getInFits.$$
207    iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
208    igetStatus=`$GREP "^ERROR" ${OUT}`
209    if [ "<$igetStatus>" != "<>" ]; then
210        $ECHO "FATAL (${scriptName}): error while iget fits files:"
211        $ECHO $igetStatus
212        $ECHO " ==> skip this run $sourceRadio $dateDAQ"
213        continue
214    fi
215    $RM -f ${OUT}
216  done
217  $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
218#
219  mode="On"
220  $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
221  inFileDirectory="./${mode}"
222  $MKDIR -p ${inFileDirectory}
223
224  listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
225  IFS='
226'
227  listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
228  IFS=$DefaultIFS
229
230  firstCycleOn=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
231  nCyclesOn=`expr ${#listOfDataCycle[*]} - 1`
232
233  if [ ${nCyclesOn} -eq -1 ]; then
234      continue
235  fi
236
237  lastCycleOn=`$ECHO ${listOfDataCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
238
239  listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
240
241#
242#Check if there is the same number of cycles in Off and On mode
243#     
244#first cycle = max(firstCycleOn, firstCycleOff)
245#last  cycle = min(lastCycleOn,lastCycleOff)
246
247  if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
248      firstCycle=${firstCycleOff}
249      lastCycle=${lastCycleOff}
250  else
251      $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
252      $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
253      firstCycle=${firstCycleOff}
254      if [ ${firstCycleOn} -gt ${firstCycle} ]; then
255          firstCycle=${firstCycleOn}
256      fi
257      lastCycle=${lastCycleOff}
258      if [ ${lastCycleOn} -lt ${lastCycle} ]; then
259          lastCycle=${lastCycleOn}
260      fi
261  fi
262  $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
263 
264
265  for j in ${listOfDataCycle[@]}
266    do
267    irodsDownDir="${j}"
268    OUT=./getInFits.$$
269    iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
270    igetStatus=`$GREP "^ERROR" ${OUT}`
271    if [ "<$igetStatus>" != "<>" ]; then
272        $ECHO "FATAL (${scriptName}): error while iget fits files:"
273        $ECHO $igetStatus
274        $ECHO " ==> skip this run $sourceRadio $dateDAQ"
275        continue
276    fi
277    $RM -f ${OUT}
278  done
279  $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
280#Get sca file
281  $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
282  OUT1=./getScaStatus.$$
283  $RM -f ${tmppublicpath}/getscafiles.finished
284  ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
285  while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
286      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
287      date +%T
288      sleep 30
289  done
290  $RM ${tmppublicpath}/getscafiles.finished
291  $ECHO "DEBUG (${scriptName}): END"
292#
293  getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
294#       
295  if [ "<${getScaStatus}>" != "<>" ]; then
296      $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
297      $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
298      $CAT ${OUT1}
299      $ECHO "DEBUG (${scriptName}): END"
300      continue
301  fi
302  $RM -f ${OUT1}
303
304  scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
305
306 
307#prepare job submission
308  cd ${aboveOnOffDir}
309#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
310
311  logFile="analyse_RAWDATA_${sourceRadio}_${dateDAQ}.log"
312  $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
313  if [ ${simulationMode} = "On" ]; then
314      $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act rawOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debug 1 >& ${logFile}"
315      cat > ${logFile} <<EOF
316      Ok rawOnOff finished
317EOF
318  else
319      ${scriptpath}/Objs/analyse -act rawOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debug 1 >& ${logFile}
320  fi
321  $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
322  rcstatus=`$GREP -i "Ok rawOnOff finished" ${logFile} | $WC -l`
323  if [ ${rcstatus} -eq 0 ]; then
324      $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
325      $ECHO "                      START logfile"
326      $CAT ${logFile}
327      $ECHO "                      END   logFile"
328#clean and leave
329      cd ${aboveOnOffDir}
330      $LS | $XARGS -i $RM -rf {}
331      continue
332  fi
333#Save into Irods
334  $ECHO "DEBUG (${scriptName}): START save output files"
335  $LS ${aboveOnOffDir}
336#use -f option for iput ONLY to force override
337  if [ ${simulationMode} = "On" ]; then
338      $ECHO "SIMUL: here we look for output files and put them in Irods"
339  else
340      $LS -lrth
341      $FIND . -name "dataRaw_*"      -print | $XARGS -i  iput -f -v -K {} ${i}
342      $FIND . -name "diffOnOffRaw_*" -print | $XARGS -i  iput -f -v -K {} ${i}
343  fi
344#save analysis logfile
345  $LS -l ${tmppublicpath} > /dev/null
346  $CP ${logFile} ${tmppublicpath}
347  $ECHO "DEBUG (${scriptName}): END save output files"
348#clean up to avoid scratch SIZE EXCEED LIMIT
349  cd ${aboveOnOffDir}
350  $LS | $XARGS -i $RM -rf {}
351
352done
353
354exit 0
Note: See TracBrowser for help on using the repository browser.