From 84530cf9fb7edbc01c830fc67359543c39071844 Mon Sep 17 00:00:00 2001 From: Greg Hulands Date: Sun, 13 Aug 2017 14:35:17 -0700 Subject: [PATCH 1/2] Issue 24: Fix negative azimuth values --- pysolar/solar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pysolar/solar.py b/pysolar/solar.py index 40de458..cad5c99 100644 --- a/pysolar/solar.py +++ b/pysolar/solar.py @@ -342,13 +342,13 @@ def get_solar_time(longitude_deg, when): # Topocentric functions calculate angles relative to a location on the surface of the earth. def get_topocentric_azimuth_angle(topocentric_local_hour_angle, latitude, topocentric_sun_declination): - """Measured eastward from north""" + """West is negative, East is positive, Masters p. 395""" tlha_rad = math.radians(topocentric_local_hour_angle) latitude_rad = math.radians(latitude) tsd_rad = math.radians(topocentric_sun_declination) a = math.sin(tlha_rad) b = math.cos(tlha_rad) * math.sin(latitude_rad) - math.tan(tsd_rad) * math.cos(latitude_rad) - return 180.0 + math.degrees(math.atan2(a, b)) % 360 + return (180.0 + math.degrees(math.atan2(a, b))) % 360 def get_topocentric_elevation_angle(latitude, topocentric_sun_declination, topocentric_local_hour_angle): latitude_rad = math.radians(latitude) From 354c82be16099a915b737e38cfa242c8ed0a7d6c Mon Sep 17 00:00:00 2001 From: Greg Hulands Date: Sun, 13 Aug 2017 14:35:17 -0700 Subject: [PATCH 2/2] Issue 24: Fix negative azimuth values --- pysolar/solar.py | 16 +++++++--------- test/testsolar.py | 10 +++++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pysolar/solar.py b/pysolar/solar.py index 40de458..a6cf1c5 100644 --- a/pysolar/solar.py +++ b/pysolar/solar.py @@ -100,11 +100,10 @@ def get_position(latitude_deg, longitude_deg, when, elevation=0, refraction_correction = get_refraction_correction(pressure, temperature, topocentric_elevation_angle) - altitude_deg = topocentric_elevation_angle + refraction_correction + altitude_deg = topocentric_elevation_angle + refraction_correction - azimuth_deg = (180 - - get_topocentric_azimuth_angle(topocentric_local_hour_angle, - latitude_deg, topocentric_sun_declination)) + azimuth_deg = get_topocentric_azimuth_angle(topocentric_local_hour_angle, + latitude_deg, topocentric_sun_declination) return azimuth_deg, altitude_deg @@ -141,9 +140,8 @@ def get_azimuth(latitude_deg, longitude_deg, when, elevation = 0): topocentric_sun_declination, topocentric_local_hour_angle = \ get_topocentric_position(latitude_deg, longitude_deg, when, elevation) - azimuth = (180 - - get_topocentric_azimuth_angle(topocentric_local_hour_angle, - latitude_deg, topocentric_sun_declination)) + azimuth = get_topocentric_azimuth_angle(topocentric_local_hour_angle, + latitude_deg, topocentric_sun_declination) return azimuth @@ -342,13 +340,13 @@ def get_solar_time(longitude_deg, when): # Topocentric functions calculate angles relative to a location on the surface of the earth. def get_topocentric_azimuth_angle(topocentric_local_hour_angle, latitude, topocentric_sun_declination): - """Measured eastward from north""" + """West is negative, East is positive, Masters p. 395""" tlha_rad = math.radians(topocentric_local_hour_angle) latitude_rad = math.radians(latitude) tsd_rad = math.radians(topocentric_sun_declination) a = math.sin(tlha_rad) b = math.cos(tlha_rad) * math.sin(latitude_rad) - math.tan(tsd_rad) * math.cos(latitude_rad) - return 180.0 + math.degrees(math.atan2(a, b)) % 360 + return (180.0 + math.degrees(math.atan2(a, b))) % 360 def get_topocentric_elevation_angle(latitude, topocentric_sun_declination, topocentric_local_hour_angle): latitude_rad = math.radians(latitude) diff --git a/test/testsolar.py b/test/testsolar.py index 422d32f..98bad51 100755 --- a/test/testsolar.py +++ b/test/testsolar.py @@ -161,13 +161,17 @@ class TestApi(unittest.TestCase): test_when = datetime.datetime(2016, 12, 19, 23, 0, 0, tzinfo=datetime.timezone.utc ) def testGetPosition(self): + az, al = solar.get_position(59.6365662,12.5350953, TestApi.test_when) + self.assertAlmostEqual(az, 357.1496112) + self.assertAlmostEqual(al, -53.7673267) + az, al = solar.get_position(-43, 172, TestApi.test_when) - self.assertAlmostEqual(az, -230.4936927) + self.assertAlmostEqual(az, 50.4936927) self.assertAlmostEqual(al, 63.0945557) # From Greenwich az, al = solar.get_position(51.4826, 0, TestApi.test_when) - self.assertAlmostEqual(az, -153.0476242) + self.assertAlmostEqual(az, 333.0476242) self.assertAlmostEqual(al, -59.8384205) def testGetAltitude(self): @@ -176,7 +180,7 @@ def testGetAltitude(self): def testGetAzimuth(self): az = solar.get_azimuth(-43, 172, TestApi.test_when) - self.assertAlmostEqual(az, -230.4936927) + self.assertAlmostEqual(az, 50.4936927) if __name__ == "__main__":