|
-
Apr 27th, 2009, 08:08 AM
#1
Calculate sunrise / sunset
I'd like to set up functions to calculate sunrise and sunset times. I don't need huge precision; just a rough estimate (like within a half hour) would do. But then again, I certainly wouldn't complain about a solution with high precision.
Ideally you'd just send the function latitude and longitude coordinates. Something like:
Code:
Public Function Sunrise(pdblLatitude As Double, pdblLongitude As Double) As Date
End Function
Public Function Sunset(pdblLatitude As Double, pdblLongitude As Double) As Date
End Function
Google turned up a ton of possible ways to go, none of which made a lick of sense to me.
-
Apr 27th, 2009, 09:49 AM
#2
Re: Calculate sunrise / sunset
 Originally Posted by Ellis Dee
I'd like to set up functions to calculate sunrise and sunset times. I don't need huge precision; just a rough estimate (like within a half hour) would do. But then again, I certainly wouldn't complain about a solution with high precision.
Ideally you'd just send the function latitude and longitude coordinates. Something like:
Code:
Public Function Sunrise(pdblLatitude As Double, pdblLongitude As Double) As Date
End Function
Public Function Sunset(pdblLatitude As Double, pdblLongitude As Double) As Date
End Function
Google turned up a ton of possible ways to go, none of which made a lick of sense to me.
Have you considered using a webservice to achieve this goal?
I would write a .NET component to call a webservice. Then build a COM wrapper around the .NET component and call the functions exposed by the COM wrapper.
I have done something similar when my VB6 app needed to call a webservice running on SIEBEL.
Earthtools.org has a webservice that you might want to look at.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Apr 27th, 2009, 09:57 AM
#3
Re: Calculate sunrise / sunset
I know of a website that lists today's sunrise/sunset times for my town along with the current weather. I can download and parse the contents of that page, but it takes a couple seconds to gather the data. I was hoping for an algorithm, which would be immediate.
Using your link I did manage to find a complete calendar for sunrise & sunset times for my town; I'll see if I can't cobble something together that at least gets me within a half hour of the correct times.
I don't need a ton of precision; I just don't want to be off by more than an hour.
-
Apr 27th, 2009, 10:51 AM
#4
Hyperactive Member
Re: Calculate sunrise / sunset
The actual formula is posted on Wikipedia. I don't know how you convert that math to something VB can understand, but here it is...
http://en.wikipedia.org/wiki/Sunrise_equation
-
Apr 27th, 2009, 11:12 AM
#5
Re: Calculate sunrise / sunset
Yeah, exactly. All that stuff is greek to me. But I imagine for somebody who understands math it wouldn't be too hard to implement...
-
Apr 27th, 2009, 12:23 PM
#6
Hyperactive Member
Re: Calculate sunrise / sunset
I googled 'Vb6 Calculate Sunrise' and there's a couple of class modules out there that seem to do what you want, you might could look at those. I tried ripping one down but the math is way over my head.
-
Apr 27th, 2009, 04:39 PM
#7
Re: Calculate sunrise / sunset
don't forget to throw daylight savings into the pot
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 17th, 2012, 02:57 AM
#8
Re: Calculate sunrise / sunset
Yes, this is a gravedig but I thought people might end up here looking for an answer when they tried a search.
I have updated LunarPhase - Phase of the Moon adding another class SunRS - Sun Rise/Set/Day Length in post #5 there.
I hope this helps somebody.
-
Sep 17th, 2012, 07:53 AM
#9
Re: Calculate sunrise / sunset
Copied from Planet Code:
Have not tried it.....but SOUNDS like the solution:
'**************************************
' Name: Sunset - SunRise
' Description:It returns the time of the sunset or sunrise when you supply the latitude and longtitude
' By: JoelL
'
' Assumes:I didnt have time to clean it. just add a text box command button and date picker
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb...gWId=1'for details.'**************************************
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: These functions calculate sunrise and sunset times for any given:::
'::: latitude and longitude. They may also be used to calculate such:::
'::: things as astronomical twilight, nautical twilight and civil:::
'::: twilight. :::
'::: :::
'::: SPECIAL NOTES: This code is valid for dates from 1901 to 2099, and :::
'::: will not calculate sunrise/set times for latitudes :::
'::: above/below 63/-63 degrees. :::
'::: :::
'::: This code is based on the work of several others, including Jean:::
'::: Meeus, Todd Guillory, Christophe David, Kieth Burnett and Roger W.:::
'::: Sinnott (credit where due!):::
'::: :::
'::: Converted to VBScript and cleaned-up by Mike Shaffer. :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Const pi = 3.14159265358979
Public degrees, radians As Variant
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::Returns an angle in range of 0 to (2 * pi):::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function GetRange(x)
Dim temp1
Dim temp2
temp1 = x / (2 * pi)
temp2 = (2 * pi) * (temp1 - Fix(temp1))
If temp2 < 0 Then
temp2 = (2 * pi) + temp2
End If
GetRange = temp2
End Function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::Returns 24 hour time from decimal time:::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function GetMilitaryTime(DecimalTime, GMTOffset)
Dim temp1
Dim temp2
' Handle 24-hour time wrap
If DecimalTime + GMTOffset < 0 Then DecimalTime = DecimalTime + 24
If DecimalTime + GMTOffset > 24 Then DecimalTime = DecimalTime - 24
temp1 = Abs(DecimalTime + GMTOffset)
temp2 = Int(temp1)
temp1 = 60 * (temp1 - temp2)
temp1 = Right("0000" & CStr(Int(temp2 * 100 + temp1 + 0.5)), 4)
GetMilitaryTime = Left(temp1, 2) & ":" & Right(temp1, 2)
End Function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::This routine does all the real work:::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function GetSunRiseSet(latitude, ByVal longitude, ZoneRelativeGMT, RiseOrSet, Year, Month, Day)
If Abs(latitude) > 63 Then
GetSunRiseSet = "{invalid latitude}"
Exit Function
End If
y = Year
m = Month
d = Day
' An altitude of -0.833 is generally accepted as the angle of
' the sun at which sunrise/sunset occurs. It is not exactly
' zero because of refraction effects of the earth's atmosphere.
altitude = -0.833
Select Case UCase(RiseOrSet)
Case "S"
RS = -1
Case Else
RS = 1
End Select
Ephem2000Day = 367 * y - 7 * (y + (m + 9) \ 12) \ 4 + 275 * m \ 9 + d - 730531.5
utold = pi
utnew = 0
sinalt = CDbl(Sin(altitude * radians))' solar altitude
sinphi = CDbl(Sin(latitude * radians))' viewer's latitude
cosphi = CDbl(Cos(latitude * radians))'
longitude = CDbl(longitude * radians) ' viewer's longitude
Err.Clear
On Error Resume Next
Do While (Abs(utold - utnew) > 0.001) And (ct < 35)
ct = ct + 1
utold = utnew
days = Ephem2000Day + utold / (2 * pi)
t = days / 36525
' These 'magic' numbers are orbital elements of the sun, and should not be changed
L = GetRange(4.8949504201433 + 628.331969753199 * t)
G = GetRange(6.2400408 + 628.3019501 * t)
ec = 0.033423 * Sin(G) + 0.00034907 * Sin(2# * G)
lambda = L + ec
E = -1 * ec + 0.0430398 * Sin(2# * lambda) - 0.00092502 * Sin(4# * lambda)
obl = 0.409093 - 0.0002269 * t
' Obtain ASIN of (SIN(obl) * SIN(lambda))
delta = Sin(obl) * Sin(lambda)
delta = Atn(delta / (Sqr(1 - delta * delta)))
GHA = utold - pi + E
cosc = (sinalt - sinphi * Sin(delta)) / (cosphi * Cos(delta))
Select Case cosc
Case cosc > 1
correction = 0
Case cosc < -1
correction = pi
Case Else
correction = Atn((Sqr(1 - cosc * cosc)) / cosc)
End Select
utnew = GetRange(utold - (GHA + longitude + RS * correction))
Loop
If Err = 0 Then
GetSunRiseSet = GetMilitaryTime(utnew * degrees / 15, ZoneRelativeGMT)
Else
GetSunRiseSet = "{err}"
End If
End Function
Private Sub Command1_Click()
If IsDate(MyMaskDate) = False Then Exit Sub
y = Year(MyMaskDate)
m = Month(MyMaskDate)
d = Day(MyMaskDate)
If IsNumeric(Text1.Text) = True Then
' Set these to the latitude/longitude of the observer
Set dbs = OpenDatabase(App.Path & "/Zipcodes.Mdb")
Set rst = dbs.OpenRecordset("Select * From Zip Where Zip_Code = '" & Text1.Text & "'")
If rst.RecordCount > 0 Then
MyLatitude = rst!latitude
MyLongitude = rst!longitude
End If
End If
If MyLatitude = "" Then 'This is the setting for brooklyn NY
MyLatitude = 40.633157
MyLongitude = -73.996953
End If
' Set this to your offset from GMT (e.g. for Dallas is -6)
' NOTE: The routine does NOT handle switches to/from daylight savings
'time, so beware!
MyTimeZone = -5
' Note:Set RiseOrSet to "R" for sunrise, "S" for sunset
RiseOrSet = "R"
Rise = GetSunRiseSet(MyLatitude, MyLongitude, MyTimeZone, _
RiseOrSet, y, m, d)
SSEt = GetSunRiseSet(MyLatitude, MyLongitude, MyTimeZone, _
"s", y, m, d)
Label1.Caption = Format(Rise, "H:nn AMPM") & vbNewLine & Format(SSEt, "H:nn AMPM")
End Sub
Private Sub Form_Load()
degrees = 180 / pi
radians = pi / 180
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|