Created
November 29, 2023 23:56
-
-
Save Peetz0r/bd6341fd58dbbcdb17fa2a201ddeb63a to your computer and use it in GitHub Desktop.
KNMI dinges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import requests, datetime, netCDF4, re | |
# 6215: Voorschoten | |
# 6344: Rotterdam The Hague Airport | |
# 6330: Hoek van Holland | |
weather_stations = [6330, 6344, 6215] | |
apiKey = 'ey...J9' | |
fields = [ | |
('temperature', 'ta'), | |
('groundtemperature', 'tgn'), | |
('humidity', 'rh'), | |
] | |
dtNow = datetime.datetime.utcnow() | |
dtSearch = dtNow - datetime.timedelta(minutes = 20 + dtNow.minute % 10) | |
session = requests.Session() | |
session.headers.update({'Authorization': apiKey}) | |
print("Finding the right file...") | |
files = session.get('https://api.dataplatform.knmi.nl/open-data/datasets/Actuele10mindataKNMIstations/versions/2/files', params = { | |
'startAfterFilename': f"KMDS__OPER_P___10M_OBS_L2_{dtSearch:%Y%m%d%H%M}.nc" | |
}) | |
fileName = files.json()['files'][-1]['filename'] | |
fileMetadata = session.get(f"https://api.dataplatform.knmi.nl/open-data/datasets/Actuele10mindataKNMIstations/versions/2/files/{fileName}/url") | |
rlRemaining = fileMetadata.headers['X-Ratelimit-Remaining'] | |
rlLimit = fileMetadata.headers['X-Ratelimit-Limit'] | |
rlReset = datetime.datetime.fromtimestamp(int(fileMetadata.headers['X-Ratelimit-Reset'])) | |
print(f"Ratelimit stats: {rlRemaining}/{rlLimit} remaining (resets at {rlReset})") | |
print(f"Found {fileName}, last modified at {files.json()['files'][-1]['lastModified']}, starting download...") | |
theFile = requests.get(fileMetadata.json()['temporaryDownloadUrl']) | |
print(f"Downloading completed ({len(theFile.content)/1024:.0f} kb)") | |
dataset = netCDF4.Dataset(fileName, memory=theFile.content) | |
assert dataset.variables['time'].units == 'seconds since 1950-01-01 00:00:00', f"{dataset.variables['time']=:}" | |
# so, we're dealing with a 1950-01-01 epoch. Let's convert it to a unix timestamp | |
# there are 5 leap years between those epochs: '52, '56, '60, '64, and '68 | |
dt = datetime.datetime.utcfromtimestamp(dataset.variables['time'][:][0] - (20*365 + 5) * 24*60*60) | |
for station in weather_stations: | |
try: | |
idx = list(dataset.variables['station']).index(f"{station:05d}") | |
print(f"Valid for {dt} (UTC) at station {dataset.variables['stationname'][idx].title()}") | |
for field in fields: | |
print(f"{field[0]}, {dataset.variables[field[1]][idx][0]})") | |
except ValueError: | |
print(f"Can't find weather station {station}, skipping...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment