Skip to content

Instantly share code, notes, and snippets.

@boozeman
Last active October 12, 2023 17:43
Show Gist options
  • Save boozeman/a20e1a1354e260fbaad429c49daf96cf to your computer and use it in GitHub Desktop.
Save boozeman/a20e1a1354e260fbaad429c49daf96cf to your computer and use it in GitHub Desktop.
Weather Data using FMI open data iteration3. The rest of the sensors
# Copy this on root of config directory or any place you want
# Correct the file sensor paths for configuration.yaml if needed
# Usage: python3 FMIWeather.py station parameter
# Examples:
# FMIWeather.py Tampere windspeed
# FMIWeather.py Hämeenlinna rain1h
# NOTE! If you live place without FMI weather station, like Turenki, Renko or Lammi, It takes the readings for the closest station.
# You can find the stations here: https://www.tuulikartta.info/#lang=fi#latlon=61.04,24.29,9#parameter=ws_10min for example
# All data with 10 min resolution
# ws_10min = Wind Speed 10min avg (in m/s)
# wg_10min = Wind Gusts 1omin avg (in m/s)
# wd_10min = Wind Direction 10min avg (in degrees)
# t2m = Temperature (in Celcius)
# rh = Humidity (in hPa)
# td = Devpoint (in Celcius)
# r_1h = Rain 1 hour (in millimeters)
# ri_10min = Rain intensity (in millimeters)
# snow_aws = Snow Depth (in centimeters)
# p_sea = Atmospheric Pressure (Hpa)
# vis = Visibility (in meters)
# n_man = Cloud Coverage (1 to 8 meaning 1/8 to 8/8)
# wawa = weather code -> Weathersymbol3 https://www.ilmatieteenlaitos.fi/latauspalvelun-pikaohje
import requests
import xml.etree.ElementTree as ET
import json
import sys
def fetch_data(station, parameter):
parameter_mapping = {
"windspeed": "ws_10min",
"windgust": "wg_10min",
"winddirection": "wd_10min",
"temperature": "t2m",
"humidity": "rh",
"devpoint": "td",
"rain1h": "r_1h",
"rainintensity": "ri_10min",
"snowdepth": "snow_aws",
"pressure": "p_sea",
"visibility": "vis",
"cloudcoverage": "n_man",
"weathercode": "wawa"
}
if parameter not in parameter_mapping:
print("Invalid parameter. Please provide windspeed, windgust, winddirection, temperature, humidity, devpoint, rain1h, rainintensity, snowdepth, pressure, visibilty, cloudcoverage or weathercode.")
sys.exit(1)
parameter_name = parameter_mapping[parameter]
url = f"https://opendata.fmi.fi/wfs/fin?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::observations::weather::timevaluepair&place={station}&parameters={parameter_name}&"
response = requests.get(url)
root = ET.fromstring(response.content)
json_data = []
for point in root.findall(".//{http://www.opengis.net/waterml/2.0}point"):
time = point.find(".//{http://www.opengis.net/waterml/2.0}time").text
value = point.find(".//{http://www.opengis.net/waterml/2.0}value").text
json_data.append({"time": time, "value": value})
return json_data
def main():
if len(sys.argv) != 3:
print("Please provide two arguments: station and parameter (windspeed, windgust, winddirection, temperature, humidity, devpoint, rain1h, rainintensity, snowdepth, pressure, visibilty, cloudcoverage or weathercode).")
sys.exit(1)
station = sys.argv[1]
parameter = sys.argv[2]
data = fetch_data(station, parameter)
json_data = json.dumps({"result": data})
print(json_data)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment