Calculation of Sunshine Duration


The weather station I have, the Fine Offset HP1000 Weather Station with WiFi, has a brightness sensor. From this, it also calculates solar radiation. This information is uploaded to Weather Underground (Wunderground). However, neither the weather station nor Wunderground calculate actual sunshine time. There are instruments which do allow this to be recorded, such as the Campbell-Stokes recorder and some other electronic weather stations and weather software, however there is an element of uncertainty in making this calculation due to climatic variations, thin cloud, fog, pollution etc.

In order to make a calculation, I have used the "Carpentras Method". This has been developed at the World Meteorolgical Organisation (WMO) Regional Radiation Centre in Carpentras, France in 1998 and 2012. This is discussed in further detail here. This is a draft document, but is used for the purposes of my analysis. I have written code in PHP which uses the information uploaded and the equations defined in the above documentation to calculate the amount of sunshine time for any date, for any weather station which uploads solar radiation data to Wunderground.

If you wish to calculate the sunshine duration for any date using information uploaded to Wunderground, click here. All you need is the Wunderground station ID, e.g. INEWSOUT435, and the date you wish to calculate the sunshine duration for.

What Do You Need?

  1. In order to access the data, you need access to the Weather Underground API including history. This is free up to a certain level of usage and you can request an API code here.
  2. You need to know the wunderground station ID of the station you are analysing.
  3. You need webspace to upload your PHP file to so that you can run the code. You will need to know your website address as part of the process of registering your API.
  4. You will need to use the code below and alter the details in the top section to reference the site you wish to calculate sunshine time for and the date.

The Equations

So here's the science bit! Referring to the WMO document referenced above, there are several steps in the calculation.

1)    If the elevation of the sun is below 3° above the horizon, it is considered to be not sunny.

If the elevation is above 3°, then a calculation is needed. If the measured solar radiation, referred to as G is greater than a threshold radiation, referred to as Gthr, then it is considered as sunny.

2)    To calculate Gthr further equations are needed:

Gthr = F x Mod                                  (1)
Mod = 1080 (sin(h))1.25                     (2)
F = A + B x cos (2 x pi x d / 365)       (3)

A and B are variables that vary by location. Generally, A is considered to increase with latitude. B is generally negative in the southern hemisphere. There will need to be an element of trial and error and analysis to refine these. However for Sydney, Australia, I use

A = 0.5
B = -0.05

The other variables which are not defined at this stage are h and d. h is the elevation of the sun. d is the day of the year. d can be calculated using a PHP function. h is calculated using equations described on Wikipedia at

3)     Calculating h (sun's elevation)

The equations on the Wikipedia article are as follows

sin(elev) = cos (ha) x cos (dec) x cos (lat) + sin (dec) x sin (lat)        (4)


elev is the sun's elevation (h in above formula)
ha is the sun's hour angle
dec is the sun's declination
lat is the latitude of the observing site. This is recorded in the Wunderground data.

4)    So this creates the need to solve ha and dec. Wikipedia also gives equations for the declination.

dec = -23.44 x cos ((360/365) x (N+10)         (5)

where N is the day of the year. This can be calculated using a PHP function.

ha is the local solar angle and is 0 degrees at the solar noon. It is 15° less for each hour before noon and 15° more for each hour after. PHP has a function to calculate sunrise and sunset times, based on latitude, longitude and date. The solar noon is half way between these times. The calculations for time are all done using the UNIX timestamp as Wunderground gives times in this format and PHP can output in this format. This removes the need for time zone correction.

So putting all of that together, we can now solve for all the variables to determine at any time, anywhere, based on the solar radiation reading, whether it is sunny.

We then write a program in PHP which reads in the Wunderground values for any given date at a weather station and for each entry determines whether it was sunny or not. If it was sunny, then the daily total is simply incremented by the interval between readings.

The Code

The code below uses the information above. You can paste this into your website, updating the variables in the top section and it will calculate the sunshine time for the date you have entered. Then simply loading the web page will output the sunshine hours. Click here for an example.

If you have any comments, or want any information, please send me an  email at

This page was last updated 10 November 2016.

Click here to go to the main weather page.