3

I am using PyEphem to calculate the location of the Sun in the sky at various times.

I have an Observer point (happens to be at Stonehenge) and can use PyEphem to calculate sunrise, sunset, and the altitude angle and azimuth (degrees from N) for the Sun at any hour of the day. Brilliant, no problem.

However, what I really need is to be able to calculate the altitude angle of the Sun from an known azimuth. So I would set the same observer point (long/lat/elev/date (just yy/mm/dd, not time)) and an azimuth for the Sun. And from this input, calculate the altitude of the Sun and the time it is at that azimuth.

I had hoped I would be able to just set Sun.date and Sun.az and work backwards from those values, but alas. Any thoughts on how to approach this (and if it even is approachable) with PyEphem?

The only other option I'm seeing available is to "sneak up" on the azimuth by iterating over a sequence of times until I get within a margin of error of the azimuth I desire, but that is just gross.

thanks in advance, Dave

IT Ninja
  • 6,174
  • 10
  • 42
  • 65
davehunt00
  • 31
  • 3

2 Answers2

2

Astronomy software predicts the location of the Sun by taking JPL predictions of where the Earth and Sun will be, which the JPL expresses as a series of polynomials that cover specific ranges of dates. Asking “when will the sun be at azimuth z?” is asking when three different polynomials, that are each varying at a different rate (the polynomial for the Sun, for the Earth-Moon barycenter revolving around the Sun, and the Earth revolving around the barycenter), will happen to bring the difference between the two positions to precisely a certain angle.

And, it turns out, that problem falls into the class of “gross” math problems — or, as professionals say, “non-closed-form-solution problems.” But I like your word “gross” because it catches very well how most of us feel when we discover that much of the world has to be tackled by trial-and-error instead of just giving us an answer.

Fortunately, a vast enough swatch of science is “gross” in this sense that there are standard ways of asking “when will this big complicated function reach exactly value z?” If you are able to install and try out SciPy, the increasingly popular science library for Python, you will find that it has a whole collection of routines that sneak up on solutions, each using a different tactic. The other answerer has already identified one such tactic — halving the search space with each trial — but that is generally the slowest (though in some extreme cases, the safest) approach; here are some others:

http://docs.scipy.org/doc/scipy/reference/optimize.html

Create a little function that returns “how far off” the Sun's azimuth is a time t from the azimuth you want, where the function will finally return zero when the azimuth is exactly right, like:

def f(t):
    ...        
    return desired_az - sun.az

Then try out one of the “root finding scalar functions” from that SciPy page. The bisect() function will, just like the other answerer suggests, keep cutting the search space in half to narrow things down. But my guess is that you'll find a Newton's method to be far less “gross” and far faster — try newton() or brentq(), and see what happens!

Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147
  • thank you! I will check these out and report back on which direction I take. I appreciate the time you took to respond. D – davehunt00 Feb 25 '13 at 02:39
0

Without knowing the details of the internal calculations that PyEphem is doing I don't know how easy or difficult it would be to invert those calculations to give the result you want.

With regards to the "sneaking up on it" option however, you could pick two starting times (eg sunrise and noon) where the azimuth is known to be either side (one greater and one less than) the desired value. Then just use a simple "halving the interval" approach to quickly find an approximate solution.

Stuart
  • 875
  • 6
  • 12