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

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