Last active
October 12, 2023 17:43
-
-
Save boozeman/a20e1a1354e260fbaad429c49daf96cf to your computer and use it in GitHub Desktop.
Weather Data using FMI open data iteration3. The rest of the sensors
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
| # Copy this on root of config directory or any place you want | |
| # Correct the file sensor paths for configuration.yaml if needed | |
| # Usage: python3 wind_data.py station speed|direction|gust | |
| # Example: wind_data.py Tampere speed | |
| # You can find the stations here: https://www.tuulikartta.info/#lang=fi#latlon=61.04,24.29,9#parameter=ws_10min for example | |
| import requests | |
| import xml.etree.ElementTree as ET | |
| import json | |
| import sys | |
| def fetch_data(station, parameter): | |
| parameter_mapping = { | |
| "speed": "ws", | |
| "gust": "wg", | |
| "direction": "wd" | |
| } | |
| if parameter not in parameter_mapping: | |
| print("Invalid parameter. Please provide speed, gust, or direction.") | |
| 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}¶meters={parameter_name}_10min&" | |
| 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 (speed, gust, or direction).") | |
| 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