Index: /trunk/DataCheck/QuickLook/FlareAlerts.sh
===================================================================
--- /trunk/DataCheck/QuickLook/FlareAlerts.sh	(revision 18782)
+++ /trunk/DataCheck/QuickLook/FlareAlerts.sh	(revision 18783)
@@ -48,4 +48,11 @@
 query="SELECT fSourceKey FROM RunInfo WHERE fNight="$night" AND fRunTypeKey=1 AND NOT ISNULL(fSourceKey) GROUP BY fSourceKey"
 sourcekeys=( `sendquery` )
+if [ ${#sourcekeys[@]} -eq 0 ]
+then
+   echo "no sources found for "$night
+   finish
+fi
+
+printprocesslog "INFO Checking the "${#sourcekeys[@]}" sourcekeys: "${sourcekeys[@]}
 
 # some stuff for queries: 
@@ -149,4 +156,118 @@
 }
 
+function print_voevent_file()
+{
+   # put here voevent file
+   # make sure that it is written to amon-folder
+   echo '<?xml version="1.0" ?>'
+   echo '<voe:VOEvent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
+   echo 'xmlns:voe="http://www.ivoa.net/xml/VOEvent/v2.0"'
+   echo 'xsi:schemaLocation="http://www.ivoa.net/xml/VOEvent/v2.0 http://www.ivoa.net/xml/VOEvent/VOEvent-v2.0.xsd"'
+   echo ' version="2.0" role="test" ivorn="ivo://amon/fact_#5_3772_0">'
+   echo '    <Who>'
+   echo '        <AuthorIVORN>ivo://FACT</AuthorIVORN>'
+   echo '        <Date>'`date +%F\ %H:%M:%S`'</Date>'
+   echo '    </Who>'
+   # FACT = 5 in AMON
+   echo '    <What>'
+   echo '        <Param name="stream" dataType="float" value="5" ucd="meta.number" unit=" ">'
+   echo '            <Description>Stream number</Description>'
+   echo '        </Param>'
+   # event identifier
+   # nightly binning: night+sourcekey
+   # 20 min binning: night+source+?
+   echo '        <Param name="id" dataType="float" value="3772" ucd="meta.number" unit=" ">'
+   echo '            <Description>Id number</Description>'
+   echo '        </Param>'
+   # keep 0 for the beginning
+   # how to handle updates? e.g. in case of full disk and random processing
+   echo '        <Param name="rev" dataType="float" value="0" ucd="meta.number" unit=" ">'
+   echo '            <Description>Revision number</Description>'
+   echo '        </Param>'
+   # what to do with this? in IC it's number of neutrinos, but does number of gammas make sense? 
+   #  (excevts? depends on time range...)
+   echo '        <Param name="nevents" dataType="float" value="1" ucd="meta.number" unit=" ">'
+   echo '            <Description>Number of events</Description>'
+   echo '        </Param>'
+   # get time window (stop of last - start of first run)
+   # maybe: don't send alert if time window > xxx
+   echo '        <Param name="deltaT" dataType="float" value="0.0" ucd="time.duration" unit="s">'
+   echo '            <Description>Time window of the burst</Description>'
+   echo '        </Param>'
+   # probably 0
+   echo '        <Param name="sigmaT" dataType="float" value="0.0" ucd="time" unit="s">'
+   echo '            <Description>Uncertainty of the time window</Description>'
+   echo '        </Param>'
+   # FPR - to be calculated
+   echo '        <Param name="false_pos" dataType="float" value="-1" ucd="stat.probability" unit="s-1.sr-1 ">'
+   echo '            <Description>False positive rate</Description>'
+   echo '        </Param>'
+   # some significane value - how significant is the alert?
+   # to be discussed and calculated
+   echo '        <Param name="pvalue" dataType="float" value="1.0" ucd="stat.probability" unit=" ">'
+   echo '            <Description>Pvalue</Description>'
+   echo '        </Param>'
+   # use source RA/Dec? but then naming doesn't fit
+   # -> leave empty for the moment
+   echo '        <Param name="point_RA" dataType="float" value="-1.0" ucd="os.eq.ra" unit="deg">'
+   echo '            <Description>Pointing RA</Description>'
+   echo '        </Param>'
+   echo '        <Param name="point_dec" dataType="float" value="-1.0" ucd="os.eq.dec" unit="deg">'
+   echo '            <Description>Pointing Dec</Description>'
+   echo '        </Param>'
+   # shape of psf - to be determined - fit thetaplot? for crab or mc
+   echo '        <Param name="psf_type" dataType="string" value="fisher" ucd="meta.code.multip" unit=" ">'
+   echo '            <Description>Type of psf (skymap, fisher, kent, king)</Description>'
+   echo '        </Param>'
+   echo '        <Group name="aux_params">'
+   # which other information to add? 
+   echo '            <Param name="zenith" dataType="float" value="0.0" ucd="os.lc.ze" unit="deg"/>'
+   echo '            <Param name="xyz" dataType="float" value="0.0" ucd="os.lc.ze" unit="xyz"/>'
+   echo '        </Group>'
+   echo '    </What>'
+   echo '    <WhereWhen>'
+   echo '        <ObsDataLocation>'
+   echo '            <ObservatoryLocation>'
+   echo '                <AstroCoordSystem id="UTC-GEOD-TOPO"/>'
+   echo '                <AstroCoords coord_system_id="UTC-GEOD-TOPO">'
+   echo '                    <Position3D unit="deg-deg-m">'
+   echo '                        <Name1>longitude</Name1>'
+   echo '                        <Name2>latitude</Name2>'
+   echo '                        <Name3>elevation</Name3>'
+   echo '                        <Value3>'
+   echo '                            <C1>-17.88</C1>'
+   echo '                            <C2>28.76</C2>'
+   echo '                            <C3>2200</C3>'
+   echo '                        </Value3>'
+   echo '                    </Position3D>'
+   echo '                </AstroCoords>'
+   echo '            </ObservatoryLocation>'
+   echo '            <ObservationLocation>'
+   echo '                <AstroCoordSystem id="UTC-ICRS-TOPO"/>'
+   echo '                <AstroCoords coord_system_id="UTC-ICRS-TOPO">'
+   echo '                    <Time unit="s">'
+   echo '                        <TimeInstant>'
+   # python needs the .0 in the time-format - as I don't have the time more accurately here, I put .0
+   echo '                            <ISOTime>'${results[$num+4]}' '${results[$num+5]}'.0</ISOTime>'
+   echo '                        </TimeInstant>'
+   echo '                    </Time>'
+   echo '                    <Position2D unit="deg-deg">'
+   echo '                        <Name1>RA</Name1>'
+   echo '                        <Name2>Dec</Name2>'
+   echo '                        <Value2>'
+   echo '                            <C1>'${sourceinfo[2]}'</C1>' # RA in deg
+   echo '                            <C2>'${sourceinfo[0]}'</C2>' # decl in deg
+   echo '                        </Value2>'
+   echo '                        <Error2Radius>0.1</Error2Radius>' # PSF of FACT
+   echo '                    </Position2D>'
+   echo '                </AstroCoords>'
+   echo '            </ObservationLocation>'
+   echo '        </ObsDataLocation>'
+   echo '    </WhereWhen>'
+   echo '    <Description>FACT flare event information</Description>'
+   echo '</voe:VOEvent>'
+   
+}
+
 function evaluate_result()
 {
@@ -236,4 +357,7 @@
               trigger=`echo " ( ( $slope >= $slopelimit && $slopeprev >= $slopelimit && $slope60 >= $slopelimit ) || ( - $slope >= $slopelimit && - $slopeprev >= $slopelimit && - $slope60 >= $slopelimit  ) ) && $sig >= $siglimit " | bc -l`
               ;;
+         4)   #echo "std trigger: criteria ( $exc >= $exclimit && $sig >= $siglimit && $higher )"
+              trigger=`echo " $exc >= $exclimit && $sig >= $siglimit && $higher " | bc -l`
+              ;;
          *)   echo $triggertype" not yet implemented" >> $logfile
               ;;
@@ -255,8 +379,8 @@
             # or check value if it's increasing? 
             # missing: get information from previous trigger + interpret
-            triggerfile="/home/fact/flare.alerts/"$night"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
+            triggerfile=$flarealertspath"/"$night"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
          else
             # smaller binning: check if trigger is identical
-            triggerfile="/home/fact/flare.alerts/"$night"_"`printf %03d $runid`"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
+            triggerfile=$flarealertspath"/"$night"_"`printf %03d $runid`"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype
             donetriggerfile=$triggerfile".done"
             #ls $donetriggerfile
@@ -346,4 +470,13 @@
          
          # missing AMON cases - create VOEvent-File
+         if [ $triggertype -eq 4 ]
+         then
+            echo "amon"
+            voeventfile="/home/dorner/amon/test/"$night"_"`printf %03d $runid`"-"$exclimit"_"$siglimit"-source"$sourcekey".trigger"$triggertype".xml"
+            echo $voeventfile
+            echo ${sourceinfo[@]}
+            #print_voevent_file
+            print_voevent_file > $voeventfile
+         fi
 
          # send email only of $donetriggerfile doesn't exists
@@ -366,8 +499,20 @@
 }
 
+
+# main part of the script
+
+# missing: check if twistd client is running 
+#   + check if there are remaining files in the to-send-folder of amon
+# -> send email if amon connection has problem
+
+
 for sourcekey in ${sourcekeys[@]}
 do
    query="SELECT fSourceName FROM Source WHERE fSourceKey="$sourcekey
-   sourcename=`sendquery`
+   sourcename=`sendquery` #do not combine this with other source info as sourcename can have spaces
+   
+   # getting some information on the source
+   query="SELECT fDeclination, fRightAscension, fRightAscension/24.*15 FROM Source WHERE fSourceKey="$sourcekey
+   sourceinfo=( `sendquery` )
 
    # ignore Crab
@@ -469,8 +614,13 @@
    onlyifhigher="no"
    bin=20
+   echo "AMON"
+   echo " "$bin" min binning..." >> $logfile
+   get_query_minute_binning
+   results=( `sendquery` )
+   evaluate_result
    
    echo "" >> $logfile
    echo "" >> $logfile
- done
+done
 
 finish 
