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

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