Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the code #92

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 21 additions & 32 deletions src/astral/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def dms_to_float(
_dms_re = r"(?P<deg>\d{1,3})[°]((?P<min>\d{1,2})[′'])?((?P<sec>\d{1,2})[″\"])?(?P<dir>[NSEW])?" # noqa
dms_match = re.match(_dms_re, str(dms), flags=re.IGNORECASE)
if dms_match:
deg = dms_match.group("deg") or 0.0
min_ = dms_match.group("min") or 0.0
sec = dms_match.group("sec") or 0.0
deg = dms_match.group("deg") or 0
min_ = dms_match.group("min") or 0
sec = dms_match.group("sec") or 0
dir_ = dms_match.group("dir") or "E"

res = float(deg)
Expand All @@ -133,38 +133,27 @@ def dms_to_float(
) from exc

if limit is not None:
if res > limit:
res = limit
elif res < -limit:
res = -limit

res = min(max(-limit, res), limit)
return res


def hours_to_time(value: float) -> datetime.time:
"""Convert a floating point number of hours to a datetime.time"""

hour = int(value)
value -= hour
value *= 60
minute = int(value)
value -= minute
value *= 60
second = int(value)
value -= second
microsecond = int(value * 1000000)

rest = int(value * 60 * 60 * 1_000_000)
rest, microsecond = divmod(rest, 1_000_000)
rest, second = divmod(rest, 60)
hour, minute = divmod(rest, 60)
return datetime.time(hour, minute, second, microsecond)


def time_to_hours(value: datetime.time) -> float:
"""Convert a datetime.time to a floating point number of hours"""

hours = 0.0
hours = 0
hours += value.hour
hours += value.minute / 60
hours += value.second / 3600
hours += value.microsecond / 1000000
hours += value.microsecond / 3_600_000_000

return hours

Expand All @@ -180,24 +169,24 @@ def refraction_at_zenith(zenith: float) -> float:
"""Calculate the degrees of refraction of the sun due to the sun's elevation."""

elevation = 90 - zenith
if elevation >= 85.0:
if elevation >= 85:
return 0

refraction_correction = 0.0
refraction_correction = 0
te = tan(radians(elevation))
if elevation > 5.0:
if elevation > 5:
refraction_correction = (
58.1 / te - 0.07 / (te * te * te) + 0.000086 / (te * te * te * te * te)
)
elif elevation > -0.575:
step1 = -12.79 + elevation * 0.711
step2 = 103.4 + elevation * step1
step3 = -518.2 + elevation * step2
refraction_correction = 1735.0 + elevation * step3
refraction_correction = 1735 + elevation * step3
else:
refraction_correction = -20.774 / te

refraction_correction = refraction_correction / 3600.0
refraction_correction = refraction_correction / 3600

return refraction_correction

Expand Down Expand Up @@ -253,13 +242,13 @@ class Observer:

latitude: Degrees = 51.4733
longitude: Degrees = -0.0008333
elevation: Elevation = 0.0
elevation: Elevation = 0

def __setattr__(self, name: str, value: Union[str, float, Elevation]):
if name == "latitude":
value = dms_to_float(value, 90.0)
value = dms_to_float(value, 90)
elif name == "longitude":
value = dms_to_float(value, 180.0)
value = dms_to_float(value, 180)
elif name == "elevation":
if isinstance(value, tuple):
value = (float(value[0]), float(value[1]))
Expand Down Expand Up @@ -296,15 +285,15 @@ class LocationInfo:

def __setattr__(self, name: str, value: Union[Degrees, str]):
if name == "latitude":
value = dms_to_float(value, 90.0)
value = dms_to_float(value, 90)
elif name == "longitude":
value = dms_to_float(value, 180.0)
value = dms_to_float(value, 180)
super().__setattr__(name, value)

@property
def observer(self):
"""Return an Observer at this location"""
return Observer(self.latitude, self.longitude, 0.0)
return Observer(self.latitude, self.longitude, 0)

@property
def tzinfo(self): # type: ignore
Expand Down
15 changes: 7 additions & 8 deletions src/astral/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
"-r", "--region", dest="region", default="On Earth", help="Region (free-form text)"
)
options.add_argument(
"-d", "--date", dest="date", help="Date to compute times for (yyyy-mm-dd)"
"-d", "--date",
dest="date",
type=datetime.date.fromisoformat,
default=datetime.date.today(),
help="Date to compute times for (yyyy-mm-dd)",
)
options.add_argument("-t", "--tzname", help="Timezone name")
options.add_argument("latitude", type=float, help="Location latitude (float)")
options.add_argument("longitude", type=float, help="Location longitude (float)")
options.add_argument(
"elevation", nargs="?", type=float, default=0.0, help="Elevation in metres (float)"
"elevation", nargs="?", type=float, default=0, help="Elevation in metres (float)"
)
args = options.parse_args()

Expand All @@ -44,12 +48,7 @@

kwargs: Dict[str, Any] = {}
kwargs["observer"] = obs

if args.date is not None:
try:
kwargs["date"] = datetime.datetime.strptime(args.date, "%Y-%m-%d").date()
except: # noqa: E722
kwargs["date"] = datetime.date.today()
kwargs["date"] = args.date

sun_as_str = {}
format_str = "%Y-%m-%dT%H:%M:%S"
Expand Down
8 changes: 4 additions & 4 deletions src/astral/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ def _locationinfo_from_str(info: str) -> LocationInfo:
name=idxable[0],
region=idxable[1],
timezone=idxable[2],
latitude=dms_to_float(idxable[3], 90.0),
longitude=dms_to_float(idxable[4], 180.0),
latitude=dms_to_float(idxable[3], 90),
longitude=dms_to_float(idxable[4], 180),
)


Expand All @@ -481,8 +481,8 @@ def _locationinfo_from_indexable(
name=idxable[0],
region=idxable[1],
timezone=idxable[2],
latitude=dms_to_float(idxable[3], 90.0),
longitude=dms_to_float(idxable[4], 180.0),
latitude=dms_to_float(idxable[3], 90),
longitude=dms_to_float(idxable[4], 180),
)


Expand Down
21 changes: 8 additions & 13 deletions src/astral/julian.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ class Calendar(Enum):


def day_fraction_to_time(fraction: float) -> datetime.time:
s = fraction * (24 * 60 * 60)
h = int(s / (60 * 60))
s -= h * 60 * 60
m = int(s / 60)
s -= m * 60
s = int(s)
rest = int(fraction * (24 * 60 * 60))
rest, s = divmod(rest, 60)
h, m = divmod(rest, 60)
return datetime.time(h, m, s)


Expand All @@ -32,12 +29,10 @@ def _time_to_seconds(t: datetime.time) -> int:
year = at.year
month = at.month
day = at.day
day_fraction = 0.0
day_fraction = 0
if isinstance(at, datetime.datetime):
t = _time_to_seconds(at.time())
day_fraction = t / (24 * 60 * 60)
else:
day_fraction = 0.0

if month <= 2:
year -= 1
Expand Down Expand Up @@ -95,7 +90,7 @@ def julianday_to_datetime(jd: float) -> datetime.datetime:
a = z
else:
alpha = int((z - 1867216.25) / 36524.25)
a = z + 1 + alpha + int(alpha / 4.0)
a = z + 1 + alpha + int(alpha / 4)

b = a + 1524
c = int((b - 122.1) / 365.25)
Expand Down Expand Up @@ -127,14 +122,14 @@ def julianday_to_datetime(jd: float) -> datetime.datetime:

def julianday_to_juliancentury(julianday: float) -> float:
"""Convert a Julian Day number to a Julian Century"""
return (julianday - 2451545.0) / 36525.0
return (julianday - 2451545) / 36525


def juliancentury_to_julianday(juliancentury: float) -> float:
"""Convert a Julian Century number to a Julian Day"""
return (juliancentury * 36525.0) + 2451545.0
return (juliancentury * 36525) + 2451545


def julianday_2000(at: Union[datetime.datetime, datetime.date]) -> float:
"""Calculate the numer of Julian Days since Jan 1.5, 2000"""
return julianday(at) - 2451545.0
return julianday(at) - 2451545
Loading