Skip to content

Commit

Permalink
Fixed a few minor issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnobile2012 committed Dec 16, 2023
1 parent 5f4a5ba commit ad0d3ca
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ month, and day needs to be entered.
python sunrisesunset/sunrisesunset.py
Enter a address: Chicago
Enter an address: Chicago
Chicago, Cook County, Illinois, United States
Enter date in ISO format (yyyy-mm-dd hh:mm:ss): 2023-12-16 12
Timezone: America/Chicago
Expand Down Expand Up @@ -92,3 +92,6 @@ month, and day needs to be entered.
Sunrise: 2023-12-16 06:30:29.951023-05:00
Sunset: 2023-12-16 19:00:39.782789-05:00
Is night: False
Test 24 hours
...
2 changes: 1 addition & 1 deletion include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
# Django Dynamic Column version info.
MAJORVERSION = 1
MINORVERSION = 0
PATCHLEVEL = 0
PATCHLEVEL = 1
VERSION = ${MAJORVERSION}.${MINORVERSION}.${PATCHLEVEL}
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# -*- coding: utf-8 -*-

import os
import re
from setuptools import setup

def version():
"""
Find the version.
:return: The version.
:rtype: str
"""
regex = r'(?m)(^{}[\s]*=[\s]*(?P<ver>\d*)$)'

with open(os.path.join(os.path.dirname(__file__), 'include.mk')) as f:
Expand All @@ -13,7 +21,7 @@ def version():
patch = re.search(regex.format('PATCHLEVEL'), ver).group('ver')
# Look for a tag indicating a pre-release candidate. ex. rc1
env_value = os.environ.get('PR_TAG', '')
return "{}.{}.{}{}".format(major, minor, patch, env_value)
return f"{major}.{minor}.{patch}{env_value}"

with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
README = readme.read()
Expand Down
2 changes: 1 addition & 1 deletion sunrisesunset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

from .sunrisesunset import SunriseSunset

__all__ = (SunriseSunset,)
__all__ = ('SunriseSunset',)
58 changes: 31 additions & 27 deletions sunrisesunset/sunrisesunset.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SunriseSunset:
'astronomical': -18.0}

def __init__(self, date:datetime, lat:float, lon:float,
zenith:str='official'):
zenith:str='official') -> None:
"""
Set the values for the sunrise and sunset calculation.
Expand Down Expand Up @@ -114,25 +114,27 @@ def __determine_rise_set(self):
self.__sunrise = self.__determine_rise_or_set(ephem2000day, 1)
self.__sunset = self.__determine_rise_or_set(ephem2000day, -1)

def __determine_rise_or_set(self, ephem2000day:float, rs) -> datetime:
def __determine_rise_or_set(self, ephem2000day:float, rs:int) -> datetime:
"""
Determine either the sunrise or the sunset.
:param ephem2000day: The Ephemeris from the beginning of the
21st century.
:type ephem2000day: float
:param rs: The factor that determines either sunrise or sunset where
1 equals sunrise and -1 sunset.
:type rs: int
:return: Either the sunrise or sunset as a C{datetime} object.
"""
utold = pi
utnew = 0
altitude = self.__ZENITH[self.__zenith]
sin_alt = sin(radians(altitude)) # solar altitude
sin_phi = sin(radians(self.__lat)) # viewer's latitude
cos_Phi = cos(radians(self.__lat)) #
cos_phi = cos(radians(self.__lat)) #
lon = radians(self.__lon) # viewer's longitude
ct = 0
#print(rs, ephem2000day, sin_alt, sin_phi, cos_Phi, lon)
#print(rs, ephem2000day, sin_alt, sin_phi, cos_phi, lon)

while fabs(utold - utnew) > 0.001 and ct < 35:
ct += 1
Expand All @@ -149,7 +151,7 @@ def __determine_rise_or_set(self, ephem2000day:float, rs) -> datetime:
delta = sin(obl) * sin(lam)
delta = atan2(delta, sqrt(1 - delta * delta))
gha = utold - pi + e
cosc = (sin_alt - sin_phi * sin(delta)) / (cos_Phi * cos(delta))
cosc = (sin_alt - sin_phi * sin(delta)) / (cos_phi * cos(delta))

if cosc > 1:
correction = 0
Expand All @@ -161,9 +163,9 @@ def __determine_rise_or_set(self, ephem2000day:float, rs) -> datetime:
#print(cosc, correction, utold, utnew)
utnew = self.__get_range(utold - (gha + lon + rs * correction))

decimalTime = degrees(utnew) / 15
#print(utnew, decimalTime)
return self.__get_24_hour_local_time(rs, decimalTime)
decimal_time = degrees(utnew) / 15
#print(utnew, decimal_time)
return self.__get_24_hour_local_time(decimal_time)

def __get_range(self, value:float) -> float:
"""
Expand All @@ -176,17 +178,17 @@ def __get_range(self, value:float) -> float:
"""
tmp1 = value / (2.0 * pi)
tmp2 = (2.0 * pi) * (tmp1 - int(tmp1))
if tmp2 < 0.0: tmp2 += (2.0 * pi)

if tmp2 < 0.0:
tmp2 += (2.0 * pi)

return tmp2

def __get_24_hour_local_time(self, rs:int, decimal_time:float) -> datetime:
def __get_24_hour_local_time(self, decimal_time:float) -> datetime:
"""
Convert the decimal time into a local time (C{datetime} object)
and correct for a 24 hour clock.
:param rs: The factor that determines either sunrise or sunset where
1 equals sunrise and -1 sunset.
:type rs: int
:param decimal_time: The decimal time.
:type decimal_time: float
:return: The C{datetime} objects set to either sunrise or sunset.
Expand Down Expand Up @@ -226,14 +228,15 @@ def __get_rise_set(date, lat=35.9513, lon=-83.9142, zenith='official'):


if __name__ == '__main__':
import sys, pytz
import sys
import pytz
#from pprint import pprint
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder

ret = 1
geolocator = Nominatim(user_agent='sunrisesunset')
address = input('Enter a address: ')
address = input('Enter an address: ')
location = geolocator.geocode(address)
print(location)

Expand All @@ -242,38 +245,39 @@ def __get_rise_set(date, lat=35.9513, lon=-83.9142, zenith='official'):
lat = float(raw['lat'])
lon = float(raw['lon'])
#pprint(raw)
date = input('Enter date in ISO format (yyyy-mm-dd hh:mm:ss): ')
text_date = input('Enter date in ISO format (yyyy-mm-dd hh:mm:ss): ')
tf = TimezoneFinder()
tz = tf.timezone_at(lng=lon, lat=lat)
print(f"Timezone: {tz}")

try:
zone = pytz.timezone(tz)
dt = datetime.datetime.fromisoformat(date)
dt = datetime.datetime.fromisoformat(text_date)
except pytz.UnknownTimeZoneError:
print(f"The entered timezone '{tz}' is not valid.")
except ValueError:
print(f"The entered date '{date}' is not valid.")
print(f"The entered date '{text_date}' is not valid.")
else:
# Get sunrise and sunset for the given time and calculate
# for all zenith types.
dt = dt.astimezone(zone)
# Get the zenith types.
zenithKeys = SunriseSunset._SunriseSunset__ZENITH.keys()
zenith_keys = SunriseSunset._SunriseSunset__ZENITH.keys()
print("Test zenith")

for zenith in zenithKeys:
for zenith in zenith_keys:
__get_rise_set(dt, lat=lat, lon=lon, zenith=zenith)

# Get sunrise sunset for every hour of the day using the
# default zenith.
## print("\nTest 24 hours")

## for hour in range(24):
## for minute in range(0, 60, 10):
## date = now.replace(hour=hour, minute=minute,
## second=0, microsecond=0)
## __get_rise_set(date, lat=lat, lon=lon)
print("\nTest 24 hours")

for hour in range(24):
# Every 20 minutes for an hour.
for minute in range(0, 60, 20):
date = dt.replace(hour=hour, minute=minute,
second=0, microsecond=0)
__get_rise_set(date, lat=lat, lon=lon)

ret = 0
else:
Expand Down

0 comments on commit ad0d3ca

Please sign in to comment.