source: BAORadio/AmasNancay/trunk/rawonoffmaker.sh @ 575

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

introduce SCRIPTPATH environment variable (jec)

  • Property svn:executable set to *
File size: 10.1 KB
Line 
1#!/bin/sh
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 ]
36do
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
84    $ECHO "Usage in INTERACTIVE may be destructive... To be used with great care !!!!"
85    exit 0;
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=${SCRIPTPATH}
103
104#get the daq current irod status
105#tag=`${DATE} +%F`
106#OUT1=${publicpath}/baodaqstatus-${tag}.txt
107####JEC 1/10/11 tag=`${DATE} +%F`
108#Use generic baodaqstatus name
109OUT1=${publicpath}/baodaqstatus-current.txt
110
111if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
112    $ECHO "FATAL (${scriptName}): ${OUT1} has a problem"
113    exit 1
114#JEC 29/9/11 avoid finished stuff
115#    $RM -f ${tmppublicpath}/statusdaq.finished
116#    ${scriptpath}/statusdaq.sh > ${OUT1}
117#    while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
118#       sleep 30
119#    done
120#    $RM ${tmppublicpath}/statusdaq.finished
121#protect against remove/rewriting   
122#    $CHMOD -v 444 ${OUT1}
123fi
124
125
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} [date = ${dateSelected}]"
131tableau=( `$GREP -i "${dateSelected}${srclower}" ${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  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
146  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
147
148  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
149  $LS -lrt
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#
178  somethingTodo=1
179  input4dataOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` 
180  input4dataOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` 
181#
182  if [ "<${input4dataOff}>" != "<>" -a "<${input4dataOn}>" != "<>"  ]; then
183#look at the content of the datacycle directory as may be the previous batch job has crashed
184      tableauOff=( `$ECHO ${input4dataOff}` )
185      IFS='
186'
187      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
188      IFS=$DefaultIFS
189#
190      nTotFilesOff=0
191      for j in ${tableauOff[@]}
192        do
193        nFiles=`ils ${i}/Off/${j} | $GREP -i "medfiltmtx" | $WC -l`
194        nTotFilesOff=`expr ${nTotFilesOff} + ${nFiles}`
195      done
196#
197      tableauOn=( `$ECHO ${input4dataOn}` )
198      IFS='
199      '
200      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
201      IFS=$DefaultIFS
202#
203      nTotFilesOn=0
204      for j in ${tableauOn[@]}
205        do
206        nFiles=`ils ${i}/On/${j} | $GREP -i "medfiltmtx" | $WC -l`
207        nTotFilesOn=`expr ${nTotFilesOn} + ${nFiles}`
208      done
209      diffOnOff=`expr ${nTotFilesOff} - ${nTotFilesOn}`
210      if [ ${diffOnOff} -lt 0 ]; then
211          zero=0
212          diffOnOff=`expr ${zero} - ${diffOnOff}` 
213      fi
214#allow files differences but non zero
215      if [  ${nTotFilesOff} -gt 0 -a ${nTotFilesOn} -gt 0  ]; then
216          somethingTodo=0
217          $ECHO "DEBUG (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files but OK"
218      else
219          $ECHO "INFO (${scriptName}): $sourceRadio $dateDAQ has <${nTotFilesOff}> Off-files and <${nTotFilesOn}> On-files => one should rebuild has something is wrong"
220      fi
221#
222#look at consecutive cycles
223
224      rmDirectory=0
225
226      cycleIdOff=`$ECHO $input4dataOff | sed 's/datacycle//g'`
227      tableauOff=( `$ECHO ${cycleIdOff}` )
228      IFS='
229'
230      tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
231      IFS=$DefaultIFS
232     
233      lastIdOff=`expr ${#tableauOff[*]} - 1`
234      ncycleOff=`expr ${tableauOff[${lastIdOff}]} - ${tableauOff[0]} + 1`
235      ncycleOffVerif=${#tableauOff[*]}
236      if [ "<${ncycleOff}>" != "<${ncycleOffVerif}>"  ]; then
237          $ECHO "$sourceRadio $dateDAQ: There is a lack of OFF cycles in the sequence: ${tableauOff[@]}"
238          rmDirectory=1
239          somethingTodo=1
240      else
241          $ECHO "$sourceRadio $dateDAQ: Ok at least consecutive OFF cycles: ${tableauOff[@]}"
242      fi
243
244      cycleIdOn=`$ECHO $input4dataOn | sed 's/datacycle//g'`
245      tableauOn=( `$ECHO ${cycleIdOn}` )
246      IFS='
247'
248      tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
249      IFS=$DefaultIFS
250     
251      lastIdOn=`expr ${#tableauOn[*]} - 1`
252      ncycleOn=`expr ${tableauOn[${lastIdOn}]} - ${tableauOn[0]} + 1`
253      ncycleOnVerif=${#tableauOn[*]}
254      if [ "<${ncycleOn}>" != "<${ncycleOnVerif}>"  ]; then
255          $ECHO "$sourceRadio $dateDAQ: There is a lack of ON cycles in the sequence: ${tableauOn[@]}"
256          rmDirectory=1
257          somethingTodo=1
258      else
259          $ECHO "$sourceRadio $dateDAQ: Ok at least consecutive ON cycles:  ${tableauOn[@]}"
260      fi
261
262      if [ "<${cycleIdOff}>" != "<${cycleIdOn}>" ]; then
263          $ECHO "$sourceRadio $dateDAQ: cycles seq. missmatch ON and OFF"
264          rmDirectory=1
265          somethingTodo=1         
266      fi
267#
268  fi 
269########
270  if [ ${somethingTodo} -eq 1 ]; then
271#
272      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ANALOFF"
273
274      if [ ${rmDirectory} -eq 1 ]; then
275#delete datacycle Off
276          dirToRmOff=`ils ${i}/Off 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}'` 
277          tableauOff=( `$ECHO ${dirToRmOff}` )
278          IFS='
279'
280          tableauOff=( $( $PRINTF "%s\n" "${tableauOff[@]}" | $AWK 'x[$0]++ == 0' ) )
281          IFS=$DefaultIFS
282          for j in ${tableauOff[@]}
283            do
284            irm -f -r -v ${j}
285          done
286#delete datacycle On
287          dirToRmOn=`ils ${i}/On 2>&1 | $GREP -i "datacycle" | $AWK 'BEGIN{FS="C- "}{print $2}'` 
288          tableauOn=( `$ECHO ${dirToRmOn}` )
289          IFS='
290'
291          tableauOn=( $( $PRINTF "%s\n" "${tableauOn[@]}" | $AWK 'x[$0]++ == 0' ) )
292          IFS=$DefaultIFS
293          for j in ${tableauOn[@]}
294            do
295            irm -f -r -v ${j}
296          done
297      fi
298         
299      $RM -f ${tmppublicpath}/proc_script.finished
300      if [ ${simulationMode} = "On" ]; then
301          $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF"
302      else
303          ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ OFF
304          while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
305              $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
306              date +%T
307              sleep 60
308          done
309      fi
310      $ECHO "INFO (${scriptName}): proc_script has finished"
311      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ OFF"
312      $ECHO ">>>>>>>>>>>>>>>> Do $sourceRadio $dateDAQ ON"
313      $RM ${tmppublicpath}/proc_script.finished
314     
315      if [ ${simulationMode} = "On" ]; then
316          $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON"
317      else
318          ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ ON
319          while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
320              $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
321              date +%T
322              sleep 60
323          done
324      fi
325      $ECHO "INFO (${scriptName}): proc_script has finished"
326      $RM ${tmppublicpath}/proc_script.finished
327#Todo see if there is errors in the log file before copy
328      $ECHO ">>>>>>>>>>>> Done $sourceRadio $dateDAQ ON"
329  fi
330done
331
332exit 0
Note: See TracBrowser for help on using the repository browser.