13,050,896 members (64,122 online)
alternative version

#### Stats

81.9K views
78 bookmarked
Posted 4 May 2010

# Solar Calculator - Calculate Sunrise, Sunset, and Maximum Solar Radiation

, 10 Jan 2011
 Rate this:
A C# assembly for calculating Sunrise, Sunset, and Maximum Solar Radiation

## Introduction

I was looking for a .NET library to calculate the time the sun rises and sets. There were some libraries available, including here on The Code Project, but they had some problems. After trying some, I decided to create one myself. I needed the sunrise, sunset, and maximum solar radiation for a WordPress plug-in on my blog. The plug-in shows a map of the Netherlands together with the current weather conditions. The top of this post shows a screenshot of the plug-in. The map shows the real-time temperature including an icon. The icon is a translation of the measured solar radiation against the maximum solar radiation.

## Background

It is possible to calculate the sunrise, sunset, and maximum solar radiation using some known algorithms. For those of you who are interested in these algorithms, 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 at how to use the code.

## Using the Code

The code is packed in a Visual Studio 2008 solution. It contains two assemblies: Astronomy and AstronomyTest. The assembly Astronomy contains the SunCalculator class which performs the actual calculation. The assembly AstronomyTest contains several unit-tests that validate the calculations against external sources.

SunCalculator is a single class that does not depend on external classes. Although this somewhat goes against the Single Responsibility Principle, it makes reuse of this class easier. The SunCalculator class needs the longitude, latitude, and time-zone of your location. You should also indicate whether to use daylight savings. An instance of the SolarCalculator can be created like this:

const Double Longitute = 5.127869;
const Double Latitude = 52.108192;
const int LongituteTimeZone = 15;
const bool UseSummerTime = true;

SunCalculator sunCalculator = new SunCalculator(Longitute, Latitude,
LongituteTimeZone, UseSummerTime);

You have to supply the longitude and the latitude from the location that you want to calculate the sunrise and sunset time. These are the two first arguments of the constructor. For locations that use daylight savings, you should set UseSummerTime to the actual daylight savings status. For locations that don't use daylight savings, set it to false.

The actual calculation of sunrise, sunset, and maximum solar radiation can be seen below:

DateTime sunRise = sunCalculator.CalculateSunRise(new DateTime(2010, 4, 1));
DateTime sunSet = sunCalculator.CalculateSunSet(new DateTime(2010, 4, 1));
sunCalculator.CalculateMaximumSolarRadiation(new DateTime(2010, 1, 26, 16, 30, 0));

The DateTime that is 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.

## Points of Interest

The code first calculates the declination of the sun, cosine of the sun position, sinus of the sun position, and the difference between the solar and the actual time. All these parameters are used to calculate the sunrise and sunset times.

If you want to see the plug-in live, see my blog www.semanticarchitecture.net. The data that is retrieved and shown on the map comes from LetsGrow.com, the company that I work for.

## History

• v1.0 02/04/2010: Initial and first release
• v1.1 28/05/2010: Added a test case for Los Angeles, and a Console application that demonstrates the library in the source code
• v1.2 08/11/2011: Fixed failing tests

## Share

 Architect http://www.simpletechture.nl Netherlands
Patrick Kalkman is a senior Software Architect with more than 20 years professional development experience. He works for SimpleTechture where he helps teams develop state of the art web applications.

Patrick enjoys writing his blog. It discusses agile software development. Patrick can be reached at patrick@simpletechture.nl.

Published Windows 8 apps:

Published Windows Phone apps:

Awards:

Best Mobile article of March 2012
Best Mobile article of June 2012

## You may also be interested in...

 First Prev Next
 What is the value of longitudeTimeZone v8dave3-Nov-15 12:14 v8dave 3-Nov-15 12:14
 Re: What is the value of longitudeTimeZone Garth J Lancaster3-Nov-15 12:38 Garth J Lancaster 3-Nov-15 12:38
 Why can't you calculate the longituteTimeZone parameter? hichaeretaqua18-May-14 8:07 hichaeretaqua 18-May-14 8:07
 Re: Why can't you calculate the longituteTimeZone parameter? Riaan Stander9-Oct-14 12:50 Riaan Stander 9-Oct-14 12:50
 Re: Why can't you calculate the longituteTimeZone parameter? RBinSFla19-Feb-15 7:40 RBinSFla 19-Feb-15 7:40
 My vote of 5 ridoy9-May-13 9:53 ridoy 9-May-13 9:53
 My vote of 5 Sperneder Patrick30-Apr-13 4:02 Sperneder Patrick 30-Apr-13 4:02
 Re: My vote of 5 Patrick Kalkman30-Apr-13 4:22 Patrick Kalkman 30-Apr-13 4:22
 How to calcualte LongituteTimeZone? fortuna_621-Jan-13 8:14 fortuna_6 21-Jan-13 8:14
 Re: How to calcualte LongituteTimeZone? Patrick Kalkman30-Apr-13 4:24 Patrick Kalkman 30-Apr-13 4:24
 Re: My vote of 4 Patrick Kalkman30-Apr-13 4:18 Patrick Kalkman 30-Apr-13 4:18
 Hi Patrick: squizfloats17-Aug-12 12:53 squizfloats 17-Aug-12 12:53
 Re: Hi Patrick: Patrick Kalkman30-Apr-13 4:22 Patrick Kalkman 30-Apr-13 4:22
 Solar radiation Ruben Tacq3-Jul-12 21:11 Ruben Tacq 3-Jul-12 21:11
 Re: Solar radiation Patrick Kalkman30-Apr-13 4:19 Patrick Kalkman 30-Apr-13 4:19
 Re: Wrong result Patrick Kalkman24-Mar-12 4:02 Patrick Kalkman 24-Mar-12 4:02
 Hyperlink Failure Vasudevan Deepak Kumar29-Feb-12 4:34 Vasudevan Deepak Kumar 29-Feb-12 4:34
 Re: Hyperlink Failure Patrick Kalkman24-Mar-12 4:04 Patrick Kalkman 24-Mar-12 4:04
 wrong calculation Member 816554716-Aug-11 11:08 Member 8165547 16-Aug-11 11:08
 Re: wrong calculation Patrick Kalkman24-Mar-12 4:06 Patrick Kalkman 24-Mar-12 4:06
 I see an "update" for Nov 8, 2011.... henslecd3-Jun-11 2:08 henslecd 3-Jun-11 2:08
 Re: I see an "update" for Nov 8, 2011.... Patrick Kalkman7-Jun-11 20:43 Patrick Kalkman 7-Jun-11 20:43
 Hi henslecd, Not yet, I am working on an article that describes an Android app that I am developing. I am using the algorithms in the App. I want to incorporate the NOAA code in that app. Patrick Kalkman My latest article: Androng, a Pong clone for Android My Blog: SemanticArchitecture.net
 Not able to get corret sunrise and sunset time Paresh Rathod11-May-11 9:56 Paresh Rathod 11-May-11 9:56
 Re: Not able to get corret sunrise and sunset time Patrick Kalkman17-May-11 2:24 Patrick Kalkman 17-May-11 2:24
 Sky brightness Andrew Jones19-Sep-10 4:55 Andrew Jones 19-Sep-10 4:55
 how to determine timezone martkoch5-Sep-10 23:06 martkoch 5-Sep-10 23:06
 Re: how to determine timezone Patrick Kalkman6-Sep-10 0:58 Patrick Kalkman 6-Sep-10 0:58
 Could this be used to calculate the time of day that a given solar angle occurs? [modified] tidyup3-Sep-10 0:11 tidyup 3-Sep-10 0:11
 Could this help the accuracy? henslecd30-May-10 17:46 henslecd 30-May-10 17:46
 Re: Could this help the accuracy? Patrick Kalkman31-May-10 6:51 Patrick Kalkman 31-May-10 6:51
 Re: Could this help the accuracy? henslecd31-May-10 6:59 henslecd 31-May-10 6:59
 Re: Could this help the accuracy? henslecd18-Jun-10 16:44 henslecd 18-Jun-10 16:44
 Incorrect Zamir Farooqi27-May-10 12:08 Zamir Farooqi 27-May-10 12:08
 Re: Incorrect Patrick Kalkman27-May-10 18:47 Patrick Kalkman 27-May-10 18:47
 Re: Incorrect Patrick Kalkman27-May-10 19:52 Patrick Kalkman 27-May-10 19:52
 Re: Incorrect ZamirF28-May-10 7:15 ZamirF 28-May-10 7:15
 Example? t.a berglund27-May-10 9:36 t.a berglund 27-May-10 9:36
 Re: Example? Patrick Kalkman27-May-10 18:49 Patrick Kalkman 27-May-10 18:49
 Re: Example? Patrick Kalkman27-May-10 20:19 Patrick Kalkman 27-May-10 20:19
 Re: Example? t.a berglund28-May-10 5:22 t.a berglund 28-May-10 5:22
 Last Visit: 31-Dec-99 18:00     Last Update: 25-Jul-17 22:51 Refresh 1