source: trunk/DataCheck/FillAuxData.sh@ 12948

Last change on this file since 12948 was 12935, checked in by Daniela Dorner, 14 years ago
implemented function for getting statistics; usage of sendquery
  • Property svn:executable set to *
File size: 14.6 KB
Line 
1#!/bin/bash
2
3# missing:
4# vferify of files
5# only treat file if it is there
6
7# mjd of 1970-01-01
8# needed in this script as for 1 day the mjd in the aux files are inconsistent
9# mjdref=40587
10
11# known:
12# 2011/11/22 MJDREF in DRIVE empty, Time > 55000
13# 2011/11/23 MJDREF in DRIVE not empty, Time > 55000
14# 2011/11/24 MJDREF in DRIVE not empty, Time > 15000
15# raw files
16# 2011/11/21 no MJDREF
17# 2011/11/22 MJDREF
18# further things: https://www.fact-project.org/logbook/showthread.php?tid=67
19
20# trigger rate has as first value -1, but with using the median it should be fine
21
22errlog=/home/fact/SW.automatic.processing/DataCheck/log/error.log
23
24function getfitsstatistics()
25{
26 # $1 filename
27 # $2 colname
28 # $3 tstart
29 # $4 tstop
30 good=
31 min=
32 mean=
33 median=
34 max=
35 tmpfile=`dirname $0`/`basename $1`.tmp
36 echo "ftcopy $1'[col Time]' - | ftstat - | grep 'mean' | grep -E -o [0-9]+[.] | sed -e 's/[.]//g'" >> $errlog
37 timefromfile=`ftcopy $1'[col Time]' - 2>>$errlog | ftstat - 2>>$errlog | grep 'mean' | grep -E -o [0-9]+[.] | sed -e 's/[.]//g'`
38 if [ $timefromfile -gt 30000 ]
39 then
40 echo "ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -" >> $errlog
41 #ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -
42 ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - 2>>$errlog | ftcopy -'[col '${2}']' - 2>>$errlog | ftstat - 2>>$errlog > $tmpfile
43 else
44 echo "ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -" >> $errlog
45 #ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -
46 ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - 2>>$errlog | ftcopy -'[col '${2}']' - 2>>$errlog | ftstat - 2>>$errlog > $tmpfile
47 fi
48 good=`cat $tmpfile | grep 'good' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
49 min=`cat $tmpfile | grep 'min' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
50 mean=`cat $tmpfile | grep 'mean' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
51 median=`cat $tmpfile | grep 'median' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
52 max=`cat $tmpfile | grep 'max' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
53 if [ "$good" = "" ]
54 then
55 printprocesslog "WARN couldn't get statistics from file $1 for run $date $file"
56 rm $tmpfile
57 finish
58 fi
59 #echo "good: "$good
60 #echo "min: "$min
61 #echo "max: "$max
62 #echo "mean: "$max
63 #echo "median: "$max
64 rm $tmpfile
65}
66
67# option
68doupdate="yes" # update all entries (needed when new fields have been added)
69doupdate="no" # fill only entries which are not yet existing (default)
70
71source `dirname $0`/Sourcefile.sh
72printprocesslog "INFO starting $0 with option doupdate="$doupdate
73
74# setup to use ftools
75source $HEADAS/headas-init.sh
76
77pwfile=`dirname $0`/.pw
78password=`cat $pwfile 2>/dev/null`
79if [ "$password" == "" ]
80then
81 echo "please insert password in $pwfile"
82 printprocesslog "ERROR password for DB access in $pwfile missing"
83 finish
84fi
85
86# check if software is available
87if ! ls $factpath/fitsdump >/dev/null 2>&1
88then
89 printprocesslog "ERROR "$factpath"/fitsdump is not available."
90 finish
91fi
92
93
94# get last 2 nights
95dates=( `date +%Y/%m/%d --date="-12hour"` `date +%Y/%m/%d --date="-36hour"` `date +%Y/%m/%d --date="-60hour"` )
96#dates=( `find $ziprawdata -mindepth 3 -type d | sort | sed "s/\${ziprawdata_for_sed}//g" | sed -e 's/^\///'` ) #all available dates in /loc_data/zipraw
97#echo ${dates[@]}
98dates=( "2012/02/21" "2012/02/20" "2012/02/19" "2012/02/18" )
99printprocesslog "INFO processing the following night(s): "${dates[@]}
100
101# do filling of aux data
102for date in ${dates[@]}
103do
104 auxdir=$auxdata/$date
105 runnumber=`echo $date | sed -e 's/\///g'`
106
107 # check if aux files are available from that night
108 if ! [ -d $auxdir ]
109 then
110 printprocesslog "INFO no data available in "$auxdir
111 continue
112 else
113 printprocesslog "INFO processing files in "$auxdir
114 fi
115
116 # get file numbers from DB
117 # but only for not-corrupted files
118 query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
119 printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
120 filenumbers=( `sendquery $query` )
121 if [ ${#filenumbers} -eq 0 ]
122 then
123 printprocesslog "INFO No files found in the DB for night "$date
124 continue
125 fi
126
127 # get daily fits files
128 trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
129 if ! [ -e $trackingfile ]
130 then
131 printprocesslog "WARN "$trackingfile" not found."
132 else
133 tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
134 if [ $tracknumerrors -gt 0 ]
135 then
136 printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
137 fi
138 fi
139
140 triggerratefile=$auxdir/$runnumber.FTM_CONTROL_TRIGGER_RATES.fits
141 if ! [ -e $triggerratefile ]
142 then
143 printprocesslog "WARN "$triggerratefile" not found."
144 else
145 trignumerrors=`fverify $triggerratefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
146 if [ $trignumerrors -gt 0 ]
147 then
148 printprocesslog "WARN for $triggerratefile fverify returned "$trignumerrors" error(s)."
149 fi
150 fi
151
152 thresholdfile=$auxdir/$runnumber.FTM_CONTROL_STATIC_DATA.fits
153 if ! [ -e $thresholdfile ]
154 then
155 printprocesslog "WARN "$thresholdfile" not found."
156 else
157 treshnumerrors=`fverify $thresholdfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
158 if [ $treshnumerrors -gt 0 ]
159 then
160 printprocesslog "WARN for $thresholdfile fverify returned "$treshnumerrors" error(s)."
161 fi
162 fi
163
164 biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
165 if ! [ -e $biasvoltagefile ]
166 then
167 printprocesslog "WARN "$biasvoltagefile" not found."
168 else
169 biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
170 if [ $biasnumerrors -gt 0 ]
171 then
172 printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
173 fi
174 fi
175
176 # fill auxiliary information for files
177 for filenum in ${filenumbers[@]}
178 do
179 printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
180 # get information from rawfile
181 rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.gz
182 if ! [ -e $rawfile ]
183 then
184 printprocesslog "ERROR: "$rawfile" not found."
185 continue
186 fi
187 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
188 #if [ "$checkfitsfile" == "" ]
189 #then
190 # numfitserrors=1
191 # printprocesslog "WARN: "$rawfile" probably corrupted."
192 # continue
193 #fi
194 runtype=`$factpath/fitsdump -h -t Events $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
195 mjdrefraw=`$factpath/fitsdump -h -t Events $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
196 tstarti=`$factpath/fitsdump -h -t Events $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
197 tstartf=`$factpath/fitsdump -h -t Events $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
198 tstopi=`$factpath/fitsdump -h -t Events $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
199 tstopf=`$factpath/fitsdump -h -t Events $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
200 if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
201 then
202 printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
203 continue
204 fi
205 # assuming that at least TSTARTI and TSTOPI are consistent
206 #echo $rawfile
207 #echo $tstarti
208 #echo $tstopi
209 #echo $tstartf
210 #echo $tstopf
211 if [ $tstarti -gt 30000 ]
212 then
213 tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
214 tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
215 #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
216 tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
217 else
218 tstart=`echo " $tstarti + $tstartf " | bc -l`
219 tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
220 #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
221 tstop=`echo " $tstopi + $tstopf " | bc -l`
222 fi
223 #echo $tstart
224 #echo $tstop
225 #if [ $runnumber -eq 20111123 ]
226 #then
227 # # add mjdref for days were aux files were inconsistent
228 # tstart=`echo " $tstart + $mjdref " | bc -l`
229 # tstart2=`echo " $tstart2 + $mjdref " | bc -l`
230 # tstop=`echo " $tstop + $mjdref " | bc -l`
231 #fi
232
233 # build query to update runinfo in DB
234 query="UPDATE RunInfo SET "
235
236 # get information from tracking
237 if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
238 then
239 # RA
240 getfitsstatistics $trackingfile "Ra" $tstart $tstop
241 if [ "$min" == "$max" ] && [ $good -gt 0 ]
242 then
243 query=$query" fRightAscension="$mean
244 else
245 query=$query" fRightAscension=NULL"
246 if [ $good -gt 0 ]
247 then
248 printprocesslog "WARN for $rawfile RA changes within run (min: "$min", max: "$max")."
249 fi
250 fi
251 # Declination
252 getfitsstatistics $trackingfile "Dec" $tstart $tstop
253 if [ "$decmin" == "$decmax" ] && [ $good -gt 0 ]
254 then
255 query=$query", fDeclination="$mean
256 else
257 query=$query", fDeclination=NULL"
258 if [ $good -gt 0 ]
259 then
260 printprocesslog "WARN for $rawfile declination changes within run (min: "$min", max: "$max")."
261 fi
262 fi
263 # Zd
264 getfitsstatistics $trackingfile "Zd" $tstart $tstop
265 if [ $good -gt 0 ]
266 then
267 query=$query", fZenithDistanceMin="$min
268 query=$query", fZenithDistanceMean="$mean
269 query=$query", fZenithDistanceMax="$max
270 else
271 query=$query", fZenithDistanceMin=NULL"
272 query=$query", fZenithDistanceMean=NULL"
273 query=$query", fZenithDistanceMax=NULL"
274 fi
275 # Az
276 getfitsstatistics $trackingfile "Az" $tstart $tstop
277 if [ $good -gt 0 ]
278 then
279 query=$query", fAzimuthMin="$min
280 query=$query", fAzimuthMean="$mean
281 query=$query", fAzimuthMax="$max
282 else
283 query=$query", fAzimuthMin=NULL"
284 query=$query", fAzimuthMean=NULL"
285 query=$query", fAzimuthMax=NULL"
286 fi
287 else
288 query=$query" fRightAscension=NULL"
289 query=$query", fDeclination=NULL"
290 query=$query", fZenithDistanceMin=NULL"
291 query=$query", fZenithDistanceMean=NULL"
292 query=$query", fZenithDistanceMax=NULL"
293 query=$query", fAzimuthMin=NULL"
294 query=$query", fAzimuthMean=NULL"
295 query=$query", fAzimuthMax=NULL"
296 fi
297
298 # get information from trigger
299 if [ -e $triggerratefile ] && [ $trignumerrors -eq 0 ]
300 then
301 #echo " $triggerratefile TriggerRate $tstart $tstop"
302 getfitsstatistics $triggerratefile "TriggerRate" $tstart $tstop
303# if [ "$mjdreftrig" == "" ]
304# then
305# ratemin=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'min' | grep -E -o '[0-9]+[.]?[0-9]*'`
306# ratemax=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'max' | grep -E -o '[0-9]+[.]?[0-9]*'`
307# ratemean=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'mean' | grep -E -o '[0-9]+[.]?[0-9]*'`
308# ratemedian=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'median' | grep -E -o '[0-9]+[.]?[0-9]*'`
309# else
310# ratemin=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'min' | grep -E -o '[0-9]+[.]?[0-9]*'`
311# ratemax=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'max' | grep -E -o '[0-9]+[.]?[0-9]*'`
312# ratemean=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'mean' | grep -E -o '[0-9]+[.]?[0-9]*'`
313# ratemedian=`ftcopy $triggerratefile'[Time> '${tstart}' && Time< '${tstop}' && TriggerRate!=-1][col TriggerRate;Time]' - | ftcopy -'[col TriggerRate]' - | ftstat - | grep 'median' | grep -E -o '[0-9]+[.]?[0-9]*'`
314# fi
315 if [ $good -gt 0 ]
316 then
317 query=$query", fTriggerRateMedian="$median
318 else
319 query=$query", fTriggerRateMedian=NULL"
320 fi
321 else
322 query=$query", fTriggerRateMedian=NULL"
323 fi
324
325 # get information from thresholds
326 if [ -e $thresholdfile ] && [ $treshnumerrors -eq 0 ]
327 then
328 getfitsstatistics $thresholdfile "PatchThresh" $tstart $tstop
329 if [ $good -eq 0 ]
330 then
331 getfitsstatistics $thresholdfile "PatchThresh" $tstart2 $tstop
332 fi
333 if [ $good -gt 0 ]
334 then
335 query=$query", fThresholdMedian="$median
336 else
337 query=$query", fThresholdMedian=NULL"
338 fi
339 else
340 query=$query", fThresholdMedian=NULL"
341 fi
342
343 # get information from bias: U
344 if [ -e $biasvoltagefile ] && [ $biasnumerrors -eq 0 ]
345 then
346 getfitsstatistics $biasvoltagefile "U" $tstart $tstop
347 if [ $good -eq 0 ]
348 then
349 getfitsstatistics $biasvoltagefile "U" $tstart2 $tstop
350 fi
351 if [ $good -gt 0 ]
352 then
353 query=$query", fBiasVoltageMedian="$median
354 else
355 query=$query", fBiasVoltageMedian=NULL"
356 fi
357 else
358 query=$query", fBiasVoltageMedian=NULL"
359 fi
360
361 # add where condition
362 query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
363
364 # send query to DB
365 sendquery >/dev/null
366 done
367done
368
369finish
370
371
Note: See TracBrowser for help on using the repository browser.