astroplan icon indicating copy to clipboard operation
astroplan copied to clipboard

LocalTimeConstraint does not consider local time-zone

Open Lagrang3 opened this issue 4 years ago • 9 comments

AltitudeConstraint does not consider DST (daylight saving time). For instance consider the following code:

#!/usr/bin/env python3

import astroplan, astropy.time
import astropy.units as u
import pytz,datetime

polse=astroplan.Observer(
    latitude=
        46+(28+9.1/60)/60,
    longitude=
        13+(0+42.6/60)/60,
    elevation=
        750*u.meter,
    timezone=
        pytz.timezone('Europe/Rome'))
        
target = astroplan.FixedTarget.from_name('Aldebaran')
constraint = astroplan.AltitudeConstraint(min=20*u.deg)
    
print(constraint(polse,target,times=astropy.time.Time('2020-04-10 21:00',
        location=polse.location)))

It determines whether Aldebaran is visible above 20 deg. on the evening of 2020-04-10. In Stellarium at that time the star is above 25 deg. and at 22:00 the star is below the 20 deg. However the code returns False after 20:00. When it should be True until 21:00.

I am running anaconda on Linux:

astroplan                 0.6             
astropy                   4.0.1.post1 

Lagrang3 avatar Aug 21 '20 16:08 Lagrang3

This snippet

T = astropy.time.Time('2020-04-10 20:00',location=polse.location)
print(polse.altaz(T,target).alt)

prints 16d30m12.0672s, which corresponds to the actual altitude at 22:00.

Lagrang3 avatar Aug 21 '20 16:08 Lagrang3

Uh... At 20:00 astropy.time says it corresponds to Julian Day 2458950.3333333335, when actually it should be 2458950.41667. I see the astropy.time object is giving back a time point corresponding to 20:00 utc, not the local time at the location polse.

Lagrang3 avatar Aug 21 '20 16:08 Lagrang3

Hi @Lagrang3 , see the astropy time notes about location. Basically the timezones only work in certain situations and when converting between different scales.

We choose to just always use UTC for working with any dates or times and only convert it to a local time for final display purposes or initial input.

wtgee avatar Aug 21 '20 16:08 wtgee

Hi @wtgee, thank you for the clarification. Then I must assume that the T:

T = astropy.time.Time('2020-04-10 20:00',location=polse.location)

is meant UTC. Then what do you make up of this:

constraint = astroplan.LocalTimeConstraint(min=datetime.time(19,30),max=datetime.time(20,30))
print(constraint(polse,target,times=T))

printing True when polse is an observer at UTC+2.

Lagrang3 avatar Aug 21 '20 18:08 Lagrang3

In my opinion some of the constraint classes are interpreting the time in the input with respect to UTC while other are assuming them as relative to the observer.

Lagrang3 avatar Aug 21 '20 18:08 Lagrang3

In fact in the code the timezome is copied from the observer but then it is not used at all.

Lagrang3 avatar Aug 21 '20 18:08 Lagrang3

I don't use the LocalTimeConstraint, but it's odd that the timezone is ignored here @bmorris3

https://github.com/astropy/astroplan/blob/7a894acdfb850f560b9d968236a959279f331518/astroplan/constraints.py#L762

wtgee avatar Aug 21 '20 18:08 wtgee

In fact in the code the timezome is copied from the observer but then it is not used at all.

You beat me to it. ;)

wtgee avatar Aug 21 '20 18:08 wtgee

Here's a full example.

#!/usr/bin/env python3

import astroplan, astropy.time
import astropy.units as u
import datetime, pytz

polse=astroplan.Observer(
    latitude=
        46+(28+9.1/60)/60,
    longitude=
        13+(0+42.6/60)/60,
    elevation=
        750*u.meter,
    timezone=
        pytz.timezone('Europe/Rome'))
        
target = astroplan.FixedTarget.from_name('Aldebaran')
T = astropy.time.Time( [ '2020-04-10 19:05', '2020-04-10 20:05' ],location=polse.location)
altC = astroplan.AltitudeConstraint(min=20*u.deg)
timeC = astroplan.LocalTimeConstraint(min=datetime.time(21,0),max=datetime.time(21,30))

print(altC(polse,target,times=T))
print(timeC(polse,target,times=T))

it should print

[ True False]
[ True False]

Lagrang3 avatar Aug 22 '20 08:08 Lagrang3