solshade.irradiance¶
compute_energy_metrics
¶
compute_energy_metrics(flux)
Compute daily, total, peak energy, and day-of-peak from a time series of flux.
Strategy: - Daily integration uses trapezoidal rule over time per day. - If any NaNs are present in a day's flux values for a pixel, that day's energy is set to NaN. - If any daily energy is NaN for a pixel, all metrics for that pixel are set to NaN.
Returns:
| Name | Type | Description |
|---|---|---|
daily_energy |
DataArray
|
|
total_energy |
DataArray
|
|
peak_energy |
DataArray
|
|
day_of_peak |
DataArray
|
|
Logging
- INFO: reports daily integration and masks.
- DEBUG: shapes and dtype of outputs.
Source code in src/solshade/irradiance.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
compute_flux_timeseries
¶
compute_flux_timeseries(horizon, sun_alt_deg, sun_az_deg, sun_au, sun_enu, normal_enu, times_utc, *, toa=1361.0, dtype=np.dtype(np.float32), n_jobs=-1, batch_size=512, backend='threading', prefer='threads')
Compute horizon-masked, Lambertian irradiance time series over a DEM in parallel.
Overview
Per time step t:
1) Sun direction sun_enu[t] (unit vector in ENU).
2) Terrain unit normals per pixel from normal_enu bands (east, north, up).
3) Sample horizon at nearest azimuth to sun_az_deg[t] to get skyline elevation per pixel.
4) Shadow: if sun_alt_deg[t] < horizon, irradiance = 0; else Lambertian max(0, n·s).
5) Scale by TOA (toa) and propagate NaNs from horizon.
Parallel Execution
Time steps are processed in parallel with joblib. You can control:
- n_jobs: number of workers (use -1 for all cores),
- batch_size: number of time steps batched per dispatch,
- backend: "threading" (recommended here) or "loky",
- prefer: scheduler hint; "threads" helps nudge thread pool behavior.
TOA and units
toa is the top-of-atmosphere solar irradiance in W·m⁻² (default 1361).
Output irradiance is toa * max(0, n · s_enu) with no atmospheric correction.
NaN propagation
If horizon is NaN for a pixel at time t, the output flux at that (t, y, x) is set to NaN — regardless of Sun altitude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
horizon
|
DataArray
|
Horizon elevation angles (deg), shape (azimuth|band, y, x). The azimuth dimension is either "azimuth" or "band" (GeoTIFF). |
required |
sun_alt_deg
|
(ndarray, shape(T))
|
Sun altitude in degrees per time step. |
required |
sun_az_deg
|
(ndarray, shape(T))
|
Sun azimuth in degrees (CW from North) per time step. |
required |
sun_au
|
(ndarray, shape(T))
|
Sun distance in Astronomical Units (au). |
required |
sun_enu
|
(ndarray, shape(T, 3))
|
Unit Sun direction vectors (E, N, U). |
required |
normal_enu
|
xarray.DataArray, shape (band=3, y, x)
|
Terrain unit normal components with bands ["east","north","up"]. |
required |
times_utc
|
ndarray
|
Numpy datetime64 array which is attached as the DataArray coordinate and also stored as ISO UTC strings in attrs. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
toa |
float
|
Top-of-atmosphere irradiance in W·m⁻². |
dtype |
dtype
|
Output dtype (and the computation dtype for inputs we cast below). |
n_jobs |
int
|
Parallel workers (via joblib). -1 = all cores. |
batch_size |
int
|
Number of time steps per joblib batch. |
backend |
(threading, loky)
|
Joblib backend. "threading" is typically best here. |
prefer |
(threads, processes, None)
|
Scheduler hint for joblib. |
Returns:
| Name | Type | Description |
|---|---|---|
flux |
xarray.DataArray (time, y, x), dtype=`dtype`
|
Irradiance (W·m⁻²), horizon-masked Lambertian model.
attrs:
- "units": "W m^-2"
- "note": description
- "time_utc_iso": JSON list of ISO UTC strings (if |
Raises:
| Type | Description |
|---|---|
ValueError
|
On mismatched shapes, missing dims or missing attributes |
KeyError
|
If |
Logging
- INFO: Problem size and parallel execution parameters.
- DEBUG: shape checks, dtype casting, AU scaling, batching progress.
Source code in src/solshade/irradiance.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |