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 idfkit/weather/index.py
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 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 | |
countries
property
¶
Sorted list of unique country codes in the index.
stations
property
¶
All stations in the index.
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 idfkit/weather/index.py
filter(*, country=None, state=None, wmo_region=None, climate_zone=None, climate_zone_determined=None)
¶
Filter stations by metadata criteria.
All specified criteria must match (logical AND).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country
|
str | None
|
ISO 3166 country code. Case-insensitive. |
None
|
state
|
str | None
|
State or province abbreviation. Case-insensitive. |
None
|
wmo_region
|
int | None
|
WMO region number, inferred from the download URL. |
None
|
climate_zone
|
str | None
|
ASHRAE climate zone code, e.g. |
None
|
climate_zone_determined
|
bool | None
|
Whether the station's zone was determined
upstream. |
None
|
Source code in idfkit/weather/index.py
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 | |
from_stations(stations)
classmethod
¶
Create an index from an explicit list of stations (useful for tests).
get_by_filename(filename)
¶
Look up stations by EPW filename.
The filename can include or omit the extension (.zip,
.epw, .ddy). Matching is case-insensitive.
When the exact filename matches an indexed entry, those stations are returned. Otherwise the method extracts the WMO number from the filename and falls back to get_by_wmo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
An EPW filename or stem, e.g.
|
required |
Returns:
| Type | Description |
|---|---|
list[WeatherStation]
|
Matching stations (may be empty). |
Source code in idfkit/weather/index.py
get_by_wmo(wmo)
¶
Look up stations by WMO number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wmo
|
str
|
WMO station number as a string (e.g. |
required |
Returns a list because a single WMO number can correspond to multiple stations or dataset variants.
Source code in idfkit/weather/index.py
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 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.
Each result is one physical station: result.station is its newest
dataset within max_distance_km and result.variants lists its datasets
there, newest first. Distances and filters use each dataset's own
coordinates, and distance_km is result.station's. Datasets of one
station whose coordinates lie more than a few kilometres apart are
returned as separate results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latitude
|
float
|
Decimal degrees, north positive. |
required |
longitude
|
float
|
Decimal degrees, east positive. |
required |
limit
|
int
|
Maximum number of stations 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 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 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- and accent-insensitive ("Montréal" finds
"Montreal") and uses substring / token-prefix heuristics (no
external NLP dependencies). The query is split on whitespace,
commas, semicolons, dots and hyphens, and a token equal to a station's state or
country code qualifies the match rather than having to appear in
the name, so "Boulder, CO" ranks Boulder, Colorado first. A station
whose name starts with the query ranks above one that only contains it
later on, so "London" lists London City and London Gatwick before East
London, South Africa; add a qualifier ("London, GBR") or pass country
to narrow further. An exact WMO number scores 1.0. Canonical EPW
filenames (e.g.
"USA_IL_Chicago.Ohare.Intl.AP.725300_TMYx.2009-2023") are
detected automatically and resolved via
get_by_filename.
Each result is one physical station: result.station is its newest
dataset (the latest TMYx year range) and result.variants lists all
of them, newest first. An exact EPW filename returns that dataset alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Free-text search query or EPW filename. |
required |
limit
|
int
|
Maximum number of stations to return. |
10
|
country
|
str | None
|
If given, restrict to stations in this country code. |
None
|
Source code in 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
|
Source tag from the upstream index (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 idfkit/weather/station.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 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 | |
ashrae_climate_zone
instance-attribute
¶
cdd10
instance-attribute
¶
city
instance-attribute
¶
cooling_design_db_c
instance-attribute
¶
cooling_design_db_f
property
¶
1% cooling design dry-bulb temperature in °F.
country
instance-attribute
¶
dataset_variant
property
¶
Extract the TMYx dataset variant from the download URL.
Returns a string like "TMYx", "TMYx.2007-2021", or
"TMYx.2009-2023".
dataset_years
property
¶
The first and last year of the dataset's period, or None when it names none.
(2009, 2023) for "TMYx.2009-2023"; None for a plain "TMYx".
design_conditions_source_wmo = None
class-attribute
instance-attribute
¶
display_name
property
¶
Human-readable station name with location context.
Dots in the city name are replaced with spaces for readability.
elevation
instance-attribute
¶
filename_stem
property
¶
The canonical EPW filename stem derived from the download URL.
Returns the ZIP filename without the .zip extension, e.g.
"USA_IL_Chicago.Ohare.Intl.AP.725300_TMYx.2009-2023".
hdd18
instance-attribute
¶
heating_design_db_c
instance-attribute
¶
heating_design_db_f
property
¶
99% heating design dry-bulb temperature in °F.
latitude
instance-attribute
¶
longitude
instance-attribute
¶
source
instance-attribute
¶
state
instance-attribute
¶
station_key
property
¶
What every dataset of one physical station shares: the filename stem without its variant.
"USA_IL_Chicago.Ohare.Intl.AP.725300" for each of that station's TMYx datasets.
timezone
instance-attribute
¶
url
instance-attribute
¶
wmo
instance-attribute
¶
from_dict(data)
classmethod
¶
Deserialize from a plain dictionary.
The required climate fields use data[key] so a stale or
corrupt payload fails loudly. design_conditions_source_wmo
is genuinely optional.
Source code in idfkit/weather/station.py
to_dict()
¶
Serialize to a plain dictionary for JSON storage.
Source code in idfkit/weather/station.py
SearchResult¶
idfkit.weather.station.SearchResult
dataclass
¶
A text search result with relevance score: one physical station.
station is the station's newest dataset; variants holds every dataset
of it the search matched (the TMYx year ranges), newest first.
Source code in idfkit/weather/station.py
match_field
instance-attribute
¶
Which field matched: "wmo", "name", "state", "country", or "filename".
score
instance-attribute
¶
Relevance score from 0.0 to 1.0, higher is better.
station
instance-attribute
¶
variants = field(default=())
class-attribute
instance-attribute
¶
Every dataset of this station, newest first; variants[0] is station.
SpatialResult¶
idfkit.weather.station.SpatialResult
dataclass
¶
A spatial proximity result with great-circle distance: one physical station.
station is the station's newest dataset; variants holds its datasets
(the TMYx year ranges) at this location, newest first. A station's datasets
do not always share coordinates, and those lying more than a few kilometres
from the rest come back as a separate result.
Source code in 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. Its |
Example
lat, lon = geocode("Empire State Building, NYC") print(f"{lat:.4f}, {lon:.4f}") 40.7484, -73.9857
Source code in idfkit/weather/geocode.py
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 | |
GeocodingError¶
idfkit.weather.geocode.GeocodingError
¶
Bases: Exception
Raised when an address or IP cannot be resolved to coordinates.
Branch on kind rather than on the message, whose wording may change.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable description. |
required |
kind
|
GeocodingErrorKind
|
Why the call failed. Defaults to |
'unreachable'
|
status
|
int | None
|
The HTTP status, when |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
GeocodingErrorKind
|
Why the call failed. |
status |
int | None
|
The HTTP status when |
Source code in idfkit/weather/geocode.py
GeocodingErrorKind¶
idfkit.weather.geocode.GeocodingErrorKind = Literal['not-found', 'unreachable', 'http', 'invalid-response']
module-attribute
¶
Why a geocoding call failed.
"not-found": the service answered and has no location for the query. Retrying will not help; a different query might."unreachable": the request never got an answer (offline, DNS, refused, timed out). A retry may help."http": the service answered with an HTTP error; status holds the code. A429or5xxmay clear on retry."invalid-response": the service answered with a body that is not the expected JSON, which is what a captive portal or an intercepting proxy produces.