source: trunk/DataCheck/Processing/FillAuxData.sh@ 17597

Last change on this file since 17597 was 17597, checked in by Daniela Dorner, 11 years ago
give warning about missing files only when runinfo in DB available
  • Property svn:executable set to *
File size: 17.9 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
22# option whether to fill all row or only those where information is missing
23# $doupdate might be given as environment variable
24if [ "$doupdate" = "" ]
25then
26 doupdate="yes" # update all entries (needed when new fields have been added)
27 doupdate="no" # fill only entries which are not yet existing (default)
28fi
29
30source `dirname $0`/../Sourcefile.sh
31printprocesslog "INFO starting $0 with option doupdate="$doupdate
32
33logfile=$runlogpath"/FillAuxData-"$datetime".log"
34date >> $logfile
35
36function getfitsstatistics()
37{
38 # $1 filename
39 # $2 colname
40 # $3 tstart
41 # $4 tstop
42 good=
43 min=
44 mean=
45 median=
46 max=
47 tmpfile=`dirname $0`/`basename $1`.tmp
48 echo "ftcopy $1'[col Time]' - | ftstat - | grep 'mean' | grep -E -o [0-9]+[.] | sed -e 's/[.]//g'" >> $logfile
49 timefromfile=`ftcopy $1'[col Time]' - 2>>$logfile | ftstat - 2>>$logfile | grep 'mean' | grep -E -o [0-9]+[.] | sed -e 's/[.]//g'`
50 echo "here-"$timefromfile"-" >> $logfile
51 if [ "$timefromfile" == "" ]
52 then
53 printprocesslog "WARN couldn't get time from file "$1
54 echo "WARN couldn't get time from file "$1
55 continue
56 fi
57 if [ $timefromfile -gt 30000 ]
58 then
59 echo "ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -" >> $logfile
60 #ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -
61 ftcopy $1'[Time - 40587> '${3}' && Time - 40587< '${4}' ][col '${2}';Time]' - 2>>$logfile | ftcopy -'[col '${2}']' - 2>>$logfile | ftstat - 2>>$logfile > $tmpfile
62 else
63 echo "ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -" >> $logfile
64 #ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - | ftcopy -'[col '${2}']' - | ftstat -
65 ftcopy $1'[Time> '${3}' && Time< '${4}' ][col '${2}';Time]' - 2>>$logfile | ftcopy -'[col '${2}']' - 2>>$logfile | ftstat - 2>>$logfile > $tmpfile
66 fi
67 good=`cat $tmpfile | grep 'good' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
68 min=`cat $tmpfile | grep 'min' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
69 mean=`cat $tmpfile | grep 'mean' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
70 median=`cat $tmpfile | grep 'median' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
71 max=`cat $tmpfile | grep 'max' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
72 sigma=`cat $tmpfile | grep 'sigma' | grep -E -o '[-]?[0-9]+[.]?[0-9]*'`
73 if [ "$good" = "" ]
74 then
75 printprocesslog "WARN couldn't get statistics from file $1 for run $date $file"
76 rm $tmpfile
77 continue
78 #finish
79 fi
80 #echo "good: "$good
81 #echo "min: "$min
82 #echo "max: "$max
83 #echo "mean: "$max
84 #echo "median: "$max
85 rm $tmpfile
86}
87
88# setup to use ftools
89source $HEADAS/headas-init.sh
90
91# check if software is available
92if ! ls $factpath/fitsdump >/dev/null 2>&1
93then
94 printprocesslog "ERROR "$factpath"/fitsdump is not available."
95 finish
96fi
97
98# get dates
99if [ "$certaindate" != "" ]
100then
101 getdates $certaindate
102else
103 # get all night
104 #getdates "all"
105 # get last 3 nights if hour between 7 and 19h, else only current night
106 getdates 3 7 19
107fi
108
109printprocesslog "INFO processing the following night(s): "${dates[@]}
110echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1
111
112# do filling of aux data
113for date in ${dates[@]}
114do
115 auxdir=$auxdata/$date
116 runnumber=`echo $date | sed -e 's/\///g'`
117
118 # get file numbers from DB
119 # but only for not-corrupted files
120 query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
121 # only runs which are not yet filled
122 if [ "$doupdate" = "no" ]
123 then
124 query=$query" AND ISNULL(fRightAscension) "
125 fi
126 printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
127 filenumbers=( `sendquery $query` )
128 # proceed only if there are files available
129 if [ ${#filenumbers} -eq 0 ]
130 then
131 printprocesslog "INFO No files found in the DB for night "$date
132 continue
133 fi
134
135 # check if aux files are available from that night
136 if ! [ -d $auxdir ]
137 then
138 printprocesslog "INFO no data available in "$auxdir
139 continue
140 else
141 printprocesslog "INFO processing files in "$auxdir
142 fi
143
144 # get daily fits files
145 trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
146 if ! [ -e $trackingfile ]
147 then
148 printprocesslog "WARN "$trackingfile" not found."
149 else
150 tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
151 if [ $tracknumerrors -gt 0 ]
152 then
153 printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
154 fi
155 fi
156
157 sourceposfile=$auxdir/$runnumber.DRIVE_CONTROL_SOURCE_POSITION.fits
158 if ! [ -e $sourceposfile ]
159 then
160 printprocesslog "WARN "$sourceposfile" not found."
161 else
162 sourceposnumerrors=`fverify $sourceposfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
163 if [ $sourceposnumerrors -gt 0 ]
164 then
165 printprocesslog "WARN for $sourceposfile fverify returned "$sourceposnumerrors" error(s)."
166 fi
167 sourceposfiletstarti=`$factpath/fitsdump -h $sourceposfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
168 sourceposfiletstartf=`$factpath/fitsdump -h $sourceposfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
169 if [ $sourceposfiletstarti -gt 30000 ]
170 then
171 sourceposfiletstart=`echo " $sourceposfiletstarti + $sourceposfiletstartf - 40587 " | bc -l`
172 else
173 sourceposfiletstart=`echo " $sourceposfiletstarti + $sourceposfiletstartf " | bc -l`
174 fi
175 #echo "tstart: "$sourceposfiletstart
176 fi
177
178 triggerratefile=$auxdir/$runnumber.FTM_CONTROL_TRIGGER_RATES.fits
179 if ! [ -e $triggerratefile ]
180 then
181 printprocesslog "WARN "$triggerratefile" not found."
182 else
183 trignumerrors=`fverify $triggerratefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
184 if [ $trignumerrors -gt 0 ]
185 then
186 printprocesslog "WARN for $triggerratefile fverify returned "$trignumerrors" error(s)."
187 fi
188 fi
189
190 thresholdfile=$auxdir/$runnumber.FTM_CONTROL_STATIC_DATA.fits
191 if ! [ -e $thresholdfile ]
192 then
193 printprocesslog "WARN "$thresholdfile" not found."
194 else
195 treshnumerrors=`fverify $thresholdfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
196 if [ $treshnumerrors -gt 0 ]
197 then
198 printprocesslog "WARN for $thresholdfile fverify returned "$treshnumerrors" error(s)."
199 fi
200 fi
201
202 biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
203 if ! [ -e $biasvoltagefile ]
204 then
205 printprocesslog "WARN "$biasvoltagefile" not found."
206 else
207 biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
208 if [ $biasnumerrors -gt 0 ]
209 then
210 printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
211 fi
212 fi
213
214 # fill auxiliary information for files
215 for filenum in ${filenumbers[@]}
216 do
217 printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
218 echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
219 # get information from rawfile
220 rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.fz
221 if ! [ -e $rawfile ]
222 then
223 printprocesslog "ERROR: "$rawfile" not found."
224 continue
225 fi
226 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
227 #if [ "$checkfitsfile" == "" ]
228 #then
229 # numfitserrors=1
230 # printprocesslog "WARN: "$rawfile" probably corrupted."
231 # continue
232 #fi
233 runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
234 mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
235 tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
236 tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
237 tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
238 tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
239 if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
240 then
241 printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
242 continue
243 fi
244 # assuming that at least TSTARTI and TSTOPI are consistent
245 #echo $rawfile
246 #echo $tstarti
247 #echo $tstopi
248 #echo $tstartf
249 #echo $tstopf
250 if [ $tstarti -gt 30000 ]
251 then
252 tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
253 tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
254 #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
255 tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
256 else
257 tstart=`echo " $tstarti + $tstartf " | bc -l`
258 tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
259 #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
260 tstop=`echo " $tstopi + $tstopf " | bc -l`
261 fi
262 #echo $tstart
263 #echo $tstop
264 #if [ $runnumber -eq 20111123 ]
265 #then
266 # # add mjdref for days were aux files were inconsistent
267 # tstart=`echo " $tstart + $mjdref " | bc -l`
268 # tstart2=`echo " $tstart2 + $mjdref " | bc -l`
269 # tstop=`echo " $tstop + $mjdref " | bc -l`
270 #fi
271
272 # get information from source_pos file
273 #echo $sourceposfile
274 if [ -e $sourceposfile ] && [ $sourceposnumerrors -eq 0 ]
275 then
276 #sourcename=`${factpath}/fitsdump ${sourceposfile} -c Time -c Name --filter='[1]<'${tstop} 2>/dev/null | tail -1 2>&1 | grep -o -E "['][a-zA-Z0-9\ ]+[']" | sed -e "s/'//g"`
277 #sourcename=`/home/fact/FACT++.dbg/fitsdump ${sourceposfile} -c Time -c Name --filter='[1]<'${tstop} 2>/dev/null | tail -1 2>&1 | grep -o -E "['][a-zA-Z0-9\+\-\ ]+[']" | sed -e "s/'//g"`
278 sourcename=`$factpath/fitsdump ${sourceposfile} -c Time -c Name --filter='[1]<'${tstop}' && [1]>'${sourceposfiletstart} 2>/dev/null | tail -1 2>&1 | grep -o -E "['][a-zA-Z0-9\ \.\+\-]+[']" | sed -e "s/'//g"`
279 #echo "$factpath/fitsdump ${sourceposfile} -c Time -c Name --filter='[1]<'${tstop} --filter='[1]>'${sourceposfiletstart} 2>/dev/null | tail -1 2>&1 | grep -o -E \"['][a-zA-Z0-9\ \.\+\-]+[']\" | sed -e \"s/'//g\""
280 #echo $sourcename
281 if [ "$sourcename" == "" ]
282 then
283 printprocesslog "INFO couldn't get sourcename ("$sourcename") from "$sourceposfile" for "$runnumber"_"$filenum
284 else
285 query="SELECT fSourceKey FROM Source WHERE fSourceName='"$sourcename"'"
286 #echo $query
287 sourcekey=`sendquery`
288 if [ "$sourcename" == "" ]
289 then
290 printprocesslog "WARN couldn't get sourcekey for source "$sourcename" from DB for "$runnumber"_"$filenum
291 fi
292 #echo $sourcekey
293 fi
294 fi
295
296 # build query to update runinfo in DB
297 query="UPDATE RunInfo SET "
298 # fill source key only if available
299 if ! [ "$sourcekey" = "" ]
300 then
301 query=$query" fSourceKey="$sourcekey", "
302 else
303 query=$query" fSourceKey=NULL, "
304 fi
305
306 # get information from tracking
307 if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
308 then
309 # RA
310 getfitsstatistics $trackingfile "Ra" $tstart $tstop
311 if [ "$min" == "$max" ] && [ $good -gt 0 ]
312 then
313 query=$query" fRightAscension="$mean
314 else
315 query=$query" fRightAscension=NULL"
316 if [ $good -gt 0 ]
317 then
318 printprocesslog "WARN for $rawfile RA changes within run (min: "$min", max: "$max")."
319 fi
320 fi
321 # Declination
322 getfitsstatistics $trackingfile "Dec" $tstart $tstop
323 if [ "$decmin" == "$decmax" ] && [ $good -gt 0 ]
324 then
325 query=$query", fDeclination="$mean
326 else
327 query=$query", fDeclination=NULL"
328 if [ $good -gt 0 ]
329 then
330 printprocesslog "WARN for $rawfile declination changes within run (min: "$min", max: "$max")."
331 fi
332 fi
333 # Zd
334 getfitsstatistics $trackingfile "Zd" $tstart $tstop
335 if [ $good -gt 0 ]
336 then
337 query=$query", fZenithDistanceMin="$min
338 query=$query", fZenithDistanceMean="$mean
339 query=$query", fZenithDistanceMax="$max
340 else
341 query=$query", fZenithDistanceMin=NULL"
342 query=$query", fZenithDistanceMean=NULL"
343 query=$query", fZenithDistanceMax=NULL"
344 fi
345 # Az
346 getfitsstatistics $trackingfile "Az" $tstart $tstop
347 if [ $good -gt 0 ]
348 then
349 query=$query", fAzimuthMin="$min
350 query=$query", fAzimuthMean="$mean
351 query=$query", fAzimuthMax="$max
352 else
353 query=$query", fAzimuthMin=NULL"
354 query=$query", fAzimuthMean=NULL"
355 query=$query", fAzimuthMax=NULL"
356 fi
357 else
358 query=$query" fRightAscension=NULL"
359 query=$query", fDeclination=NULL"
360 query=$query", fZenithDistanceMin=NULL"
361 query=$query", fZenithDistanceMean=NULL"
362 query=$query", fZenithDistanceMax=NULL"
363 query=$query", fAzimuthMin=NULL"
364 query=$query", fAzimuthMean=NULL"
365 query=$query", fAzimuthMax=NULL"
366 fi
367
368 # get information from trigger
369 if [ -e $triggerratefile ] && [ $trignumerrors -eq 0 ]
370 then
371 #echo " $triggerratefile TriggerRate $tstart $tstop"
372 getfitsstatistics $triggerratefile "TriggerRate" $tstart $tstop
373# if [ "$mjdreftrig" == "" ]
374# then
375# 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]*'`
376# 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]*'`
377# 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]*'`
378# 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]*'`
379# else
380# 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]*'`
381# 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]*'`
382# 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]*'`
383# 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]*'`
384# fi
385 if [ $good -gt 0 ]
386 then
387 query=$query", fTriggerRateMedian="$median
388 else
389 query=$query", fTriggerRateMedian=NULL"
390 fi
391 else
392 query=$query", fTriggerRateMedian=NULL"
393 fi
394
395 # get information from thresholds
396 if [ -e $thresholdfile ] && [ $treshnumerrors -eq 0 ]
397 then
398 getfitsstatistics $thresholdfile "PatchThresh" $tstart $tstop
399 if [ $good -eq 0 ]
400 then
401 getfitsstatistics $thresholdfile "PatchThresh" $tstart2 $tstop
402 fi
403 if [ $good -gt 0 ]
404 then
405 query=$query", fThresholdMedian="$median
406 else
407 query=$query", fThresholdMedian=NULL"
408 fi
409 else
410 query=$query", fThresholdMedian=NULL"
411 fi
412
413 # get information from bias: U
414 if [ -e $biasvoltagefile ] && [ $biasnumerrors -eq 0 ]
415 then
416 if [ $runnumber -gt 20120324 ]
417 then
418 value="Uout"
419 else
420 value="U"
421 fi
422 getfitsstatistics $biasvoltagefile $value $tstart $tstop
423 if [ $good -eq 0 ]
424 then
425 getfitsstatistics $biasvoltagefile $value $tstart2 $tstop
426 fi
427 if [ $good -gt 0 ]
428 then
429 query=$query", fBiasVoltageMedian="$median
430 else
431 query=$query", fBiasVoltageMedian=NULL"
432 fi
433 else
434 query=$query", fBiasVoltageMedian=NULL"
435 fi
436
437 # add where condition
438 query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
439 #echo $query
440
441 # send query to DB
442 sendquery >/dev/null
443 done
444done
445
446finish
447
448
Note: See TracBrowser for help on using the repository browser.