source: BAORadio/AmasNancay/trunk/anacalibmaker.sh @ 584

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

correct bug: threshold to detect 1st level of DAB (jec)

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