| import openmeteo_requests |
| import pandas as pd |
| from retry_requests import retry |
| import pvlib |
| import requests_cache |
|
|
|
|
| def get_tmy(latitude: float, longitude: float) -> pd.DataFrame: |
| tmy = pvlib.iotools.get_pvgis_tmy( |
| latitude, |
| longitude, |
| outputformat="json", |
| usehorizon=True, |
| userhorizon=None, |
| startyear=2005, |
| endyear=2020, |
| map_variables=True, |
| url="https://re.jrc.ec.europa.eu/api/v5_2/", |
| timeout=60, |
| )[0] |
|
|
| daily_tmy = tmy.groupby(tmy.index.date).agg( |
| {"temp_air": ["min", "max", "mean"], "wind_speed": "mean", "ghi": "sum"} |
| ) |
| daily_tmy.columns = daily_tmy.columns.droplevel(0) |
| daily_tmy.columns = ["temp_min", "temp_max", "temp_mean", "wind", "irradiance"] |
|
|
| |
| |
| |
| cache_session = requests_cache.CachedSession(".cache", expire_after=-1) |
| retry_session = retry(cache_session, retries=5, backoff_factor=0.2) |
| openmeteo = openmeteo_requests.Client(session=retry_session) |
|
|
| url = "https://archive-api.open-meteo.com/v1/archive" |
| params = { |
| "latitude": latitude, |
| "longitude": longitude, |
| "start_date": "2005-01-01", |
| "end_date": "2022-01-01", |
| "daily": "precipitation_sum", |
| } |
| responses = openmeteo.weather_api(url, params=params) |
|
|
| response = responses[0] |
| daily = response.Daily() |
| precipitation_sum = daily.Variables(0).ValuesAsNumpy() |
|
|
| daily_rain = { |
| "date": pd.date_range( |
| start=pd.to_datetime(daily.Time(), unit="s"), |
| end=pd.to_datetime(daily.TimeEnd(), unit="s"), |
| freq=pd.Timedelta(seconds=daily.Interval()), |
| inclusive="left", |
| ) |
| } |
| daily_rain["rain"] = precipitation_sum |
|
|
| daily_rain = pd.DataFrame(data=daily_rain) |
| daily_rain.date = pd.to_datetime(daily_rain.date) |
| daily_rain = daily_rain.set_index("date") |
|
|
| |
| df = daily_tmy.join(daily_rain) |
| df.index = df.index.map(lambda t: t.replace(year=1990)) |
|
|
| return df |
|
|