Skip to content

Instantly share code, notes, and snippets.

@KMarshland
Last active December 18, 2024 04:35
Show Gist options
  • Save KMarshland/10d46b5ec79997a3449100173bc123c7 to your computer and use it in GitHub Desktop.
Save KMarshland/10d46b5ec79997a3449100173bc123c7 to your computer and use it in GitHub Desktop.
Script to download geopotential forecast from the Windborne API
import requests
import re
import os
import xarray as xr
from urllib.parse import urlparse, unquote
def download_forecast(initialization_time):
"""
Downloads a geopotential forecast from the Windborne API
This will create a file for each hour in the forecast at 6 hour resolution, and return those filenames
These files will contain the 500mb geopotential forecasts for the entire globe in npy format
initialization_time is expected to be a ISO string
"""
url = f"https://forecasts.windbornesystems.com/api/v1/gridded/historical/500/geopotential"
files = []
expected_file_size = 720*1440*4 + 16036 # 720x1440 grid, 4 bytes per float, 16036 bytes of metadata
for hour in range(24, 24*7 + 1, 24):
response = requests.get(url, params={'forecast_hour': hour, 'initialization_time': initialization_time}, allow_redirects=True, stream=True)
response.raise_for_status()
# take the filename from the response header; you're welcome to change this
# NOTE: you may want to change this download location
filename = None
if 'content-disposition' in response.headers:
filename = re.findall("filename=(.+)", response.headers['content-disposition'])[0]
if filename is None:
path = urlparse(response.url).path
forecast_time = unquote(path.split('/')[-2].split('.')[0])
filename = forecast_time + '_geopotential_500.nc'
if os.path.exists(filename) and os.path.getsize(filename) == expected_file_size:
print(f"{filename} already downloaded")
files.append(filename)
continue
print(f"Downloading {filename}")
with open(filename, "wb") as handle:
for data in response.iter_content(chunk_size=1024*1024):
handle.write(data)
files.append(filename)
return files
if __name__ == "__main__":
initialization_time = "2024-12-03T00:00:00Z"
forecast = download_forecast(initialization_time)
print(f"Downloaded {len(forecast)} files")
data = xr.open_dataset(forecast[0])
print("First forecast:", data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment