Skip to content

Instantly share code, notes, and snippets.

@boozeman
Last active October 12, 2023 17:43
Show Gist options
  • Select an option

  • Save boozeman/a20e1a1354e260fbaad429c49daf96cf to your computer and use it in GitHub Desktop.

Select an option

Save boozeman/a20e1a1354e260fbaad429c49daf96cf to your computer and use it in GitHub Desktop.
Weather Data using FMI open data iteration3. The rest of the sensors
import requests
import xml.etree.ElementTree as ET
import json
import sys
url_direction = "https://opendata.fmi.fi/wfs/fin?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::observations::weather::timevaluepair&place=Hämeenlinna&parameters=wd_10min&"
url_speed = "https://opendata.fmi.fi/wfs/fin?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::observations::weather::timevaluepair&place=Hämeenlinna&parameters=ws_10min&"
url_gusts = "https://opendata.fmi.fi/wfs/fin?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::observations::weather::timevaluepair&place=Hämeenlinna&parameters=wg_10min&"
def fetch_data(url):
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) < 2:
print("Please provide an argument: direction, speed, or gusts")
sys.exit(1)
arg = sys.argv[1]
if arg == "direction":
data = fetch_data(url_direction)
elif arg == "speed":
data = fetch_data(url_speed)
elif arg == "gusts":
data = fetch_data(url_gusts)
else:
print("Invalid argument. Please provide direction, speed, or gusts.")
sys.exit(1)
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