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

Last change on this file since 18066 was 17907, checked in by Daniela Dorner, 11 years ago
included and/or improved check of variable certaindate
  • Property svn:executable set to *
File size: 17.4 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 checkstring=`echo $certaindate | grep -E -o '^20[0-9][0-9]\/[01][0-9]\/[0-3][0-9]$'`
102 if [ "$checkstring" = "" ]
103 then
104 echo "Please give the variable certaindate in the correct format (YYYY/MM/DD)"
105 finish
106 fi
107 getdates $certaindate
108else
109 # get all night
110 #getdates "all"
111 # get last 3 nights if hour between 7 and 19h, else only current night
112 getdates 3 7 19
113fi
114
115printprocesslog "INFO processing the following night(s): "${dates[@]}
116echo `date`": processing the following night(s): "${dates[@]} >> $logfile 2>&1
117
118# do filling of aux data
119for date in ${dates[@]}
120do
121 auxdir=$auxdata/$date
122 runnumber=`echo $date | sed -e 's/\///g'`
123
124 # get file numbers from DB
125 # but only for not-corrupted files
126 query="SELECT fRunID from RunInfo WHERE fNight="$runnumber" AND fFitsFileErrors=0 "
127 # only runs which are not yet filled
128 if [ "$doupdate" = "no" ]
129 then
130 query=$query" AND ISNULL(fRightAscension) "
131 fi
132 printprocesslog "DEBUG get filenumbers from DB: QUERY: "$query
133 filenumbers=( `sendquery $query` )
134 # proceed only if there are files available
135 if [ ${#filenumbers} -eq 0 ]
136 then
137 printprocesslog "INFO No files found in the DB for night "$date
138 continue
139 fi
140
141 # check if aux files are available from that night
142 if ! [ -d $auxdir ]
143 then
144 printprocesslog "INFO no data available in "$auxdir
145 continue
146 else
147 printprocesslog "INFO processing files in "$auxdir
148 fi
149
150 # get daily fits files
151 trackingfile=$auxdir/$runnumber.DRIVE_CONTROL_TRACKING_POSITION.fits
152 if ! [ -e $trackingfile ]
153 then
154 printprocesslog "WARN "$trackingfile" not found."
155 else
156 tracknumerrors=`fverify $trackingfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
157 if [ $tracknumerrors -gt 0 ]
158 then
159 printprocesslog "WARN for $trackingfile fverify returned "$tracknumerrors" error(s)."
160 fi
161 fi
162
163 sourceposfile=$auxdir/$runnumber.DRIVE_CONTROL_SOURCE_POSITION.fits
164 if ! [ -e $sourceposfile ]
165 then
166 printprocesslog "WARN "$sourceposfile" not found."
167 else
168 sourceposnumerrors=`fverify $sourceposfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
169 if [ $sourceposnumerrors -gt 0 ]
170 then
171 printprocesslog "WARN for $sourceposfile fverify returned "$sourceposnumerrors" error(s)."
172 fi
173 sourceposfiletstarti=`$factpath/fitsdump -h $sourceposfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
174 sourceposfiletstartf=`$factpath/fitsdump -h $sourceposfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
175 if [ $sourceposfiletstarti -gt 30000 ]
176 then
177 sourceposfiletstart=`echo " $sourceposfiletstarti + $sourceposfiletstartf - 40587 " | bc -l`
178 else
179 sourceposfiletstart=`echo " $sourceposfiletstarti + $sourceposfiletstartf " | bc -l`
180 fi
181 #echo "tstart: "$sourceposfiletstart
182 fi
183
184 triggerratefile=$auxdir/$runnumber.FTM_CONTROL_TRIGGER_RATES.fits
185 if ! [ -e $triggerratefile ]
186 then
187 printprocesslog "WARN "$triggerratefile" not found."
188 else
189 trignumerrors=`fverify $triggerratefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
190 if [ $trignumerrors -gt 0 ]
191 then
192 printprocesslog "WARN for $triggerratefile fverify returned "$trignumerrors" error(s)."
193 fi
194 fi
195
196 thresholdfile=$auxdir/$runnumber.FTM_CONTROL_STATIC_DATA.fits
197 if ! [ -e $thresholdfile ]
198 then
199 printprocesslog "WARN "$thresholdfile" not found."
200 else
201 treshnumerrors=`fverify $thresholdfile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
202 if [ $treshnumerrors -gt 0 ]
203 then
204 printprocesslog "WARN for $thresholdfile fverify returned "$treshnumerrors" error(s)."
205 fi
206 fi
207
208 biasvoltagefile=$auxdir/$runnumber.BIAS_CONTROL_VOLTAGE.fits
209 if ! [ -e $biasvoltagefile ]
210 then
211 printprocesslog "WARN "$biasvoltagefile" not found."
212 else
213 biasnumerrors=`fverify $biasvoltagefile 2>/dev/null | grep -o '[0-9][ ]error(s)' | grep -E -o '[0-9]'`
214 if [ $biasnumerrors -gt 0 ]
215 then
216 printprocesslog "WARN for $biasvoltagefile fverify returned "$biasnumerrors" error(s)."
217 fi
218 fi
219
220 # fill auxiliary information for files
221 for filenum in ${filenumbers[@]}
222 do
223 printprocesslog "INFO processing file number "$runnumber"_"`printf %03d $filenum`
224 echo `date`": processing file number "$runnumber"_"`printf %03d $filenum` >> $logfile 2>&1
225 # get information from rawfile
226 rawfile=$ziprawdata/$date/$runnumber"_"`printf %03d $filenum`.fits.fz
227 if ! [ -e $rawfile ]
228 then
229 printprocesslog "ERROR: "$rawfile" not found."
230 continue
231 fi
232 #checkfitsfile=`fverify $rawfile 2>/dev/null | grep '0 error(s)'`
233 #if [ "$checkfitsfile" == "" ]
234 #then
235 # numfitserrors=1
236 # printprocesslog "WARN: "$rawfile" probably corrupted."
237 # continue
238 #fi
239 runtype=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep RUNTYPE | grep -E -o "['][a-z-]+[']" | sed -e "s/'//g"`
240 mjdrefraw=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'MJDREF' | grep -E -o '[0-9]{5}'`
241 tstarti=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTI' | grep -E -o '[0-9]{5}'`
242 tstartf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTARTF' | grep -E -o '0[.][0-9]+'`
243 tstopi=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPI' | grep -E -o '[0-9]{5}'`
244 tstopf=`$factpath/fitsdump -h $rawfile 2>/dev/null | grep 'TSTOPF' | grep -E -o '0[.][0-9]+'`
245 if [ "$tstarti" == "" ] || [ "$tstopi" == "" ] || [ "$tstartf" == "" ] || [ "$tstopf" == "" ]
246 then
247 printprocesslog "WARN: "$rawfile": one of the following keywords is empty or 0: TSTARTI TSTARTF TSTOPI TSTOPF "
248 continue
249 fi
250 # assuming that at least TSTARTI and TSTOPI are consistent
251 #echo $rawfile
252 #echo $tstarti
253 #echo $tstopi
254 #echo $tstartf
255 #echo $tstopf
256 if [ $tstarti -gt 30000 ]
257 then
258 tstart=`echo " $tstarti + $tstartf - 40587 " | bc -l`
259 tstart2=`echo " $tstarti + $tstartf - 40587 - 0.00011574 " | bc -l` # 10 sec
260 #tstart2=`echo " $tstarti + $tstartf - 40587 - 0.000023148 " | bc -l` # 2 sec
261 tstop=`echo " $tstopi + $tstopf - 40587 " | bc -l`
262 else
263 tstart=`echo " $tstarti + $tstartf " | bc -l`
264 tstart2=`echo " $tstarti + $tstartf - 0.00011574 " | bc -l` # 10 sec
265 #tstart2=`echo " $tstarti + $tstartf - 0.000023148 " | bc -l` # 2 sec
266 tstop=`echo " $tstopi + $tstopf " | bc -l`
267 fi
268 #echo $tstart
269 #echo $tstop
270 #if [ $runnumber -eq 20111123 ]
271 #then
272 # # add mjdref for days were aux files were inconsistent
273 # tstart=`echo " $tstart + $mjdref " | bc -l`
274 # tstart2=`echo " $tstart2 + $mjdref " | bc -l`
275 # tstop=`echo " $tstop + $mjdref " | bc -l`
276 #fi
277
278 # get information from source_pos file
279 if [ -e $sourceposfile ] && [ $sourceposnumerrors -eq 0 ]
280 then
281 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"`
282 if [ "$sourcename" == "" ]
283 then
284 printprocesslog "INFO couldn't get sourcename ("$sourcename") from "$sourceposfile" for "$runnumber"_"$filenum
285 else
286 query="SELECT fSourceKey FROM Source WHERE fSourceName='"$sourcename"'"
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 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.