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

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