-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
Milestone
Description
As indicated in in clearsky.lookup_linke_turbidity
on L212-224
This is approximate and could be made more accurate.
>>> import numpy as np
>>> np.linspace(-15, 380, num=14)
array([ -15. , 15.38461538, 45.76923077, 76.15384615,
106.53846154, 136.92307692, 167.30769231, 197.69230769,
228.07692308, 258.46153846, 288.84615385, 319.23076923,
349.61538462, 380. ])
Another method would be to use the calendar month days:
>>> import calendar
>>> import numpy as np
>>> mdays = calendar.mdays[1:]
>>> np.concatenate([[-calendar.mdays[1] / 2.0],
np.cumsum(mdays) - np.array(mdays) / 2.,
[365 + calendar.mdays[-1] / 2.0]])
array([ -15.5, 15.5, 45. , 74.5, 105. , 135.5, 166. , 196.5,
227.5, 258. , 288.5, 319. , 349.5, 380.5])
This still doesn't account for leap years, but we can use calendar.isleap(<year>)
:
import calendar
import numpy as np
def calendar_month_middles(year):
mdays = np.array(calendar.mdays[1:]) # copy
ydays = 365
if calendar.isleap(year):
mdays[1] = mdays[1] + 1
ydays = 366
return np.concatenate([[-calendar.mdays[-1] / 2.0],
np.cumsum(mdays) - np.array(mdays) / 2.,
[ydays + calendar.mdays[1] / 2.0]])
now you can see the difference in leap year
>>> calendar_month_middles(2016) # a leap year
array([ -15.5, 15.5, 45.5, 75.5, 106. , 136.5, 167. , 197.5,
228.5, 259. , 289.5, 320. , 350.5, 381.5])
>>> calendar_month_middles(2015) # NOT a leap year
array([ -15.5, 15.5, 45. , 74.5, 105. , 135.5, 166. , 196.5,
227.5, 258. , 288.5, 319. , 349.5, 380.5])
Note: it's important to copy mdays
because it's a borrowed reference, so if you change it, the change is permanent.
>>> calendar.mdays
[0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
>>> calendar.mdays[3] = 100
>>> calendar.mdays
[0, 31, 28, 100, 30, 31, 30, 31, 31, 30, 31, 30, 31]
calendar_month_middles(2015) # yikes!
array([ -15.5, 15.5, 45. , 109. , 174. , 204.5, 235. , 265.5,
296.5, 327. , 357.5, 388. , 418.5, 380.5])