Calculate sunrise, sunset and maximum solar radiation
I got some requests for releasing the library that calculates the sunrise, sunset and maximum solar radiation of the LetsGrow WordPress Weather plug-in. I extracted the algorithms from the implementation on the platform LetsGrow.com and decided to release it.
It is possible to calculate the sunrise, sunset and maximum solar radiation using some well-known formulas. For those of you who are interested in these formulas take a look at the following pages at Wikipedia (Declination of the Sun, Sunrise, Sunset). If you just want to calculate the sunrise, sunset and maximum solar radiation take a look below on how to use the code.
I wrote the code in C# and packed it in a Visual Studio 2008 solution. It has two assemblies, Astronomy and AstronomyTest. The assembly Astronomy has the SolarCalculator class which performs the real calculation. The assembly AstronomyTest has several unit-tests that validates the calculations against external sources. The SolarCalculator class needs the longitude, latitude, time-zone and whether to use daylight savings. You can create an instance of the SolarCalculator like this:
const Double Longitute = 5.127869; const Double Latitude = 52.108192; const int LongituteTimeZone = 15; const bool UseSummerTime = true; SunCalculator calculator = new SunCalculator(Longitute, Latitude, LongituteTimeZone, UseSummerTime);
The actual calculation of sunrise, sunset and maximum solar radiation can be seen below.
DateTime sunRise = calculator.CalculateSunRise(
new DateTime(2010, 4, 1));
DateTime sunSet = calculator.CalculateSunSet(
new DateTime(2010, 4, 1));
Double maximumSolarRadiation =
calculator.CalculateMaximumSolarRadiation(
new DateTime(2010, 1, 1, 1, 3, 0));
The DateTime returned from CalculateSunRise and CalculateSunSet includes the sunrise and sunset time. For more information take a look at the unit-tests in the assembly AstronomyTest which explain the usage in more detail.
I added an article to The Code Project which also explains the usage of the library including a download link of the source code.
