#!/usr/bin/env python
"""
plugins.comets

MOPS Alert Rule that returns DerivedObjects for which
    ( a>4.5 AU && e>0.5 ) || e>0.95
"""

from base import DerivedObjectRule
import sys
from MOPS.Alerts.plugins.Constants import CH_COMETS

class Comets(DerivedObjectRule):
    """
    Return all new or newly modified DerivedObject instances.
    """
    def IsComet(self, obj):
        """
        Return True if obj has ( a>4.5 AU && e>0.5 ) || e>0.95

        q = a * ( 1 - e )

        @param obj: DerivedObject instance
        """
        return(obj.alertObj.orbit.e > 0.95 or
               (obj.alertObj.orbit.e > 0.5 and obj.alertObj.orbit.q / (1 - obj.alertObj.orbit.e) > 4.5))

    
    def evaluate(self):
        """
        Return a list of DerivedObject instances that satisfy the rule.
        """
        
        #Set the channel that will be used to publish the alert.
        self.channel = CH_COMETS
        return([obj for obj in self.newAlerts if self.IsComet(obj)])
# <-- end class

def main(args=sys.argv[1:]):
    rule = Comets()
    alerts = rule.evaluate()
    
    for alert in alerts:
        print(str(alert) + '\n\n')
# <-- end main    

if(__name__ == '__main__'):
    sys.exit(main())
# <-- end if
