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 third-party dependencies are 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
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 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 | |
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.
If the cached file is from an older idfkit version and fails to
deserialise (e.g. missing schema fields after a format migration),
it is quarantined to stations.json.gz.stale and the bundled
index is used instead.
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 the regional KML indexes from climate.onebuilding.org and rebuild the cache.
Uses the Python standard library only — no third-party dependencies.
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 KML 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, WMO number, or EPW filename.
Matching is case-insensitive and uses substring / token-prefix
heuristics (no external NLP dependencies). Canonical EPW
filenames (e.g.
"USA_IL_Chicago.Ohare.Intl.AP.725300_TMYx.2009-2023") are
detected automatically and resolved via
get_by_filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Free-text search query or EPW filename. |
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. |
ashrae_climate_zone |
str
|
ASHRAE HOF climate zone label
(e.g. |
heating_design_db_c |
float
|
99% heating design dry-bulb temperature in °C. |
cooling_design_db_c |
float
|
1% cooling design dry-bulb temperature in °C. |
hdd18 |
int
|
Heating degree-days base 18 °C. |
cdd10 |
int
|
Cooling degree-days base 10 °C. |
design_conditions_source_wmo |
str | None
|
When the station inherits design
conditions from a neighbouring station, this holds that
station's WMO number; otherwise |
Source code in src/idfkit/weather/station.py
9 10 11 12 13 14 15 16 17 18 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 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 | |
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