Search code examples
pythonastronomypyephem

Python: Printing data only when number enters or leaves interval


Currently I'm making a script that, given a set of celestial coordinates, will tell you on the next days when that point will be visible for a specific telescope. The criteria is simple, in the Horizontal Coordinate system, altitude of the object must be between 30 and 65 degrees(Variable "crit" here represents that, but in radians). So I have a set of parameters for the telescope called "Ant" and then, using Pyephem:

#imported ephem as ep
obj= ep.FixedBody()
obj._ra= E.ra
obj._dec= E.dec
obj._epoch = E.epoch
Ant.date = ep.now()
for d in range(days):
    for i in range(24):
        for j in range (60):
            Ant.date += ep.minute       
            obj.compute(Ant)
            crit= float(obj.alt)
            if crit>=0.523599 and crit <=1.13446:
                print "Visible at %s" %Ant.date

Which results in printing a lot of "Visible at 2016/7/11 19:41:21", 1 for every minute. I Just want it to print something like "Enters visibility at 2016/7/11 19:41:21, leaves at 2016/7/11 23:41:00", for example. Any Ideas will be appreciated.

Disclaimer: Sorry, not a native english speaker.


Solution

  • You need to keep track of whether it is already in range. So, for instance, at the beginning you'd initialize it:

    is_visible = False
    

    and your if statement might look like:

    if crit>=0.523599 and crit <=1.13446:
        if not is_visible:
            print "Visible at %s" %Ant.date
            is_visible = True
    else:
        if is_visible:
            print "No longer visible at %s" % Ant.date
            is_visible = False