Skip to content

Commit

Permalink
Merge pull request #54 from ghulands/fix_issue_24
Browse files Browse the repository at this point in the history
Issue 24: Fix negative azimuth values
  • Loading branch information
pingswept committed Aug 14, 2017
2 parents 7a0a7b6 + b2fa4ea commit 55debbb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
16 changes: 7 additions & 9 deletions pysolar/solar.py
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions test/testsolar.py
Expand Up @@ -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):
Expand All @@ -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__":
Expand Down

0 comments on commit 55debbb

Please sign in to comment.