question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Moon phase data correct only for waxing crescent/gibbous

See original GitHub issue

I have a problem trying to get Moon Phase information from Skyfield 1.11. I want to replicate the results I get correctly from PyEphem using this (simplified) code:

def moonphase(date):
    # returns the moon's elongation (angle to the sun)
    obs = ephem.Observer()
    obs.date = date
    m = ephem.Moon()
    m.compute(date+0.5)
    #phase = int(round(m.elong.norm*180/math.pi))	# moon phase (0:new to 180:full to 360:new degrees)
    phase = m.elong.norm+0.0    # moon phase as float (0:new to π:full to 2π:new)
    return phase

PyEphem gives me values from 0 to π in the WAXING phase and from π to 2π in the WANING phase. However Skyfield gives me only values from 0 to π in both WAXING and WANING phases. Attached here are the results (in degrees) I get from Skyfield for selected dates while calculating a Nautical Almanac for 2019: phase_angle.zip

I am using this kind of code in Skyfield:

eph = load('de421.bsp')	# ephemeris
def moonphase(d):
    # returns the moon's elongation (angle to the sun)
    t12 = ts.utc(d.year, d.month, d.day, 12, 0, 0)    # moon phase is calculated at noon
    phase_angle = almanac.phase_angle(eph, 'moon', t12)
    elong = phase_angle.radians
    if elong >= 0 and elong <= math.pi:
        phase = math.pi - phase_angle.radians
    if elong > math.pi:
        phase = (3 * math.pi) - phase_angle.radians   # ??????
    print d, phase_angle.degrees       # FOR DEBUGGING ONLY
    return phase

For example, the ‘phase’ results are correct between NEW MOON on 6th Jan 2019 up to FULL MOON on 21 Jan 2019. During this time phase_angle.degrees goes from 180 to zero (assumed correct) but then till the next NEW MOON on 4th. Feb 2019 it goes from zero to 180 again (assumed incorrect)… I expected it to go from 360 to 180 degrees.

In other words, the Skyfield value range is 0 to π instead of 0 to 2π.

Do I understand correctly that phase_angle should be able to return a value in radians between 0 and 2π?

Kind thanks in appreciation of your time & effort to look into this!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tkirchercommented, Nov 19, 2020

@brandon-rhodes gave it a test and it works great. Thanks!

1reaction
aendiecommented, Aug 29, 2019

Yes, you can close this thread as I have implemented “before or after Full Moon” as indication of the angle. I didn’t think it is incorrect - I thought that Skyfield left out important information in providing an angle that’s only between 0 and π instead of between 0 and 2π. So the suggestion (from barrycarter) to add a flag ‘angle increasing’ or ‘angle decreasing’ I think is very appropriate.

I haven’t looked at ReportLab - I continued using LaTeX that the original developer, Enno Rodegerdts, chose. I have one open issue with pdflatex (included in MiKTeX) that I’ll pick up with them directly. But thanks for the suggestion. I’ll also look into the navigational stars in the latest PyEphem. Thanks!

The PyEphem output PDFs are already published online here: https://www.thenauticalalmanac.com/ I am in touch with the site owner and my name is mentioned very kindly there too. He does some clever post-processing before publishing. I’ve just finished the Skyfield version, so I expect it will be published when we’re sure the data is okay.

By the way, the moon image was fun to create. I overlaid a semi-transparent shadow onto the moon. First I simply deleted the black pixels from a moon image and the result was hilarious: a “hairy” moon (due to moonshine). I then wrote code to place transparent pixels outside a circle, carefully positioning it over the moon, which is almost a circle. Finally I thought maybe it looks better without the moon, so I included a parameter in config.py to replace it with the original data in that table location.

Thanks for your helpful comments!

P.S. My comment about Skyfield performance… clearly PyEphem uses different algorithms, but do you perhaps see a way to make the following methods execute faster?

def daylength(topos, degBelowHorizon):
    # Build a function of time that returns the daylength.
    topos_at = (earth + topos).at

    def is_sun_up_at(t):
        t._nutation_angles = iau2000b(t.tt)
        # Return `True` if the sun has risen by time `t`.
        return topos_at(t).observe(sun).apparent().altaz()[0].degrees > -degBelowHorizon

    is_sun_up_at.rough_period = 0.5  # twice a day
    return is_sun_up_at

and

def moonday(topos, degBelowHorizon):
    # Build a function of time that returns the "moon day" length.
    topos_at = (earth + topos).at

    def is_moon_up_at(t):
        t._nutation_angles = iau2000b(t.tt)
        # Return `True` if the moon has risen by time `t`.
        return topos_at(t).observe(moon).apparent().altaz()[0].degrees > -degBelowHorizon

    is_moon_up_at.rough_period = 0.5  # twice a day
    return is_moon_up_at

… should I enter this as a new thread?

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Guide to the Phases of the Moon - The Nine Planets
The Waxing Crescent Moon appears higher on the mid-spring – May 5 in the Northern Hemisphere / November 7 – Southern Hemisphere, than...
Read more >
The 8 Phases of the Moon in Order - AstroBackyard
There are a total of eight moon phases: New moon; Waxing Crescent; First quarter; Waxing Gibbous; Full moon; Waning Gibbous; Last (third) quarter...
Read more >
Experiment Two – Lunar Phases | JCCC Astronomy
Introduction: ; NewMoon. New Moon. 0 degrees ; WaxingCrescent. Waxing Crescent. 45 degrees behind ; FirstQuarter. 1 st Quarter. 90 degrees behind ;...
Read more >
Moon Phase Calendar for 2022 and 2023
This phase occurs between the new Moon and first quarter phases. At the beginning of this stage, we see a thin, crescent-shape Moon,...
Read more >
Moon Phases: Waxing, Waning and Lunar Cycle - Study.com
The phase of the moon we see after the first quarter moon is called the waxing gibbous moon, because 'gibbous' means 'swollen' or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found