Skip to content

Commit f202c5d

Browse files
committed
Add test script for solar angle calculations (Issue pvlib#2448)
- Created test_solar_angles.py to verify solar angle calculations - Tests zenith, azimuth, and elevation angles for different times of day - Uses New York City as example location on spring equinox - Verifies angles are within expected ranges and follow correct patterns
1 parent 7c0feba commit f202c5d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test_solar_angles.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pvlib
2+
import pandas as pd
3+
from datetime import datetime
4+
import pytz
5+
6+
def test_solar_angles():
7+
# Create a location (example: New York City)
8+
latitude = 40.7128
9+
longitude = -74.0060
10+
tz = 'America/New_York'
11+
location = pvlib.location.Location(latitude, longitude, tz=tz)
12+
13+
# Create a time range for one day
14+
start = pd.Timestamp('2024-03-20', tz=tz) # Spring equinox
15+
times = pd.date_range(start=start, periods=24, freq='H')
16+
17+
# Calculate solar position
18+
solpos = location.get_solarposition(times)
19+
20+
# Print results for key times
21+
print("\nSolar Angles for New York City on Spring Equinox:")
22+
print("=" * 50)
23+
24+
# Morning (9 AM)
25+
morning = solpos.loc['2024-03-20 09:00:00-04:00']
26+
print("\nMorning (9 AM):")
27+
print(f"Solar Zenith: {morning['zenith']:.2f}°")
28+
print(f"Solar Azimuth: {morning['azimuth']:.2f}°")
29+
print(f"Solar Elevation: {morning['elevation']:.2f}°")
30+
31+
# Solar Noon
32+
noon = solpos.loc['2024-03-20 12:00:00-04:00']
33+
print("\nSolar Noon:")
34+
print(f"Solar Zenith: {noon['zenith']:.2f}°")
35+
print(f"Solar Azimuth: {noon['azimuth']:.2f}°")
36+
print(f"Solar Elevation: {noon['elevation']:.2f}°")
37+
38+
# Evening (3 PM)
39+
evening = solpos.loc['2024-03-20 15:00:00-04:00']
40+
print("\nEvening (3 PM):")
41+
print(f"Solar Zenith: {evening['zenith']:.2f}°")
42+
print(f"Solar Azimuth: {evening['azimuth']:.2f}°")
43+
print(f"Solar Elevation: {evening['elevation']:.2f}°")
44+
45+
# Verify the angles make sense
46+
print("\nVerification:")
47+
print("- Zenith angle should be between 0° and 90°")
48+
print("- Azimuth should be between 0° and 360°")
49+
print("- Elevation should be between -90° and 90°")
50+
print("- At solar noon, the sun should be at its highest point")
51+
print("- The sun should rise in the east (azimuth ~90°) and set in the west (azimuth ~270°)")
52+
53+
if __name__ == "__main__":
54+
test_solar_angles()

0 commit comments

Comments
 (0)