solshade.solar¶
compute_solar_ephem
¶
compute_solar_ephem(lat, lon, startutc=None, stoputc=None, timestep=3600, cache_dir='data/skyfield')
Compute the Sun’s apparent altitude/azimuth and the unit ENU direction vector over a time range at a given site.
The ENU vector is derived directly from the local topocentric (alt, az) returned by Skyfield—avoiding intermediate ECEF rotations and their numerical pitfalls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Geographic latitude in degrees (+N). |
required |
lon
|
float
|
Geographic longitude in degrees (+E). |
required |
startutc
|
datetime
|
UTC start time. If None, uses start of current UTC year. If naive, assumed UTC. |
None
|
stoputc
|
datetime
|
UTC stop time. If None, defaults to one year after |
None
|
timestep
|
int
|
Sampling step in seconds. Must be > 0. |
3600
|
cache_dir
|
str or Path
|
Skyfield cache directory (timescale + DE440s kernel). Default: |
'data/skyfield'
|
Returns:
| Name | Type | Description |
|---|---|---|
times_utc |
ndarray of datetime64[ns], shape (N,)
|
UTC timestamps for each sample. |
alt_deg |
ndarray of float, shape (N,)
|
Apparent altitude (degrees). No atmospheric refraction applied. |
az_deg |
ndarray of float, shape (N,)
|
Apparent azimuth (degrees), clockwise from true north, wrapped to [0, 360). |
dist_au |
ndarray of float, shape (N,)
|
Apparent distance between observer and Sun in astronomical units |
enu_unit |
ndarray of float, shape (N, 3)
|
Unit vectors pointing from the site toward the Sun in local ENU coordinates (columns: E, N, U). Each row has norm ~= 1. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
FileNotFoundError
|
If Skyfield cache files (timescale or ephemeris) are missing. |
Notes
- Uses the compact DE440s kernel.
- Apparent alt/az include light-time/aberration, no refraction.
-
ENU unit vector is computed via:
E = cos(alt) * sin(az) N = cos(alt) * cos(az) U = sin(alt)
where azimuth is CW from north (Skyfield’s default for topocentric alt/az).
Logging
- INFO: parameters (lat/lon, duration, timestep).
- DEBUG: step counts, times constructed, ENU unit vector shape.
Source code in src/solshade/solar.py
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 | |
load_sun_ephemeris
¶
load_sun_ephemeris(cache_dir='data/skyfield')
Load the Sun and Earth ephemeris segments from the DE440s file and a Timescale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str or Path
|
Directory for ephemeris and timescale cache. Defaults to ./data/skyfield. |
'data/skyfield'
|
Returns:
| Name | Type | Description |
|---|---|---|
sun |
object
|
Skyfield segment for the Sun (use with |
earth |
object
|
Skyfield segment for the Earth (use with |
ts |
Timescale
|
Timescale object for constructing Skyfield times. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If required ephemeris or timescale files are missing from the cache. |
Notes
- Uses the compact DE440s kernel (sufficient for Sun/Earth work).
- You must run once with internet access to populate the Skyfield cache, or manually place 'de440s.bsp' and timescale data in the cache_dir.
Logging
- INFO: confirms successful loading of ephemeris/timescale.
- ERROR: missing files raise exceptions.
Source code in src/solshade/solar.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
nearest_horizon_indices
¶
nearest_horizon_indices(sun_az_deg, horizon_az_deg)
Map each solar azimuth to the nearest horizon azimuth index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sun_az_deg
|
(ndarray, shape(M))
|
Solar azimuths in degrees (any real values). Wrapped to [0, 360). |
required |
horizon_az_deg
|
(ndarray, shape(N))
|
Uniformly spaced horizon azimuths in degrees over [0, 360).
Typically from |
required |
Returns:
| Name | Type | Description |
|---|---|---|
idx |
(ndarray, shape(M))
|
Indices into |
Logging
- DEBUG: reports horizon spacing and input count.
Source code in src/solshade/solar.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
solar_envelope_by_folding
¶
solar_envelope_by_folding(times_utc, alt_deg, az_deg, smooth_n=360)
Build daily solar envelopes (min/max altitude vs azimuth) by folding a uniformly sampled time series into UTC slots and taking per-slot extrema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
times_utc
|
np.ndarray of datetime64[*]
|
UTC timestamps at uniform cadence covering >= 1 full day. Any datetime64 unit is accepted and internally normalized to seconds. |
required |
alt_deg
|
(ndarray, shape(N))
|
Solar altitude samples in degrees. |
required |
az_deg
|
(ndarray, shape(N))
|
Solar azimuth samples in degrees. Values are wrapped to [0, 360). |
required |
smooth_n
|
int
|
Number of equally spaced azimuth points (degrees) for the smoothed envelope. Must be >= 4. |
360
|
Returns:
| Name | Type | Description |
|---|---|---|
az_plot |
(ndarray, shape(smooth_n + 1))
|
Azimuth grid in degrees, wrapped to include 360 for closed plotting. |
alt_min_plot |
(ndarray, shape(smooth_n + 1))
|
Smoothed minimum-altitude envelope at az_plot. |
alt_max_plot |
(ndarray, shape(smooth_n + 1))
|
Smoothed maximum-altitude envelope at az_plot. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Logging
- DEBUG: input sizes, cadence, slots per day.
- INFO: completion and output grid length.
Source code in src/solshade/solar.py
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 302 303 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 | |