source: trunk/DataCheck/QuickLook/FlareAlerts.sh@ 18775

Last change on this file since 18775 was 18773, checked in by Daniela Dorner, 8 years ago
added decay to type 3, added logging, uncommented sending email
  • Property svn:executable set to *
File size: 18.9 KB
Line 
1#!/bin/bash
2#
3
4# missing:
5# implement DB-entries for being called
6# prepare directly template for email
7# use current value for CU in triggers
8#
9# add more information:
10# mjd, obs-summary, weather info (clouds? dust?), CU, corrected excrates
11#
12# evaluate output of all nights (check also for crab to estimate fluctuations)
13# error emails in case no DB content / no QLA
14# calculate delay of QLA and send email if > 30 Min
15
16# run for more nights:
17# for (( i=0; i < 70 ; i++)); do date=`date --date="-${i}days" +%Y%m%d`; /home/fact/SW.automatic.processing/DataCheck/QuickLook/FlareAlerts.sh $date; done > logfile
18
19source `dirname $0`/../Sourcefile.sh
20printprocesslog "INFO starting $0"
21
22emailfrom=dorner@astro.uni-wuerzburg.de
23emailto=fact-online@lists.phys.ethz.ch
24
25logfile=$runlogpath"/FlareAlerts-"$datetime".log"
26date > $logfile
27
28# get date
29if [ "$1" != "" ]
30then
31 checkstring=`echo $1 | grep -E -o '^20[0-9][0-9][01][0-9][0-3][0-9]$'`
32 echo $checkstring
33 if [ "$checkstring" = "" ]
34 then
35 night=`date +%Y%m%d --date="-12 HOUR"`
36 else
37 night=$1
38 fi
39else
40 night=`date +%Y%m%d --date="-12 HOUR"`
41fi
42
43echo "Processing "$night >> $logfile
44
45
46# get sources for current night from DB (RunInfo)
47query="SELECT fSourceKey FROM RunInfo WHERE fNight="$night" AND fRunTypeKey=1 AND NOT ISNULL(fSourceKey) GROUP BY fSourceKey"
48sourcekeys=( `sendquery` )
49
50# some stuff for queries:
51ontime="IF(ISNULL(fEffectiveOn), fOnTimeAfterCuts, TIME_TO_SEC(TIMEDIFF(fRunStop,fRunStart))*fEffectiveOn)"
52threshold="IF (ISNULL(fThresholdMinSet), fThresholdMedian, fThresholdMinSet)"
53cu="20.0"
54corr="1"
55# missing: corrected excessrates
56# missing: time-dependent CU
57
58function get_query_nightly_binning()
59{
60 # query to get information from DB
61 query="SELECT fSourceKey AS num, "
62 query=$query"fNight AS night, MIN(fRunID) AS runmin, MAX(fRunID) AS runmax, "
63 query=$query"MIN(fRunStart) AS start, MAX(fRunStop) AS stop, "
64 query=$query"ROUND(SUM("$ontime")/3600.,1) AS ontime, "
65 query=$query"SUM(fNumSigEvts) AS sig, SUM(fNumBgEvts) AS bg, "
66 query=$query"ROUND(SUM(fNumBgEvts)/SUM("$ontime")*3600,1) AS bgrate, "
67 query=$query"SUM(fNumExcEvts) AS exc, "
68 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts)), 1) AS excerr, "
69 query=$query"ROUND(SUM(fNumExcEvts)/SUM("$ontime")*3600,1) AS excrate, "
70 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600, 1) AS excrateerr, "
71 query=$query"ROUND(SUM(fNumExcEvts*"$corr")/SUM("$ontime")*3600,1) as corexcrate, " # put here correction factor
72 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600*SUM(fNumExcEvts)/SUM(fNumExcEvts*"$corr"), 1) AS corexcrateerr, " # correctionfactor = exc / exc_cor, put here correction factor
73 query=$query"ROUND(LiMa(SUM(fNumSigEvts), SUM(fNumBgEvts)),1) AS signif, "
74 query=$query"ROUND(SUM(fNumExcEvts)/SUM("$ontime")*3600/"$cu",1) AS cu, " # make value time dependent
75 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600/"$cu", 1) AS cuerr, " # make value time dependent
76 query=$query"ROUND(SUM(fNumExcEvts*"$corr")/SUM("$ontime")*3600/"$cu",1) as corcu, " # make value time dependent # put here correction factor
77 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600*SUM(fNumExcEvts)/SUM(fNumExcEvts*"$corr")/"$cu", 1) AS corcuerr, " # correctionfactor = exc / exc_cor # make value time dependent # put here correction factor
78 query=$query"MIN(fZenithDistanceMin) as zdmin, MAX(fZenithDistanceMax) as zdmax, "
79 query=$query"MIN("$threshold") as thmin, MAX("$threshold") as thmax "
80 query=$query"FROM AnalysisResultsRunLP "
81 query=$query"LEFT JOIN RunInfo USING (fNight, fRunID) "
82 query=$query"WHERE fSourceKey="$sourcekey" AND fNight="$night" AND NOT ISNULL(fNumExcEvts) "
83 query=$query"GROUP BY fNight, fSourceKey "
84# query=$query"ORDER BY fRunStart "
85 query=$query"HAVING ontime > 0.5 " # at least 30 minutes of observation
86}
87
88function get_query_minute_binning()
89{
90 # set binning
91 if [ "$1" != "" ]
92 then
93 bin2=$1
94 else
95 bin2=$bin
96 fi
97 # query to get information from DB
98 query="SELECT MAX(o.b) AS num, "
99 query=$query"MIN(o.n) AS night, MIN(o.run) AS runmin, MAX(o.run) AS runmax, "
100 query=$query"MIN(o.start) AS start, MAX(o.stop) AS stop, "
101 query=$query"ROUND(SUM(o.ot)/60.,1) AS ontime, "
102 query=$query"SUM(o.sig) AS sig, SUM(o.bg) AS bg, "
103 query=$query"ROUND(SUM(o.bg)/SUM(o.ot)*3600,1) AS bgrate, "
104 query=$query"SUM(o.exc) AS exc, "
105 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg)), 1) AS excerr, "
106 query=$query"ROUND(SUM(o.exc)/SUM(o.ot)*3600,1) AS excrate, "
107 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg))/SUM(o.ot)*3600, 1) AS excrateerr, "
108 query=$query"ROUND(SUM(o.exccor)/SUM(o.ot)*3600,1) as corexcrate, "
109 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg))/SUM(o.ot)*3600*SUM(o.exc)/SUM(o.exccor), 1) AS corexcrateerr, " # correctionfactor = exc / exc_cor
110 query=$query"ROUND(LiMa(SUM(o.sig), SUM(o.bg)),1) AS signif, "
111 query=$query"ROUND(SUM(o.exc)/SUM(o.ot)*3600/o.cu,1) AS cu, "
112 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg))/SUM(o.ot)*3600/o.cu, 1) AS cuerr, "
113 query=$query"ROUND(SUM(o.exccor)/SUM(o.ot)*3600/o.cu,1) as corcu, "
114 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg))/SUM(o.ot)*3600*SUM(o.exc)/SUM(o.exccor)/o.cu, 1) AS corcuerr, " # correctionfactor = exc / exc_cor
115 query=$query"MIN(o.zdmin) as zdmin, MAX(o.zdmax) as zdmax, "
116 query=$query"MIN(o.th) as thmin, MAX(o.th) as thmax "
117 query=$query"FROM ("
118 query=$query"SELECT "
119 query=$query"fRunID AS run, fNight AS n, "
120 query=$query"@ot:= "$ontime" AS ot, "
121 query=$query"fRunStart AS start, fRunStop AS stop, "
122 query=$query"fNumExcEvts AS exc, fNumBgEvts AS bg, fNumSigEvts AS sig, "
123 query=$query"fNumExcEvts*"$corr" AS exccor, " # put here correction factor
124 query=$query$cu" as cu, " # make value time dependent
125 query=$query"fZenithDistanceMin AS zdmin, fZenithDistanceMax AS zdmax, "
126 query=$query$threshold" AS th, "
127 query=$query"IF (@night=fNight AND FLOOR((@os+@ot)/"$bin2"./60.)<1, @bl, @bl := @bl + 1) AS b, "
128 query=$query"IF (@night=fNight AND FLOOR((@os+@ot)/"$bin2"./60.)<1, @os:=@os + @ot, @os := @ot) AS os, @"
129 query=$query"night :=fNight AS night FROM AnalysisResultsRunLP "
130 query=$query"LEFT JOIN RunInfo USING (fNight, fRunID) "
131 query=$query"CROSS JOIN (SELECT @night :=0, @ot :=0, @os :=0, @bl:=0) PARAMS "
132 query=$query"WHERE fSourceKey="$sourcekey" AND fNight="$night" AND NOT ISNULL(fNumExcEvts) "
133 if [ "$1" != "" ]
134 then
135 query=$query" AND fRunID <="${results[$num+3]}
136 fi
137 query=$query" ORDER BY fRunStart "
138 if [ "$1" != "" ]
139 then
140 query=$query"DESC"
141 fi
142 query=$query" ) o GROUP BY b HAVING ontime > "$bin2"*0.75 ORDER BY start "
143 if [ "$1" != "" ]
144 then
145 query=$query"DESC"
146 fi
147 #echo $query
148}
149
150function evaluate_result()
151{
152 oldexc=0
153 exc=0
154 excold=0
155 slope=0
156 slopeprev=0
157 i=0
158 # be careful with start and stop (space inbetween) -> 27 columns instead of 25
159 while [ 0 -lt 1 ]
160 do
161 trigger=0
162 num=`echo "$i * 27" | bc -l`
163 if [ "${results[$num]}" = "" ]
164 then
165 break
166 fi
167 #night=${results[$num+1]}
168 runid=${results[$num+2]}
169 sig=${results[$num+18]} # significance
170 #exc=${results[$num+14]} # excrate
171 excold=$exc
172 exc=${results[$num+19]} # excrate in CU
173
174 if [ "$onlyifhigher" = "yes" ]
175 then
176 higher=` echo " $exc > $oldexc " | bc `
177 if [ $higher -eq 1 ]
178 then
179 # keep old value
180 oldexc=$exc
181 fi
182 fi
183
184 if [ "$bin" = "" ]
185 then
186 echo " ontime: "${results[$num+8]}" h" >> $logfile
187 fi
188
189 # fast rise/decay trigger
190 if [ $triggertype -eq 3 ]
191 then
192 slopeprev=$slope
193 sigprev=$sig
194 if [ "$excold" = "0" ]
195 then
196 slope=0
197 else
198 slope=`echo " scale=1; ( $exc - $excold ) / ( $bin / 60. ) " | bc -l `
199 fi
200 get_query_minute_binning 60
201 #echo $query
202 results2=( `sendquery` )
203 # need to check last hour backward
204 if [ "${results2[19]}" = "" ]
205 then
206 slope60=0
207 sig60=0
208 else
209 sig60=${results2[18]} # significance 1h
210 if [ "${results2[27+19]}" = "" ]
211 then
212 # maybe treat this case differently
213 slope60=${results2[19]}
214 else
215 slope60=`echo " ${results2[19]} - ${results2[27+19]} " | bc -l` # ie /1h
216 fi
217 fi
218 #echo "exc "$exc" excold "$excold
219 #echo "slope "$slope" prev "$slopeprev" sig "$sig" prevsig "$sigprev" sig60 "$sig60" slope60 "$slope60
220 fi
221 # missing: probably one should check also 20 min binning (s example 20170103)
222 # missing: check on still available observation time
223
224 echo " "$i" "${results[$num+2]}"-"${results[$num+3]}"["${results[$num+8]}"] "$exc" "$sig >> $logfile
225
226 case $triggertype in
227 1) #echo "std trigger: criteria ( $exc >= $exclimit && $sig >= $siglimit && $higher )"
228 trigger=`echo " $exc >= $exclimit && $sig >= $siglimit && $higher " | bc -l`
229 ;;
230 2) #echo "magic 501: criteria ( $exc >= $exclimit && $sig >= $siglimit && $higher )"
231 trigger=`echo " $exc >= $exclimit && $sig >= $siglimit && $higher " | bc -l`
232 ;;
233 3) #echo "magic fast rise/decay"
234 #trigger=`echo " $slope >= $slopelimit && $slopeprev >= $slopelimit && $slope60 >= $slopelimit && $sig >= $siglimit " | bc -l`
235 trigger=`echo " ( ( $slope >= $slopelimit && $slopeprev >= $slopelimit && $slope60 >= $slopelimit ) || ( - $slope >= $slopelimit && - $slopeprev >= $slopelimit && - $slope60 >= $slopelimit ) ) && $sig >= $siglimit " | bc -l`
236 ;;
237 *) echo $triggertype" not yet implemented" >> $logfile
238 ;;
239 esac
240
241 # missing: maybe use $donetriggerfile for all cases - update of nightly case can be triggered differently
242 # missing: adapt triggerfilename for type 3 (slopelimit)
243 if [ $trigger -eq 1 ]
244 then
245 # do whatever to be done to trigger
246 # - send email/sms / call
247 # - create amon file
248 # - prepare email for alert
249 # - entry in DB
250
251 if [ "$bin" = "" ]
252 then
253 # nightly file: simply overwrite, but do not send trigger again
254 # or check value if it's increasing?
255 # missing: get information from previous trigger + interpret
256 triggerfile="/home/fact/flare.alerts/"$night"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
257 else
258 # smaller binning: check if trigger is identical
259 triggerfile="/home/fact/flare.alerts/"$night"_"`printf %03d $runid`"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
260 donetriggerfile=$triggerfile".done"
261 #ls $donetriggerfile
262 # write new file only if old files do not agree
263 if [ "$donetriggerfile" != "" ] && [ -e $donetriggerfile ]
264 then
265 diff $donetriggerfile $triggerfile >/dev/null
266 checkstatus=`echo $?`
267 if [ $checkstatus -eq 0 ]
268 then
269 echo " alert already done "$donetriggerfile >> $logfile
270 i=`echo $i +1 | bc -l`
271 continue
272 fi
273 fi
274 if [ -e $triggerfile ]
275 then
276 mv $triggerfile $donetriggerfile
277 fi
278 fi
279 #echo $night"_"$runid" "$sourcekey" -> "$triggerfile
280 echo " writing "$triggerfile >> $logfile
281 touch $triggerfile
282 echo "Trigger found: " > $triggerfile
283 echo "-------------- " >> $triggerfile
284 echo " type: "$triggertype >> $triggerfile
285 echo " excess limit: "$exclimit" evts/h" >> $triggerfile
286 echo " significance limit: "$siglimit" sigma" >> $triggerfile
287 if [ "$bin" = "" ]
288 then
289 echo " nightly binning " >> $triggerfile
290 else
291 echo " binning: "$bin" min" >> $triggerfile
292 fi
293 echo "Summary of flare event: " >> $triggerfile
294 echo "----------------------- " >> $triggerfile
295 echo " source: "$sourcename >> $triggerfile
296 echo " night: "${results[$num+1]} >> $triggerfile
297 echo " runs: "${results[$num+2]}" - "${results[$num+3]} >> $triggerfile
298 echo " start: "${results[$num+4]}" "${results[$num+5]}" UTC" >> $triggerfile
299 echo " stop: "${results[$num+6]}" "${results[$num+7]}" UTC" >> $triggerfile
300 if [ "$bin" = "" ]
301 then
302 echo " ontime: "${results[$num+8]}" h" >> $triggerfile
303 else
304 echo " ontime: "${results[$num+8]}" min" >> $triggerfile
305 fi
306 #echo " ontime: "`echo "scale=1; ${results[$num+8]} / 60. " | bc -l`" min" #scale doesn't round properly
307 echo " signal: "${results[$num+9]}" evts" >> $triggerfile
308 echo " background: "${results[$num+10]}" evts" >> $triggerfile
309 echo " bgrate: "${results[$num+11]}" evts/h" >> $triggerfile
310 echo " exc: "${results[$num+12]}" +- "${results[$num+13]}" evts" >> $triggerfile
311 echo " excrate: "${results[$num+14]}" +- "${results[$num+15]}" evts/h" >> $triggerfile
312 echo " corr. excrate: "${results[$num+16]}" - "${results[$num+17]}" evts/h" >> $triggerfile
313 echo " significance: "${results[$num+18]}" sigma" >> $triggerfile
314 echo " cu: "${results[$num+19]}" +- "${results[$num+20]}" CU" >> $triggerfile
315 echo " corr. cu: "${results[$num+21]}" +- "${results[$num+22]}" CU" >> $triggerfile
316 echo " zd: "${results[$num+23]}" - "${results[$num+24]}" degree" >> $triggerfile
317 echo " th: "${results[$num+25]}" - "${results[$num+26]}" DAC counts" >> $triggerfile
318 # additional information fast rise/decay trigger
319 if [ $triggertype -eq 3 ]
320 then
321 echo "Flux doubling/halfing times: " >> $triggerfile
322 echo "---------------------------- " >> $triggerfile
323 echo " excess: "$exc >> $triggerfile
324 echo " excess old: "$excold >> $triggerfile
325 echo " significance: "$sig >> $triggerfile
326 echo " significance old: "$sigprev >> $triggerfile
327 echo " slope: "$excold >> $triggerfile
328 echo " slope old: "$slopeold >> $triggerfile
329 echo " excess 60 min: "${results2[19]} >> $triggerfile
330 echo " excess 60 min old: "${results2[27+19]} >> $triggerfile
331 echo " slope 60 min: "$slope60 >> $triggerfile
332 echo " significance 60 min: "$sig60 >> $triggerfile
333 fi
334
335 if [ "$donetriggerfile" != "" ] && [ -e $donetriggerfile ]
336 then
337 diff $donetriggerfile $triggerfile >/dev/null
338 checkstatus=`echo $?`
339 if [ $checkstatus -gt 0 ]
340 then
341 rm $donetriggerfile
342 fi
343 fi
344 # missing: get summary of whole observation
345
346 # missing AMON cases - create VOEvent-File
347
348 # send email only of $donetriggerfile doesn't exists
349 if ! [ -e $donetriggerfile ]
350 then
351 cat $triggerfile | mail -s 'test flare alert ' -b $emailfrom -r $emailfrom $emailto
352 fi
353
354 #cat $triggerfile | mail -s "test flare alert for $sourcename " $emailto
355 # that's also the cases for making a call
356
357 fi
358
359 # counter
360 i=`echo $i +1 | bc -l`
361 done
362
363 echo " found "$i" data point(s)." >> $logfile
364 echo "" >> $logfile
365}
366
367for sourcekey in ${sourcekeys[@]}
368do
369 query="SELECT fSourceName FROM Source WHERE fSourceKey="$sourcekey
370 sourcename=`sendquery`
371
372 # ignore Crab
373 if [ $sourcekey -eq 5 ]
374 then
375 continue
376 fi
377 printprocesslog "INFO Evaluation for $sourcename ... "
378 echo "Evaluation for $sourcename ... " >> $logfile
379
380 # missing: get limits from DB (structure needs to be defined)
381
382 # triggers in the frame of the MoU in the gamma-ray community
383 triggertype=1
384 # limits
385 siglimit=3.0 # sigma
386 #if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ]
387 if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 5 ]
388 then
389 exclimit=3.0 # CU
390 else
391 exclimit=0.5 # CU
392 fi
393 # only if rate goes even higher, we have to react
394 onlyifhigher="yes"
395 higher=1
396
397 printprocesslog "INFO checking for [General gamma-ray MoU]" >> $logfile
398 echo "[General gamma-ray MoU]" >> $logfile
399 echo " nightly binning..." >> $logfile
400 # checking nightly binning
401 bin=
402 get_query_nightly_binning
403 results=( `sendquery` )
404 evaluate_result
405
406 # 20 min binning
407 bin=20
408 echo " "$bin" min binning..." >> $logfile
409 get_query_minute_binning
410 results=( `sendquery` )
411 evaluate_result
412
413 # triggers to MAGIC
414 # Mrk 501 proposal
415 triggertype=2
416 if [ $sourcekey -eq 2 ]
417 then
418 siglimit=3.0
419 exclimit=2.0 # cu
420 printprocesslog "INFO checking for [Trigger to MAGIC 501 proposal]" >> $logfile
421 echo "[Trigger to MAGIC 501 proposal]" >> $logfile
422 echo " nightly binning..." >> $logfile
423 # checking nightly binning
424 bin=
425 get_query_nightly_binning
426 results=( `sendquery` )
427 evaluate_result
428 # 20 min binning
429 bin=20
430 echo " "$bin" min binning..." >> $logfile
431 get_query_minute_binning
432 results=( `sendquery` )
433 evaluate_result
434 fi
435 # Mother of ToO - fast rise/decay
436 # sources: Mrk 421, Mrk 501, 2344, 1959
437 triggertype=3
438 #if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ]
439 if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ] || [ $sourcekey -eq 5 ]
440 then
441 printprocesslog "INFO checking for [Trigger to MAGIC - fast rise/decay]" >> $logfile
442 echo "[Trigger to MAGIC - fast rise/decay]" >> $logfile
443 ## keep thresholds low (or do not use in evaluation)
444 #siglimit=2.0
445 #exclimit=0.5
446 # limits in slope
447 slopelimit=1.0 # 1CU/h
448 siglimit=3.0 # 1 sigma in 1 hour
449 # binning
450 bin=30
451 echo " "$bin" min binning..." >> $logfile
452 get_query_minute_binning
453 results=( `sendquery` )
454 evaluate_result
455 fi
456
457 # Triggers to AMON
458 triggertype=4
459 printprocesslog "INFO checking for [Trigger to AMON]" >> $logfile
460 echo "[Trigger to AMON]" >> $logfile
461 echo " still to be defined" >> $logfile
462 # missing: trigger limits and binning still to be defined
463 # also nightly?
464 # sub-threshold-triggers?
465 # FP-rate?
466 siglimit=2.0
467 exclimit=0.5
468 onlyifhigher="no"
469 bin=20
470
471 echo "" >> $logfile
472 echo "" >> $logfile
473 done
474
475finish
476
477# for archival testing:
478for (( i=0; i < 100 ; i++))
479do
480 date=`date --date="-${i}days" +%Y%m%d`
481 /home/fact/SW.automatic.processing/DataCheck/QuickLook/FlareAlerts.sh $date
482done
483
484
Note: See TracBrowser for help on using the repository browser.