Skip to content

Commit 9020109

Browse files
Merge pull request #6 from gregory-halverson/main
v1.3.0 with `UTC_offset_hours_for_longitude` and `solar_day_of_year_for_longitude`
2 parents 9afc440 + 4ab212c commit 9020109

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=60", "setuptools-scm>=8.0", "wheel"]
33

44
[project]
55
name = "solar_apparent_time"
6-
version = "1.2.0"
6+
version = "1.3.0"
77
description = "methods to translate Python datetime between solar apparent time and Coordinate Universal Time (UTC)"
88
readme = "README.md"
99
authors = [

solar_apparent_time/solar_apparent_time.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from datetime import datetime, timedelta
24
import numpy as np
35
import rasters as rt
@@ -28,6 +30,19 @@ def solar_to_UTC(time_solar: datetime, lon: float) -> datetime:
2830
"""
2931
return time_solar - timedelta(hours=(np.radians(lon) / np.pi * 12))
3032

33+
def UTC_offset_hours_for_longitude(lon: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
34+
"""
35+
Calculates the offset in hours from UTC based on the given longitude.
36+
37+
Args:
38+
lon (Union[float, np.ndarray]): The longitude in degrees.
39+
40+
Returns:
41+
Union[float, np.ndarray]: The calculated offset in hours from UTC.
42+
"""
43+
# Convert longitude to radians and calculate the offset in hours from UTC
44+
return np.radians(lon) / np.pi * 12
45+
3146
def UTC_offset_hours_for_area(geometry: rt.RasterGeometry) -> rt.Raster:
3247
"""
3348
Calculates the UTC offset in hours for a given raster geometry.
@@ -61,6 +76,29 @@ def solar_day_of_year_for_area(time_UTC: datetime, geometry: rt.RasterGeometry)
6176

6277
return doy
6378

79+
def solar_day_of_year_for_longitude(time_UTC: datetime, lon: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
80+
"""
81+
Calculates the day of year based on the given UTC time and longitude.
82+
83+
Args:
84+
time_UTC (datetime.datetime): The UTC time to calculate the day of year for.
85+
lon (Union[float, np.ndarray]): The longitude in degrees.
86+
87+
Returns:
88+
Union[float, np.ndarray]: The calculated day of year.
89+
"""
90+
# Calculate the day of year at the given longitude
91+
DOY_UTC = time_UTC.timetuple().tm_yday
92+
hour_UTC = time_UTC.hour + time_UTC.minute / 60 + time_UTC.second / 3600
93+
offset = UTC_offset_hours_for_longitude(lon)
94+
hour_of_day = hour_UTC + offset
95+
DOY = DOY_UTC
96+
# Adjust the day of year if the hour of day is outside the range [0, 24]
97+
DOY = np.where(hour_of_day < 0, DOY - 1, DOY)
98+
DOY = np.where(hour_of_day > 24, DOY + 1, DOY)
99+
100+
return DOY
101+
64102
def solar_hour_of_day_for_area(time_UTC: datetime, geometry: rt.RasterGeometry) -> rt.Raster:
65103
"""
66104
Calculates the hour of the day for a given UTC time and raster geometry.

solar_apparent_time/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.0
1+
1.3.0

0 commit comments

Comments
 (0)