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

Last change on this file since 17896 was 17896, checked in by Daniela Dorner, 10 years ago
removed obsolete lines
  • Property svn:executable set to *
File size: 17.2 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 if [ -e $sourceposfile ] && [ $sourceposnumerrors -eq 0 ]
274 then
275 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"`
276 if [ "$sourcename" == "" ]
277 then
278 printprocesslog "INFO couldn't get sourcename ("$sourcename") from "$sourceposfile" for "$runnumber"_"$filenum
279 else
280 query="SELECT fSourceKey FROM Source WHERE fSourceName='"$sourcename"'"
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 fi
287 fi
288
289 # build query to update runinfo in DB
290 query="UPDATE RunInfo SET "
291 # fill source key only if available
292 if ! [ "$sourcekey" = "" ]
293 then
294 query=$query" fSourceKey="$sourcekey", "
295 else
296 query=$query" fSourceKey=NULL, "
297 fi
298
299 # get information from tracking
300 if [ -e $trackingfile ] && [ $tracknumerrors -eq 0 ]
301 then
302 # RA
303 getfitsstatistics $trackingfile "Ra" $tstart $tstop
304 if [ "$min" == "$max" ] && [ $good -gt 0 ]
305 then
306 query=$query" fRightAscension="$mean
307 else
308 query=$query" fRightAscension=NULL"
309 if [ $good -gt 0 ]
310 then
311 printprocesslog "WARN for $rawfile RA changes within run (min: "$min", max: "$max")."
312 fi
313 fi
314 # Declination
315 getfitsstatistics $trackingfile "Dec" $tstart $tstop
316 if [ "$decmin" == "$decmax" ] && [ $good -gt 0 ]
317 then
318 query=$query", fDeclination="$mean
319 else
320 query=$query", fDeclination=NULL"
321 if [ $good -gt 0 ]
322 then
323 printprocesslog "WARN for $rawfile declination changes within run (min: "$min", max: "$max")."
324 fi
325 fi
326 # Zd
327 getfitsstatistics $trackingfile "Zd" $tstart $tstop
328 if [ $good -gt 0 ]
329 then
330 query=$query", fZenithDistanceMin="$min
331 query=$query", fZenithDistanceMean="$mean
332 query=$query", fZenithDistanceMax="$max
333 else
334 query=$query", fZenithDistanceMin=NULL"
335 query=$query", fZenithDistanceMean=NULL"
336 query=$query", fZenithDistanceMax=NULL"
337 fi
338 # Az
339 getfitsstatistics $trackingfile "Az" $tstart $tstop
340 if [ $good -gt 0 ]
341 then
342 query=$query", fAzimuthMin="$min
343 query=$query", fAzimuthMean="$mean
344 query=$query", fAzimuthMax="$max
345 else
346 query=$query", fAzimuthMin=NULL"
347 query=$query", fAzimuthMean=NULL"
348 query=$query", fAzimuthMax=NULL"
349 fi
350 else
351 query=$query" fRightAscension=NULL"
352 query=$query", fDeclination=NULL"
353 query=$query", fZenithDistanceMin=NULL"
354 query=$query", fZenithDistanceMean=NULL"
355 query=$query", fZenithDistanceMax=NULL"
356 query=$query", fAzimuthMin=NULL"
357 query=$query", fAzimuthMean=NULL"
358 query=$query", fAzimuthMax=NULL"
359 fi
360
361 # get information from trigger
362 if [ -e $triggerratefile ] && [ $trignumerrors -eq 0 ]
363 then
364 #echo " $triggerratefile TriggerRate $tstart $tstop"
365 getfitsstatistics $triggerratefile "TriggerRate" $tstart $tstop
366# if [ "$mjdreftrig" == "" ]
367# then
368# 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]*'`
369# 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]*'`
370# 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]*'`
371# 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]*'`
372# else
373# 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]*'`
374# 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]*'`
375# 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]*'`
376# 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]*'`
377# fi
378 if [ $good -gt 0 ]
379 then
380 query=$query", fTriggerRateMedian="$median
381 else
382 query=$query", fTriggerRateMedian=NULL"
383 fi
384 else
385 query=$query", fTriggerRateMedian=NULL"
386 fi
387
388 # get information from thresholds
389 if [ -e $thresholdfile ] && [ $treshnumerrors -eq 0 ]
390 then
391 getfitsstatistics $thresholdfile "PatchThresh" $tstart $tstop
392 if [ $good -eq 0 ]
393 then
394 getfitsstatistics $thresholdfile "PatchThresh" $tstart2 $tstop
395 fi
396 if [ $good -gt 0 ]
397 then
398 query=$query", fThresholdMedian="$median
399 else
400 query=$query", fThresholdMedian=NULL"
401 fi
402 else
403 query=$query", fThresholdMedian=NULL"
404 fi
405
406 # get information from bias: U
407 if [ -e $biasvoltagefile ] && [ $biasnumerrors -eq 0 ]
408 then
409 if [ $runnumber -gt 20120324 ]
410 then
411 value="Uout"
412 else
413 value="U"
414 fi
415 getfitsstatistics $biasvoltagefile $value $tstart $tstop
416 if [ $good -eq 0 ]
417 then
418 getfitsstatistics $biasvoltagefile $value $tstart2 $tstop
419 fi
420 if [ $good -gt 0 ]
421 then
422 query=$query", fBiasVoltageMedian="$median
423 else
424 query=$query", fBiasVoltageMedian=NULL"
425 fi
426 else
427 query=$query", fBiasVoltageMedian=NULL"
428 fi
429
430 # add where condition
431 query=$query" WHERE fNight="$runnumber" AND fRunID="$filenum
432 #echo $query
433
434 # send query to DB
435 sendquery >/dev/null
436 done
437done
438
439finish
440
441
Note: See TracBrowser for help on using the repository browser.