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

MOPS Alert Rule that returns DerivedObjects for which
    moid_1<0.005 AU OR moid_2<0.005 AU
"""

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


class Impactors(DerivedObjectRule):
    """
    Return all new or newly modified DerivedObject instances.
    """
    def isImpactor(self, obj):
        """
        Return True if obj has moid_1<0.005 AU OR moid_2<0.005 AU

        @param obj: DerivedObject instance
        """
        return((obj.alertObj.orbit.moid_1 != None and obj.alertObj.orbit.moid_1 < 0.005) or
               ((obj.alertObj.orbit.moid_2 != None and obj.alertObj.orbit.moid_2 < 0.005)))

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

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

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