You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calculate the number of daylight hours for a given day and location.
20
24
21
-
This function is highly flexible and can be used in several ways depending on the information you have:
25
+
This function flexibly computes daylight hours based on the information provided:
22
26
23
-
- If you already know the sunrise hour angle (`SHA_deg`), provide it directly and the function will return the daylight hours for that value.
24
-
- If you do not provide `SHA_deg`, the function will compute it using the day of year (`day_of_year`) and latitude (`lat`).
25
-
- If you do not provide `lat` but do provide a `geometry` object, the function will extract the latitude from `geometry.lat`.
26
-
- If you do not provide `day_of_year` but do provide `time_UTC`, the function will convert the provided UTC time(s) to day of year. `time_UTC` can be a single datetime, a string, or an array/list of datetimes or strings.
27
+
- If `SHA_deg` (sunrise hour angle in degrees) is provided, it is used directly to compute daylight hours.
28
+
- If `SHA_deg` is not provided, it is calculated from `day_of_year` and `lat` (latitude in degrees).
29
+
- If `lat` is not provided but a `geometry` object is given, latitude is extracted from `geometry.lat`.
30
+
- If `day_of_year` is not provided but `time_UTC` is given, the function converts the UTC time(s) to day of year, accounting for longitude if available.
Sunrise hour angle in degrees. If provided, it is used directly and other parameters are ignored.
36
-
time_UTC : datetime, str, list, or np.ndarray, optional
37
-
Datetime(s) in UTC. Used to determine `day_of_year` if `day_of_year` is not provided. Accepts a single datetime, a string, or an array/list of datetimes or strings.
38
-
Example: `time_UTC=['2025-06-21', '2025-12-21']` or `time_UTC=datetime(2025, 6, 21)`
39
-
geometry : SpatialGeometry, optional
40
-
Geometry object containing latitude information. If `lat` is not provided, latitude will be extracted from `geometry.lat`.
41
47
42
48
Returns
43
49
-------
@@ -61,24 +67,16 @@ def calculate_daylight(
61
67
# If latitude is not provided, try to extract from geometry
62
68
iflatisNoneandgeometryisnotNone:
63
69
lat=geometry.lat
70
+
71
+
iflonisNoneandgeometryisnotNone:
72
+
lon=geometry.lon
73
+
74
+
ifday_of_yearisNone:
75
+
day_of_year=solar_day_of_year_for_longitude(
76
+
time_UTC=time_UTC,
77
+
lon=lon
78
+
)
64
79
65
-
# If DOY is not provided, try to extract from time_UTC
66
-
ifday_of_yearisNoneandtime_UTCisnotNone:
67
-
defto_doy(val):
68
-
ifisinstance(val, str):
69
-
val=parser.parse(val)
70
-
returnint(pd.Timestamp(val).dayofyear)
71
-
72
-
# Handle array-like or single value
73
-
ifisinstance(time_UTC, (list, np.ndarray)):
74
-
day_of_year=np.array([to_doy(t) fortintime_UTC])
75
-
else:
76
-
day_of_year=to_doy(time_UTC)
77
-
78
-
# Ensure day_of_year is a numpy array if it's a list (for downstream math)
0 commit comments