| 1 | #!/bin/bash
|
|---|
| 2 |
|
|---|
| 3 | # option
|
|---|
| 4 | doupdate="yes" # update all entries (needed when new fields have been added)
|
|---|
| 5 | doupdate="no" # fill only entries which are not yet existing (default)
|
|---|
| 6 |
|
|---|
| 7 | source `dirname $0`/../Sourcefile.sh
|
|---|
| 8 | printprocesslog "INFO starting $0 with option doupdate="$doupdate
|
|---|
| 9 |
|
|---|
| 10 | logfile=$runlogpath"/FillCurrents-"$datetime".log"
|
|---|
| 11 | date >> $logfile
|
|---|
| 12 |
|
|---|
| 13 | # setup to use ftools
|
|---|
| 14 | source $HEADAS/headas-init.sh
|
|---|
| 15 |
|
|---|
| 16 | # check if software is available
|
|---|
| 17 | if ! ls $factpath/fitsdump >/dev/null 2>&1
|
|---|
| 18 | then
|
|---|
| 19 | printprocesslog "ERROR "$factpath"/fitsdump is not available."
|
|---|
| 20 | finish
|
|---|
| 21 | fi
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | # get last 3, 6 or 9 nights
|
|---|
| 25 | dates=( `date +%Y/%m/%d --date="-12hour"` `date +%Y/%m/%d --date="-36hour"` `date +%Y/%m/%d --date="-60hour"` \
|
|---|
| 26 | # `date +%Y/%m/%d --date="-84hour"` `date +%Y/%m/%d --date="-108hour"` `date +%Y/%m/%d --date="-132hour"` \
|
|---|
| 27 | # `date +%Y/%m/%d --date="-156hour"` `date +%Y/%m/%d --date="-180hour"` `date +%Y/%m/%d --date="-204hour"` \
|
|---|
| 28 | )
|
|---|
| 29 | #dates=( `find -L $ziprawdata -mindepth 3 -type d | sort -r | sed "s/\${ziprawdata_for_sed}//g" | sed -e 's/^\///'` ) #all available dates in /loc_data/zipraw
|
|---|
| 30 |
|
|---|
| 31 | #dates=( "2013/01/31" )
|
|---|
| 32 |
|
|---|
| 33 | printprocesslog "INFO processing the following night(s): "${dates[@]}
|
|---|
| 34 | echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1
|
|---|
| 35 |
|
|---|
| 36 | echo ${dates[@]}
|
|---|
| 37 |
|
|---|
| 38 | cd $mars
|
|---|
| 39 |
|
|---|
| 40 | # do filling of aux data
|
|---|
| 41 | for date in ${dates[@]}
|
|---|
| 42 | do
|
|---|
| 43 | auxdir=$auxdata/$date
|
|---|
| 44 | runnumber=`echo $date | sed -e 's/\///g'`
|
|---|
| 45 |
|
|---|
| 46 | # if [ $runnumber -lt 20130301 ]
|
|---|
| 47 | # then
|
|---|
| 48 | # continue
|
|---|
| 49 | # fi
|
|---|
| 50 |
|
|---|
| 51 | # check if aux files are available from that night
|
|---|
| 52 | if ! [ -d $auxdir ]
|
|---|
| 53 | then
|
|---|
| 54 | printprocesslog "INFO no data available in "$auxdir
|
|---|
| 55 | continue
|
|---|
| 56 | else
|
|---|
| 57 | printprocesslog "INFO processing files in "$auxdir
|
|---|
| 58 | fi
|
|---|
| 59 |
|
|---|
| 60 | biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
|
|---|
| 61 | if ! [ -e $biasvoltagefile ]
|
|---|
| 62 | then
|
|---|
| 63 | printprocesslog "WARN "$biasvoltagefile" not found."
|
|---|
| 64 | continue
|
|---|
| 65 | else
|
|---|
| 66 | biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
|
|---|
| 67 | if [ $biasnumerrors -gt 0 ]
|
|---|
| 68 | then
|
|---|
| 69 | printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
|
|---|
| 70 | fi
|
|---|
| 71 | fi
|
|---|
| 72 |
|
|---|
| 73 | biascurrentfile=$auxdir/$runnumber.BIAS_CONTROL_CURRENT.fits
|
|---|
| 74 | if ! [ -e $biascurrentfile ]
|
|---|
| 75 | then
|
|---|
| 76 | printprocesslog "WARN "$biascurrentfile" not found."
|
|---|
| 77 | continue
|
|---|
| 78 | else
|
|---|
| 79 | biascurrnumerrors=`fverify $biascurrentfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
|
|---|
| 80 | if [ $biascurrnumerrors -gt 0 ]
|
|---|
| 81 | then
|
|---|
| 82 | printprocesslog "WARN for $biascurrentfile fverify returned "$biascurrnumerrors" error(s)."
|
|---|
| 83 | fi
|
|---|
| 84 | fi
|
|---|
| 85 |
|
|---|
| 86 | feedbackcalfile=$auxdir/$runnumber.FEEDBACK_CALIBRATION.fits
|
|---|
| 87 | if ! [ -e $feedbackcalfile ]
|
|---|
| 88 | then
|
|---|
| 89 | printprocesslog "WARN "$feedbackcalfile" not found."
|
|---|
| 90 | continue
|
|---|
| 91 | else
|
|---|
| 92 | feedbacknumerrors=`fverify $feedbackcalfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
|
|---|
| 93 | if [ $feedbacknumerrors -gt 0 ]
|
|---|
| 94 | then
|
|---|
| 95 | printprocesslog "WARN for $feedbackcalfile fverify returned "$feedbacknumerrors" error(s)."
|
|---|
| 96 | fi
|
|---|
| 97 | fi
|
|---|
| 98 |
|
|---|
| 99 | echo "run calibrate.C for night "$runnumber
|
|---|
| 100 | root -q -b -l fact/calibrate.C\($runnumber\)
|
|---|
| 101 |
|
|---|
| 102 | calcurrentsfile=$auxdir/$runnumber.CALIBRATED_CURRENTS.fits
|
|---|
| 103 | calcurrentsfile=/scratch_nfs/calibrated_currents/$runnumber.CALIBRATED_CURRENTS.fits
|
|---|
| 104 | if ! [ -e $calcurrentsfile ]
|
|---|
| 105 | then
|
|---|
| 106 | printprocesslog "WARN "$calcurrentsfile" not found."
|
|---|
| 107 | continue
|
|---|
| 108 | else
|
|---|
| 109 | calnumerrors=`fverify $calcurrentsfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
|
|---|
| 110 | if [ $calnumerrors -gt 0 ]
|
|---|
| 111 | then
|
|---|
| 112 | printprocesslog "WARN for $calcurrentsfile fverify returned "$calnumerrors" error(s)."
|
|---|
| 113 | fi
|
|---|
| 114 | fi
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | # get file numbers from DB
|
|---|
| 118 | # but only for not-corrupted files
|
|---|
| 119 | query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
|
|---|
| 120 | if [ "$doupdate" = "no" ]
|
|---|
| 121 | then
|
|---|
| 122 | query=$query" AND ISNULL(fCurrentsMedMean) "
|
|---|
| 123 | fi
|
|---|
| 124 |
|
|---|
| 125 | printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
|
|---|
| 126 | filenumbers=( `sendquery $query` )
|
|---|
| 127 | if [ ${#filenumbers} -eq 0 ]
|
|---|
| 128 | then
|
|---|
| 129 | printprocesslog "INFO No files found in the DB for night "$date
|
|---|
| 130 | continue
|
|---|
| 131 | fi
|
|---|
| 132 |
|
|---|
| 133 | # fill auxiliary information for files
|
|---|
| 134 | for filenum in ${filenumbers[@]}
|
|---|
| 135 | do
|
|---|
| 136 | printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
|
|---|
| 137 | echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
|
|---|
| 138 | # get information from rawfile
|
|---|
| 139 | rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.gz
|
|---|
| 140 | if ! [ -e $rawfile ]
|
|---|
| 141 | then
|
|---|
| 142 | printprocesslog "ERROR: "$rawfile" not found."
|
|---|
| 143 | continue
|
|---|
| 144 | fi
|
|---|
| 145 | runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
|
|---|
| 146 | mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
|
|---|
| 147 | tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
|
|---|
| 148 | tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
|
|---|
| 149 | tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
|
|---|
| 150 | tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
|
|---|
| 151 | if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
|
|---|
| 152 | then
|
|---|
| 153 | printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
|
|---|
| 154 | continue
|
|---|
| 155 | fi
|
|---|
| 156 | # assuming that at least TSTARTI and TSTOPI are consistent
|
|---|
| 157 | #echo $rawfile
|
|---|
| 158 | #echo $tstarti
|
|---|
| 159 | #echo $tstopi
|
|---|
| 160 | #echo $tstartf
|
|---|
| 161 | #echo $tstopf
|
|---|
| 162 | if [ $tstarti -gt 30000 ]
|
|---|
| 163 | then
|
|---|
| 164 | tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
|
|---|
| 165 | tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
|
|---|
| 166 | #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
|
|---|
| 167 | tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
|
|---|
| 168 | else
|
|---|
| 169 | tstart=`echo " $tstarti + $tstartf " | bc -l`
|
|---|
| 170 | tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
|
|---|
| 171 | #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
|
|---|
| 172 | tstop=`echo " $tstopi + $tstopf " | bc -l`
|
|---|
| 173 | fi
|
|---|
| 174 |
|
|---|
| 175 | # build query to update runinfo in DB
|
|---|
| 176 | query="UPDATE RunInfo SET "
|
|---|
| 177 |
|
|---|
| 178 | # get information from fsc: T[31]
|
|---|
| 179 | if [ -e $calcurrentsfile ] && [ $calnumerrors -eq 0 ]
|
|---|
| 180 | then
|
|---|
| 181 | #root -q -b -l fact/curavg.C\("\"$calcurrentsfile\""\,$tstart\,$tstop\) # | grep "result" | grep -E -o '[0-9]+[.]?[0-9]*'
|
|---|
| 182 | currents=( `root -q -b -l fact/curavg.C\("\"$calcurrentsfile\""\,$tstart\,$tstop\) | grep "result" | grep -E -o '[0-9]+[.]?[0-9]*'` )
|
|---|
| 183 | if [ "${currents[0]}" == "" ]
|
|---|
| 184 | then
|
|---|
| 185 | query=$query"fCurrentsMedMean=NULL"
|
|---|
| 186 | else
|
|---|
| 187 | query=$query"fCurrentsMedMean="${currents[0]}
|
|---|
| 188 | fi
|
|---|
| 189 | if [ "${currents[1]}" == "" ]
|
|---|
| 190 | then
|
|---|
| 191 | query=$query", fCurrentsMedRms=NULL"
|
|---|
| 192 | else
|
|---|
| 193 | query=$query", fCurrentsMedRms="${currents[1]}
|
|---|
| 194 | fi
|
|---|
| 195 | if [ "${currents[2]}" == "" ]
|
|---|
| 196 | then
|
|---|
| 197 | query=$query", fCurrentsDevMean=NULL"
|
|---|
| 198 | else
|
|---|
| 199 | query=$query", fCurrentsDevMean="${currents[2]}
|
|---|
| 200 | fi
|
|---|
| 201 | if [ "${currents[3]}" == "" ]
|
|---|
| 202 | then
|
|---|
| 203 | query=$query", fCurrentsDevRms=NULL"
|
|---|
| 204 | else
|
|---|
| 205 | query=$query", fCurrentsDevRms="${currents[3]}
|
|---|
| 206 | fi
|
|---|
| 207 | if [ "${currents[4]}" == "" ]
|
|---|
| 208 | then
|
|---|
| 209 | query=$query", fCurrentsMedMeanBeg=NULL"
|
|---|
| 210 | else
|
|---|
| 211 | query=$query", fCurrentsMedMeanBeg="${currents[4]}
|
|---|
| 212 | fi
|
|---|
| 213 | if [ "${currents[5]}" == "" ]
|
|---|
| 214 | then
|
|---|
| 215 | query=$query", fCurrentsMedMeanEnd=NULL"
|
|---|
| 216 | else
|
|---|
| 217 | query=$query", fCurrentsMedMeanEnd="${currents[5]}
|
|---|
| 218 | fi
|
|---|
| 219 | else
|
|---|
| 220 | query=$query" fCurrentsMedMean=NULL"
|
|---|
| 221 | query=$query", fCurrentsMedRms=NULL"
|
|---|
| 222 | query=$query", fCurrentsDevMean=NULL"
|
|---|
| 223 | query=$query", fCurrentsDevRms=NULL"
|
|---|
| 224 | query=$query", fCurrentsMedMeanBeg=NULL"
|
|---|
| 225 | query=$query", fCurrentsMedMeanEnd=NULL"
|
|---|
| 226 | fi
|
|---|
| 227 |
|
|---|
| 228 | # add where condition
|
|---|
| 229 | query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
|
|---|
| 230 |
|
|---|
| 231 | echo $query
|
|---|
| 232 | # send query to DB
|
|---|
| 233 | sendquery >/dev/null
|
|---|
| 234 | done
|
|---|
| 235 | done
|
|---|
| 236 |
|
|---|
| 237 | finish
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|