Is there a way in C# to calculate given a latitude and longitude when the sun will set and rise for a given day?
Is there a way in C# to calculate given a latitude and longitude when the sun will set and rise for a given day?
I have tested this nuget package in UWP.
https://www.nuget.org/packages/SolarCalculator/
The documentation is a bit sketchy, and is here:
https://github.com/porrey/Solar-Calculator
You can use this to get the sunrise, given
la = latitude; and lo = longitude; for your area:
SolarTimes solarTimes = new SolarTimes(DateTime.Now, la, lo);
DateTime sr = solarTimes.Sunrise;
DateTime dt = Convert.ToDateTime(sr);
textblockb.Text = dt.ToString("h:mm:ss");
You can install it in Visual Studio using the PM manager
Install-Package SolarCalculator -Version 2.0.2
or by looking up SolarCalculator in the "Manage NuGet Packages" Visual Studio library.
Yes quit a few.
A few links for patterns.
http://williams.best.vwh.net/sunrise_sunset_example.htm
http://www.codeproject.com/Articles/29306/C-Class-for-Calculating-Sunrise-and-Sunset-Times
https://gist.github.com/cstrahan/767532
http://pointofint.blogspot.com/2014/06/sunrise-and-sunset-in-c.html
http://yaddb.blogspot.com/2013/01/how-to-calculate-sunrise-and-sunset.html
https://forums.asp.net/t/1810934.aspx?Sunrise+and+Sunset+timings+Calculation+
http://www.ip2location.com/tutorials/display-sunrise-sunset-time-using-csharp-and-mysql-database
http://en.pudn.com/downloads270/sourcecode/windows/csharp/detail1235934_en.html
http://regator.com/p/25716249/c_class_for_calculating_sunrise_and_sunset_times
http://forums.xkcd.com/viewtopic.php?t=102253
http://www.redrok.com/solar_position_algorithm.pdf
http://sidstation.loudet.org/sunazimuth-en.xhtml
https://sourceforge.net/directory/os:windows/?q=sunrise/set%20times
https://www.nuget.org/packages/SolarCalculator/
http://www.grasshopper3d.com/forum/topics/solar-calculation-plugin
and this was a project that I did for Planet Source Code long ago but luckily I saved it elsewhere because that site lost data.
https://github.com/DouglasAllen/SunTimes.VSCS.Net
uses this Gist plus
https://gist.github.com/DouglasAllen/c682e4c412a0b9d8f536b014c1766f20
Now for a brief explanation of the technique to do that.
First for any day you need true solar noon or transit for your location.
That takes into account your local longitude. It may be converted to a time just by dividing it by 15.
That is how much time later you are from Zulu zone time or zero longitude.
That starts at 12:00 PM or Noon.
And on your time calculated from the longitude.
Now the hard part. You need a way to calculate the Equation of Time.
That is a time difference caused by Earth tilt and orbit around the Sun.
This will give you an idea... https://en.wikipedia.org/wiki/Equation_of_time
But they have a formula that is much easier.... https://en.wikipedia.org/wiki/Sunrise_equation
This guy has some books that a lot of people go by or buy. :-D https://en.wikipedia.org/wiki/Jean_Meeus
Use your first calculation for your mean solar transit and calculate a JDN... https://en.wikipedia.org/wiki/Julian_day
This gets used by all the angle formulas as a time in Julian century https://en.wikipedia.org/wiki/Julian_year_(astronomy)
https://en.wikipedia.org/wiki/Epoch_(astronomy)
It's basically your JDN minus the epoch such as J2000 or 2451545.0 all divided by 36525.0 to give you the Julian century or t which gets used for most formula that have t as a parameter. Sometimes Julian millennia is used. In that case it's 3652500.0
The trick is to find those angle formulas that help you solve the Equation of Time.
Then you get your true solar transit and subtract the half day or add the half day of sunlight for your location. You'll find those around in the answers and the software.
Once you get something going you can check it against a search for the times or online calculators.
I hope this is enough to get you going. There are libraries all over the place but it's not that hard to make your own. I did but it's in Ruby. It could prove useful....https://github.com/DouglasAllen/gem-equationoftime
good luck!