Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1328

Python • open meteo - extract sunrise time

$
0
0
Hi,

I have just moved to Open Meteo for weather data, from Open Weather who now require credit card details.

I am using this code generated by Open Meteo documentation to load a 7 day forecast of max temp, min temp, sunrise time ,sunset etc

Code:

import openmeteo_requestsimport requests_cacheimport pandas as pdfrom retry_requests import retry# Setup the Open-Meteo API client with cache and retry on errorcache_session = requests_cache.CachedSession('.cache', expire_after = 3600)retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)openmeteo = openmeteo_requests.Client(session = retry_session)# Make sure all required weather variables are listed here# The order of variables in hourly or daily is important to assign them correctly belowurl = "https://api.open-meteo.com/v1/forecast"params = {"latitude": 52.52,"longitude": 13.41,"daily": ["weather_code", "temperature_2m_max", "temperature_2m_min", "sunrise", "sunset", "uv_index_max", "precipitation_probability_max"],"timezone": "Europe/London"}responses = openmeteo.weather_api(url, params=params)# Process first location. Add a for-loop for multiple locations or weather modelsresponse = responses[0]print(f"Coordinates {response.Latitude()}°N {response.Longitude()}°E")print(f"Elevation {response.Elevation()} m asl")print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")# Process daily data. The order of variables needs to be the same as requested.daily = response.Daily()daily_weather_code = daily.Variables(0).ValuesAsNumpy()daily_temperature_2m_max = daily.Variables(1).ValuesAsNumpy()daily_temperature_2m_min = daily.Variables(2).ValuesAsNumpy()daily_sunrise = daily.Variables(3).ValuesAsNumpy()daily_sunset = daily.Variables(4).ValuesAsNumpy()daily_uv_index_max = daily.Variables(5).ValuesAsNumpy()daily_precipitation_probability_max = daily.Variables(6).ValuesAsNumpy()
While I can extract the 7 days of data for max temp etc, I just do not understand how to extract Sunrise (and Sunset) times from 'daily_sunrise'

I am expecting the data to look like
2024-05-27T03:53
2024-05-28T03:52
2024-05-29T03:51
2024-05-30T03:50
2024-05-31T03:49
2024-06-01T03:48
2024-06-02T03:48

so I want 03:53 for first value. Any thing I try - which is very very very basic just gives me a single 0. I have not even heard about Numpy until this week.

I just need a string that contains "03:53"

Please ignore pandas - I am not using that part of code or should I?

Thanks

Statistics: Posted by cblx5 — Tue May 28, 2024 12:05 pm



Viewing all articles
Browse latest Browse all 1328

Trending Articles