River_Network / scripts /download_elevation.py
ageraustine's picture
Upload folder using huggingface_hub
a74054f verified
Raw
History Blame Contribute Delete
2.3 kB
"""Download elevation data for all 27 stations using Open Topo Data API."""
import requests
import pandas as pd
import time
from pathlib import Path
# Load stations
stations = pd.read_csv('datasets/station_list.csv')
print(f"Downloading elevation for {len(stations)} stations...")
elevations = []
for idx, row in stations.iterrows():
station_code = row['station_code']
lat = row['lat']
lon = row['lon']
# Query Open Topo Data API
url = f"https://api.opentopodata.org/v1/eudem25m?locations={lat},{lon}"
try:
response = requests.get(url, timeout=10)
if response.status_code == 200:
data = response.json()
elevation = data['results'][0]['elevation']
elevations.append({
'station_code': station_code,
'station_name': row['station_name'],
'latitude': lat,
'longitude': lon,
'elevation_m': elevation,
'data_source': 'EU-DEM 25m (Open Topo Data)'
})
print(f"βœ“ {station_code}: {elevation:.1f} m")
else:
print(f"βœ— {station_code}: API error {response.status_code}")
elevations.append({
'station_code': station_code,
'station_name': row['station_name'],
'latitude': lat,
'longitude': lon,
'elevation_m': None,
'data_source': 'Failed'
})
except Exception as e:
print(f"βœ— {station_code}: {e}")
elevations.append({
'station_code': station_code,
'station_name': row['station_name'],
'latitude': lat,
'longitude': lon,
'elevation_m': None,
'data_source': 'Failed'
})
# Rate limiting
time.sleep(3)
# Save
df_elevation = pd.DataFrame(elevations)
output_file = Path('datasets/station_elevations.csv')
df_elevation.to_csv(output_file, index=False)
print(f"\nβœ“ Saved to: {output_file}")
print(f"βœ“ Success: {df_elevation['elevation_m'].notna().sum()}/{len(df_elevation)} stations")
print(f"\nElevation range: {df_elevation['elevation_m'].min():.1f} - {df_elevation['elevation_m'].max():.1f} m")