Skip to content

Commit b21878c

Browse files
Merge pull request #10 from gregory-halverson/main
type-hints
2 parents 25d8d15 + f9f3e77 commit b21878c

File tree

9 files changed

+61
-43
lines changed

9 files changed

+61
-43
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 = "sun_angles"
6-
version = "1.1.4"
6+
version = "1.2.0"
77
description = "calculates solar zenith and azimuth and daylight hours"
88
readme = "README.md"
99
authors = [

sun_angles/SHA.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
from typing import Union
12
import warnings
23
import numpy as np
4+
from rasters import Raster
35

46
from .day_angle import day_angle_rad_from_DOY
57
from .declination import solar_dec_deg_from_day_angle_rad
68

7-
def SHA_deg_from_DOY_lat(DOY: np.ndarray, latitude: np.ndarray) -> np.ndarray:
9+
def SHA_deg_from_DOY_lat(DOY: Union[Raster, np.ndarray], latitude: Union[Raster, np.ndarray]) -> Union[Raster, np.ndarray]:
810
"""
911
Calculate the sunrise hour angle in degrees from the latitude in degrees and the day of the year.
1012
1113
Parameters:
12-
DOY (np.ndarray): A numpy array containing day of the year values (integers between 1 and 365).
13-
latitude (np.ndarray): A numpy array containing latitude values in degrees.
14+
DOY (Union[Raster, np.ndarray]): A Raster or numpy array containing day of the year values (integers between 1 and 365).
15+
latitude (Union[Raster, np.ndarray]): A Raster or numpy array containing latitude values in degrees.
1416
1517
Returns:
16-
np.ndarray: A numpy array containing the corresponding sunrise hour angles in degrees.
18+
Union[Raster, np.ndarray]: A Raster or numpy array containing the corresponding sunrise hour angles in degrees.
1719
1820
The function performs the following steps:
1921
1. Calculate the day angle in radians from the day of the year using the function `day_angle_rad_from_DOY`.
@@ -58,5 +60,4 @@ def SHA_deg_from_DOY_lat(DOY: np.ndarray, latitude: np.ndarray) -> np.ndarray:
5860
sunrise_deg = np.where(sunrise_cos >= 1, 0, sunrise_deg)
5961
sunrise_deg = np.where(sunrise_cos <= -1, 180, sunrise_deg)
6062

61-
return sunrise_deg
62-
63+
return sunrise_deg

sun_angles/SZA.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
from datetime import datetime
33

44
import numpy as np
5-
from rasters import Raster
5+
from rasters import Raster, CoordinateArray
66

7-
from solar_apparent_time import solar_day_of_year_for_longitude
7+
from solar_apparent_time import solar_day_of_year_for_longitude, solar_hour_of_day_for_area
88

99
from .day_angle import day_angle_rad_from_DOY
1010
from .declination import solar_dec_deg_from_day_angle_rad
1111

1212
def SZA_deg_from_lat_dec_hour(
13-
latitude: np.ndarray,
13+
latitude: Union[Raster, np.ndarray],
1414
solar_dec_deg: Union[Raster, np.ndarray],
15-
hour: Union[Raster, np.ndarray]) -> np.ndarray:
15+
hour: Union[Raster, np.ndarray]) -> Union[Raster, np.ndarray]:
1616
"""
1717
This function calculates the solar zenith angle (SZA) given the latitude, solar declination, and solar time.
1818
The SZA is the angle between the zenith and the center of the sun's disc. The zenith is the point on the celestial
@@ -95,8 +95,7 @@ def calculate_SZA_from_datetime(time_UTC: datetime, lat: float, lon: float):
9595
# Calculate the day of year based on the UTC time and longitude
9696
doy = solar_day_of_year_for_longitude(time_UTC, lon)
9797
# Calculate the hour of the day based on the UTC time and longitude
98-
# FIXME missing `hour_of_day` implementation
99-
hour = hour_of_day(time_UTC, lon)
98+
hour = solar_hour_of_day_for_area(time_UTC=time_UTC, geometry=CoordinateArray(x=lon, y=lat))
10099
# Calculate the solar zenith angle in degrees based on the latitude, solar declination angle, and hour of the day
101100
SZA = calculate_SZA_from_DOY_and_hour(lat, lon, doy, hour)
102101

sun_angles/azimuth.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
from typing import Union
12
import warnings
23
import numpy as np
4+
from rasters import Raster
35

4-
def calculate_solar_azimuth(solar_dec_deg: np.ndarray, SZA_deg: np.ndarray, hour: np.ndarray) -> np.ndarray:
6+
def calculate_solar_azimuth(
7+
solar_dec_deg: Union[Raster, np.ndarray],
8+
SZA_deg: Union[Raster, np.ndarray],
9+
hour: Union[Raster, np.ndarray]) -> Union[Raster, np.ndarray]:
510
"""
611
Calculate the solar azimuth angle based on the solar declination, solar zenith angle, and hour of the day.
712
813
Parameters:
9-
solar_dec_deg (np.ndarray): Solar declination in degrees.
10-
SZA_deg (np.ndarray): Solar zenith angle in degrees.
11-
hour (np.ndarray): Hour of the day, where 0 corresponds to 00:00 and 23 corresponds to 23:00.
14+
solar_dec_deg (Union[Raster, np.ndarray]): Solar declination in degrees.
15+
SZA_deg (Union[Raster, np.ndarray]): Solar zenith angle in degrees.
16+
hour (Union[Raster, np.ndarray]): Hour of the day, where 0 corresponds to 00:00 and 23 corresponds to 23:00.
1217
1318
Returns:
14-
np.ndarray: Solar azimuth angle in degrees.
19+
Union[Raster, np.ndarray]: Solar azimuth angle in degrees.
1520
1621
Note:
1722
This function ignores any warnings that might be generated during the calculations.
@@ -36,4 +41,4 @@ def calculate_solar_azimuth(solar_dec_deg: np.ndarray, SZA_deg: np.ndarray, hour
3641
# Convert the solar azimuth from radians to degrees
3742
solar_azimuth_deg = np.degrees(solar_azimuth_rad)
3843

39-
return solar_azimuth_deg
44+
return solar_azimuth_deg

sun_angles/day_angle.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
from typing import Union
12
import numpy as np
3+
from rasters import Raster
24

3-
def day_angle_rad_from_DOY(DOY: np.ndarray) -> np.ndarray:
5+
def day_angle_rad_from_DOY(DOY: Union[Raster, np.ndarray]) -> Union[Raster, np.ndarray]:
46
"""
57
Calculate the day angle in radians from the day of the year.
68
79
Parameters:
8-
DOY (np.ndarray): A numpy array containing day of the year values (integers between 1 and 365).
10+
DOY (Union[Raster, np.ndarray]): A Raster object or a numpy array containing
11+
day of the year values (integers between 1 and 365).
912
1013
Returns:
11-
np.ndarray: A numpy array containing the corresponding day angles in radians.
14+
Union[Raster, np.ndarray]: A Raster object or a numpy array containing the
15+
corresponding day angles in radians.
1216
1317
The day angle is calculated using the formula:
1418
day_angle = (2 * π * (DOY - 1)) / 365
@@ -20,4 +24,4 @@ def day_angle_rad_from_DOY(DOY: np.ndarray) -> np.ndarray:
2024
Reference:
2125
Duffie, J. A., & Beckman, W. A. (2013). Solar Engineering of Thermal Processes (4th ed.). Wiley.
2226
"""
23-
return (2 * np.pi * (DOY - 1)) / 365
27+
return (2 * np.pi * (DOY - 1)) / 365

sun_angles/daylight.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from typing import Union
12
import numpy as np
3+
from rasters import Raster
24

3-
def daylight_from_SHA(SHA_deg: np.ndarray) -> np.ndarray:
5+
def daylight_from_SHA(SHA_deg: Union[Raster, np.ndarray]) -> Union[Raster, np.ndarray]:
46
"""
5-
This function calculates daylight hours from the sunrise hour angle (SHA) in degrees.
6-
7+
Calculates daylight hours from the sunrise hour angle (SHA) in degrees.
8+
79
The calculation is based on the formula:
810
911
$$ daylight = \frac{2}{15} * SHA $$
@@ -16,17 +18,17 @@ def daylight_from_SHA(SHA_deg: np.ndarray) -> np.ndarray:
1618
1719
Parameters
1820
----------
19-
SHA_deg : np.ndarray
20-
Sunrise hour angle in degrees. Must be a numpy array.
21+
SHA_deg : Union[Raster, np.ndarray]
22+
Sunrise hour angle in degrees. Can be a `Raster` object or a numpy array.
2123
2224
Returns
2325
-------
24-
np.ndarray
25-
Daylight hours. Returns a numpy array of the same shape as `SHA_deg`.
26+
Union[Raster, np.ndarray]
27+
Daylight hours. Returns a `Raster` object or a numpy array of the same shape as `SHA_deg`.
2628
2729
References
2830
----------
2931
- Allen, R.G., Pereira, L.S., Raes, D., Smith, M., 1998. Crop evapotranspiration-Guidelines for computing crop water requirements-FAO Irrigation and drainage paper 56. FAO, Rome, 300(9).
3032
- Duffie, J. A., & Beckman, W. A. (2013). Solar Engineering of Thermal Processes (4th ed.). Wiley.
3133
"""
32-
return (2.0 / 15.0) * SHA_deg
34+
return (2.0 / 15.0) * SHA_deg

sun_angles/declination.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
from typing import Union
12
import numpy as np
3+
from rasters import Raster
24

3-
def solar_dec_deg_from_day_angle_rad(day_angle_rad: np.ndarray) -> np.ndarray:
5+
def solar_dec_deg_from_day_angle_rad(day_angle_rad: Union[Raster, np.ndarray]) -> Union[Raster, np.ndarray]:
46
"""
57
Calculate solar declination in degrees from the day angle in radians.
68
79
Parameters:
8-
day_angle_rad (np.ndarray): A numpy array containing day angles in radians.
10+
day_angle_rad (Union[Raster, np.ndarray]): A Raster or numpy array containing day angles in radians.
911
1012
Returns:
11-
np.ndarray: A numpy array containing the corresponding solar declination angles in degrees.
13+
Union[Raster, np.ndarray]: A Raster or numpy array containing the corresponding solar declination angles in degrees.
1214
1315
The solar declination is calculated using the following formula:
1416
solar_declination = 0.006918 - 0.399912 * cos(day_angle_rad) + 0.070257 * sin(day_angle_rad)
@@ -23,4 +25,4 @@ def solar_dec_deg_from_day_angle_rad(day_angle_rad: np.ndarray) -> np.ndarray:
2325
"""
2426
return (0.006918 - 0.399912 * np.cos(day_angle_rad) + 0.070257 * np.sin(day_angle_rad)
2527
- 0.006758 * np.cos(2 * day_angle_rad) + 0.000907 * np.sin(2 * day_angle_rad)
26-
- 0.002697 * np.cos(3 * day_angle_rad) + 0.00148 * np.sin(3 * day_angle_rad)) * (180 / np.pi)
28+
- 0.002697 * np.cos(3 * day_angle_rad) + 0.00148 * np.sin(3 * day_angle_rad)) * (180 / np.pi)

sun_angles/sunrise.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
from typing import Union
12
import numpy as np
3+
from rasters import Raster
24

3-
def sunrise_from_SHA(SHA_deg: np.ndarray) -> np.ndarray:
5+
def sunrise_from_SHA(SHA_deg: Union[Raster, np.ndarray]) -> Union[Raster, np.ndarray]:
46
"""
57
Calculate the sunrise hour from the sunrise hour angle (SHA) in degrees.
68
7-
This function takes an array of sunrise hour angles (SHA) in degrees and
8-
converts it to the corresponding sunrise hours. The conversion is based on
9-
the fact that the Earth rotates 15 degrees per hour.
9+
This function takes a `Raster` or `numpy.ndarray` of sunrise hour angles (SHA)
10+
in degrees and converts it to the corresponding sunrise hours. The conversion
11+
is based on the fact that the Earth rotates 15 degrees per hour.
1012
1113
Parameters:
12-
SHA_deg (np.ndarray): Array of sunrise hour angles in degrees.
14+
SHA_deg (Union[Raster, np.ndarray]): A `Raster` or `numpy.ndarray` containing
15+
sunrise hour angles in degrees.
1316
1417
Returns:
15-
np.ndarray: Array of calculated sunrise hours.
18+
Union[Raster, np.ndarray]: A `Raster` or `numpy.ndarray` containing the
19+
calculated sunrise hours.
1620
1721
Example:
22+
>>> import numpy as np
1823
>>> SHA_deg = np.array([0, 15, 30, 45])
1924
>>> sunrise_from_SHA(SHA_deg)
2025
array([12., 11., 10., 9.])
@@ -26,4 +31,4 @@ def sunrise_from_SHA(SHA_deg: np.ndarray) -> np.ndarray:
2631
[1] Duffie, J.A., & Beckman, W.A. (2013). Solar Engineering of Thermal Processes.
2732
John Wiley & Sons.
2833
"""
29-
return 12.0 - (SHA_deg / 15.0)
34+
return 12.0 - (SHA_deg / 15.0)

sun_angles/version.txt

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

0 commit comments

Comments
 (0)