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

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