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


MOPS Alert Rule that returns DerivedObjects for which
  a>=3.7 AU && a<=4.2 AU && e<=0.3 && i<=20.0
"""

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


class HildaFamily(DerivedObjectRule):
    """
    Return all new or newly modified DerivedObject instances.
    """
    def isHildaFamily(self, obj):
        """
        Return True if obj has a>=3.7 AU && a<=4.2 AU && e<=0.3 && i<=20.0
        
        q = a * ( 1 - e )
        
        @param obj: DerivedObject instance
        """
        try:
            a = obj.alertObj.orbit.q / (1 - obj.alertObj.orbit.e)
        except:
            return(False)
        if(a == None):
            return(False)
        return(a >= 3.7  and a <= 4.2  and
               obj.alertObj.orbit.e <= 0.3 and obj.alertObj.orbit.i <= 20.0)


    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_HILDAS
        return([obj for obj in self.newAlerts \
                if self.isHildaFamily(obj)])
# <-- end class

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

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