Click here to Skip to main content
Licence CPOL
First Posted 4 May 2010
Views 27,418
Downloads 957
Bookmarked 59 times

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

By | 10 Jan 2011 | Article
A C# assembly for calculating Sunrise, Sunset, and Maximum Solar Radiation
LetsGrow_Weather_WordPress_PlugIn.png

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));
Double maximumSolarRadiation = 
  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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Patrick Kalkman

Architect
http://www.hinttech.nl
Netherlands Netherlands

Member

Follow on Twitter Follow on Twitter
Patrick Kalkman is a senior Software Architect with more than 20 years professional development experience. He has designed and developed the software architecture for real-time process computers for Hoogendoorn and internet-centric applications for LetsGrow.com
 
He works for the company Hinttech developing state of the art web applications.

Patrick enjoys writing his blog. It discusses software architectures using semantic web technologies. Patrick can be reached at pkalkie@gmail.com.
 
Published Windows Phone apps:
Awards:

Best Mobile article of March 2012

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWrong result Pinmembermadcat12:18 23 Mar '12  
AnswerRe: Wrong result [modified] PinmemberPatrick Kalkman4:02 24 Mar '12  
QuestionHyperlink Failure PinmemberVasudevan Deepak Kumar4:34 29 Feb '12  
AnswerRe: Hyperlink Failure PinmemberPatrick Kalkman4:04 24 Mar '12  
Questionwrong calculation PinmemberMember 816554711:08 16 Aug '11  
AnswerRe: wrong calculation PinmemberPatrick Kalkman4:06 24 Mar '12  
GeneralI see an "update" for Nov 8, 2011.... Pinmemberhenslecd2:08 3 Jun '11  
GeneralRe: I see an "update" for Nov 8, 2011.... PinmemberPatrick Kalkman20:43 7 Jun '11  
GeneralNot able to get corret sunrise and sunset time PinmemberParesh Rathod9:56 11 May '11  
GeneralRe: Not able to get corret sunrise and sunset time PinmemberPatrick Kalkman2:24 17 May '11  
GeneralSky brightness PinmemberAndrew Jones4:55 19 Sep '10  
Questionhow to determine timezone Pinmembermartkoch23:06 5 Sep '10  
AnswerRe: how to determine timezone PinmemberPatrick Kalkman0:58 6 Sep '10  
QuestionCould this be used to calculate the time of day that a given solar angle occurs? [modified] Pinmembertidyup0:11 3 Sep '10  
QuestionCould this help the accuracy? Pinmemberhenslecd17:46 30 May '10  
AnswerRe: Could this help the accuracy? PinmemberPatrick Kalkman6:51 31 May '10  
GeneralRe: Could this help the accuracy? Pinmemberhenslecd6:59 31 May '10  
GeneralRe: Could this help the accuracy? Pinmemberhenslecd16:44 18 Jun '10  
GeneralIncorrect PinmemberZamir Farooqi12:08 27 May '10  
GeneralRe: Incorrect PinmemberPatrick Kalkman18:47 27 May '10  
GeneralRe: Incorrect PinmemberPatrick Kalkman19:52 27 May '10  
GeneralRe: Incorrect PinmemberZamirF7:15 28 May '10  
QuestionExample? Pinmembert.a berglund9:36 27 May '10  
AnswerRe: Example? PinmemberPatrick Kalkman18:49 27 May '10  
AnswerRe: Example? PinmemberPatrick Kalkman20:19 27 May '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Jan 2011
Article Copyright 2010 by Patrick Kalkman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid