source: BAORadio/AmasNancay/trunk/anarawonoffmaker.sh @ 579

Last change on this file since 579 was 579, checked in by torrento, 13 years ago

version adaptee a grid engin (ana)

  • Property svn:executable set to *
File size: 10.0 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
81. ${SCRIPTPATH}/isInteractive.sh
82isInteractive=$(isInteractive)
83if [ ${isInteractive} == "1" ]; then
84  $ECHO "Usage in INTERACTIVE may be destructive... To be used with great care !!!!"
85   exit 0;
86fi
87
88. ${SCRIPTPATH}/set_iojobpath.sh
89iojobpath=$(set_iojobpath)
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=${SCRIPTPATH}
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
111fi
112
113
114
115$ECHO "You have selected sourceRadio = ${sourceRadio} [date = ${dateSelected}]"
116tableau=( `$GREP -i "${dateSelected}${srclower}" ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
117IFS='
118' 
119tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
120IFS=$DefaultIFS
121
122
123for i in ${tableau[@]}
124  do
125#
126# start a "fresh" session 
127  cd ${topDir}
128  $LS | $XARGS -i $RM -rf {}
129 
130  $ECHO "DEBUG: (${scriptName}) irods root dir $i"
131  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
132
133  $ECHO "DEBUG: (${scriptName}): we are in directory `pwd` the topDir=${topDir}"
134  $LS -lrt
135
136  gainDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
137  if [ ! ${gainDone} -gt 0 ]; then
138      $ECHO "FATAL (${scriptName}): gain file DOES NOT exist skip `basename $i` "
139      continue
140  fi
141
142######
143  $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
144#go to the .../source/date-sourcelowercase directory
145#download Off/datacycle<> and On/datacycle<> directories
146  $MKDIR -p ${topDir}/${dateDAQ}${srclower}
147  cd ${topDir}/${dateDAQ}${srclower}
148  aboveOnOffDir=`pwd`
149  $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
150#
151  mode="Off"
152  $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
153  inFileDirectory="./${mode}"
154  $MKDIR -p ${inFileDirectory}
155  listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle"|$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
156  IFS='
157'
158  listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
159  IFS=$DefaultIFS
160
161  firstCycleOff=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
162  nCyclesOff=`expr ${#listOfDataCycle[*]} - 1`
163
164  if [ ${nCyclesOff} -eq -1 ]; then
165      continue
166  fi
167
168  lastCycleOff=`$ECHO ${listOfDataCycle[${nCyclesOff}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
169
170  listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
171
172 
173  for j in ${listOfDataCycle[@]}
174    do
175    irodsDownDir="${j}"
176    OUT=./getInFits.$$
177    iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
178    igetStatus=`$GREP "^ERROR" ${OUT}`
179    if [ "<$igetStatus>" != "<>" ]; then
180        $ECHO "FATAL (${scriptName}): error while iget fits files:"
181        $ECHO $igetStatus
182        $ECHO " ==> skip this run $sourceRadio $dateDAQ"
183        continue
184    fi
185    $RM -f ${OUT}
186  done
187  $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
188#
189  mode="On"
190  $ECHO "DEBUG (${scriptName}): START download the ${mode} data fits files"
191  inFileDirectory="./${mode}"
192  $MKDIR -p ${inFileDirectory}
193
194  listOfDataCycle=( `ils ${i}/${mode} 2>&1 | $GREP -i "datacycle" |$AWK 'BEGIN{FS="C- "}{print $2}' | $XARGS -i basename {} | $SORT -k1.10n` )
195  IFS='
196'
197  listOfDataCycle=( $( $PRINTF "%s\n" "${listOfDataCycle[@]}" | $AWK 'x[$0]++ == 0' ) )
198  IFS=$DefaultIFS
199
200  firstCycleOn=`$ECHO ${listOfDataCycle[0]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
201  nCyclesOn=`expr ${#listOfDataCycle[*]} - 1`
202
203  if [ ${nCyclesOn} -eq -1 ]; then
204      continue
205  fi
206
207  lastCycleOn=`$ECHO ${listOfDataCycle[${nCyclesOn}]} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
208
209  listOfDataCycle=( "${listOfDataCycle[@]/#/${i}/${mode}/}" )
210
211#
212#Check if there is the same number of cycles in Off and On mode
213#     
214#first cycle = max(firstCycleOn, firstCycleOff)
215#last  cycle = min(lastCycleOn,lastCycleOff)
216
217  if [ ${firstCycleOn} -eq ${firstCycleOff} -a ${lastCycleOn} -eq ${lastCycleOff} ]; then
218      firstCycle=${firstCycleOff}
219      lastCycle=${lastCycleOff}
220  else
221      $ECHO "INFO (${scriptName}): missmatch between Off and On mode"
222      $ECHO "${firstCycleOn} != ${firstCycleOff} OR ${lastCycleOn} != ${lastCycleOff}"
223      firstCycle=${firstCycleOff}
224      if [ ${firstCycleOn} -gt ${firstCycle} ]; then
225          firstCycle=${firstCycleOn}
226      fi
227      lastCycle=${lastCycleOff}
228      if [ ${lastCycleOn} -lt ${lastCycle} ]; then
229          lastCycle=${lastCycleOn}
230      fi
231  fi
232  $ECHO "INFO (${scriptName}): use cycles [${firstCycle}, ${lastCycle}]"
233 
234
235  for j in ${listOfDataCycle[@]}
236    do
237    irodsDownDir="${j}"
238    OUT=./getInFits.$$
239    iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
240    igetStatus=`$GREP "^ERROR" ${OUT}`
241    if [ "<$igetStatus>" != "<>" ]; then
242        $ECHO "FATAL (${scriptName}): error while iget fits files:"
243        $ECHO $igetStatus
244        $ECHO " ==> skip this run $sourceRadio $dateDAQ"
245        continue
246    fi
247    $RM -f ${OUT}
248  done
249  $ECHO "DEBUG (${scriptName}): END download the ${mode} data fits files"
250#Get sca file
251  $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
252  OUT1=./getScaStatus.$$
253  $RM -f ${tmppublicpath}/getscafiles.finished
254  ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
255  while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
256      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
257      date +%T
258      sleep 30
259  done
260  $RM ${tmppublicpath}/getscafiles.finished
261  $ECHO "DEBUG (${scriptName}): END"
262#
263  getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
264#       
265  if [ "<${getScaStatus}>" != "<>" ]; then
266      $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
267      $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
268      $CAT ${OUT1}
269      $ECHO "DEBUG (${scriptName}): END"
270      continue
271  fi
272  $RM -f ${OUT1}
273
274  scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
275
276 
277#prepare job submission
278  cd ${aboveOnOffDir}
279#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
280
281  logFile="analyse_RAWDATA_${sourceRadio}_${dateDAQ}.log"
282  $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
283  if [ ${simulationMode} = "On" ]; then
284      $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act rawOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debug 1 >& ${logFile}"
285      cat > ${logFile} <<EOF
286      Ok rawOnOff finished
287EOF
288  else
289      ${scriptpath}/Objs/analyse -act rawOnOff -inPath ${iojobpath}  -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir datacycle -numcycle ${firstCycle},${lastCycle} -debug 1 >& ${logFile}
290  fi
291  $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
292  rcstatus=`$GREP -i "Ok rawOnOff finished" ${logFile} | $WC -l`
293  if [ ${rcstatus} -eq 0 ]; then
294      $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
295      $ECHO "                      START logfile"
296      $CAT ${logFile}
297      $ECHO "                      END   logFile"
298#clean and leave
299      cd ${aboveOnOffDir}
300      $LS | $XARGS -i $RM -rf {}
301      continue
302  fi
303#Save into Irods
304  $ECHO "DEBUG (${scriptName}): START save output files"
305  $LS ${aboveOnOffDir}
306#use -f option for iput ONLY to force override
307  if [ ${simulationMode} = "On" ]; then
308      $ECHO "SIMUL: here we look for output files and put them in Irods"
309  else
310      $LS -lrth
311      $FIND . -name "dataRaw_*"      -print | $XARGS -i  iput -f -v -K {} ${i}
312      $FIND . -name "diffOnOffRaw_*" -print | $XARGS -i  iput -f -v -K {} ${i}
313  fi
314#save analysis logfile
315  $LS -l ${tmppublicpath} > /dev/null
316  $CP ${logFile} ${tmppublicpath}
317  $ECHO "DEBUG (${scriptName}): END save output files"
318#clean up to avoid scratch SIZE EXCEED LIMIT
319  cd ${aboveOnOffDir}
320  $LS | $XARGS -i $RM -rf {}
321
322done
323
324exit 0
Note: See TracBrowser for help on using the repository browser.