desihub / desisurvey

Code for desi survey planning and implementation
BSD 3-Clause "New" or "Revised" License
2 stars 7 forks source link

fix YEARMDD vs. MJD of sunset #27

Closed sbailey closed 7 years ago

sbailey commented 7 years ago

This PR fixes an off-by-one bug in the YEARMMDD vs. MJD of the sunset, originally discovered with code like this:

from desisurvey.nightcal import getCalAll
from astropy.time import Time
from astropy import units

#- 19:00 UTC is noon Arizona
start = Time('2019-09-01T19:00:00')
end = Time('2019-10-01T19:00:00')
ephem = getCalAll(start, end, use_cache=False)

#- AZ local time of sunset
t = Time(ephem['MJDsunset'][0], format='mjd') - 7*units.hour
print(t.to_datetime(), 'vs', ephem['dirName'][0])

Which results in:

2019-09-02 18:50:34.175709 vs b'20190901'

Note: September 2 vs. September 1, i.e. the MJD of the sunset is being reported for the next day, not the current day.

I didn't fully discover why the original code didn't work, but this fix uses time conversions like

row['MJDsunset'] = Time(mayall.next_setting(ephem.Sun()).datetime()).mjd

This PR includes unit tests that check the consistency of the MJD for the sunset, sunrise, and twilights compared to the YEARMMDD dirname in the ephemeris. These test fail on current master and pass with this fix.

mlandriau commented 7 years ago

This looks good, but I'd like to know why the original didn't work. Perhaps when I have some spare time... M