Created
January 6, 2017 18:39
-
-
Save anttilipp/ed3ab35258c7636d87de6499475301ce to your computer and use it in GitHub Desktop.
Python function to compute the length of the day given day of the year and latitude.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def daylength(dayOfYear, lat): | |
"""Computes the length of the day (the time between sunrise and | |
sunset) given the day of the year and latitude of the location. | |
Function uses the Brock model for the computations. | |
For more information see, for example, | |
Forsythe et al., "A model comparison for daylength as a | |
function of latitude and day of year", Ecological Modelling, | |
1995. | |
Parameters | |
---------- | |
dayOfYear : int | |
The day of the year. 1 corresponds to 1st of January | |
and 365 to 31st December (on a non-leap year). | |
lat : float | |
Latitude of the location in degrees. Positive values | |
for north and negative for south. | |
Returns | |
------- | |
d : float | |
Daylength in hours. | |
""" | |
latInRad = np.deg2rad(lat) | |
declinationOfEarth = 23.45*np.sin(np.deg2rad(360.0*(283.0+dayOfYear)/365.0)) | |
if -np.tan(latInRad) * np.tan(np.deg2rad(declinationOfEarth)) <= -1.0: | |
return 24.0 | |
elif -np.tan(latInRad) * np.tan(np.deg2rad(declinationOfEarth)) >= 1.0: | |
return 0.0 | |
else: | |
hourAngle = np.rad2deg(np.arccos(-np.tan(latInRad) * np.tan(np.deg2rad(declinationOfEarth)))) | |
return 2.0*hourAngle/15.0 |
Thank you very much!! You safe me a lot of time!
Thanks!
Nice code! Thanks!
As a stand alone function it improves performance using math
package instead of numpy
.
See:
https://gist.github.com/mluis7/4caeb4edcadcef0e74d0a7c3fde8df5c
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, sir.