Index: trunk/DataCheck/QuickLook/FlareAlerts.sh
===================================================================
--- trunk/DataCheck/QuickLook/FlareAlerts.sh	(revision 18791)
+++ trunk/DataCheck/QuickLook/FlareAlerts.sh	(revision 18792)
@@ -72,4 +72,6 @@
 # missing: time-dependent CU
 
+
+# missing: excerr is NOT rate
 function get_query_nightly_binning()
 {
@@ -506,4 +508,24 @@
 }
 
+get_average_flux()
+{
+   # query average flux from DB
+   query="SELECT ROUND(SUM(fNumExcEvts)/SUM("$ontime")*3600,1) as excrate,  "
+   query=$query"ROUND(ExcErr(SUM(fNumSigEvts), SUM(fNumBgEvts))/SUM("$ontime")*3600, 1) AS excerr, "
+   query=$query"ROUND(SUM("$ontime")/3600.,1) AS ontime "
+   query=$query"FROM AnalysisResultsRunLP "
+   query=$query"LEFT JOIN RunInfo USING (fNight, fRunID) "
+   if [ "$1" = "" ]
+   then 
+      query=$query"WHERE fSourceKey="$sourcekey" AND NOT ISNULL(fNumExcEvts) "
+   else
+      query=$query"WHERE fSourceKey="$sourcekey" AND fNight BETWEEN "$1" AND "$2" AND NOT ISNULL(fNumExcEvts) "
+   fi
+   query=$query"GROUP BY fSourceKey "
+   sendquery
+   # missing - get in CU to correct for fluctuations of CU
+}
+
+
 
 # main part of the script
@@ -519,4 +541,16 @@
    sourcename=`sendquery` #do not combine this with other source info as sourcename can have spaces
    
+   # todo: what about data check ? 
+   # should avg include current night?
+   total=( `get_average_flux` )
+   month=( `get_average_flux \`date -d $night' - 1 MONTH' +%Y%m%d\` $night` )
+   year=( `get_average_flux \`date -d $night' - 1 YEAR' +%Y%m%d\` $night` )
+   avgflux=${total[0]}
+   avgfluxmonth=${month[0]}
+   avgfluxyear=${year[0]}
+   error=${total[1]}
+   errormonth=${month[1]}
+   erroryear=${year[1]}
+   
    # getting some information on the source
    query="SELECT fDeclination, fRightAscension, fRightAscension/24.*15 FROM Source WHERE fSourceKey="$sourcekey
@@ -530,4 +564,5 @@
    printprocesslog "INFO Evaluation for $sourcename ... "
    echo "Evaluation for $sourcename ... " >> $logfile
+   echo " average fluxes: "$avgfluxmonth"+-"$errormonth" "$avgfluxyear"+-"$erroryear" "$avgflux"+-"$error
    
    # missing: get limits from DB (structure needs to be defined)
@@ -573,7 +608,7 @@
       printprocesslog "INFO checking for [Trigger to MAGIC 501 proposal]" >> $logfile
       echo "[Trigger to MAGIC 501 proposal]" >> $logfile
-      echo " nightly binning..." >> $logfile
       # checking nightly binning
       bin=
+      echo " nightly binning..." >> $logfile
       get_query_nightly_binning
       results=( `sendquery` )
@@ -589,6 +624,6 @@
    # sources: Mrk 421, Mrk 501, 2344, 1959
    triggertype=3
-   #if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ]
-   if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ] || [ $sourcekey -eq 5 ]
+   #if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ] || [ $sourcekey -eq 5 ] # for testing
+   if [ $sourcekey -eq 1 ] || [ $sourcekey -eq 2 ] || [ $sourcekey -eq 3 ] || [ $sourcekey -eq 7 ]
    then 
       printprocesslog "INFO checking for [Trigger to MAGIC - fast rise/decay]" >> $logfile
@@ -608,18 +643,24 @@
    fi
    
-   # Triggers to AMON
+   # AMON - automatic triggers using VOEvent files
    triggertype=4
    printprocesslog "INFO checking for [Trigger to AMON]" >> $logfile
    echo "[Trigger to AMON]" >> $logfile
-   echo " still to be defined" >> $logfile
+   echo " details still to be defined" >> $logfile
    # missing: trigger limits and binning still to be defined
-   # also nightly? 
-   # sub-threshold-triggers?
-   # FP-rate?
-   siglimit=2.0
+   #   x times above average + significance limit ? 
+   #   sub-threshold? 
+   # FP-rate to be calculated
+   # use for the moment 0.5 CU and 3 sigma 
+   siglimit=3.0
    exclimit=0.5
    onlyifhigher="no"
    bin=20
-   #echo "AMON"
+   # checking nightly binning
+   bin=
+   echo " nightly binning..." >> $logfile
+   get_query_nightly_binning
+   results=( `sendquery` )
+   evaluate_result
    echo " "$bin" min binning..." >> $logfile
    get_query_minute_binning
