Skip to content

Commit 2c327e9

Browse files
authored
Create 'NOON-SUN-ANGLE.py' - Trianglium (#70)
* Create Moon-phase.py - Trianglium # Moon Phase ### A Simple program to calculate the current (approximate) Moon Phase. ## Requirements This program uses a module in python's standard library, 'datetime' ## Inspiration ['Calculate Moon Phase' by Subsystems.us](https://www.subsystems.us/uploads/9/8/9/4/98948044/moonphase.pdf) ### Status This program runs completely fine, 'as is'. Though, **I'm open to any suggestions on how to make the moon age and moon phase names, more accurate**. There was mixed information online and the phases mentioned in the 'inspiration' article, were opposite of the actual moon phases. ***hacktoberfest2021*** * create 'noon-sun-angle.py' - Trianglium # Noon Sun Angle ### A Simple program to calculate the current (approximate) Angle of the Sun at Noon based on your location, the current date, its in relation to the most recent equinox or solstice and the subsolar point. ## Usage #### Windows: >python noon-sun-angle.py ### Output 20 October 2021 Noon Sun Angle: 37.9120 Sunlight Intensity: High Zenith Angle: 42.980 ### Requirements * Python's standard [**Datetime**](https://docs.python.org/3/library/datetime.html) Module [Geocoder](https://pypi.org/project/geocoder/) ## Inspiration #### ['Pulse of the Planet: Calculating Sun Angles' (lesson plan) by Arizona State University](https://geoalliance.asu.edu/node/171)
1 parent fce96a9 commit 2c327e9

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

noon-sun-angle.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import datetime
2+
import geocoder
3+
4+
class Sun_Angle:
5+
6+
def __init__(self, latitude=''):
7+
# Latitude of User (Optional) - Will use IP address of device for latitude if nothing is entered.
8+
self.latitude = latitude
9+
# Classification of how intense the Sunlight is vs. the angle of the Sun at noon. The article only gave two categories: Low, or High.
10+
self.ray_intensity= [('LOW', 45.0), ('HIGH', 90.0)]
11+
self.today = datetime.datetime.now()
12+
13+
# Approximate Equinox and Solstice Dates - and their corresponding subsolar point(s) in the list below.
14+
# format: (DD, MM, Angle)
15+
self.seasons = ((22, 3), (21, 6), (22, 9), (21, 12))
16+
17+
self.subsolar_point = (0, 23.5, 0, -23.5)
18+
19+
20+
def current_location(self):
21+
""" Get Current Latitude from IP or enter Latitude desired """
22+
if self.latitude == '':
23+
self.latitude = geocoder.ip('me').latlng[0]
24+
else:
25+
self.latitude = float(self.latitude)
26+
return self.latitude
27+
28+
29+
def day_of_year_number(self, dates):
30+
""" Convert the Equinox and Solstice dates to their day of the year number and compare with the current date """
31+
# get the datetime objects for each date in the self.seasons list
32+
# Reformt to (Day of Year Number, Datetime Object) for each item in list
33+
return [(int(date.strftime("%j")), date) for date in dates]
34+
35+
36+
def current_season(self):
37+
""" Reformat all seasons dates into datetime objects with the current year, so that they can later be convert into their 'day of the year' numbers """
38+
# Use Current year
39+
#year = int(self.today.strftime("%Y"))
40+
# List of Datetime objects for each date
41+
datetime_objects = [datetime.datetime(int(self.today.strftime("%Y")), season[1], season[0]) for season in self.seasons]
42+
return self.day_of_year_number(datetime_objects)
43+
44+
def last_subsolar_point(self):
45+
""" Compare the Current day of the year number, to the day of the year numbers for the solstice and equinox dates to find the current subsolar point (the 'last' subsolar point as in, what it changed to most recently) """
46+
current = self.day_of_year_number([self.today])
47+
seasonal =self.current_season()
48+
# Return the latitude to guage where the sun will form a 90° angle at noon (based on season).
49+
# It will be (+/-)23.5° or 0° depending on the date.
50+
return [(seasonal[seasonal.index(tilt)-1][0], tilt[0], self.subsolar_point[seasonal.index(tilt)-1]) for tilt in seasonal if current[0][0] <= tilt[0]][0][2]
51+
52+
53+
def zenith_angle(self):
54+
""" Calculate the Zenith Angle """
55+
# The angle at noon based on the difference of the User's current latitude and subsolar point. It will be the same as the current location if the current date is on or right after an equinox date.
56+
return self.current_location() - self.last_subsolar_point()
57+
58+
59+
def noon_sun_angle(self):
60+
""" Calculate for the Noon sun's angle """
61+
return 90 - self.zenith_angle()
62+
63+
def sunlight_intensity(self):
64+
""" The intensity of the sun's rays """
65+
noon_sun = self.noon_sun_angle()
66+
if noon_sun > 90:
67+
noon_sun = noon_sun - 90
68+
return [(angle[0], noon_sun) for angle in self.ray_intensity if noon_sun <= angle[1]][0]
69+
70+
def __str__(self):
71+
sun_info = self.sunlight_intensity()
72+
return "{}\n \nNoon Sun Angle: {} \nSunlight Intensity: {} \nZenith Angle: {}".format(self.today.strftime('%d %B %Y'), sun_info[1], sun_info[0], 90-sun_info[1])
73+
74+
75+
76+
if __name__ == '__main__':
77+
latitude = input("Enter your latitude or press 'continue' to use your device's IP address: ")
78+
sun = Sun_Angle(latitude)
79+
print(sun.__str__())

0 commit comments

Comments
 (0)