<p>Here is my attempt to calculate the answer to your question using Python and Astropy.</p>
<p>It uses <a href="https://docs.astropy.org/en/stable/api/astropy.coordinates.get_body.html" rel="nofollow noreferrer"><code>astropy.coordinates.get_body()</code></a> to compute the position of the Moon in GCRS coordinates, and <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.root_scalar.html" rel="nofollow noreferrer"><code>scipy.optimize.root_scalar()</code></a> to find the time that the Moon's center of mass crosses the horizon (accounting for refraction).</p>
<p>Given a grid of longitudes and latitudes, the program loops through each one, computes the time of moonrise/moonset, and then saves the azimuth of the Moon at that time in an array.</p>
<p>Here is a <a href="https://colab.research.google.com/drive/1uNGPQCLDegO_Lfxq8QLzbWkrgk9mY9hB?usp=sharing" rel="nofollow noreferrer">live version</a> in case you don't have Python installed.</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
import astropy.units as u
import astropy.time
import astropy.coordinates

# Save the time format we're using since
# scipy.optimize uses bare floats, but
# astropy needs to use astropy.time.Time
# instances
format_time = &quot;jd&quot;

def get_moon(
    t: float,
    location: astropy.coordinates.EarthLocation,
) -&gt; float:
  &quot;&quot;&quot;
  Compute the altitude and azimuth of the moon
  given a time and location.

  Parameters
  ----------
  t
    The current Julian day, expressed in UTC.
  location
    A point on the Earth
  &quot;&quot;&quot;

  # Convert the given Julian Day into a Time object
  time = astropy.time.Time(t, format=format_time)

  # Load the coordinates of the Moon for the
  # given time.
  moon = astropy.coordinates.get_body(
      body=&quot;moon&quot;,
      time=time,
      location=location,
      ephemeris=&quot;de440&quot;,
  )

  # Define a topocentric coordinate frame
  # at the given time and location
  frame = astropy.coordinates.AltAz(
      location=location,
      obstime=time,
      pressure=1013.25 * u.mbar,
      temperature=15 * u.deg_C,
      obswl=550*u.nm,
  )

  # Transfrom the Moon's coordinates
  # into the topocentric frame
  moon = moon.transform_to(frame)

  return moon

def azimuth_moonset(
    time_guess: astropy.time.Time,
    location: astropy.coordinates.EarthLocation,
) -&gt; u.Quantity:
  &quot;&quot;&quot;
  Given a location and a guess for the time of moonrise/moonset, compute
  the azimuth of the Moon at the moment it's center of mass crosses the horizon.

  Parameters
  ----------
  time_guess
    The time at which to start the search
  location
    A point on the Earth.
  &quot;&quot;&quot;

  # Save the dimensions of the location grid
  shape = location.shape

  # Loop over every point in the location grid
  result = np.empty(shape) &lt;&lt; u.deg
  for index in np.ndindex(shape):

    # Isolate the current point in the location grid
    loc = location[index]

    # Define a function to find the root of
    def func(t: float):
      return get_moon(t, location[index]).alt.value

    # Compute the moment of moonrise/moonset
    t = scipy.optimize.root_scalar(
        f=func,
        x0=time_guess.to_value(format_time),
        x1=(time_guess + 10 * u.hr).to_value(format_time),
    ).root

    # Save the azimuth of the Moon at that moment
    result[index] = get_moon(t, loc).az

  return result

# Define a grid of locations on the Earth
lat = 45 * u.deg
lon = np.linspace(0, 100, num=11) * u.deg
location = astropy.coordinates.EarthLocation.from_geodetic(lat=lat, lon=lon)

# Compute the azimuth of the Moon
# at the moment of moonrise/moonset
az = azimuth_moonset(
    time_guess=astropy.time.Time(&quot;2025-07-03T17:30&quot;),
    location=location
)

# Plot the result
fig, ax = plt.subplots()
ax.plot(lon, az);
ax.set_xlabel(f&quot;longitude ({lon.unit:latex_inline})&quot;);
ax.set_ylabel(f&quot;moonset azimuth ({az.unit:latex_inline})&quot;);
</code></pre>
<p><a href="https://i.sstatic.net/ynh8YV0w.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ynh8YV0w.png" alt="azimuth-vs-longitude" /></a></p>

CC BY-SA 4.0
2024-05-31T08:33:59.980Z
Nice one, although I am looking for something for Excel if it's possible. Haven't precise it in the question. Do you know the source of the scientific formula?
Geographos
CC BY-SA 4.0
2024-05-31T12:36:20.350Z
@Geographos You can't get this level of precision from a simple Excel formula. [Lunar theory](https://en.wikipedia.org/wiki/Lunar_theory) is complicated. As Wikipedia says, the lunar theory of E. W. Brown, developed around the turn of the twentieth century, used over 1400 terms. I have some info about the Moon's motion in https://astronomy.stackexchange.com/a/48594/16685 & https://astronomy.stackexchange.com/a/55112/16685
PM 2Ring
CC BY-SA 4.0
2024-05-31T12:40:57.820Z
@Geographos (cont) OTOH, if you can tolerate errors of a minute or so, and you're only trying to calculate lunar motion over a limited range of years (eg, 1 or 2 centuries), you can approximate the position of the Moon using formulas with around 20 or so trigonometry terms.
PM 2Ring
CC BY-SA 4.0
2024-05-31T16:22:27.753Z
You can import the data from this script into Excel.
Greg Miller
CC BY-SA 4.0
2024-05-31T16:37:41.947Z
I haven't tried to this yet, but apparently you can also run Python inside Excel https://support.microsoft.com/en-us/office/get-started-with-python-in-excel-a33fbcbe-065b-41d3-82cf-23d05397f53d
Roy Smart
CC BY-SA 4.0
2024-05-31T02:56:11.210Z
2024-05-31T02:56:11.210Z
Roy Smart
<p>I found a useful formula for calculating the azimuth of moonrise/moonset, which can be found in this post:</p>
<p><a href="https://astronomy.stackexchange.com/questions/240/how-does-moonrise-moonset-azimuth-vary-with-time">How does moonrise/moonset azimuth vary with time?</a></p>
<p>However the formula refers to the fixed lunar declination, which can be found i.e. here:</p>
<p><a href="https://astropixels.com/ephemeris/moon/moondec2001.html" rel="nofollow noreferrer">https://astropixels.com/ephemeris/moon/moondec2001.html</a></p>
<p>We cannot assume, that the declination of the Moon will be the same over a whole day. Even by visiting the link above you are informed about the greatest declination at the given day.
If we consider the major lunar standstill on March 7 2025 the maximum declination of +28°43.0' N is to be reached at 15:56 UTC. In practice, it means that this declination will be different at the moonset occurring about 6 hours earlier. Indeed, the same applies to the Sun, but it's less noticeable. As a result, it can be omitted. In the case of the Moon not necessarily, as the declination especially during the major lunar standstill might change up to 11,6' every 12 hours! Thom has already mentioned it in 1971 in the Megalithic lunar observations book.</p>
<p>The live example you can see below. I've selected 4 places separated by just 1 degree of longitude and the changes are noticeable on the small distance such as this.</p>
<p><a href="https://i.sstatic.net/Wi8rDUIw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Wi8rDUIw.png" alt="enter image description here" /></a></p>
<p>Therefore I am kindly asking about the formula, which could easily compute the lunar declination correctness against its most extreme point.</p>

CC BY-SA 4.0
2024-05-30T17:04:07.963Z
The moon is close enough that the observer's position on Earth causes significant parallax, and that also must be taken into account as the observer rotates around the Earth from moonrise to moonset.  And that's in addition to the motion of the moon.  So the best way to compute moon rise and set is to take an iterative approach.  Compute the rise/set for the moon's apparent position at a random time, then use those times to re-compute the moon's apparent position and compute the rise/set times again.  Usually two iterations are enough to get within 1 minute unless you're near the poles.
Greg Miller
CC BY-SA 4.0
2024-05-30T17:54:12.043Z
What does *"the lunar declination correctness against its most extreme point"* mean? As the graphs in [my answer](https://astronomy.stackexchange.com/a/57591/16685) show, the rate of change of the Moon's declination varies in a complicated way, although there are general trends. But you need more than just the declination to compute moonrise & moonset.
PM 2Ring
CC BY-SA 4.0
2024-05-31T08:36:11.860Z
The most extreme point is the point reached by the Moon during the major lunar standstill
Geographos
CC BY-SA 4.0
2024-05-31T12:21:54.740Z
Ok, but that doesn't explain what "the lunar declination correctness against its most extreme point" means.
PM 2Ring
<the-moon><declination>
How does azimuth of moonrise/moonset vary by longitude?
Geographos
# How does azimuth of moonrise/moonset vary by longitude?

(Asked by: username_0 on 2024-05-30)

I found a useful formula for calculating the azimuth of moonrise/moonset, which can be found in this post:

[How does moonrise/moonset azimuth vary with time?](https://astronomy.stackexchange.com/questions/240/how-does-moonrise-moonset-azimuth-vary-with-time)

However the formula refers to the fixed lunar declination, which can be found i.e. here:

<https://astropixels.com/ephemeris/moon/moondec2001.html>

We cannot assume, that the declination of the Moon will be the same over a whole day. Even by visiting the link above you are informed about the greatest declination at the given day.
If we consider the major lunar standstill on March 7 2025 the maximum declination of +28°43.0' N is to be reached at 15:56 UTC. In practice, it means that this declination will be different at the moonset occurring about 6 hours earlier. Indeed, the same applies to the Sun, but it's less noticeable. As a result, it can be omitted. In the case of the Moon not necessarily, as the declination especially during the major lunar standstill might change up to 11,6' every 12 hours! Thom has already mentioned it in 1971 in the Megalithic lunar observations book.

The live example you can see below. I've selected 4 places separated by just 1 degree of longitude and the changes are noticeable on the small distance such as this.

[![enter image description here](https://i.sstatic.net/Wi8rDUIw.png)](https://i.sstatic.net/Wi8rDUIw.png)

Therefore I am kindly asking about the formula, which could easily compute the lunar declination correctness against its most extreme point.

**Tags:** the-moon, declination
> **Comments:**
> **username_1**: The moon is close enough that the observer's position on Earth causes significant parallax, and that also must be taken into account as the observer rotates around the Earth from moonrise to moonset.  And that's in addition to the motion of the moon.  So the best way to compute moon rise and set is to take an iterative approach.  Compute the rise/set for the moon's apparent position at a random time, then use those times to re-compute the moon's apparent position and compute the rise/set times again.  Usually two iterations are enough to get within 1 minute unless you're near the poles.  
> **username_2**: What does *"the lunar declination correctness against its most extreme point"* mean? As the graphs in [my answer](https://astronomy.stackexchange.com/a/57591/16685) show, the rate of change of the Moon's declination varies in a complicated way, although there are general trends. But you need more than just the declination to compute moonrise & moonset.  
> **username_0**: The most extreme point is the point reached by the Moon during the major lunar standstill  
> **username_2**: Ok, but that doesn't explain what "the lunar declination correctness against its most extreme point" means.  

---

## 1 answers

### Answer by username_3 on 2024-05-31. Score: 1

Here is my attempt to calculate the answer to your question using Python and Astropy.
It uses astropy.coordinates.get_body() to compute the position of the Moon in GCRS coordinates, and scipy.optimize.root_scalar() to find the time that the Moon's center of mass crosses the horizon (accounting for refraction).
Given a grid of longitudes and latitudes, the program loops through each one, computes the time of moonrise/moonset, and then saves the azimuth of the Moon at that time in an array.
Here is a live version in case you don't have Python installed.
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
import astropy.units as u
import astropy.time
import astropy.coordinates

# Save the time format we're using since
# scipy.optimize uses bare floats, but
# astropy needs to use astropy.time.Time
# instances
format_time = "jd"

def get_moon(
    t: float,
    location: astropy.coordinates.EarthLocation,
) -> float:
  """
  Compute the altitude and azimuth of the moon
  given a time and location.

  Parameters
  ----------
  t
    The current Julian day, expressed in UTC.
  location
    A point on the Earth
  """

  # Convert the given Julian Day into a Time object
  time = astropy.time.Time(t, format=format_time)

  # Load the coordinates of the Moon for the
  # given time.
  moon = astropy.coordinates.get_body(
      body="moon",
      time=time,
      location=location,
      ephemeris="de440",
  )

  # Define a topocentric coordinate frame
  # at the given time and location
  frame = astropy.coordinates.AltAz(
      location=location,
      obstime=time,
      pressure=1013.25 * u.mbar,
      temperature=15 * u.deg_C,
      obswl=550*u.nm,
  )

  # Transfrom the Moon's coordinates
  # into the topocentric frame
  moon = moon.transform_to(frame)

  return moon

def azimuth_moonset(
    time_guess: astropy.time.Time,
    location: astropy.coordinates.EarthLocation,
) -> u.Quantity:
  """
  Given a location and a guess for the time of moonrise/moonset, compute
  the azimuth of the Moon at the moment it's center of mass crosses the horizon.

  Parameters
  ----------
  time_guess
    The time at which to start the search
  location
    A point on the Earth.
  """

  # Save the dimensions of the location grid
  shape = location.shape

  # Loop over every point in the location grid
  result = np.empty(shape) << u.deg
  for index in np.ndindex(shape):

    # Isolate the current point in the location grid
    loc = location[index]

    # Define a function to find the root of
    def func(t: float):
      return get_moon(t, location[index]).alt.value

    # Compute the moment of moonrise/moonset
    t = scipy.optimize.root_scalar(
        f=func,
        x0=time_guess.to_value(format_time),
        x1=(time_guess + 10 * u.hr).to_value(format_time),
    ).root

    # Save the azimuth of the Moon at that moment
    result[index] = get_moon(t, loc).az

  return result

# Define a grid of locations on the Earth
lat = 45 * u.deg
lon = np.linspace(0, 100, num=11) * u.deg
location = astropy.coordinates.EarthLocation.from_geodetic(lat=lat, lon=lon)

# Compute the azimuth of the Moon
# at the moment of moonrise/moonset
az = azimuth_moonset(
    time_guess=astropy.time.Time("2025-07-03T17:30"),
    location=location
)

# Plot the result
fig, ax = plt.subplots()
ax.plot(lon, az);
ax.set_xlabel(f"longitude ({lon.unit:latex_inline})");
ax.set_ylabel(f"moonset azimuth ({az.unit:latex_inline})");

> **Comments:**
> **username_0**: Nice one, although I am looking for something for Excel if it's possible. Haven't precise it in the question. Do you know the source of the scientific formula?  
> **username_2**: @Geographos You can't get this level of precision from a simple Excel formula. [Lunar theory](https://en.wikipedia.org/wiki/Lunar_theory) is complicated. As Wikipedia says, the lunar theory of E. W. Brown, developed around the turn of the twentieth century, used over 1400 terms. I have some info about the Moon's motion in https://astronomy.stackexchange.com/a/48594/16685 & https://astronomy.stackexchange.com/a/55112/16685  
> **username_2**: @Geographos (cont) OTOH, if you can tolerate errors of a minute or so, and you're only trying to calculate lunar motion over a limited range of years (eg, 1 or 2 centuries), you can approximate the position of the Moon using formulas with around 20 or so trigonometry terms.  
> **username_1**: You can import the data from this script into Excel.  
> **username_3**: I haven't tried to this yet, but apparently you can also run Python inside Excel https://support.microsoft.com/en-us/office/get-started-with-python-in-excel-a33fbcbe-065b-41d3-82cf-23d05397f53d  

---

