View Full Version : Moonrise/moonset calculations
CardiffSalt
05/04/2004, 02:31 PM
Does anyonw know of a source for the equations to calculate moonrise and moonset?
I am writing a vb program to control x-10 devices and would like to incorporate moonlights that come on based on the actual moonrise/moonset and dimmed according to phase.
I've been searching for days and so far I haven't come up with anything that shows the equations (algorithms) used.
Thanks
CardiffSalt
kgross
05/05/2004, 02:13 AM
I am not sure where to find the algorithms, but I will look. But I do have a question for you. My tank is 6 foot long and I am currently using 3 cold cathode lights for moonlighting, which do not seem to be dimmable. What I would love to be able to do is have the x10 controller control all three cold cathodes seperatly, and depending on the moon phase, have 1 2 or all 3 on at the same time, with the the moon moving acrost the tank from 1 to 2 to 3 for dim nights, and 1+2 to 2+3 for 75% moon light and then 1+2+3 for full moon. Is this something you would consider adding to your software or would you possible release the source code so it could be modified to do this?
I have looked at elightmaster, and it does everything but this, and I can not add this..........
Kim
kgross
05/05/2004, 02:21 AM
I have been doing some searching and here is what I have found so far.
http://www.weatherimages.org/latlonsuncontact.html
http://www.dacre.net/moon/moonframe.html
http://aa.usno.navy.mil/data/docs/RS_OneDay.html
These links do not have the formulas, but they have calculators and maybe the webmasters would be willing to share the formulas they use.
Kim
Here is a Visual Basic Function to get the sunrise and sunset for a given latitude and longitude for today.
Private Sub calcSunTime()
'
' Calculate Sunrise and Sunset times based on latitude and longitude
' Calculations Based on work published in Sky and Telescope Including work by:
' Roger W. Sinnott
' Sky & Telescope for August 1994, page 84
'
' Converted to Visual Basic by James Bednar, 2003
Dim A(2), D(2)
P1 = 3.14159265: P2 = 2 * P1
Dr = P1 / 180: K1 = 15 * Dr * 1.0027379
'dblLat is the Latitude of the suntiming
dblLat = 6.2
B5 = Int(dblLat)
'dblLong is the Latitude of the suntiming
dblLong = 106.9
L5 = Int(dblLong)
'intTZ is the Time zone
intTZ = -7
H = intTZ
L5 = L5 / 360
Z0 = H / 24
'
' Set the current Year, Month, and Day using the Now() Function
'
Y = Year(Now)
M = Month(Now)
Dq = Day(Now)
G = 1
If Y < 1583 Then
G = 0
End If
D1 = Int(Dq)
F = Dq - D1 - 0.5
J = -Int(7 * (Int((M + 9) / 12) + Y) / 4)
If G = 0 Then
J = J + Int(275 * M / 9) + D1 + G * J3
J = J + 1721027 + 2 * G + 367 * Y
Else
S = Sgn(M - 9): Ab = Abs(M - 9)
J3 = Int(Y + S * Int(Ab / 7))
J3 = -Int((Int(J3 / 100) + 1) * 3 / 4)
J = J + Int(275 * M / 9) + D1 + G * J3
J = J + 1721027 + 2 * G + 367 * Y
End If
If F >= 0 Then
'do nothing
Else
F = F + 1
J = J - 1
End If
T = (J - 2451545) + F
TT = T / 36525 + 1
T0 = T / 36525
S = 24110.5 + 8640184.813 * T0
S = S + 86636.6 * Z0 + 86400 * L5
S = S / 86400
S = S - Int(S)
T0 = S * 360 * Dr
T = T + Z0
' Fundamental arguments
' (Van Flandern &
' Pulkkinen, 1979)
L = 0.779072 + 0.00273790931 * T
G = 0.993126 + 0.0027377785 * T
L = L - Int(L)
G = G - Int(G)
L = L * P2: G = G * P2
V = 0.39785 * Sin(L)
V = V - 0.01 * Sin(L - G)
V = V + 0.00333 * Sin(L + G)
V = V - 0.00021 * TT * Sin(L)
U = 1 - 0.03349 * Cos(G)
U = U - 0.00014 * Cos(2 * L)
U = U + 0.00008 * Cos(L)
W = -0.0001 - 0.04129 * Sin(2 * L)
W = W + 0.03211 * Sin(G)
W = W + 0.00104 * Sin(2 * L - G)
W = W - 0.00035 * Sin(2 * L + G)
W = W - 0.00008 * TT * Sin(G)
S = W / Sqr(U - V * V)
A5 = L + Atn(S / Sqr(1 - S * S))
S = V / Sqr(U): D5 = Atn(S / Sqr(1 - S * S))
R5 = 1.00021 * Sqr(U)
A(1) = A5
D(1) = D5
T = T + 1
' Fundamental arguments
' (Van Flandern &
' Pulkkinen, 1979)
L = 0.779072 + 0.00273790931 * T
G = 0.993126 + 0.0027377785 * T
L = L - Int(L)
G = G - Int(G)
L = L * P2: G = G * P2
V = 0.39785 * Sin(L)
V = V - 0.01 * Sin(L - G)
V = V + 0.00333 * Sin(L + G)
V = V - 0.00021 * TT * Sin(L)
U = 1 - 0.03349 * Cos(G)
U = U - 0.00014 * Cos(2 * L)
U = U + 0.00008 * Cos(L)
W = -0.0001 - 0.04129 * Sin(2 * L)
W = W + 0.03211 * Sin(G)
W = W + 0.00104 * Sin(2 * L - G)
W = W - 0.00035 * Sin(2 * L + G)
W = W - 0.00008 * TT * Sin(G)
S = W / Sqr(U - V * V)
A5 = L + Atn(S / Sqr(1 - S * S))
S = V / Sqr(U): D5 = Atn(S / Sqr(1 - S * S))
R5 = 1.00021 * Sqr(U)
A(2) = A5
D(2) = D5
If A(2) < A(1) Then A(2) = A(2) + P2
Z1 = Dr * 90.833: ' Zenith dist.
S = Sin(B5 * Dr)
C = Cos(B5 * Dr)
Z = Cos(Z1)
M8 = 0
W8 = 0
A0 = A(1)
D0 = D(1)
DA = A(2) - A(1)
DD = D(2) - D(1)
For C0 = 0 To 23
P = (C0 + 1) / 24
A2 = A(1) + P * DA
D2 = D(1) + P * DD
L0 = T0 + C0 * K1: L2 = L0 + K1
H0 = L0 - A0: H2 = L2 - A2
H1 = (H2 + H0) / 2 ' Hour angle,
D1 = (D2 + D0) / 2 ' declination,
If C0 > 0 Then
V2 = S * Sin(D2) + C * Cos(D2) * Cos(H2) - Z
Else
V0 = S * Sin(D0) + C * Cos(D0) * Cos(H0) - Z
V2 = S * Sin(D2) + C * Cos(D2) * Cos(H2) - Z
End If
If Sgn(V0) = Sgn(V2) Then
'do nothing
Else
V1 = S * Sin(D1) + C * Cos(D1) * Cos(H1) - Z
Ar = 2 * V2 - 4 * V1 + 2 * V0
B = 4 * V1 - 3 * V0 - V2
Dr = B * B - 4 * Ar * V0
If Dr < 0 Then
' do nothing
Else
Dr = Sqr(Dr)
If V0 < 0 And V2 > 0 Then M8 = 1
If V0 > 0 And V2 < 0 Then W8 = 1
E = (-B + Dr) / (2 * Ar)
If E > 1 Or E < 0 Then E = (-B - Dr) / (2 * Ar)
T3 = C0 + E + 1 / 120 ' Round off
H3 = Int(T3)
M3 = Int((T3 - H3) * 60)
If V0 < 0 And V2 > 0 Then
' populate strSunrise with a formatted sunrise time
strSunrise = Right$("00" & H3, 2) & ":" & Right$("00" & M3, 2)
ElseIf V0 > 0 And V2 < 0 Then
' populate strSunset with a formatted sunset time
strSunset = Right$("00" & H3, 2) & ":" & Right$("00" & M3, 2)
End If
H7 = H0 + E * (H2 - H0)
N7 = -Cos(D1) * Sin(H7)
D7 = C * Sin(D1) - S * Cos(D1) * Cos(H7)
AZ = Atn(N7 / D7) / Dr
If D7 < 0 Then AZ = AZ + 180
If AZ < 0 Then AZ = AZ + 360
If AZ > 360 Then AZ = AZ - 360
End If
End If
'
A0 = A2
D0 = D2
V0 = V2
Next
' Here are your output variables, they are strings
' containing the sunrise and sunset times in 24h in
' hh:mm format.
MsgBox strSunrise
MsgBox strSunset
End Sub
I'm not sure if it is 100% accurate, but I don't think I made any errors transcribing the calculations into VB from BASIC or whatever the origin language was.. don't remember. But, as you can see pretty obviously, figuring out sunrise and sunset is no trivial matter.
To get Clifton, NJ, my hometown, go to a web site that you can find your latitudes and longitudes.. I use wunderground.com. Insert the values:
'dblLat is the Latitude of the suntiming
dblLat = 40.9
B5 = Int(dblLat)
'dblLong is the Latitude of the suntiming
dblLong = 74.1
L5 = Int(dblLong)
'intTZ is the Time zone
intTZ = -6
H = intTZ
intTZ is the difference from GMT. You can use this value to shift the band to get the photoperiod where you want it. You don't always want the sun going up in your tank when the sun goes up in Jakarta, so change the intTZ value to move it where you want it. Play with the function and you'll see how the rise and set times change with variations in intTZ.
The code was modified slightly to be pasted in here, I have this function and some others encapsulated into an object in VB. I use this "Sun" object within my controller program to do exactly what you are trying to do.
jb
CardiffSalt
05/05/2004, 08:23 AM
Hi KGross,
Are you a vb programmer?? I am just learning as I go. I have the program up and running and everything is working fine. I wrote it to control up to 8 devices. It also has override buttons and feeding buttons. I am also incorporating databases into it for water log, livestock, expenses etc. I have so many things going on right now that it is hard to find time to get back to the keyboard to finish it!
Anyway, To do what you want with one x-10 device would not be possible but it should be doable with 3. Each device can only turn on or off a single outlet. You can split the outlet but there is now control for each split outlet seperatly.
I will have to find code to calculate the moon phases as well.
Give me some time and I'll see what I can come up with
CardiffSalt
CardiffSalt
05/05/2004, 08:28 AM
Hi Grim,
Excellent!! I was also thinking of something for sunrise and sunset! I am still hoping to find something for the moonrise and phases. I have been really suprised that it is so hard to find published code.
I do have a program that was written in q-basic (it only does moonrise and set, not phases) but not being familiar with q-basic it could be a real challenge to convert it to VB.
Thanks again grim, I'll let you know how I make out!
CardiffSalt
JHemdal
05/05/2004, 09:00 AM
Hi,
I'm just curious - do you believe that moonlight is the driving factor for circadian rythyms of marine organisms? My understanding has been that it isn't the moonlight per-se, but rather the tidal influence. Thus, during a spring tide / new moon combination, there is no moon out, but still there is very strong tidal influence, thus things under water will take that as a cue to "happen". The only reef animal that I am certain uses moonlight to directly control its behavoir are flashlight fish - they only move into shallow water on moonless nights (but even for them, a cloudy night is as good as a new moon phase). Think about it, researchers that need to observe mass spawning events on reefs do use moon phase to try to determine timing of the events, yet there is no concern if that night will be cloudy (and thus no light) as the corals will still spawn - because they are keying in on tidal, not light influences.
jhemdal
I have moonlight calculations encapsulated within functions as well, although I'll have to dig them up..
This is the trick to moonlight, when calculating moonrise and set, make sure you use the latitude and longitude of the tank and use the correct difference from GMT to sync the moon in your tank with the real moon outside the window. This gives you the ability to synchronize with the tides as well.
When calculating sunrise and set, use the latitude and longitude of Fiji, Marshall Islands, where ever.. But use the GMT shift to move the lighting period where you want it.
Now, obviously there will be some differences, but it's still better than saying (at 21:00 turn on moonlight, at 7:00 turn off moonlight).
I do believe it is a combination of three things, tidal influence, the moon, and finally, the change in the length of a day. The function above will change the length of the day throughout the year exactly as it is happening at the set longitude and latitude. Be aware, if you run halides or other types of lighting, you will need to bracket those numbers to either cut down the length of the day, or increase it. I have another function that uses this and takes in another variable called "bracket". This lets me cut down the day length two hours to control halides (bracket = -2), and use the full length to control my VHOs (bracket = 0).
jb
My controller application has a significant base of functionality, actually, everything I could possibly think of.
The coolest being:
I can monitor the current conditions of my tank from my website, also have a webcam hooked up to watch.
By using a remote sql database that all data is logged to, I can run queries from the web client to see all sorts of neat data (graph pH and light cycle against time) from my hotel room in the carribean.
I can get a mini version of my site from my cell phone
I get a instant message to my cell phone if something goes wrong (overtemperature warnings, overflow warnings).
I have multiple failover controls to prevent disaster.. If the temp goes over 81 turn canopy fans on, if the temp goes over 82 and the fans are on shut the lights off.
Was working on other cool toys based on a few suggestions from fellow DIYers on here.. Monitoring current draw of the return and skimmer pump to monitor 'pump health'. Should the pump start to seize up, there should be a noticable spike in current draw, etc.
It's really limitless, you can do whatever you want. Of course, since this was a windows application, it was at the whim of whatever the operating system saw fit. And, well, what good is the system if the OS decides to crash, hang, slow down, etc. So now I'm working on other systems that are PLC based to do the same thing. PLCs are much more reliable, but, much more difficult to implement with.
jb
Oscar19
05/05/2004, 02:43 PM
grim are u using AquaController ???
what ur web site?
No, my own proprietary system... I did most of the work on the software part of the system as part of my masters thesis project when I was in grad school..
jb
GratefulDiver
05/05/2004, 03:44 PM
Originally posted by grim
It's really limitless, you can do whatever you want. Of course, since this was a windows application, it was at the whim of whatever the operating system saw fit. And, well, what good is the system if the OS decides to crash, hang, slow down, etc. So now I'm working on other systems that are PLC based to do the same thing. PLCs are much more reliable, but, much more difficult to implement with.
Good, you added that.. :D
And don't get bought out before you make sure your product can get into our hands too! ;)
I'm an open source kind of guy, I have no problems making whatever I do free to anyone out there.. If anyone is seriously interested in collaborating on an open source project along these lines, drop me an email, maybe we can work something out.
Until then, ask away, I ran through pretty much every problem you can think of implementing my own system, no sense reinventing fire you know..
Jim
CardiffSalt
05/13/2004, 09:58 PM
Hi Grim,
Any chance of you sharing your source code? I wouldn't have a use for a lot of the web stuff, but I'm sure it would give me some hints for the control portion.
Thanks
Don
BeanAnimal
05/13/2004, 10:44 PM
I also have the sun and moon calcs encapsulated into functions for use in my VB based aquarium automation. My code calculates the rise, set and degree of brightness for any givin date (or time). I was working on geographic calcs at one point also, but decided enough was enough. The idea is to control my lighting via the PC. (pumps, wavemakers and everhting else also)
HRmm one day I will get back around to working on the project. I have a box full of solid state relays and designed the driver boards ADC/DAC boards... just never got around to finishing any of it.
Bill
vBulletin® v3.8.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.