solshade.utils¶
configure_logging
¶
configure_logging(level=logging.INFO, log_file=None, *, fmt='[%(asctime)s %(levelname)-8s ] %(name)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', force=False)
Configure the solshade root logger exactly once (idempotent).
Safe to call many times across your app; on subsequent calls (without force=True)
we simply update the level and keep existing handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str
|
Logging level (e.g., 10/20 or "DEBUG"/"INFO"). |
logging.INFO
|
log_file
|
Path | None
|
Optional path to a file that also receives logs. |
None
|
fmt
|
str
|
Formatter string applied to installed handlers. |
'[%(asctime)s %(levelname)-8s ] %(name)s: %(message)s'
|
datefmt
|
str
|
Date format string. |
'%Y-%m-%d %H:%M:%S'
|
force
|
bool
|
If True, remove existing handlers and reconfigure from scratch. |
False
|
Returns:
| Type | Description |
|---|---|
Logger
|
The configured |
Source code in src/solshade/utils.py
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 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 | |
get_logger
¶
get_logger(name=None)
Return a logger under the solshade namespace.
Typical usage in library modules:
>>> from solshade.utils import get_logger
>>> log = get_logger(__name__)
>>> log.debug("hello")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Logger name. Use |
None
|
Returns:
| Type | Description |
|---|---|
Logger
|
A child logger (or the solshade root if |
Source code in src/solshade/utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
parse_iso_utc
¶
parse_iso_utc(s)
Parse an ISO-8601 timestamp into a timezone-aware UTC datetime.
Accepts Z-suffix or offset (e.g., ...Z, +00:00). Naive inputs are
assumed to be UTC. Returns None if input is None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str | None
|
ISO-8601 timestamp string, or None. |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
UTC-normalized datetime, or None. |
Source code in src/solshade/utils.py
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 | |
read_geotiff
¶
read_geotiff(path)
Read a GeoTIFF into an xarray.DataArray with CRS/transform retained.
If the file was previously saved with transfer_spatial_metadata and contains
extra_dim_* attrs, the function will:
- rename the band dimension to the recorded logical name, and
- attach the recorded coordinate values (re-decoded from JSON, including
ISO datetimes restored to datetime64[s]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the GeoTIFF. |
required |
Returns:
| Type | Description |
|---|---|
DataArray
|
The loaded array with spatial metadata and any restored logical dimension. |
Source code in src/solshade/utils.py
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 | |
transfer_spatial_metadata
¶
transfer_spatial_metadata(dst, ref, *, extra_dim=None, attrs=None)
Copy spatial metadata (CRS, transform, spatial dims) from ref to dst,
and optionally record a logical leading dimension for GeoTIFF round-trips.
This is useful when writing 3D arrays to GeoTIFF where the leading dimension
(e.g., "time" or "azimuth") will be flattened to band. We persist enough
info in attrs to restore that logical dimension on read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst
|
DataArray
|
Destination array to annotate with spatial metadata. |
required |
ref
|
DataArray
|
Reference array providing CRS/transform and spatial dims. |
required |
extra_dim
|
(str, array - like) | None
|
If provided, stores:
- |
None
|
attrs
|
dict | None
|
Additional attrs to merge into |
None
|
Returns:
| Type | Description |
|---|---|
DataArray
|
Updated |
Source code in src/solshade/utils.py
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 | |
write_geotiff
¶
write_geotiff(da, path, **kwargs)
Write an xarray.DataArray to GeoTIFF via rioxarray, preserving spatial metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
Array with CRS/transform set ( |
required |
path
|
str
|
Output file path. |
required |
**kwargs
|
Extra args passed through to |
{}
|
Notes
This function does not alter the array; ensure CRS + transform are already set.
Source code in src/solshade/utils.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |