|
| 1 | +from typing import Union |
| 2 | + |
1 | 3 | from datetime import datetime, timedelta
|
2 | 4 | import numpy as np
|
3 | 5 | import rasters as rt
|
@@ -28,6 +30,19 @@ def solar_to_UTC(time_solar: datetime, lon: float) -> datetime:
|
28 | 30 | """
|
29 | 31 | return time_solar - timedelta(hours=(np.radians(lon) / np.pi * 12))
|
30 | 32 |
|
| 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 | + |
31 | 46 | def UTC_offset_hours_for_area(geometry: rt.RasterGeometry) -> rt.Raster:
|
32 | 47 | """
|
33 | 48 | 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)
|
61 | 76 |
|
62 | 77 | return doy
|
63 | 78 |
|
| 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 | + |
64 | 102 | def solar_hour_of_day_for_area(time_UTC: datetime, geometry: rt.RasterGeometry) -> rt.Raster:
|
65 | 103 | """
|
66 | 104 | Calculates the hour of the day for a given UTC time and raster geometry.
|
|
0 commit comments