Solar Position

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]



Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only




Problem Description

The problem is to calculate the time and location of sunrise and  sunset for any location on earth as well as the sun's location for any date and time.    OK, let's throw in the "analemma", a plot of solar location if observed at a fixed time each day for a year. 


Background & Techniques

Another case of "fool's rush in"...   I had little idea what I was getting into when I decided to calculate sunset positions for a plaque I'm planning.  The plaque will be an outline of the mountain peaks from visible from the computer room with sunset positions and dates marked.

Astronomers have several coordinate systems and several time measurement systems to choose from.  Useful for them I suppose, but complicated for the beginner.    After working for several days on a comprehensive TAstronomy component,  I scaled back to the current effort.   Latitude, longitude, azimuth and altitude are the only user interface concepts required.   

Latitude is the North/South angle between the equator and our position (0 to 90 degrees, North or South).   Longitude is the angle that we are around from a circle that rune through the poles and Greenwich, England (0 to 180 degrees, East or West).  Altitude measures the angle from the horizon to some object in  the sky (-90  to 90 degrees, only 0 to 90 visible).  Azimuth is the angle measured clockwise from north to the point on the horizon directly under the object ( 0 to 360 degrees)).       

The central subroutine, SunPos, is adapted from C code written  Joe Cychosz, Purdue University CADLAB.   I fixed an error or two, and changed several equations to more accurate versions found in other programs and texts.  The current version seems to match pretty well with all the calibration points I've tried - if you find any significant errors, please let me know.  

Most of the rest of the code for the basic problem is straightforward, a GetBaseData procedure validates  and converts input fields (latitude, longitude, date, time, and time-zone/daylight saving time offsets).    A TMemo control displays the results.  A plot is also created showing the azimuth angles for sunrise and sunset.  A TPaintBox control provides the canvas for drawing this plot and the analemma (see below).  TDateTimePicker controls provide the fields for inputting date and time info.  

One of the interesting things I learned in writing this program  this week is that it actually takes 366.25 days to make a full orbit of the sun. We lose one revolution for anything inside of our orbit, kind of like rolling one quarter coin around another,  George Washington makes 2 revolutions in one orbit.   

The analemma plot was an afterthought.  An analemma is a that figure 8 shaped graphic that appears on globes and sundials.  It describes the position of the sun if observed at the same time each day for a year.  If the earth's orbit were circular and we weren't tilted in relation to the plane of our orbit, the plot would be a dot, i.e. the sun would be at the same location each day.  Since we're tilted 23.5 degrees, the sun appears high in the sky when our pole is pointed toward the sun and lower 6 months later  when we're pointed away.   The analemma in this case should appear as a straight line, highest on June 21 and lowest on Dec 21 if you live in the northern hemisphere,  reversed in the southern hemisphere.    The second complication arises because the earth travels in an elliptical rather that circular orbit.  This causes its orbital speed around the sun to vary throughout the year and thus sometimes we are ahead of, and sometimes behind the average position.  Our clocks aren't smart enough to speed up and slow down as the the earth's speed changes so if we measure the sun's position at the same clock time each day, sometimes we measure early (left of the mean line) and  sometimes  late (right of the mean line).  The result is the analemma.  In fact the analemma on a sundial is there to tell us how many  minutes we must add or subtract from solar time to get back to average time. 

There are three  plot options for the analemma, each with problems. "Shadow" mode is the projection of the tip of the shadow of a vertical stick onto the ground - for early and late times, the shadows get long (infinite at rising and setting times) which can cause scaling problems.    "Camera South" draws the analemma as a projection onto a south facing vertical plane.  In this version,  a "fisheye lens" effect distorts the figure for early and late times.     I think I figured how to point the camera in a specific direction (using user input date and time) to get better images.   None of this has really been verified, I think that they mostly look reasonable except when measured near the horizon.  The toughest problem was adjusting the scaling and panning so that the analemma remains reasonably positioned in the plot area for different times.             

A discussion of analemma math really needs graphic aids but my pages of scribblings aren't up to publishing quality so I'll defer for a separate Math Topics page in the future.  There are some good definitions and images at this Wikipedia entry 

Addendum -  August 5, 2003:    A viewer from New Zealand pointed that I had his sun rising in the evening and setting in the morning.  A slight southern hemisphere glitch was the cause.  It is fixed with today's posting.     As usual, I found another problem; the analemma graph only worked after the "Sunrise Sunset"  button had been clicked.  Fixed that one also.    

Addendum - July 23, 2004:  Another viewer, Denmark this time, is working on a program to predict the Civil Sunrise and Sunset times used to define daylight hours for pilots flying under Visual Flight Rules (VFR).   Here's a version a that displays that data.   Civil twilight starts and ends when the sun is 6 degrees below the horizon.  

Addendum April 6, 2005:    This is version 2.0 corrects several minor errors and adds an enhancement or two.

  1. There are a few time zones which are not fractional hours increments from GMT. Most of these are an odd number of half hour increments and are included here. 
  2. Near the poles, the Civil twilight calculations caused "Invalid operation errors' when the sun never got within -6 degrees below the horizon. Error message is now produced.
  3. The previous solar plot was confusing as well incorrect under some conditions. It has been replaced by an hourly geocentric projection of the sun on the earth's surface. The image is oriented with the input latittude at right angles to the screen (i.e. pointing directly at the viewer).
  4. A new button sets Date, Time, Time Zone, and Daylight Savings offset to current system values.


Running/Exploring the Program 

Suggestions for Further Explorations

Done Nov 25, 2003 (see this Astronomy Demo page

 The book "Astronomy with Your Personal Computer", Peter Duffett-Smith, Cambridge University Press,  seems to be a respected reference for accurate astronomical calculations.  It contains 250 pages of code and readable  explanatory text.  The code is in Basic - not too bad  to translate to Delphi, if you can copy 10 digit numbers without errors.   I've completed maybe 50% of a TAstronomy component based on this book, but got bogged down trying to match Peter's results for sunrise-sunset calculations.  Close, but darn it, the results should match.   Maybe I'll finish it up one of these days.  If anyone else would like to do so in the meantime, let me know and I'll send what I have.    

There's lots of room for better graphics in this program.  the problem is in presenting three dimensional data in two-dimensional plots. Full motion virtual reality displays will have to wait for a VR-toolbox, but some improvement in the current plots is within reason.    At least add some degree or time axis information to the analemma plots.   


  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.