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

Last change on this file since 17124 was 17117, checked in by Daniela Dorner, 11 years ago
bugfix (doupdate=no was ignored)
  • Property svn:executable set to *
File size: 17.8 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 # check if aux files are available from that night
119 if ! [ -d $auxdir ]
120 then
121 printprocesslog "INFO no data available in "$auxdir
122 continue
123 else
124 printprocesslog "INFO processing files in "$auxdir
125 fi
126
127 # get file numbers from DB
128 # but only for not-corrupted files
129 query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
130 # only runs which are not yet filled
131 if [ "$doupdate" = "no" ]
132 then
133 query=$query" AND ISNULL(fRightAscension) "
134 fi
135 printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
136 filenumbers=( `sendquery $query` )
137 if [ ${#filenumbers} -eq 0 ]
138 then
139 printprocesslog "INFO No files found in the DB for night "$date
140 continue
141 fi
142
143 # get daily fits files
144 trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
145 if ! [ -e $trackingfile ]
146 then
147 printprocesslog "WARN "$trackingfile" not found."
148 else
149 tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
150 if [ $tracknumerrors -gt 0 ]
151 then
152 printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
153 fi
154 fi
155
156 sourceposfile=$auxdir/$runnumber.DRIVE_CONTROL_SOURCE_POSITION.fits
157 if ! [ -e $sourceposfile ]
158 then
159 printprocesslog "WARN "$sourceposfile" not found."
160 else
161 sourceposnumerrors=`fverify $sourceposfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
162 if [ $sourceposnumerrors -gt 0 ]
163 then
164 printprocesslog "WARN for $sourceposfile fverify returned "$sourceposnumerrors" error(s)."
165 fi
166 sourceposfiletstarti=`$factpath/fitsdump -h $sourceposfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
167 sourceposfiletstartf=`$factpath/fitsdump -h $sourceposfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
168 if [ $sourceposfiletstarti -gt 30000 ]
169 then
170 sourceposfiletstart=`echo " $sourceposfiletstarti + $sourceposfiletstartf - 40587 " | bc -l`
171 else
172 sourceposfiletstart=`echo " $sourceposfiletstarti + $sourceposfiletstartf " | bc -l`
173 fi
174 #echo "tstart: "$sourceposfiletstart
175 fi
176
177 triggerratefile=$auxdir/$runnumber.FTM_CONTROL_TRIGGER_RATES.fits
178 if ! [ -e $triggerratefile ]
179 then
180 printprocesslog "WARN "$triggerratefile" not found."
181 else
182 trignumerrors=`fverify $triggerratefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
183 if [ $trignumerrors -gt 0 ]
184 then
185 printprocesslog "WARN for $triggerratefile fverify returned "$trignumerrors" error(s)."
186 fi
187 fi
188
189 thresholdfile=$auxdir/$runnumber.FTM_CONTROL_STATIC_DATA.fits
190 if ! [ -e $thresholdfile ]
191 then
192 printprocesslog "WARN "$thresholdfile" not found."
193 else
194 treshnumerrors=`fverify $thresholdfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
195 if [ $treshnumerrors -gt 0 ]
196 then
197 printprocesslog "WARN for $thresholdfile fverify returned "$treshnumerrors" error(s)."
198 fi
199 fi
200
201 biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
202 if ! [ -e $biasvoltagefile ]
203 then
204 printprocesslog "WARN "$biasvoltagefile" not found."
205 else
206 biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
207 if [ $biasnumerrors -gt 0 ]
208 then
209 printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
210 fi
211 fi
212
213 # fill auxiliary information for files
214 for filenum in ${filenumbers[@]}
215 do
216 printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
217 echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
218 # get information from rawfile
219 rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.gz
220 if ! [ -e $rawfile ]
221 then
222 printprocesslog "ERROR: "$rawfile" not found."
223 continue
224 fi
225 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
226 #if [ "$checkfitsfile" == "" ]
227 #then
228 # numfitserrors=1
229 # printprocesslog "WARN: "$rawfile" probably corrupted."
230 # continue
231 #fi
232 runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
233 mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
234 tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
235 tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
236 tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
237 tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
238 if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
239 then
240 printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
241 continue
242 fi
243 # assuming that at least TSTARTI and TSTOPI are consistent
244 #echo $rawfile
245 #echo $tstarti
246 #echo $tstopi
247 #echo $tstartf
248 #echo $tstopf
249 if [ $tstarti -gt 30000 ]
250 then
251 tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
252 tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
253 #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
254 tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
255 else
256 tstart=`echo " $tstarti + $tstartf " | bc -l`
257 tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
258 #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
259 tstop=`echo " $tstopi + $tstopf " | bc -l`
260 fi
261 #echo $tstart
262 #echo $tstop
263 #if [ $runnumber -eq 20111123 ]
264 #then
265 # # add mjdref for days were aux files were inconsistent
266 # tstart=`echo " $tstart + $mjdref " | bc -l`
267 # tstart2=`echo " $tstart2 + $mjdref " | bc -l`
268 # tstop=`echo " $tstop + $mjdref " | bc -l`
269 #fi
270
271 # get information from source_pos file
272 #echo $sourceposfile
273 if [ -e $sourceposfile ] && [ $sourceposnumerrors -eq 0 ]
274 then
275 #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"`
276 #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"`
277 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"`
278 #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\""
279 #echo $sourcename
280 if [ "$sourcename" == "" ]
281 then
282 printprocesslog "INFO couldn't get sourcename ("$sourcename") from "$sourceposfile" for "$runnumber"_"$filenum
283 else
284 query="SELECT fSourceKey FROM Source WHERE fSourceName='"$sourcename"'"
285 #echo $query
286 sourcekey=`sendquery`
287 if [ "$sourcename" == "" ]
288 then
289 printprocesslog "WARN couldn't get sourcekey for source "$sourcename" from DB for "$runnumber"_"$filenum
290 fi
291 #echo $sourcekey
292 fi
293 fi
294
295 # build query to update runinfo in DB
296 query="UPDATE RunInfo SET "
297 # fill source key only if available
298 if ! [ "$sourcekey" = "" ]
299 then
300 query=$query" fSourceKey="$sourcekey", "
301 else
302 query=$query" fSourceKey=NULL, "
303 fi
304
305 # get information from tracking
306 if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
307 then
308 # RA
309 getfitsstatistics $trackingfile "Ra" $tstart $tstop
310 if [ "$min" == "$max" ] && [ $good -gt 0 ]
311 then
312 query=$query" fRightAscension="$mean
313 else
314 query=$query" fRightAscension=NULL"
315 if [ $good -gt 0 ]
316 then
317 printprocesslog "WARN for $rawfile RA changes within run (min: "$min", max: "$max")."
318 fi
319 fi
320 # Declination
321 getfitsstatistics $trackingfile "Dec" $tstart $tstop
322 if [ "$decmin" == "$decmax" ] && [ $good -gt 0 ]
323 then
324 query=$query", fDeclination="$mean
325 else
326 query=$query", fDeclination=NULL"
327 if [ $good -gt 0 ]
328 then
329 printprocesslog "WARN for $rawfile declination changes within run (min: "$min", max: "$max")."
330 fi
331 fi
332 # Zd
333 getfitsstatistics $trackingfile "Zd" $tstart $tstop
334 if [ $good -gt 0 ]
335 then
336 query=$query", fZenithDistanceMin="$min
337 query=$query", fZenithDistanceMean="$mean
338 query=$query", fZenithDistanceMax="$max
339 else
340 query=$query", fZenithDistanceMin=NULL"
341 query=$query", fZenithDistanceMean=NULL"
342 query=$query", fZenithDistanceMax=NULL"
343 fi
344 # Az
345 getfitsstatistics $trackingfile "Az" $tstart $tstop
346 if [ $good -gt 0 ]
347 then
348 query=$query", fAzimuthMin="$min
349 query=$query", fAzimuthMean="$mean
350 query=$query", fAzimuthMax="$max
351 else
352 query=$query", fAzimuthMin=NULL"
353 query=$query", fAzimuthMean=NULL"
354 query=$query", fAzimuthMax=NULL"
355 fi
356 else
357 query=$query" fRightAscension=NULL"
358 query=$query", fDeclination=NULL"
359 query=$query", fZenithDistanceMin=NULL"
360 query=$query", fZenithDistanceMean=NULL"
361 query=$query", fZenithDistanceMax=NULL"
362 query=$query", fAzimuthMin=NULL"
363 query=$query", fAzimuthMean=NULL"
364 query=$query", fAzimuthMax=NULL"
365 fi
366
367 # get information from trigger
368 if [ -e $triggerratefile ] && [ $trignumerrors -eq 0 ]
369 then
370 #echo " $triggerratefile TriggerRate $tstart $tstop"
371 getfitsstatistics $triggerratefile "TriggerRate" $tstart $tstop
372# if [ "$mjdreftrig" == "" ]
373# then
374# 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]*'`
375# 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]*'`
376# 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]*'`
377# 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]*'`
378# else
379# 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]*'`
380# 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]*'`
381# 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]*'`
382# 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]*'`
383# fi
384 if [ $good -gt 0 ]
385 then
386 query=$query", fTriggerRateMedian="$median
387 else
388 query=$query", fTriggerRateMedian=NULL"
389 fi
390 else
391 query=$query", fTriggerRateMedian=NULL"
392 fi
393
394 # get information from thresholds
395 if [ -e $thresholdfile ] && [ $treshnumerrors -eq 0 ]
396 then
397 getfitsstatistics $thresholdfile "PatchThresh" $tstart $tstop
398 if [ $good -eq 0 ]
399 then
400 getfitsstatistics $thresholdfile "PatchThresh" $tstart2 $tstop
401 fi
402 if [ $good -gt 0 ]
403 then
404 query=$query", fThresholdMedian="$median
405 else
406 query=$query", fThresholdMedian=NULL"
407 fi
408 else
409 query=$query", fThresholdMedian=NULL"
410 fi
411
412 # get information from bias: U
413 if [ -e $biasvoltagefile ] && [ $biasnumerrors -eq 0 ]
414 then
415 if [ $runnumber -gt 20120324 ]
416 then
417 value="Uout"
418 else
419 value="U"
420 fi
421 getfitsstatistics $biasvoltagefile $value $tstart $tstop
422 if [ $good -eq 0 ]
423 then
424 getfitsstatistics $biasvoltagefile $value $tstart2 $tstop
425 fi
426 if [ $good -gt 0 ]
427 then
428 query=$query", fBiasVoltageMedian="$median
429 else
430 query=$query", fBiasVoltageMedian=NULL"
431 fi
432 else
433 query=$query", fBiasVoltageMedian=NULL"
434 fi
435
436 # add where condition
437 query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
438 #echo $query
439
440 # send query to DB
441 sendquery >/dev/null
442 done
443done
444
445finish
446
447
Note: See TracBrowser for help on using the repository browser.