@@ -218,82 +218,78 @@ def all_weather_relative_luminance(grid, sun_zenith, sun_azimuth, clearness, bri
218
218
return gradation * indicatrix
219
219
220
220
221
- def sun_to_zero (grid , luminance , sun_zenith , sun_azimuth , sun_hi ):
222
- """Set luminance arround the sun to zero, up to a sun_hi decrease of sky horrizontal irradiance"""
223
-
224
- ksi = ksi_grid (grid , sun_zenith = sun_zenith , sun_azimuth = sun_azimuth )
225
- sun_order = numpy .argsort (ksi , None )
226
- _ , _ , _ , sky_zenith , _ = grid
227
- shi = horizontal_irradiance (luminance , 90 - sky_zenith )
228
- lim = numpy .searchsorted (shi .flatten ()[sun_order ].cumsum (), sun_hi )
229
- newlum = luminance .flatten ()
230
- newlum [sun_order [:lim ]] = 0
231
- return newlum .reshape (luminance .shape )
232
-
233
-
234
- def sky_luminance (grid , sky_type = 'soc' , sky_irradiance = None , hide_sun = False ):
235
- """Relative sky luminance map for different conditions, periods and sky types
236
- Luminance are scaled so that total sky horizontal irradiance equals one.
221
+ def sky_luminance (grid , sky_type = 'soc' , sky_irradiance = None , add_sun = False , sun_disc = 5 ):
222
+ """Sky luminance as a function of sky type and sky_irradiance, scaled so that sky horizontal irradiance equals one.
237
223
238
224
Args:
225
+ grid: a (azimuth, zenith, az_c, z_c, w_c) tuple of sky coordinates, such as returned by astk.sky_map.sky_grid
239
226
sky_type (str): sky type, one of ('soc', 'uoc', 'clear_sky', 'blended', 'all_weather')
240
227
sky_irradiance: a datetime indexed dataframe specifying sky irradiances for the period, such as returned by
241
- astk.sky_irradiance.sky_irradiance. Needed for all sky_types except 'uoc' and 'soc'
242
- hide_sun: Should the sun be shaded ?
228
+ astk.meteorology.sky_irradiance.sky_irradiance. Needed for all sky_types except 'uoc' and 'soc'
229
+ add_sun: Should the sun be added ? If True, sun luminance is added to sky luminance in the sun region.
230
+ sun_disc: angular diameter of the sun region (degree)
243
231
"""
244
232
233
+ if add_sun or sky_type not in ('soc' , 'uoc' ):
234
+ if sky_irradiance is None :
235
+ raise ValueError ('sky_irradiance is required for this type of sky' )
236
+
245
237
azimuth , zenith , az_c , z_c , w_c = grid
246
238
lum = numpy .zeros_like (az_c )
247
239
248
- if sky_irradiance is not None :
249
- irrad = sky_irradiance .copy ()
250
- irrad ['dates' ] = irrad .index
251
-
252
- def _scale_lum (grid , _lum , row , hide_sun ):
253
- _hi = sky_hi (grid , _lum )
254
- if hide_sun :
255
- sun_hi = (row .ghi - row .dhi ) / row .ghi * _hi
256
- _lum = sun_to_zero (grid , _lum , row .zenith , row .azimuth , sun_hi )
257
- _hi = sky_hi (grid , _lum )
258
- return row .dhi / _hi * _lum
259
- else :
260
- return row .ghi / _hi * _lum
261
-
262
- if sky_type in ('soc' , 'uoc' ):
240
+ if sky_type in ('soc' , 'uoc' ) and not add_sun :
263
241
lum = w_c * cie_relative_luminance (grid = grid , type = sky_type )
264
- elif sky_type == 'clear_sky' :
265
- for row in irrad .itertuples ():
266
- _lum = w_c * cie_relative_luminance (grid = grid ,
267
- sun_zenith = row .zenith ,
268
- sun_azimuth = row .azimuth ,
269
- type = 'clear_sky' )
270
- lum += _scale_lum (grid , _lum , row , hide_sun )
271
- elif sky_type == 'all_weather' :
272
- for row in irrad .itertuples ():
273
- brightness = all_weather_sky_brightness (row .dates , row .dhi , row .zenith )
274
- clearness = all_weather_sky_clearness (row .dni , row .dhi , row .zenith )
275
- _lum = w_c * all_weather_relative_luminance (grid ,
276
- sun_zenith = row .zenith ,
277
- sun_azimuth = row .azimuth ,
278
- brightness = brightness ,
279
- clearness = clearness )
280
- lum += _scale_lum (grid , _lum , row , hide_sun )
281
- elif sky_type == 'blended' :
282
- soc = w_c * cie_relative_luminance (grid = grid , type = 'soc' )
283
- for row in irrad .itertuples ():
284
- cs = w_c * cie_relative_luminance (grid = grid ,
242
+ else :
243
+ soc = None
244
+ if sky_type == 'blended' :
245
+ soc = w_c * cie_relative_luminance (grid = grid , type = 'soc' )
246
+ for row in sky_irradiance .itertuples ():
247
+ if sky_type in ('soc' , 'uoc' ):
248
+ _lum = w_c * cie_relative_luminance (grid = grid , type = sky_type )
249
+ elif sky_type == 'clear_sky' :
250
+ _lum = w_c * cie_relative_luminance (grid = grid ,
285
251
sun_zenith = row .zenith ,
286
252
sun_azimuth = row .azimuth ,
287
253
type = 'clear_sky' )
288
- epsilon = all_weather_sky_clearness (row .dni , row .dhi , row .zenith )
289
- f_clear = f_clear_sky (epsilon )
290
- _lum = f_clear * cs + (1 - f_clear ) * soc
291
- lum += _scale_lum (grid , _lum , row , hide_sun )
292
- else :
293
- raise ValueError ('undefined sky type: ' + sky_type )
254
+ elif sky_type == 'all_weather' :
255
+ brightness = all_weather_sky_brightness (row .Index , row .dhi , row .zenith )
256
+ clearness = all_weather_sky_clearness (row .dni , row .dhi , row .zenith )
257
+ _lum = w_c * all_weather_relative_luminance (grid ,
258
+ sun_zenith = row .zenith ,
259
+ sun_azimuth = row .azimuth ,
260
+ brightness = brightness ,
261
+ clearness = clearness )
262
+ elif sky_type == 'blended' :
263
+ cs = w_c * cie_relative_luminance (grid = grid ,
264
+ sun_zenith = row .zenith ,
265
+ sun_azimuth = row .azimuth ,
266
+ type = 'clear_sky' )
267
+ epsilon = all_weather_sky_clearness (row .dni , row .dhi , row .zenith )
268
+ f_clear = f_clear_sky (epsilon )
269
+ _lum = f_clear * cs + (1 - f_clear ) * soc
270
+ else :
271
+ raise ValueError ('undefined sky type: ' + sky_type )
294
272
273
+ _hi = sky_hi (grid , _lum )
274
+ _lum = _lum / _hi * row .dhi
275
+ if add_sun :
276
+ ksi_sun = ksi_grid (grid , sun_zenith = row .zenith , sun_azimuth = row .azimuth )
277
+ if row .dni > _lum [ksi_sun <= sun_disc ].sum (): # only add sun if it is brighter than sky
278
+ lost_hi = horizontal_irradiance (_lum [ksi_sun <= sun_disc ], 90 - z_c [ksi_sun <= sun_disc ]).sum ()
279
+ _lum *= row .dhi / (row .dhi - lost_hi )
280
+ _lum [ksi_sun <= sun_disc ] = row .dni / _lum [ksi_sun <= sun_disc ].size
281
+ lum += _lum
295
282
# scale so that hi = 1
296
283
hi = sky_hi (grid , lum )
284
+ hi = 1
297
285
return lum / hi
298
286
299
287
288
+ def hide_sun (grid , luminance , sun_zenith , sun_azimuth , sun_disc = 5 ):
289
+ """Hide sun"""
290
+ hlum = luminance .copy ()
291
+ ksi_sun = ksi_grid (grid , sun_zenith = sun_zenith , sun_azimuth = sun_azimuth )
292
+ hlum [ksi_sun <= sun_disc ] = 0
293
+ return hlum
294
+
295
+
0 commit comments