source: BAORadio/AmasNancay/gainmaker.sh @ 540

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

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

File size: 9.7 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
29#$ECHO "SHOULD BE RETUNED FOR THE inPath cmd line of analyse.cc (Sorry...): 21/7/11 by JEC"
30#exit 1
31
32#Process cmd line args: the -src option is mandatory (source name as Abell85)
33sourceRadio=
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      -sim) simulationMode=On;;
41        -h)
42            echo >&2 \
43            "usage: $0 -src souce [-sim]"
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
54
55if [ "<${sourceRadio}>" = "<>" ]; then
56    $ECHO "FATAL: You have forgotten to select the source option (-src)"
57    exit 1
58fi
59
60case ${sourceRadio} in
61    Abell85) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
62    Abell2440) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
63    Abell1205) $ECHO "INFO (${scriptName}): process ${sourceRadio}";;
64    *) ECHO "FATAL (${scriptName}): process ${sourceRadio} not yet foreseen"
65    exit 1;;
66esac
67
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=${TMPPUBLICPATH}
77$LS -l ${tmppublicpath} > /dev/null
78
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
103#JEC 1/10/11 Use generic baodaqstatus name
104#get the daq current irod status
105#tag=`${DATE} +%F`
106#OUT1=${publicpath}/baodaqstatus-${tag}.txt
107OUT1=${publicpath}/baodaqstatus-current.txt
108
109if [ ! -e ${OUT1}  -o ! -r ${OUT1} ]; then
110    $ECHO "FATAL (${scriptName}): ${OUT1} has a problem"
111    exit 1
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}
120#
121fi
122
123#
124#We forbib to perform the gain for all the sources in one shot!
125#
126#if [ ! "<${sourceRadio}>" = "All" ]; then
127$ECHO "You have selected sourceRadio = $sourceRadio"
128tableau=( `$GREP -i ${sourceRadio} ${OUT1} | $AWK '( NF==4 ) { print $2 }' ` )
129#else
130#    tableau=( `$CAT ${OUT1} | $AWK '( NF==4 ) { print $2 }'`  )
131#fi
132IFS='
133'
134tableau=( $( $PRINTF "%s\n" "${tableau[@]}" | $AWK 'x[$0]++ == 0' ) )
135IFS=$DefaultIFS
136
137
138for i in ${tableau[@]}
139  do
140  $ECHO "DEBUG (${scriptName}): irods root dir $i"
141  dateDAQ=`$ECHO ${i} | $AWK '{split($0,a,"/"); print a[6];}' | $AWK "{sub(/${srclower}/,\"\"); print}"`
142#JEC 1/10/11 Use generic baodaqstatus name
143#Look if the date has changed since beginning of the job!
144#  curtag=`${DATE} +%F`
145#  if [ "<${curtag}>" != "<${tag}>" ]; then
146#      tag=${curtag}
147#it is necessary to update the daq status as the day has changed during the processing... (batch can take a long time...)
148#      OUT1=${publicpath}/baodaqstatus-${curtag}.txt
149#      $ECHO "We should bring the DAQ status more up to date, this take 1 or 2sec..."
150#      $RM -f ${tmppublicpath}/statusdaq.finished
151#      ${scriptpath}/statusdaq.sh > ${OUT1}
152#      while [ ! -f "${tmppublicpath}/statusdaq.finished" ]; do
153#         sleep 30
154#      done
155#      $RM ${tmppublicpath}/statusdaq.finished
156#protect against remove/rewriting   
157#      $CHMOD -v 444 ${OUT1}
158#  fi
159#
160# look if the fits file of the gains already exists
161  alreadyDone=`ils ${i} 2>&1 | $GREP -i "gain_.*\.fits" | $WC -l`
162  if [ ${alreadyDone} -gt 0 ]; then
163      $ECHO "INFO (${scriptName}): Fits file for gains already exists"
164      continue
165  fi
166# Something should be done...
167#look if the files for the input to gain computations have been done
168  somethingTodo=1
169  input4gainOk1=`ils ${i}/Off 2>&1 | $GREP -i "^ERROR" | $WC -l` 
170  input4gainOk2=`ils ${i}/Off 2>&1 | $GREP -i "gaincycle"` 
171  if [ $input4gainOk1 -eq 0 -a "<${input4gainOk2}>" != "<>"  ]; then
172      gaincycle=`$ECHO ${input4gainOk2} | $AWK '($1 == "C-"){print $2}'| $XARGS -i  basename {}`
173      input4gainOk3=`ils ${i}/Off/${gaincycle} 2>&1 | $GREP -i "medfiltmtx" | $WC -l`
174      if [ $input4gainOk3 -gt 0 ]; then
175          somethingTodo=0
176      fi
177  fi
178  if [ $somethingTodo -eq 1 ]; then
179#
180      $ECHO ">>>>>>>>>>>>>>>> Do proc_script $sourceRadio $dateDAQ GAIN"
181      $RM -f ${tmppublicpath}/proc_script.finished
182      if [ ${simulationMode} = "On" ]; then
183          $ECHO "SIMUL: ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ GAIN"
184      else     
185          ${scriptpath}/proc_script.sh $sourceRadio $dateDAQ GAIN
186          while [ ! -e "${tmppublicpath}/proc_script.finished"  ]; do
187              $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/proc_script.finished"
188              date +%T
189              sleep 60
190          done
191      fi
192      $ECHO "INFO (${scriptName}): proc_script has finished"
193      $RM ${tmppublicpath}/proc_script.finished
194#Todo see if there is errors in the log file before copy
195      $ECHO ">>>>>>>>>>>>>>>> Done proc_script $sourceRadio $dateDAQ GAIN"
196######
197  fi
198######
199  $ECHO ">>>>>>>>>>>>> ANALYSIS Part of ${sourceRadio} ${dateDAQ}"
200#go to the .../source/date-sourcelowercase directory
201  $MKDIR -p ${topDir}/${dateDAQ}${srclower}
202  cd ${topDir}/${dateDAQ}${srclower}
203  aboveOnOffDir=`pwd`
204  $ECHO "DEBUG (${scriptName}): we are in directory: <`pwd`>"
205# So the input files are there, so the fits file can be determined
206  mode="Off"
207  spectraGenDirName=`$ECHO ${gaincycle} | $AWK '{match($0,"[a-zA-Z]+",arr); print arr[0]}'`
208  cycleNumber=`$ECHO ${gaincycle} | $AWK '{match($0,"[0-9]+",arr); print arr[0]}'`
209#care: the gaincycle variable contains the cycle number
210  $ECHO "DEBUG (${scriptName}): START download the fits files"
211  inFileDirectory="./${mode}"
212  $MKDIR -p ${inFileDirectory}
213  irodsDownDir="${i}/${mode}/${gaincycle}"
214  OUT=./getInFits.$$
215  iget -r -f -K ${irodsDownDir} ${inFileDirectory} > ${OUT} 2>&1
216  $LS ${inFileDirectory}/${gaincycle}
217  igetStatus=`$GREP "^ERROR" ${OUT}`
218  if [ "<$igetStatus>" != "<>" ]; then
219      $ECHO "FATAL (${scriptName}): error while iget fits files:"
220      $ECHO $igetStatus
221      continue
222  fi
223  $RM -f ${OUT}
224  $ECHO "DEBUG (${scriptName}): END download the fits files"
225#Get sca files
226  $ECHO "DEBUG (${scriptName}): START call getscafiles.sh ${sourceRadio} ${dateDAQ}"
227  OUT1=./getScaStatus.$$
228  $RM -f ${tmppublicpath}/getscafiles.finished
229  ${scriptpath}/getscafiles.sh ${sourceRadio} ${dateDAQ} > ${OUT1} 2>&1
230  while [ ! -f "${tmppublicpath}/getscafiles.finished" ]; do
231      $ECHO "INFO (${scriptName}): waiting for ${tmppublicpath}/getscafiles.finished"
232      date +%T
233      sleep 30
234  done
235  $RM ${tmppublicpath}/getscafiles.finished
236  $ECHO "DEBUG (${scriptName}): END"
237#
238  getScaStatus=`$GREP "^\(FATAL\|ERROR\)" ${OUT1}`
239#       
240  if [ "<${getScaStatus}>" != "<>" ]; then
241      $ECHO "FATAL (${scriptName}): error to get sca file for $sourceRadio $dateDAQ"
242      $ECHO "DEBUG (${scriptName}): START with cat ${OUT1}"
243      $CAT ${OUT1}
244      $ECHO "DEBUG (${scriptName}): END"
245      continue
246  fi
247  $RM -f ${OUT1}
248  $ECHO "DEBUG (${scriptName}): where am i `pwd`"
249  $LS -lrt
250 
251  scaFile=`$FIND . -name "sca*.sum.trans" | $XARGS -i basename {}`
252#       
253#the default spectra name is medfiltmtx, otherwise use -specname <a_string>
254  logFile="analyse_GAIN_${sourceRadio}_${dateDAQ}.log"
255  $ECHO "DEBUG (${scriptName}): execute the analysis... should not take long. `date`"
256  if [ ${simulationMode} = "On" ]; then
257      $ECHO "SIMUL: ${scriptpath}/Objs/analyse -act gain  -inPath ${iojobpath} -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir ${spectraGenDirName} -mode ${mode} -numcycle ${cycleNumber},${cycleNumber} -debug 1 >& ${logFile}"
258      cat > ${logFile} <<EOF
259      Ok gain finished
260EOF
261
262  else
263      ${scriptpath}/Objs/analyse -act gain  -inPath ${iojobpath} -source  ${sourceRadio} -date ${dateDAQ} -sca ${scaFile} -specdir ${spectraGenDirName} -mode ${mode} -numcycle ${cycleNumber},${cycleNumber} -debuglev 1 >& ${logFile}
264  fi
265#
266  $ECHO "DEBUG (${scriptName}): analysis finished `date`. Look for errors before saving to Irods"
267  rcstatus=`$GREP -i "OK gain finished" ${logFile} | $WC -l`
268  if [ ${rcstatus} -eq 0 ]; then
269      $ECHO "INFO (${scriptName}): analysis problem for ${sourceRadio} ${dateDAQ}"
270      $ECHO "                      START logfile"
271      $CAT ${logFile}
272      $ECHO "                      END   logFile"
273#clean and leave
274      cd ${aboveOnOffDir}
275      $RM -rf ./*
276      continue
277  fi
278#Save into Irods
279  $ECHO "DEBUG (${scriptName}): START save output files"
280  $LS ${aboveOnOffDir}
281#use -f option of iput ONLY to force override
282  if [ ${simulationMode} = "On" ]; then
283      $ECHO "SIMUL: here we look for gain file and put them in Irods"
284  else
285      $FIND . -name "gain_*" -print | $XARGS -i  iput  -v -K {} ${i}
286  fi
287#save analysis logfile
288  $LS -l ${tmppublicpath} > /dev/null
289  $CP ${logFile} ${tmppublicpath}
290  $ECHO "DEBUG (${scriptName}): END save output files"
291#clean
292  cd ${aboveOnOffDir}
293  $RM -rf ./*
294done
295
296exit 0
Note: See TracBrowser for help on using the repository browser.