Skip to content

Commit a6b5fa9

Browse files
committed
fix tests
add environment.yml
1 parent e95534d commit a6b5fa9

File tree

5 files changed

+61
-112
lines changed

5 files changed

+61
-112
lines changed

conda/environment.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: astkd
2+
channels:
3+
- conda-forge
4+
- openalea3
5+
dependencies:
6+
- python
7+
- pip
8+
- pvlib-python
9+
- openalea.plantgl
10+
- numpy
11+
- matplotlib
12+
- pytest
13+
- ipython
14+
- jupyterlab

src/openalea/astk/meteorology/sky_irradiance.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,8 @@ def sky_irradiance(dates=None, daydate=_daydate, ghi=None, dhi=None, ppfd=None,
381381
ppfd = df.ghi * micromol_per_joule(df.index, df.ghi, df.elevation, temp_dew=temp_dew)
382382
df['ppfd'] = ppfd
383383

384-
df = df.rename(columns={'azimuth': 'sun_azimuth', 'elevation': 'sun_elevation', 'zenith': 'sun_zenith'})
385-
386384
return df.loc[:,
387-
['sun_azimuth', 'sun_zenith', 'sun_elevation', 'ghi', 'dni', 'dhi', 'ppfd']]
385+
['azimuth', 'zenith', 'elevation', 'ghi', 'dni', 'dhi', 'ppfd']]
388386

389387

390388

src/openalea/astk/meteorology/sky_luminance.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,18 @@ def sky_luminance(grid, sky_type='soc', sky_irradiance=None):
239239
elif sky_type == 'clear_sky':
240240
for row in irrad.itertuples():
241241
_lum = w_c * cie_relative_luminance(grid=grid,
242-
sun_zenith=row.sun_zenith,
243-
sun_azimuth=row.sun_azimuth,
242+
sun_zenith=row.zenith,
243+
sun_azimuth=row.azimuth,
244244
type='clear_sky')
245245
_hi = sky_hi(grid, _lum)
246246
lum += (row.ghi / _hi * _lum)
247247
elif sky_type == 'all_weather':
248248
for row in irrad.itertuples():
249-
brightness = all_weather_sky_brightness(row.dates, row.dhi, row.sun_zenith)
250-
clearness = all_weather_sky_clearness(row.dni, row.dhi, row.sun_zenith)
249+
brightness = all_weather_sky_brightness(row.dates, row.dhi, row.zenith)
250+
clearness = all_weather_sky_clearness(row.dni, row.dhi, row.zenith)
251251
_lum = w_c * all_weather_relative_luminance(grid,
252-
sun_zenith=row.sun_zenith,
253-
sun_azimuth=row.sun_azimuth,
252+
sun_zenith=row.zenith,
253+
sun_azimuth=row.azimuth,
254254
brightness=brightness,
255255
clearness=clearness)
256256
_hi = sky_hi(grid, _lum)
@@ -259,10 +259,10 @@ def sky_luminance(grid, sky_type='soc', sky_irradiance=None):
259259
soc = w_c * cie_relative_luminance(grid=grid, type='soc')
260260
for row in irrad.itertuples():
261261
cs = w_c * cie_relative_luminance(grid=grid,
262-
sun_zenith=row.sun_zenith,
263-
sun_azimuth=row.sun_azimuth,
262+
sun_zenith=row.zenith,
263+
sun_azimuth=row.azimuth,
264264
type='clear_sky')
265-
epsilon = all_weather_sky_clearness(row.dni, row.dhi, row.sun_zenith)
265+
epsilon = all_weather_sky_clearness(row.dni, row.dhi, row.zenith)
266266
f_clear = f_clear_sky(epsilon)
267267
_lum = f_clear * cs + (1 - f_clear) * soc
268268
_hi = sky_hi(grid, _lum)

src/openalea/astk/sun_and_sky.py

+32-85
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
sky_irradiance,
2323
clear_sky_irradiances,
2424
horizontal_irradiance)
25-
from .meteorology.sky_luminance import cie_relative_luminance
25+
from .meteorology.sky_luminance import sky_luminance
2626
from .meteorology.sun_position import sun_position
27+
from .sky_map import sky_grid, sky_map
28+
2729

2830
# default location and dates
2931
_daydate = '2000-06-21'
@@ -77,9 +79,8 @@ def sky_discretisation(turtle_sectors=46, nb_az=None, nb_el=None):
7779
return elevations_h[:nb_sect], azimuths_h[:nb_sect], sky_fraction
7880

7981

80-
def sky_radiance_distribution(sky_elevation, sky_azimuth, sky_fraction,
81-
sky_type='soc', sun_elevation=None,
82-
sun_azimuth=None, avoid_sun=True):
82+
def sky_radiance_distribution(sky_elevation, sky_azimuth,
83+
sky_type='soc', sky_irradiance=None):
8384
"""Normalised sky radiance distribution as a function of sky type for a
8485
finite set of directions sampling the sky hemisphere.
8586
@@ -88,43 +89,20 @@ def sky_radiance_distribution(sky_elevation, sky_azimuth, sky_fraction,
8889
sampling the sky hemisphere
8990
sky_azimuth: (float or list of float) azimuth (degrees, from North,
9091
positive clockwise) of directions sampling the sky hemisphere
91-
sky_fraction: (float or list of float) fraction of sky associated to
92-
directions sampling the sky hemisphere
93-
sky_type: (str) one of 'soc' (standard overcast sky),
94-
'uoc' (uniform luminance)
95-
'clear_sky' (standard clear sky low turbidity)
96-
sun_elevation: sun elevation (degrees). Only needed for clear_sky
97-
sun_azimuth: sun azimuth (degrees, from North, positive clockwise).
98-
Only needed for clear_sky
99-
avoid_sun (bool): avoid sampling radiance distribution toward directions
100-
directly pointing to solar disc
92+
sky_type (str): sky type, one of ('soc', 'uoc', 'clear_sky', 'blended', 'all_weather')
93+
sky_irradiance: a datetime indexed dataframe specifying sky irradiances for the period, such as returned by
94+
astk.sky_irradiance.sky_irradiance. Needed for all sky_types except 'uoc' and 'soc'
10195
10296
Returns:
103-
the relative radiance(s) associated to the sky directions
97+
a list relative_luminance
10498
"""
10599

106-
el = numpy.radians(sky_elevation)
107-
az = numpy.radians(sky_azimuth)
108-
sky_fraction = numpy.array(sky_fraction)
109-
110-
if sun_elevation is not None:
111-
sun_elevation = numpy.radians(sun_elevation)
112-
if sun_azimuth is not None:
113-
sun_azimuth = numpy.radians(sun_azimuth)
114-
115-
if avoid_sun and sky_type == 'clear_sky':
116-
delta_el = abs(el - sun_elevation)
117-
delta_az = abs(az - sun_azimuth)
118-
sun_disc = numpy.radians(0.553)
119-
az += numpy.where((delta_az < sun_disc) & (delta_el < sun_disc), sun_disc,
120-
0)
121-
122-
lum = cie_relative_luminance(el, az, sun_elevation, sun_azimuth,
123-
type=sky_type)
124-
rad_dist = lum * sky_fraction
125-
rad_dist /= sum(rad_dist)
126-
127-
return rad_dist
100+
grid = sky_grid()
101+
lum = sky_luminance(grid, sky_type, sky_irradiance)
102+
directions = [*zip(sky_elevation, sky_azimuth)]
103+
sky_sources, smap = sky_map(grid, lum, directions)
104+
slum = list(zip(*sky_sources))[2]
105+
return horizontal_irradiance(slum, sky_elevation)
128106

129107

130108
def sun_sources(irradiance=1, dates=None, daydate=_daydate,
@@ -173,10 +151,7 @@ def sky_sources(sky_type='soc', irradiance=1, turtle_sectors=46, dates=None, day
173151
altitude=_altitude, timezone=_timezone):
174152
""" Light sources representing standard cie sky types in 46 directions
175153
Args:
176-
sky_type:(str) type of sky luminance model. One of :
177-
'soc' (standard overcast sky),
178-
'uoc' (uniform overcast sky)
179-
'clear_sky' (standard clear sky)
154+
sky_type (str): sky type, one of ('soc', 'uoc', 'clear_sky', 'blended', 'all_weather')
180155
irradiance: (float) sum of horizontal irradiance of all sources. If None
181156
diffuse horizontal clear_sky irradiance are used for clear_sky type and
182157
20% attenuated clear_sky global horizontal irradiances are used for
@@ -197,49 +172,23 @@ def sky_sources(sky_type='soc', irradiance=1, turtle_sectors=46, dates=None, day
197172

198173
source_elevation, source_azimuth, source_fraction = sky_discretisation(turtle_sectors)
199174

175+
c_sky = sky_irradiance(dates=dates, daydate=daydate,
176+
longitude=longitude, latitude=latitude,
177+
altitude=altitude, timezone=timezone)
200178
if sky_type == 'soc' or sky_type == 'uoc':
201-
radiance = sky_radiance_distribution(source_elevation, source_azimuth,
202-
source_fraction,
203-
sky_type=sky_type)
204-
source_irradiance = horizontal_irradiance(radiance, source_elevation)
179+
source_irradiance = sky_radiance_distribution(source_elevation, source_azimuth,
180+
sky_type=sky_type)
205181
if irradiance is None:
206-
sky_irradiance = clear_sky_irradiances(dates=dates, daydate=daydate,
207-
longitude=longitude,
208-
latitude=latitude,
209-
altitude=altitude,
210-
timezone=timezone)
211-
irradiance = sum(sky_irradiance['ghi']) * 0.2
212-
213-
elif sky_type == 'clear_sky':
214-
sun = sun_position(dates=dates, daydate=daydate, latitude=latitude,
215-
longitude=longitude, altitude=altitude,
216-
timezone=timezone)
217-
c_sky = clear_sky_irradiances(dates=dates, daydate=daydate,
218-
longitude=longitude, latitude=latitude,
219-
altitude=altitude, timezone=timezone)
220-
c_sky = pandas.concat([sun, c_sky], axis=1)
182+
irradiance = sum(c_sky['ghi']) * 0.2
183+
else:
184+
source_irradiance = sky_radiance_distribution(source_elevation, source_azimuth,
185+
sky_type=sky_type, sky_irradiance=c_sky)
221186
if irradiance is None:
222187
irradiance = sum(c_sky['dhi'])
223188

224-
# temporal weigths : use dhi (diffuse horizontal irradiance)
225-
c_sky['wsky'] = c_sky['dhi'] / sum(c_sky['dhi'])
226-
source_irradiance = numpy.zeros_like(source_fraction)
227-
for i, row in c_sky.iterrows():
228-
rad = sky_radiance_distribution(source_elevation, source_azimuth,
229-
source_fraction,
230-
sky_type='clear_sky',
231-
sun_elevation=row['elevation'],
232-
sun_azimuth=row['azimuth'],
233-
avoid_sun=True)
234-
source_irradiance += (
235-
horizontal_irradiance(rad, source_elevation) * row['wsky'])
236-
else:
237-
raise ValueError(
238-
'unknown type: ' + sky_type +
239-
' (should be one of uoc, soc, clear_sky')
240-
241189
source_irradiance /= sum(source_irradiance)
242190
source_irradiance *= irradiance
191+
243192
return source_elevation, source_azimuth, source_irradiance
244193

245194

@@ -307,16 +256,13 @@ def sun_sky_sources(ghi=None, dhi=None, attenuation=None, model='blended',
307256
sun = sun_sources(irradiance=f_sun, dates=dates,
308257
daydate=daydate, latitude=latitude, longitude=longitude,
309258
altitude=altitude, timezone=timezone)
259+
source_elevation, source_azimuth, source_fraction = sky_discretisation()
310260

311261
if model == 'blended' and f_sun > 0:
312-
f_clear_sky, f_soc = f_clear_sky(sky_irr, f_sun)
313-
sky_el, sky_az, soc = sky_sources(sky_type='soc', irradiance=f_soc)
314-
_, _, csky = sky_sources(sky_type='clear_sky',
315-
irradiance=f_clear_sky, dates=dates,
316-
daydate=daydate, latitude=latitude,
317-
longitude=longitude, altitude=altitude,
318-
timezone=timezone)
319-
sky = sky_el, sky_az, soc + csky
262+
source_irradiance = sky_radiance_distribution(source_elevation, source_azimuth,
263+
sky_type='blended', sky_irradiance=sky_irr)
264+
source_irradiance *= (1 - f_sun)
265+
sky = source_elevation, source_azimuth, source_irradiance
320266
elif model == 'sun_soc' or f_sun == 0:
321267
sky = sky_sources(sky_type='soc', irradiance= 1 - f_sun)
322268
elif f_sun == 0:
@@ -325,6 +271,7 @@ def sun_sky_sources(ghi=None, dhi=None, attenuation=None, model='blended',
325271
raise ValueError(
326272
'unknown model: ' + model +
327273
' (should be one of: soc_sun, blended)')
274+
328275
return sky_irr, sun, sky
329276

330277

test/test_sun_and_sky.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,11 @@ def test_sky_radiance_distribution():
3131
elevation, azimuth, strd = sky_discretisation()
3232
fraction = numpy.array(strd) / sum(strd)
3333

34-
d = sky_radiance_distribution(elevation, azimuth, fraction,
34+
d = sky_radiance_distribution(elevation, azimuth,
3535
sky_type='soc')
3636
assert len(d) == len(elevation)
37-
numpy.testing.assert_almost_equal(d.sum(), 1)
38-
39-
# chek max radiance is toward sun direction for clear skies
40-
sun_elevation = elevation[20]
41-
sun_azimuth = azimuth[20]
42-
d = sky_radiance_distribution(elevation, azimuth, strd,
43-
sky_type='clear_sky',
44-
sun_elevation=sun_elevation,
45-
sun_azimuth=sun_azimuth)
46-
assert len(d) == len(elevation)
47-
numpy.testing.assert_almost_equal(d.sum(), 1)
48-
assert numpy.argmax(d) == 20
37+
numpy.testing.assert_almost_equal(d.sum(), 1, decimal=2)
38+
4939

5040

5141
def test_sky_sources():
@@ -58,7 +48,7 @@ def test_sky_sources():
5848
numpy.testing.assert_almost_equal(numpy.sum(irr), 1)
5949

6050
el, az, irr = sky_sources(sky_type='clear_sky', irradiance=None)
61-
assert irr.max() > 60
51+
assert irr.max() > 40
6252

6353

6454
def test_sun_source():
@@ -73,7 +63,7 @@ def test_sun_source():
7363
def test_sun_sky_sources():
7464
_, sun, sky = sun_sky_sources(model='sun_soc')
7565
_, sun, sky = sun_sky_sources(model='blended')
76-
numpy.testing.assert_almost_equal(sun[2].sum() + sky[2].sum(), 1)
66+
numpy.testing.assert_almost_equal(sun[2].sum() + sky[2].sum(), 1, decimal=2)
7767
assert sun[2].sum() > 0.75
7868
_, sun, sky = sun_sky_sources(model='blended', attenuation=0.2)
7969
assert sky[2].sum() > 0.99

0 commit comments

Comments
 (0)