Click here to Skip to main content
Click here to Skip to main content

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

By , 10 Jan 2011
 
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
Patrick Kalkman is a senior Software Architect with more than 20 years professional development experience. He works for Hinttech where he develops 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 8 apps:
 
Published Windows Phone apps:
 
Awards:

Best Mobile article of March 2012
Best Mobile article of June 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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberridoy9 May '13 - 9:53 
Excellent work indeed..
GeneralMy vote of 5memberSperneder Patrick30 Apr '13 - 4:02 
Some time ago i also implemented a library that should solve this calculations... Now i understand the Need of using the timezone in my formula Glad you solved it, updated my code...
GeneralRe: My vote of 5memberPatrick Kalkman30 Apr '13 - 4:22 
Thanks Patrick Patrick Kalkman   My latest article: Weekly Thai Recipe! for Windows Phone My Blog: SemanticArchitecture.net
QuestionHow to calcualte LongituteTimeZone?memberfortuna_621 Jan '13 - 8:14 
Hi Patrick:   How do I calculate LongituteTimeZone?   Thanks, Boris
AnswerRe: How to calcualte LongituteTimeZone?memberPatrick Kalkman30 Apr '13 - 4:24 
Hi Boris,   The LongituteTimeZone is the nearest longitude in multiple of 15 of the timezone of which you are calculating the sun rise or sun set.   Hope this helps. Patrick Kalkman   My latest article: Weekly Thai Recipe! for Windows Phone My Blog: SemanticArchitecture.net
GeneralMy vote of 4memberChristian Amado23 Aug '12 - 11:54 
Nice idea!
GeneralRe: My vote of 4memberPatrick Kalkman30 Apr '13 - 4:18 
Thank you very much Christian. Patrick Kalkman   My latest article: Weekly Thai Recipe! for Windows Phone My Blog: SemanticArchitecture.net
QuestionHi Patrick:membersquizfloats17 Aug '12 - 12:53 
This looks like really useful code. Thanks for posting it. Unfortunately, I am confused about the definition of the third parameter (LongituteTimeZone) in the constructor: SunCalculator(Longitute, Latitude, LongituteTimeZone, UseSummerTime)   It appears to be the same as Longitute, but...
AnswerRe: Hi Patrick:memberPatrick Kalkman30 Apr '13 - 4:22 
Hi Squizfloats,   The longitude is the longitude of your timezone in multiples of 15 degrees. I use the nearest multiple. Patrick Kalkman   My latest article: Weekly Thai Recipe! for Windows Phone My Blog: SemanticArchitecture.net
QuestionSolar radiationmemberRuben Tacq3 Jul '12 - 21:11 
Hi,   Very nice application but I am looking to extend it. The maximum solar radiation calculated, is this for a flat surface? I would like to calculate the radiation under a given corner, e.g. a solar pannel.   Any ideas?   Thx!
AnswerRe: Solar radiationmemberPatrick Kalkman30 Apr '13 - 4:19 
Hi Ruben,   It is indeed for a flat surface. Patrick Kalkman   My latest article: Weekly Thai Recipe! for Windows Phone My Blog: SemanticArchitecture.net
QuestionWrong resultmembermadcat23 Mar '12 - 12:18 
Why would you post a programme that has incorrect results? I tried 29N -82W -5 for time offset and it is off by hours. It may work in the Netherlands but no in North America.
AnswerRe: Wrong result [modified]memberPatrick Kalkman24 Mar '12 - 4:02 
Hi Madcat,   I created a test case for Miami which calculates the sunrise and sunset times for Miami. It is calculated correctly, I checked with Sunrise and Sunset for U.S   I created the SunCalculator for the test case as follows.   var calculator = new...
QuestionHyperlink FailurememberVasudevan Deepak Kumar29 Feb '12 - 4:34 
http://www.semanticarchitectur...
AnswerRe: Hyperlink FailurememberPatrick Kalkman24 Mar '12 - 4:04 
Thanks Vasudevan!   I will update the article. Patrick Kalkman   My latest article: PDF reporting using ASP.NET MVC3 My Blog: SemanticArchitecture.net
Questionwrong calculationmemberMember 816554716 Aug '11 - 11:08 
I set the following coordinates: Lat 55.8747 Long 37.3571 Timezone is GMT +4 UseSummerTime = false Date 17.08.2011   The program gives sunrise at 2:31 AM But sunrise in reality at 05:04
AnswerRe: wrong calculationmemberPatrick Kalkman24 Mar '12 - 4:06 
Hi Member 8165547,
 
I think you made mistake with using the api of the Suncalculator. Are you sure that you entered the correct value for the timezone. In you case this should be set to 45.
Patrick Kalkman
 
My latest article: PDF reporting using ASP.NET MVC3
My Blog: SemanticArchitecture.net

GeneralI see an "update" for Nov 8, 2011....memberhenslecd3 Jun '11 - 2:08 
Have you had a chance to integrate the code from NOAA?
GeneralRe: I see an "update" for Nov 8, 2011....memberPatrick Kalkman7 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:...
GeneralNot able to get corret sunrise and sunset timememberParesh Rathod11 May '11 - 9:56 
Hi,   Below are the Lat/Lan for my location:   Lat - 22.21290940 Lang - 70.384205   I don't know why, but some reason i am not getting the correct sunrise/sunset time. I am getting mid-night times like 12:00 AM and 1 AM as a sunrise time.   Can you please help me...
GeneralRe: Not able to get corret sunrise and sunset timememberPatrick Kalkman17 May '11 - 2:24 
Hello Paresh,   Thanks for your feedback. Do have a test date so that I can recreate and look into the issue? Patrick Kalkman   My latest article: Androng, a Pong clone for Android My Blog: SemanticArchitecture.net
GeneralSky brightnessmemberAndrew Jones19 Sep '10 - 4:55 
Thanks for this code it's great.   Could you clarify how to use the LongituteTimeZone value. Should London be 0 or 15?   How easy would it be to also output the theoretical brightness (roughly) of the sky? I display a background on a temperature chart indicating if it's day (white)...
Questionhow to determine timezonemembermartkoch5 Sep '10 - 23:06 
hi patrick, great work! could you please be so kind and tell me, how the timezone is defined. isn't utrecht UTC-1 ? i don't know where you get timezone 15 from for your calculation for utrecht.   br, martin
AnswerRe: how to determine timezonememberPatrick Kalkman6 Sep '10 - 0:58 
Hi Martin,   Thanks.   It specifies the longitude of the timezone. Each timezone represents approximately 15 degrees, it specifies that Utrecht falls in the first timezone from 0 - 15 degrees. The algorithm uses it for an internal correction.   For more information check...
QuestionCould this be used to calculate the time of day that a given solar angle occurs? [modified]membertidyup3 Sep '10 - 0:11 
Great job! Could this be used to calculate the time of day that the sun is at a given angle and the amount of solar radiation at that time? There may be multiple times that the sun passes through any given angle depending on the observers position, if this is possible how could it be...

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

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