<p>The problem was at the time of adjusting for time offset.</p>
<p>To fix it I had to calculate times for 3 days, then compare the time of the event to the other 2 days.</p>
<p>If a particular time was &lt; 0 after adjusting for offset, then it belongs to the day before, if it's &gt;= 24 then it belongs to the day after.</p>
<p>So I just needed to add some conditionals, for example:</p>
<pre><code>if EventTime &gt;= 24 then
   if YesterdayTime &gt;= 24 then Event = YesterdayTime else Event = 'No Event Today'
   end
elseif EventTime &lt; 0 then
   if TomorrowTime &lt; 0 then Event = TomorrowTime else Event = 'No Event Today'
   end
else Event = EventTime
end
</code></pre>
<p>After that now I correctly get:</p>
<pre><code>Date: 14/12/2024
Rising: 5:33 PM
Transit: No Transit Today
Setting: 6:28 AM
</code></pre>

CC BY-SA 4.0
2024-12-17T07:11:40.490Z
2024-12-17T07:11:40.490Z
RicardoTM
<p>I'm working on a full screen day/night clock that displays the position of the sun and moon using Rainmeter but the calculations are in Lua. I made it following the book Astronomical Algorithms by Jean Meeus.</p>
<p>While everything has been going great, I've faced some issues while trying to compute the rise, set and transit times for the moon. I'm using formulae from Chapter 15 of the book, I'm also using the standard altitude for the moon:</p>
<pre><code> h0= 0.7275pi - 0.5667
*pi means Moon's Horizontal Parallax*
</code></pre>
<p>However, the times are not exactly correct, for example, for December 14, 2024 I get the following:</p>
<pre><code>Rise: 5:33 PM
Transit: 11:29 PM
Set: 6:26 AM
</code></pre>
<p>These are local times for Latitude: 19.6546, Longitude: -101.2623 (West). Time Zone: -6</p>
<p>The code already switches the sign on longitude to positive for west.</p>
<p>The thing is, both rise and set are correct, however, the transit time is actually that of the day before, December 13th.</p>
<p>I've tried not limiting m times between -1 and +1, however the results are the same.</p>
<p>So, what's happening is that some days I get times for the day before or the day after. What I want is to limit the times to only those for the day in question. On December 14th there was actually no transit, since the moon transit first the day before at 11:29 PM, then set at 6:26 AM and rose the same day at 5:33 PM, then it didn't transit until 12:32AM, which is already the next day (15th), so the Moon didn't transit at all the 14th. I'd like to get the string &quot;Moon won't transit today&quot; when that's the case.</p>
<p>The same for rise and set if they don't happen on the day in question. I already coded the conditions for CosH0 &lt; -1 and &gt; +1 but those conditions are only for polar days and nights as far as I understood, which work fine.</p>
<p>Thank you in advance.</p>
<p>Edit. I found the answer (read answer)</p>

CC BY-SA 4.0
2024-12-16T02:30:09.023Z
The Moon moves a considerable amount during a day.  You need to re-compute the position of the Moon, and redo the algorithm for each of the rise, set, and transit times.  If you want more accuracy, repeat it again.  And there will be days where the Moon does not do one of transit, rise, or set.
Greg Miller
CC BY-SA 4.0
2024-12-16T04:16:15.120Z
What does *"the standard altitude for the moon: 0.7275pi - 0.5667"* mean? Are you talking about https://en.wikipedia.org/wiki/Atmospheric_refraction#Astronomical_refraction ?
PM 2Ring
CC BY-SA 4.0
2024-12-16T10:40:54.500Z
Thank you Greg, I'll try that and come back. Just a question, how will I know when there's no set, rise or transit?

2Ring, the book gives values for h0 (standard altitude). These values are -0.8333 for the sun, -0.5667 for stars and planets and 0.7275*pi -0.5667 for the moon, where pi means Moon's Horizontal Parallax. The value of 0.125 is also given for lower accuracy.
RicardoTM
CC BY-SA 4.0
2024-12-16T10:50:27.690Z
The book defines h0 as: The geometric altitude of the center of the body at the time of the apparent rising or setting.
RicardoTM
CC BY-SA 4.0
2024-12-16T13:30:39.893Z
Hey @GregMiller, I have added more iterations, and while the times are more precise, I still have the same problem. Some days times are for a day before or a day after, and some days all times are correct.
RicardoTM
CC BY-SA 4.0
2024-12-16T16:54:41.393Z
It is hard to predict which direction the algorithm will go, especially with local time zones.  I generally compute for the day before and after just to make sure. Meeus explains (in the same chapter) how to interpolate from three values to find a good approximation without as much computation.
Greg Miller
CC BY-SA 4.0
2024-12-17T00:03:27.183Z
@GregMiller I'm already using the interpolation formula for 3 tabular values. I have added my code and logs to the main question, maybe that helps finding the error. Also added comparison with other source.
RicardoTM
<algorithm>
How to calculate the rise set and transit for specific day?
RicardoTM
# How to calculate the rise set and transit for specific day?

(Asked by: username_0 on 2024-12-16)

I'm working on a full screen day/night clock that displays the position of the sun and moon using Rainmeter but the calculations are in Lua. I made it following the book Astronomical Algorithms by Jean Meeus.
While everything has been going great, I've faced some issues while trying to compute the rise, set and transit times for the moon. I'm using formulae from Chapter 15 of the book, I'm also using the standard altitude for the moon:
 h0= 0.7275pi - 0.5667
*pi means Moon's Horizontal Parallax*

However, the times are not exactly correct, for example, for December 14, 2024 I get the following:
Rise: 5:33 PM
Transit: 11:29 PM
Set: 6:26 AM

These are local times for Latitude: 19.6546, Longitude: -101.2623 (West). Time Zone: -6
The code already switches the sign on longitude to positive for west.
The thing is, both rise and set are correct, however, the transit time is actually that of the day before, December 13th.
I've tried not limiting m times between -1 and +1, however the results are the same.
So, what's happening is that some days I get times for the day before or the day after. What I want is to limit the times to only those for the day in question. On December 14th there was actually no transit, since the moon transit first the day before at 11:29 PM, then set at 6:26 AM and rose the same day at 5:33 PM, then it didn't transit until 12:32AM, which is already the next day (15th), so the Moon didn't transit at all the 14th. I'd like to get the string "Moon won't transit today" when that's the case.
The same for rise and set if they don't happen on the day in question. I already coded the conditions for CosH0 < -1 and > +1 but those conditions are only for polar days and nights as far as I understood, which work fine.
Thank you in advance.
Edit. I found the answer (read answer)

**Tags:** algorithm

> **Comments:**
> **username_1**: The Moon moves a considerable amount during a day.  You need to re-compute the position of the Moon, and redo the algorithm for each of the rise, set, and transit times.  If you want more accuracy, repeat it again.  And there will be days where the Moon does not do one of transit, rise, or set.  
> **username_2**: What does *"the standard altitude for the moon: 0.7275pi - 0.5667"* mean? Are you talking about https://en.wikipedia.org/wiki/Atmospheric_refraction#Astronomical_refraction ?  
> **username_0**: Thank you Greg, I'll try that and come back. Just a question, how will I know when there's no set, rise or transit?  2Ring, the book gives values for h0 (standard altitude). These values are -0.8333 for the sun, -0.5667 for stars and planets and 0.7275*pi -0.5667 for the moon, where pi means Moon's Horizontal Parallax. The value of 0.125 is also given for lower accuracy.  
> **username_0**: The book defines h0 as: The geometric altitude of the center of the body at the time of the apparent rising or setting.  
> **username_0**: Hey @GregMiller, I have added more iterations, and while the times are more precise, I still have the same problem. Some days times are for a day before or a day after, and some days all times are correct.  
> **username_1**: It is hard to predict which direction the algorithm will go, especially with local time zones.  I generally compute for the day before and after just to make sure. Meeus explains (in the same chapter) how to interpolate from three values to find a good approximation without as much computation.  
> **username_0**: @GregMiller I'm already using the interpolation formula for 3 tabular values. I have added my code and logs to the main question, maybe that helps finding the error. Also added comparison with other source.  

---

## 1 answers

### Answer by username_0 on 2024-12-17. Score: 1

The problem was at the time of adjusting for time offset.
To fix it I had to calculate times for 3 days, then compare the time of the event to the other 2 days.
If a particular time was < 0 after adjusting for offset, then it belongs to the day before, if it's >= 24 then it belongs to the day after.
So I just needed to add some conditionals, for example:
if EventTime >= 24 then
   if YesterdayTime >= 24 then Event = YesterdayTime else Event = 'No Event Today'
   end
elseif EventTime < 0 then
   if TomorrowTime < 0 then Event = TomorrowTime else Event = 'No Event Today'
   end
else Event = EventTime
end

After that now I correctly get:
Date: 14/12/2024
Rising: 5:33 PM
Transit: No Transit Today
Setting: 6:28 AM


---

