import math

def calculate_angle_of_incidence(solar_elevation, solar_azimuth, panel_tilt, panel_azimuth):
    """Calculate angle of incidence between sun and panel surface"""
    if solar_elevation <= 0:
        return 0
    
    # Convert to radians
    se_rad = math.radians(solar_elevation)
    sa_rad = math.radians(solar_azimuth)
    pt_rad = math.radians(panel_tilt)
    pa_rad = math.radians(panel_azimuth)
    
    # Angle of incidence formula
    cos_incidence = (
        math.sin(se_rad) * math.cos(pt_rad) +
        math.cos(se_rad) * math.sin(pt_rad) * math.cos(sa_rad - pa_rad)
    )
    
    return cos_incidence

# June 15, 10:00 - elevation 9.7°, azimuth 39°
solar_elev = 9.7
solar_az = 39.0

print(f"June 15, 10:00: Sun at {solar_elev}° elevation, {solar_az}° azimuth")
print()

# Test different tilts facing North (0°)
tilts = [70, 90]  # 70° vs 90° (vertical)

for tilt in tilts:
    cos_inc = calculate_angle_of_incidence(solar_elev, solar_az, tilt, 0)  # North facing
    angle_inc = math.degrees(math.acos(abs(cos_inc))) if cos_inc != 0 else 90
    
    print(f"Panel tilt {tilt}° (North-facing):")
    print(f"  cos_incidence = {cos_inc:.4f}")
    print(f"  angle of incidence = {angle_inc:.1f}°")
    print(f"  Direct irradiance factor = {max(0, cos_inc):.4f}")
    print()

print("Expected:")
print("- For low sun (9.7°), vertical panel should be better than 70° tilt")
print("- Vertical panel faces more directly toward the low sun")
print()

# Check what angle would be optimal for 9.7° sun
optimal_angle = 90 - solar_elev
print(f"Optimal panel angle for {solar_elev}° sun: {optimal_angle:.1f}°")
print("So 80.3° would be optimal, meaning 90° vertical should be better than 70°")