Monday, February 28, 2005

Ride into the sunset

So I finally got around to writing a program that calculates sunrise and sunset. What a pain in the ass that turned out to be.

There are dozens of websites that will do the calculations for you but I wanted a pseudo-code algorithm. Eventually, I found one from the US Naval Observatory but my trials were far from over.

I learned that there is no single algorithm that will produce acceptable results for all places and for all time. I guess that certain parameters need to be tweaked as you extend your calculations towards dates more than a couple of decades away from some reference point.

Also, I discovered that there is a lot of disagreement between the results of various calculators. This may have something to do with the different definitions of sunrise and sunset. To me, sunset is the moment at which the last visible sliver of the sun slips beneath the horizon but this is actually only one of three possible definitions. What's more, the observed time of sunset/sunrise is effected by atmospheric conditions which can only be tackled probabilistically.

So after a lot of trial and error I managed to get my program to produce results that matched up with the worked example that accompanied the pseudo code. However, the algorithm doesn't really have any explanatory material so I don't really understand the terms (what the hell is "sun's mean anomaly" for instance) and I don't know whether it's trying to account for any atmospheric conditions or for how many years this algorithm will product good results. Lame.

Theoretically, I should be able to bike the few miles to the ocean and check my results against reality but I never seem to have the time and really, I just wrote the thing to know how late I can leave work and still have sunlight enough to get me home so as long as it's correct to within 5 minutes I'm pretty happy.