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

Last change on this file since 18878 was 18859, checked in by Daniela Dorner, 8 years ago
fixed bug
  • Property svn:executable set to *
File size: 28.3 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# add note for values > 70 evts/h for X-ray triggers
9#
10# add more information:
11# mjd, obs-summary, weather info (clouds? dust?), CU, corrected excrates
12#
13# evaluate output of all nights (check also for crab to estimate fluctuations)
14# error emails in case no DB content / no QLA
15# calculate delay of QLA and send email if > 30 Min
16
17# run for more nights:
18# 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
19
20source `dirname $0`/../Sourcefile.sh
21printprocesslog "INFO starting $0"
22
23emailfrom=dorner@astro.uni-wuerzburg.de
24emailto=fact-online@lists.phys.ethz.ch
25
26if [ ! -e $flarealertspath ] || [ "$flarealertspath" == "" ]
27then
28 echo "flarealertspath "$flarealertspath" missing on "$HOSTNAME
29 printprocesslog "ERROR flarealertspath "$flarealertspath" missing on "$HOSTNAME
30 finish
31fi
32voeventpath="/home/fact/amon/flare_alerts/"
33
34logfile=$runlogpath"/FlareAlerts-"$datetime".log"
35date > $logfile
36
37# get date
38if [ "$1" != "" ]
39then
40 checkstring=`echo $1 | grep -E -o '^20[0-9][0-9][01][0-9][0-3][0-9]$'`
41 echo $checkstring
42 if [ "$checkstring" = "" ]
43 then
44 night=`date +%Y%m%d --date="-12 HOUR"`
45 else
46 night=$1
47 fi
48else
49 night=`date +%Y%m%d --date="-12 HOUR"`
50fi
51
52echo "Processing "$night >> $logfile
53
54
55# get sources for current night from DB (RunInfo)
56query="SELECT fSourceKey FROM RunInfo WHERE fNight="$night" AND fRunTypeKey=1 AND NOT ISNULL(fSourceKey) GROUP BY fSourceKey"
57sourcekeys=( `sendquery` )
58if [ ${#sourcekeys[@]} -eq 0 ]
59then
60 echo " No sources found for "$night >> $logfile
61 finish
62fi
63
64printprocesslog "INFO Checking the "${#sourcekeys[@]}" sourcekeys: "${sourcekeys[@]}
65
66# some stuff for queries:
67ontime="IF(ISNULL(fEffectiveOn), fOnTimeAfterCuts, TIME_TO_SEC(TIMEDIFF(fRunStop,fRunStart))*fEffectiveOn)"
68threshold="IF (ISNULL(fThresholdMinSet), fThresholdMedian, fThresholdMinSet)"
69cu="20.0"
70corr="1"
71# missing: corrected excessrates
72# missing: time-dependent CU
73
74
75# missing: excerr is NOT rate
76function get_query_nightly_binning()
77{
78 # query to get information from DB
79 query="SELECT fSourceKey AS num, "
80 query=$query"fNight AS night, MIN(fRunID) AS runmin, MAX(fRunID) AS runmax, "
81 query=$query"MIN(fRunStart) AS start, MAX(fRunStop) AS stop, "
82 query=$query"ROUND(SUM("$ontime")/3600.,1) AS ontime, "
83 query=$query"SUM(fNumSigEvts) AS sig, SUM(fNumBgEvts) AS bg, "
84 query=$query"ROUND(SUM(fNumBgEvts)/SUM("$ontime")*3600,1) AS bgrate, "
85 query=$query"SUM(fNumExcEvts) AS exc, "
86 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts)), 1) AS excerr, "
87 query=$query"ROUND(SUM(fNumExcEvts)/SUM("$ontime")*3600,1) AS excrate, "
88 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600, 1) AS excrateerr, "
89 query=$query"ROUND(SUM(fNumExcEvts*"$corr")/SUM("$ontime")*3600,1) as corexcrate, " # put here correction factor
90 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
91 query=$query"ROUND(LiMa(SUM(fNumSigEvts), SUM(fNumBgEvts)),1) AS signif, "
92 query=$query"ROUND(SUM(fNumExcEvts)/SUM("$ontime")*3600/"$cu",1) AS cu, " # make value time dependent
93 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600/"$cu", 1) AS cuerr, " # make value time dependent
94 query=$query"ROUND(SUM(fNumExcEvts*"$corr")/SUM("$ontime")*3600/"$cu",1) as corcu, " # make value time dependent # put here correction factor
95 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
96 query=$query"MIN(fZenithDistanceMin) as zdmin, MAX(fZenithDistanceMax) as zdmax, "
97 query=$query"MIN("$threshold") as thmin, MAX("$threshold") as thmax "
98 query=$query"FROM AnalysisResultsRunLP "
99 query=$query"LEFT JOIN RunInfo USING (fNight, fRunID) "
100 query=$query"WHERE fSourceKey="$sourcekey" AND fNight="$night" AND NOT ISNULL(fNumExcEvts) "
101 query=$query"GROUP BY fNight, fSourceKey "
102# query=$query"ORDER BY fRunStart "
103 query=$query"HAVING ontime > 0.5 " # at least 30 minutes of observation
104}
105
106function get_query_minute_binning()
107{
108 # set binning
109 if [ "$1" != "" ]
110 then
111 bin2=$1
112 else
113 bin2=$bin
114 fi
115 # query to get information from DB
116 query="SELECT MAX(o.b) AS num, "
117 query=$query"MIN(o.n) AS night, MIN(o.run) AS runmin, MAX(o.run) AS runmax, "
118 query=$query"MIN(o.start) AS start, MAX(o.stop) AS stop, "
119 query=$query"ROUND(SUM(o.ot)/60.,1) AS ontime, "
120 query=$query"SUM(o.sig) AS sig, SUM(o.bg) AS bg, "
121 query=$query"ROUND(SUM(o.bg)/SUM(o.ot)*3600,1) AS bgrate, "
122 query=$query"SUM(o.exc) AS exc, "
123 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg)), 1) AS excerr, "
124 query=$query"ROUND(SUM(o.exc)/SUM(o.ot)*3600,1) AS excrate, "
125 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg))/SUM(o.ot)*3600, 1) AS excrateerr, "
126 query=$query"ROUND(SUM(o.exccor)/SUM(o.ot)*3600,1) as corexcrate, "
127 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
128 query=$query"ROUND(LiMa(SUM(o.sig), SUM(o.bg)),1) AS signif, "
129 query=$query"ROUND(SUM(o.exc)/SUM(o.ot)*3600/o.cu,1) AS cu, "
130 query=$query"ROUND(ExcErr(SUM(o.sig), SUM(o.bg))/SUM(o.ot)*3600/o.cu, 1) AS cuerr, "
131 query=$query"ROUND(SUM(o.exccor)/SUM(o.ot)*3600/o.cu,1) as corcu, "
132 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
133 query=$query"MIN(o.zdmin) as zdmin, MAX(o.zdmax) as zdmax, "
134 query=$query"MIN(o.th) as thmin, MAX(o.th) as thmax "
135 query=$query"FROM ("
136 query=$query"SELECT "
137 query=$query"fRunID AS run, fNight AS n, "
138 query=$query"@ot:= "$ontime" AS ot, "
139 query=$query"fRunStart AS start, fRunStop AS stop, "
140 query=$query"fNumExcEvts AS exc, fNumBgEvts AS bg, fNumSigEvts AS sig, "
141 query=$query"fNumExcEvts*"$corr" AS exccor, " # put here correction factor
142 query=$query$cu" as cu, " # make value time dependent
143 query=$query"fZenithDistanceMin AS zdmin, fZenithDistanceMax AS zdmax, "
144 query=$query$threshold" AS th, "
145 query=$query"IF (@night=fNight AND FLOOR((@os+@ot)/"$bin2"./60.)<1, @bl, @bl := @bl + 1) AS b, "
146 query=$query"IF (@night=fNight AND FLOOR((@os+@ot)/"$bin2"./60.)<1, @os:=@os + @ot, @os := @ot) AS os, @"
147 query=$query"night :=fNight AS night FROM AnalysisResultsRunLP "
148 query=$query"LEFT JOIN RunInfo USING (fNight, fRunID) "
149 query=$query"CROSS JOIN (SELECT @night :=0, @ot :=0, @os :=0, @bl:=0) PARAMS "
150 query=$query"WHERE fSourceKey="$sourcekey" AND fNight="$night" AND NOT ISNULL(fNumExcEvts) "
151# if [ "$1" != "" ]
152# then
153# query=$query" AND fRunID <="${results[$num+3]}
154# fi
155 query=$query" ORDER BY fRunStart "
156# if [ "$1" != "" ]
157# then
158# query=$query"DESC"
159# fi
160 query=$query" ) o GROUP BY b HAVING ontime > "$bin2"*0.75 ORDER BY start "
161# if [ "$1" != "" ]
162# then
163# query=$query"DESC"
164# fi
165 #echo $query
166}
167
168function print_voevent_file()
169{
170 # put here voevent file
171 # make sure that it is written to amon-folder
172 echo '<?xml version="1.0" ?>'
173 echo '<voe:VOEvent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
174 echo 'xmlns:voe="http://www.ivoa.net/xml/VOEvent/v2.0"'
175 echo 'xsi:schemaLocation="http://www.ivoa.net/xml/VOEvent/v2.0 http://www.ivoa.net/xml/VOEvent/VOEvent-v2.0.xsd"'
176 echo ' version="2.0" role="test" ivorn="ivo://amon/fact_#5_3772_0">'
177 echo ' <Who>'
178 echo ' <AuthorIVORN>ivo://FACT</AuthorIVORN>'
179 echo ' <Date>'`date +%F\ %H:%M:%S`'</Date>'
180 echo ' </Who>'
181 # FACT = 5 in AMON
182 echo ' <What>'
183 echo ' <Param name="stream" dataType="float" value="5" ucd="meta.number" unit=" ">'
184 echo ' <Description>Stream number</Description>'
185 echo ' </Param>'
186 # event identifier
187 # nightly binning: night+sourcekey
188 # 20 min binning: night+source+?
189 echo ' <Param name="id" dataType="float" value="'${night}`printf %03d $runid`${sourcekey}'" ucd="meta.number" unit=" ">'
190 echo ' <Description>Id number</Description>'
191 echo ' </Param>'
192 # keep 0 for the beginning
193 # how to handle updates? e.g. in case of full disk and random processing
194 echo ' <Param name="rev" dataType="float" value="0" ucd="meta.number" unit=" ">'
195 echo ' <Description>Revision number</Description>'
196 echo ' </Param>'
197 # what to do with this? in IC it's number of neutrinos, but does number of gammas make sense?
198 # (excevts? depends on time range...)
199 echo ' <Param name="nevents" dataType="float" value="1" ucd="meta.number" unit=" ">'
200 echo ' <Description>Number of events</Description>'
201 echo ' </Param>'
202 # get time window (stop of last - start of first run)
203 # maybe: don't send alert if time window > xxx
204 echo ' <Param name="deltaT" dataType="float" value="0.0" ucd="time.duration" unit="s">'
205 echo ' <Description>Time window of the burst</Description>'
206 echo ' </Param>'
207 # probably 0
208 echo ' <Param name="sigmaT" dataType="float" value="0.0" ucd="time" unit="s">'
209 echo ' <Description>Uncertainty of the time window</Description>'
210 echo ' </Param>'
211 # FPR - to be calculated
212 echo ' <Param name="false_pos" dataType="float" value="-1" ucd="stat.probability" unit="s-1.sr-1 ">'
213 echo ' <Description>False positive rate</Description>'
214 echo ' </Param>'
215 # some significane value - how significant is the alert?
216 # to be discussed and calculated
217 echo ' <Param name="pvalue" dataType="float" value="1.0" ucd="stat.probability" unit=" ">'
218 echo ' <Description>Pvalue</Description>'
219 echo ' </Param>'
220 # use source RA/Dec? but then naming doesn't fit
221 # -> leave empty for the moment
222 echo ' <Param name="point_RA" dataType="float" value="-1.0" ucd="os.eq.ra" unit="deg">'
223 echo ' <Description>Pointing RA</Description>'
224 echo ' </Param>'
225 echo ' <Param name="point_dec" dataType="float" value="-1.0" ucd="os.eq.dec" unit="deg">'
226 echo ' <Description>Pointing Dec</Description>'
227 echo ' </Param>'
228 # shape of psf - to be determined - fit thetaplot? for crab or mc
229 echo ' <Param name="psf_type" dataType="string" value="fisher" ucd="meta.code.multip" unit=" ">'
230 echo ' <Description>Type of psf (skymap, fisher, kent, king)</Description>'
231 echo ' </Param>'
232 echo ' <Group name="aux_params">'
233 # which other information to add?
234 echo ' <Param name="zenith" dataType="float" value="0.0" ucd="os.lc.ze" unit="deg"/>'
235 echo ' <Param name="xyz" dataType="float" value="0.0" ucd="os.lc.ze" unit="xyz"/>'
236 echo ' </Group>'
237 echo ' </What>'
238 echo ' <WhereWhen>'
239 echo ' <ObsDataLocation>'
240 echo ' <ObservatoryLocation>'
241 echo ' <AstroCoordSystem id="UTC-GEOD-TOPO"/>'
242 echo ' <AstroCoords coord_system_id="UTC-GEOD-TOPO">'
243 echo ' <Position3D unit="deg-deg-m">'
244 echo ' <Name1>longitude</Name1>'
245 echo ' <Name2>latitude</Name2>'
246 echo ' <Name3>elevation</Name3>'
247 echo ' <Value3>'
248 echo ' <C1>-17.88</C1>'
249 echo ' <C2>28.76</C2>'
250 echo ' <C3>2200</C3>'
251 echo ' </Value3>'
252 echo ' </Position3D>'
253 echo ' </AstroCoords>'
254 echo ' </ObservatoryLocation>'
255 echo ' <ObservationLocation>'
256 echo ' <AstroCoordSystem id="UTC-ICRS-TOPO"/>'
257 echo ' <AstroCoords coord_system_id="UTC-ICRS-TOPO">'
258 echo ' <Time unit="s">'
259 echo ' <TimeInstant>'
260 # python needs the .0 in the time-format - as I don't have the time more accurately here, I put .0
261 echo ' <ISOTime>'${results[$num+4]}' '${results[$num+5]}'.0</ISOTime>'
262 echo ' </TimeInstant>'
263 echo ' </Time>'
264 echo ' <Position2D unit="deg-deg">'
265 echo ' <Name1>RA</Name1>'
266 echo ' <Name2>Dec</Name2>'
267 echo ' <Value2>'
268 echo ' <C1>'${sourceinfo[2]}'</C1>' # RA in deg
269 echo ' <C2>'${sourceinfo[0]}'</C2>' # decl in deg
270 echo ' </Value2>'
271 echo ' <Error2Radius>0.1</Error2Radius>' # PSF of FACT
272 echo ' </Position2D>'
273 echo ' </AstroCoords>'
274 echo ' </ObservationLocation>'
275 echo ' </ObsDataLocation>'
276 echo ' </WhereWhen>'
277 echo ' <Description>FACT flare event information</Description>'
278 echo '</voe:VOEvent>'
279
280}
281
282function evaluate_result()
283{
284 oldexc=0
285 exc=0
286 excold=0
287 slope=0
288 slopeprev=0
289 i=0
290 # be careful with start and stop (space inbetween) -> 27 columns instead of 25
291 while [ 0 -lt 1 ]
292 do
293 trigger=0
294 num=`echo "$i * 27" | bc -l`
295 if [ "${results[$num]}" = "" ]
296 then
297 break
298 fi
299 #night=${results[$num+1]}
300 runid=${results[$num+2]}
301 sig=${results[$num+18]} # significance
302 #exc=${results[$num+14]} # excrate
303 excold=$exc
304 exc=${results[$num+19]} # excrate in CU
305
306 if [ "$onlyifhigher" = "yes" ]
307 then
308 higher=` echo " $exc > $oldexc " | bc `
309 if [ $higher -eq 1 ]
310 then
311 # keep old value
312 oldexc=$exc
313 fi
314 fi
315
316 if [ "$bin" = "" ]
317 then
318 echo " ontime: "${results[$num+8]}" h" >> $logfile
319 fi
320
321 # fast rise/decay trigger
322 if [ $triggertype -eq 3 ]
323 then
324 slopeprev=$slope
325 sigprev=$sig
326 if [ "$excold" = "0" ]
327 then
328 slope=0
329 else
330 slope=`echo " scale=1; ( $exc - $excold ) / ( $bin / 60. ) " | bc -l `
331 fi
332 get_query_minute_binning 60
333 #echo $query
334 results2=( `sendquery` )
335 # need to check last hour backward
336 if [ "${results2[19]}" = "" ]
337 then
338 slope60=0
339 sig60=0
340 else
341 sig60=${results2[18]} # significance 1h
342 if [ "${results2[27+19]}" = "" ]
343 then
344 # maybe treat this case differently
345 slope60=${results2[19]}
346 else
347 slope60=`echo " ${results2[19]} - ${results2[27+19]} " | bc -l` # ie /1h
348 fi
349 fi
350 #echo "exc "$exc" excold "$excold
351 #echo "slope "$slope" prev "$slopeprev" sig "$sig" prevsig "$sigprev" sig60 "$sig60" slope60 "$slope60
352 fi
353 # missing: probably one should check also 20 min binning (s example 20170103)
354 # missing: check on still available observation time
355
356 echo " "$i" "${results[$num+2]}"-"${results[$num+3]}"["${results[$num+8]}"] "$exc" "$sig >> $logfile
357
358 case $triggertype in
359 1) #echo "std trigger: criteria ( $exc >= $exclimit && $sig >= $siglimit && $higher )"
360 trigger=`echo " $exc >= $exclimit && $sig >= $siglimit && $higher " | bc -l`
361 ;;
362 2) #echo "magic 501: criteria ( $exc >= $exclimit && $sig >= $siglimit && $higher )"
363 trigger=`echo " $exc >= $exclimit && $sig >= $siglimit && $higher " | bc -l`
364 ;;
365 3) #echo "magic fast rise/decay"
366 #trigger=`echo " $slope >= $slopelimit && $slopeprev >= $slopelimit && $slope60 >= $slopelimit && $sig >= $siglimit " | bc -l`
367 trigger=`echo " ( ( $slope >= $slopelimit && $slopeprev >= $slopelimit && $slope60 >= $slopelimit ) || ( - $slope >= $slopelimit && - $slopeprev >= $slopelimit && - $slope60 >= $slopelimit ) ) && $sig >= $siglimit " | bc -l`
368 ;;
369 4) #echo "std trigger: criteria ( $exc >= $exclimit && $sig >= $siglimit && $higher )"
370 trigger=`echo " $exc >= $exclimit && $sig >= $siglimit && $higher " | bc -l`
371 ;;
372 *) echo $triggertype" not yet implemented" >> $logfile
373 ;;
374 esac
375
376 # missing: maybe use $donetriggerfile for all cases - update of nightly case can be triggered differently
377 # missing: adapt triggerfilename for type 3 (slopelimit)
378 if [ $trigger -eq 1 ]
379 then
380 # do whatever to be done to trigger
381 # - send email/sms / call
382 # - create amon file
383 # - prepare email for alert
384 # - entry in DB
385
386 if [ "$bin" = "" ]
387 then
388 # nightly file: simply overwrite, but do not send trigger again
389 # or check value if it's increasing?
390 # missing: get information from previous trigger + interpret
391 triggerfile=$flarealertspath"/"$night"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
392 else
393 # smaller binning: check if trigger is identical
394 triggerfile=$flarealertspath"/"$night"_"`printf %03d $runid`"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
395 donetriggerfile=$triggerfile".done"
396 #ls $donetriggerfile
397 # write new file only if old files do not agree
398 if [ "$donetriggerfile" != "" ] && [ -e $donetriggerfile ]
399 then
400 diff $donetriggerfile $triggerfile >/dev/null
401 checkstatus=`echo $?`
402 if [ $checkstatus -eq 0 ]
403 then
404 echo " alert already done "$donetriggerfile >> $logfile
405 i=`echo $i +1 | bc -l`
406 continue
407 fi
408 fi
409 if [ -e $triggerfile ]
410 then
411 mv $triggerfile $donetriggerfile
412 fi
413 fi
414 #echo $night"_"$runid" "$sourcekey" -> "$triggerfile
415 echo " writing "$triggerfile >> $logfile
416 touch $triggerfile
417 echo "Trigger found: " > $triggerfile
418 echo "-------------- " >> $triggerfile
419 echo " type: "$triggertype >> $triggerfile
420 echo " excess limit: "$exclimit" evts/h" >> $triggerfile
421 echo " significance limit: "$siglimit" sigma" >> $triggerfile
422 if [ "$bin" = "" ]
423 then
424 echo " nightly binning " >> $triggerfile
425 else
426 echo " binning: "$bin" min" >> $triggerfile
427 fi
428 echo "Summary of flare event: " >> $triggerfile
429 echo "----------------------- " >> $triggerfile
430 echo " source: "$sourcename >> $triggerfile
431 echo " night: "${results[$num+1]} >> $triggerfile
432 echo " runs: "${results[$num+2]}" - "${results[$num+3]} >> $triggerfile
433 echo " start: "${results[$num+4]}" "${results[$num+5]}" UTC" >> $triggerfile
434 echo " stop: "${results[$num+6]}" "${results[$num+7]}" UTC" >> $triggerfile
435 if [ "$bin" = "" ]
436 then
437 echo " ontime: "${results[$num+8]}" h" >> $triggerfile
438 else
439 echo " ontime: "${results[$num+8]}" min" >> $triggerfile
440 fi
441 #echo " ontime: "`echo "scale=1; ${results[$num+8]} / 60. " | bc -l`" min" #scale doesn't round properly
442 echo " signal: "${results[$num+9]}" evts" >> $triggerfile
443 echo " background: "${results[$num+10]}" evts" >> $triggerfile
444 echo " bgrate: "${results[$num+11]}" evts/h" >> $triggerfile
445 echo " exc: "${results[$num+12]}" +- "${results[$num+13]}" evts" >> $triggerfile
446 echo " excrate: "${results[$num+14]}" +- "${results[$num+15]}" evts/h" >> $triggerfile
447 echo " corr. excrate: "${results[$num+16]}" - "${results[$num+17]}" evts/h" >> $triggerfile
448 echo " significance: "${results[$num+18]}" sigma" >> $triggerfile
449 echo " cu: "${results[$num+19]}" +- "${results[$num+20]}" CU" >> $triggerfile
450 echo " corr. cu: "${results[$num+21]}" +- "${results[$num+22]}" CU" >> $triggerfile
451 echo " zd: "${results[$num+23]}" - "${results[$num+24]}" degree" >> $triggerfile
452 echo " th: "${results[$num+25]}" - "${results[$num+26]}" DAC counts" >> $triggerfile
453 # additional information fast rise/decay trigger
454 if [ $triggertype -eq 3 ]
455 then
456 echo "Flux doubling/halfing times: " >> $triggerfile
457 echo "---------------------------- " >> $triggerfile
458 echo " excess: "$exc >> $triggerfile
459 echo " excess old: "$excold >> $triggerfile
460 echo " significance: "$sig >> $triggerfile
461 echo " significance old: "$sigprev >> $triggerfile
462 echo " slope: "$excold >> $triggerfile
463 echo " slope old: "$slopeold >> $triggerfile
464 echo " excess 60 min: "${results2[19]} >> $triggerfile
465 echo " excess 60 min old: "${results2[27+19]} >> $triggerfile
466 echo " slope 60 min: "$slope60 >> $triggerfile
467 echo " significance 60 min: "$sig60 >> $triggerfile
468 fi
469
470 if [ "$donetriggerfile" != "" ] && [ -e $donetriggerfile ]
471 then
472 diff $donetriggerfile $triggerfile >/dev/null
473 checkstatus=`echo $?`
474 if [ $checkstatus -gt 0 ]
475 then
476 # keep history of non-sent triggers
477 donetriggerfile2=$donetriggerfile"-changed-"`date +%Y%m%d%H%M%S`
478 cp $donetriggerfile $donetriggerfile2
479 fi
480 fi
481 # missing: get summary of whole observation
482
483 # send email only of $donetriggerfile doesn't exists
484 if ! [ -e $donetriggerfile ]
485 then
486 # AMON case: create VOEvent-File
487 if [ $triggertype -eq 4 ]
488 then
489 voeventfile=$voeventpath"/"`basename $triggerfile`".xml"
490 # missing: check for archive file
491 # if exist -> do revision
492 echo "creating "$voeventfile
493 echo "Creating "$voeventfile >> $logfile
494 #print_voevent_file
495 print_voevent_file > $voeventfile
496 # in amon-case no email needs to be sent
497 continue
498 fi
499
500 cat $triggerfile | mail -s 'test flare alert ' -b $emailfrom -r $emailfrom $emailto
501 fi
502
503 #cat $triggerfile | mail -s "test flare alert for $sourcename " $emailto
504 # that's also the cases for making a call
505
506 fi
507
508 # counter
509 i=`echo $i +1 | bc -l`
510 done
511
512 echo " found "$i" data point(s)." >> $logfile
513 echo "" >> $logfile
514}
515
516get_average_flux()
517{
518 # query average flux from DB
519 query="SELECT ROUND(SUM(fNumExcEvts)/SUM("$ontime")*3600,1) as excrate, "
520 query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600, 1) AS excerr, "
521 query=$query"ROUND(SUM("$ontime")/3600.,1) AS ontime "
522 query=$query"FROM AnalysisResultsRunLP "
523 query=$query"LEFT JOIN RunInfo USING (fNight, fRunID) "
524 if [ "$1" = "" ]
525 then
526 query=$query"WHERE fSourceKey="$sourcekey" AND NOT ISNULL(fNumExcEvts) "
527 else
528 query=$query"WHERE fSourceKey="$sourcekey" AND fNight BETWEEN "$1" AND "$2" AND NOT ISNULL(fNumExcEvts) "
529 fi
530 query=$query"GROUP BY fSourceKey "
531 sendquery
532 # missing - get in CU to correct for fluctuations of CU
533}
534
535
536
537# main part of the script
538
539# missing: check if twistd client is running
540# + check if there are remaining files in the to-send-folder of amon
541# -> send email if amon connection has problem
542
543
544for sourcekey in ${sourcekeys[@]}
545do
546 query="SELECT fSourceName FROM Source WHERE fSourceKey="$sourcekey
547 sourcename=`sendquery` #do not combine this with other source info as sourcename can have spaces
548
549 # todo: what about data check ?
550 # should avg include current night?
551 total=( `get_average_flux` )
552 month=( `get_average_flux \`date -d $night' - 1 MONTH' +%Y%m%d\` $night` )
553 year=( `get_average_flux \`date -d $night' - 1 YEAR' +%Y%m%d\` $night` )
554 avgflux=${total[0]}
555 avgfluxmonth=${month[0]}
556 avgfluxyear=${year[0]}
557 error=${total[1]}
558 errormonth=${month[1]}
559 erroryear=${year[1]}
560
561 # getting some information on the source
562 query="SELECT fDeclination, fRightAscension, fRightAscension/24.*15 FROM Source WHERE fSourceKey="$sourcekey
563 sourceinfo=( `sendquery` )
564
565 # ignore Crab
566 if [ $sourcekey -eq 5 ]
567 then
568 continue
569 fi
570 printprocesslog "INFO Evaluation for $sourcename ... "
571 echo "Evaluation for $sourcename ... " >> $logfile
572 echo " average fluxes: "$avgfluxmonth"+-"$errormonth" "$avgfluxyear"+-"$erroryear" "$avgflux"+-"$error >> $logfile
573
574 # missing: get limits from DB (structure needs to be defined)
575
576 # triggers in the frame of the MoU in the gamma-ray community
577 triggertype=1
578 # limits
579 siglimit=3.0 # sigma
580 #if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ]
581 if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 5 ]
582 then
583 exclimit=3.0 # CU
584 else
585 exclimit=0.5 # CU
586 fi
587 # only if rate goes even higher, we have to react
588 onlyifhigher="yes"
589 higher=1
590
591 printprocesslog "INFO checking for [General gamma-ray MoU]" >> $logfile
592 echo "[General gamma-ray MoU]" >> $logfile
593 echo " nightly binning..." >> $logfile
594 # checking nightly binning
595 bin=
596 get_query_nightly_binning
597 results=( `sendquery` )
598 evaluate_result
599
600 # 20 min binning
601 bin=20
602 echo " "$bin" min binning..." >> $logfile
603 get_query_minute_binning $bin
604 results=( `sendquery` )
605 evaluate_result
606
607 # triggers to MAGIC
608 # Mrk 501 proposal
609 triggertype=2
610 if [ $sourcekey -eq 2 ]
611 then
612 siglimit=3.0
613 exclimit=2.0 # cu
614 printprocesslog "INFO checking for [Trigger to MAGIC 501 proposal]" >> $logfile
615 echo "[Trigger to MAGIC 501 proposal]" >> $logfile
616 # checking nightly binning
617 bin=
618 echo " nightly binning..." >> $logfile
619 get_query_nightly_binning
620 results=( `sendquery` )
621 evaluate_result
622 # 20 min binning
623 bin=20
624 echo " "$bin" min binning..." >> $logfile
625 get_query_minute_binning $bin
626 results=( `sendquery` )
627 evaluate_result
628 fi
629 # Mother of ToO - fast rise/decay
630 # sources: Mrk 421, Mrk 501, 2344, 1959
631 triggertype=3
632 #if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ] || [ $sourcekey -eq 5 ] # for testing
633 if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ]
634 then
635 printprocesslog "INFO checking for [Trigger to MAGIC - fast rise/decay]" >> $logfile
636 echo "[Trigger to MAGIC - fast rise/decay]" >> $logfile
637 ## keep thresholds low (or do not use in evaluation)
638 #siglimit=2.0
639 #exclimit=0.5
640 # limits in slope
641 slopelimit=1.0 # 1CU/h
642 siglimit=3.0 # 1 sigma in 1 hour
643 # binning
644 bin=30
645 echo " "$bin" min binning..." >> $logfile
646 get_query_minute_binning $bin
647 results=( `sendquery` )
648 evaluate_result
649 fi
650
651 # AMON - automatic triggers using VOEvent files
652 triggertype=4
653 printprocesslog "INFO checking for [Trigger to AMON]" >> $logfile
654 echo "[Trigger to AMON]" >> $logfile
655 echo " details still to be defined" >> $logfile
656 # missing: trigger limits and binning still to be defined
657 # x times above average + significance limit ?
658 # sub-threshold?
659 # FP-rate to be calculated
660 # use for the moment 0.5 CU and 3 sigma
661 siglimit=3.0
662 exclimit=0.5
663 onlyifhigher="no"
664 # checking nightly binning
665 bin=
666 echo " nightly binning..." >> $logfile
667 get_query_nightly_binning
668 results=( `sendquery` )
669 evaluate_result
670 bin=20
671 echo " "$bin" min binning..." >> $logfile
672 get_query_minute_binning $bin
673 results=( `sendquery` )
674 evaluate_result
675
676 echo "" >> $logfile
677 echo "" >> $logfile
678done
679
680finish
681
682# for archival testing:
683for (( i=0; i < 100 ; i++))
684do
685 date=`date --date="-${i}days" +%Y%m%d`
686 /home/fact/SW.automatic.processing/DataCheck/QuickLook/FlareAlerts.sh $date
687done
688
689
Note: See TracBrowser for help on using the repository browser.