Station API¶
Weather station index, search, and geocoding.
StationIndex¶
idfkit.weather.index.StationIndex
¶
Searchable index of weather stations from climate.onebuilding.org.
Use load to load the bundled (or user-refreshed) station index.
No network access or openpyxl is required for load.
Use check_for_updates to see if upstream data has changed, and refresh to re-download and rebuild the index.
Examples:
index = StationIndex.load()
results = index.search("chicago ohare", limit=3)
for r in results:
print(r.station.display_name, r.score)
Source code in src/idfkit/weather/index.py
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 332 333 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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | |
countries
property
¶
Sorted list of unique country codes in the index.
load(*, cache_dir=None)
classmethod
¶
Load the station index from a local compressed file.
Checks for a user-refreshed cache first, then falls back to the bundled index shipped with the package. No network access is required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path | None
|
Override the default cache directory. |
None
|
Source code in src/idfkit/weather/index.py
refresh(*, cache_dir=None)
classmethod
¶
Re-download Excel indexes from climate.onebuilding.org and rebuild the cache.
Requires openpyxl. Install with pip install idfkit[weather].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path | None
|
Override the default cache directory. |
None
|
Source code in src/idfkit/weather/index.py
check_for_updates()
¶
Check if upstream Excel files have changed since this index was built.
Sends lightweight HEAD requests to climate.onebuilding.org.
Returns True if any file has a newer Last-Modified date.
Returns False if all files match or if the check fails (offline,
timeout, etc.).
Source code in src/idfkit/weather/index.py
search(query, *, limit=10, country=None)
¶
Fuzzy-search stations by name, city, state, or WMO number.
Matching is case-insensitive and uses substring / token-prefix heuristics (no external NLP dependencies).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Free-text search query. |
required |
limit
|
int
|
Maximum number of results to return. |
10
|
country
|
str | None
|
If given, restrict to stations in this country code. |
None
|
Source code in src/idfkit/weather/index.py
nearest(latitude, longitude, *, limit=5, max_distance_km=None, country=None)
¶
Find stations nearest to a geographic coordinate.
Uses the Haversine formula for great-circle distance. A bounding-box pre-filter is applied when max_distance_km is specified to avoid computing distances for stations that are obviously too far.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latitude
|
float
|
Decimal degrees, north positive. |
required |
longitude
|
float
|
Decimal degrees, east positive. |
required |
limit
|
int
|
Maximum results to return. |
5
|
max_distance_km
|
float | None
|
Exclude stations farther than this. |
None
|
country
|
str | None
|
If given, restrict to this country code. |
None
|
Source code in src/idfkit/weather/index.py
filter(*, country=None, state=None, wmo_region=None)
¶
Filter stations by metadata criteria.
All specified criteria must match (logical AND).
Source code in src/idfkit/weather/index.py
WeatherStation¶
idfkit.weather.station.WeatherStation
dataclass
¶
Metadata for a single weather file entry from climate.onebuilding.org.
Each instance represents one downloadable weather dataset. The same physical
station may appear multiple times with different source or year-range
variants (e.g. TMYx.2007-2021 vs TMYx.2009-2023).
Attributes:
| Name | Type | Description |
|---|---|---|
country |
str
|
ISO 3166 country code (e.g. |
state |
str
|
State or province abbreviation (e.g. |
city |
str
|
City or station name as it appears in the index
(e.g. |
wmo |
str
|
WMO station number as a string to preserve leading zeros
(e.g. |
source |
str
|
Dataset source identifier (e.g. |
latitude |
float
|
Decimal degrees, north positive. |
longitude |
float
|
Decimal degrees, east positive. |
timezone |
float
|
Hours offset from GMT (e.g. |
elevation |
float
|
Meters above sea level. |
url |
str
|
Full download URL for the ZIP archive. |
Source code in src/idfkit/weather/station.py
city
instance-attribute
¶
state
instance-attribute
¶
country
instance-attribute
¶
wmo
instance-attribute
¶
latitude
instance-attribute
¶
longitude
instance-attribute
¶
elevation
instance-attribute
¶
url
instance-attribute
¶
display_name
property
¶
Human-readable station name with location context.
Dots in the city name are replaced with spaces for readability.
SearchResult¶
idfkit.weather.station.SearchResult
dataclass
¶
A text search result with relevance score.
Source code in src/idfkit/weather/station.py
SpatialResult¶
idfkit.weather.station.SpatialResult
dataclass
¶
A spatial proximity result with great-circle distance.
Source code in src/idfkit/weather/station.py
geocode¶
idfkit.weather.geocode.geocode(address)
¶
Convert a street address to (latitude, longitude) via Nominatim.
Uses the free OpenStreetMap Nominatim geocoding service. No API key is required. Requests are rate-limited to one per second in compliance with Nominatim usage policy.
This function is thread-safe. Concurrent calls from multiple threads will be serialized to respect the rate limit.
Composable with spatial search: Use the splat operator to combine with nearest for address-based weather station lookup:
```python
from idfkit.weather import StationIndex, geocode
# Find weather stations near an address (one line!)
results = StationIndex.load().nearest(*geocode("350 Fifth Avenue, New York, NY"))
for r in results[:3]:
print(f"{r.station.display_name}: {r.distance_km:.0f} km")
```
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str
|
A free-form address string (e.g. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
A |
Raises:
| Type | Description |
|---|---|
GeocodingError
|
If the address cannot be resolved or the service is unreachable. |
Example
lat, lon = geocode("Empire State Building, NYC") print(f"{lat:.4f}, {lon:.4f}") 40.7484, -73.9857