hima Fernando
hima Fernando

Reputation: 147

How can we compute solar position at a given place on a given day and time?

I have UTC time (hours, minutes, seconds), longitude(deg E), latitude (deg N) and date. Can anyone provide me with a code to calculate solar zenith angle in Python 2.7?

Upvotes: 5

Views: 9663

Answers (2)

eteq
eteq

Reputation: 980

@alecxe's answer is great, but I thought I'd add a slight modification that's a bit closer to what the original question is asking (the zenithal angle at a specific time)

from astropy.coordinates import get_sun, AltAz, EarthLocation
from astropy.time import Time

sun_time = Time('2017-12-6 17:00') #UTC time
loc = EarthLocation.of_address('Baltimore, MD')  # anything the google geocoding API resolves
altaz = AltAz(obstime=sun_time, location=loc)

zen_ang = get_sun(sun_time).transform_to(altaz).zen

zen_ang is then an Angle object - see more about those in the docs, but basically they end up working like numpy scalars with associated units of "degrees".

Upvotes: 8

alecxe
alecxe

Reputation: 473863

It is an interesting problem and I think I have a good answer - well, at least starting points.

Check out the awesome astropy package. I believe you need to use the coordinates module.

Something along these lines:

import astropy.coordinates as coord
from astropy.time import Time
import astropy.units as u


loc = coord.EarthLocation(lon=0.1 * u.deg,
                          lat=51.5 * u.deg)
now = Time.now()

altaz = coord.AltAz(location=loc, obstime=now)
sun = coord.get_sun(now)

print(sun.transform_to(altaz).alt)

Here, we are getting the angle of the sun above the horizon for the 0.1 degrees longitude and 51.5 latitude location at the current time.

FYI, .zen would give you the zenith angle.

Upvotes: 12

Related Questions