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

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

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