I am working on algorithm that determines current azimuth and altitude of the sun, at given place,date and time. I need it for my automatic dual-axis solar power calibration project.
I'm using Astropy library.
Here's my code:
from astropy.coordinates import get_sun, AltAz, EarthLocation
import astropy.coordinates as coord
from astropy.time import Time
import astropy.units as u
import time
#location set to a city of Kielce, Poland
loc = coord.EarthLocation(lon=20.628569 * u.deg, lat=50.866077 * u.deg)
my_time = Time.now()
sun = coord.get_sun(my_time)
print(sun)
print("Location:", loc)
print("Time/date: ", my_time)
print("--------DEGREES--------")
while True:
#line below causes wrong results
my_time = my_time + 15 * u.min
#line below works fine and displays correct results
#my_time = Time.now()
sun = coord.get_sun(my_time)
altaz = coord.AltAz(location=loc, obstime=my_time)
current_altitude = get_sun(my_time).transform_to(altaz).alt
current_azimuth = get_sun(my_time).transform_to(altaz).az
geo = loc.geodetic
print("Time: ", my_time)
print("Loc. lat.: ", geo.lat)
print("Loc. lon.: ", geo.lon)
print("Sun alt.: ", current_altitude.degree)
print("Sun az.: ", current_azimuth.degree)
print("---------------------------------------------------")
input()
When I'm using...
my_time = Time.now()
Algorithm seems to be working fine. I've been testing results for past 48 hours and it resulted in correct answers everytime. I used website SunCalc.org to verify if it works correctly.
What I need to do is to determine position of the sun for every 15 minutes starting form current date and hour in order to store it in some sort of an array. But I can't do it since time incrementation results in wrong calculations. Here is the example of a wrong results (python shell output):
---------------------------------------------------
Time: 2019-10-23 15:08:33.073504
Loc. lat.: 50d51m57.8772s
Loc. lon.: 20d37m42.8484s
Sun alt.: 2.3219705846241796
Sun az.: 248.6310211206791
---------------------------------------------------
Time: 2019-10-23 15:23:33.073504
Loc. lat.: 50d51m57.8772s
Loc. lon.: 20d37m42.8484s
Sun alt.: 0.09371196329584214
Sun az.: 251.5553099977351
---------------------------------------------------
Time: 2019-10-23 15:38:33.073504
Loc. lat.: 50d51m57.8772s
Loc. lon.: 20d37m42.8484s
Sun alt.: -2.172539131558753
Sun az.: 254.45000928004316
---------------------------------------------------
I'm 100% sure it's wronge since sunset should occur at 17:30. I started using Python yesterday with little prior knowlege of programming (a bit of C and C++) so please be understanding.
Thank you!