SkyAlert.org >> View Streams | View Events | View Alerts

Sample Annotator for Skyalert

This one simply computes galactic coordinates. It is for illustration purposes.
#================================================================
#   loaders/actions/GalacticCoordinates.py
#================================================================

############################################################################################
#  Skyalaert annotator: ** GalacticCoordinates **
#  This is an illustrative example of how to code an annotator for Skyalert.
#  Add this as the result of a trigger rule: set "actionType" to "GalacticCoordinates"
############################################################################################
from VOEventNet.common import login
from VOEventNet.common import eventFactory
import math

# The implementer of an annotator must provide this "run" function. Arguments:
#    rule: is a django "rule" (aka "alert", "trigger") which caused this function to run
#    triggerEvent: the fresh event that is the reason the trigger was run.
#    pf: portfolio is a group of events with a "first" event. 
def run(rule, triggerEvent, pf, log):
  print>>log, "Welcome to the GalacticCoordinates annotator"

# This is the stream that will carry the output of this annotator
  streamIVORN = "ivo://testing/galeq"

# Get a blank event which is an instance of the given stream
#   copy the ordinal and role from the trigger
  newEvent = eventFactory.factory(streamIVORN, \
    triggerEvent.stream.ordinal, \
    role='test')
#    role=triggerEvent.role)

# Cite the first event of the portfolio
  newEvent.addCitation(pf.first.globalIVORN)

# Get the parameter templateas, and fill in some values
  e = newEvent.paramDictionary()

# Copy the positon of the trigger
  e["RA"]  = RA  = triggerEvent.RA
  e["Dec"] = Dec = triggerEvent.Dec

# Anything to say about what we are making?
  e["description"] = "computation of galactic coordinates"

# Here is the business logic
  (Glong, Glat) = eq_to_gal(RA, Dec)
#  print>>log, "(%s,%s) --> (%s,%s)" % (RA, Dec, Glong, Glat)
  e["Glong"] = Glong
  e["Glat"] = Glat

# Now add it into the database, and runnj the triggers to see if anyone needs
#   galactic coordinates to make a decision
  newEvent.publishLocal(pf)

# Can also be injected from remote annotator 
#  newEvent.publishXMPP()

#############  compute galactic from ICRS coordinates  #####################
def eq_to_gal(RA, Dec):
  print RA, Dec

  ap = (RA - 192.859508) * math.pi/180
  cap = math.cos(ap)
  sap = math.sin(ap)

  de = Dec*math.pi/180
  cde = math.cos(de)
  sde = math.sin(de)

  dg = 27.128336 * math.pi/180
  cdg = math.cos(dg)
  sdg = math.sin(dg)

  x = cdg*sde - sdg*cde*cap
  y = cde*sap
  z = sdg*sde + cdg*cde*cap

  b = math.asin(z) * 180/math.pi
  l = 122.932 - math.atan2(y, x) * 180/math.pi
  if l<360.0: l += 360.0
  if l>360.0: l -= 360.0
  return (l, b)

######### TEST TEST #########################
from VOEventNet.events.models import *
import sys
rule = None
eventIVORN = "ivo://uk.org.estar/pl.edu.ogle#OGLE-2008-BLG-652"
triggerEvent = Event.objects.filter(globalIVORN = eventIVORN)[0]
pf = Portfolio.objects.filter(first__globalIVORN = eventIVORN)[0]
log = sys.stdout
run(rule, triggerEvent, pf, log)