File size: 4,199 Bytes
b23cefb
 
4b837f2
b23cefb
4b837f2
b23cefb
b5f210c
5f29f14
b23cefb
 
5f29f14
 
 
 
b23cefb
 
5f29f14
 
 
b23cefb
 
 
 
 
 
 
5f29f14
 
 
 
 
b23cefb
 
 
 
 
 
 
 
 
 
 
5f29f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b23cefb
 
5f29f14
 
 
 
 
 
 
 
 
 
b126e08
5f29f14
 
4b837f2
5f29f14
4b837f2
5f29f14
 
 
b5f210c
5f29f14
 
b126e08
5f29f14
 
4b837f2
5f29f14
 
4b837f2
5f29f14
 
 
 
b23cefb
5f29f14
 
 
4b837f2
 
5f29f14
 
b23cefb
b5f210c
4b837f2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import openmeteo_requests

import matplotlib.pyplot as plt
import pandas as pd
import requests_cache
from retry_requests import retry
from compute_et0_adjusted import compute_et0


def download_historical_weather_data(
        latitude: float,
        longitude: float,
        start_year: int,
        end_year: int,
) -> pd.DataFrame:
	# Setup the Open-Meteo API client with cache and retry on error
	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)

	# Make sure all required weather variables are listed here
	# The order of variables in hourly or daily is important to assign them correctly below
	url = "https://archive-api.open-meteo.com/v1/archive"
	params = {
		"latitude": latitude,
		"longitude": longitude,
		"start_date": f"{start_year}-02-08",
		"end_date": f"{end_year}-02-22",
		"hourly": ["temperature_2m", "relative_humidity_2m",
				   "precipitation", "et0_fao_evapotranspiration",
				   "wind_speed_10m", "shortwave_radiation"],
		"timezone": "GMT"
	}
	responses = openmeteo.weather_api(url, params=params)

	# Process first location. Add a for-loop for multiple locations or weather models
	response = 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 hourly data. The order of variables needs to be the same as requested.
	hourly = response.Hourly()
	hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
	hourly_relative_humidity_2m = hourly.Variables(1).ValuesAsNumpy()
	hourly_precipitation = hourly.Variables(2).ValuesAsNumpy()
	hourly_et0_fao_evapotranspiration = hourly.Variables(3).ValuesAsNumpy()
	hourly_wind_speed_10m = hourly.Variables(4).ValuesAsNumpy()
	hourly_shortwave_radiation = hourly.Variables(5).ValuesAsNumpy()

	hourly_data = {
		"temperature_2m": hourly_temperature_2m,
		"relative_humidity_2m": hourly_relative_humidity_2m,
		"precipitation": hourly_precipitation,
		"et0_fao_evapotranspiration": hourly_et0_fao_evapotranspiration,
		"wind_speed_10m": hourly_wind_speed_10m,
		"shortwave_radiation": hourly_shortwave_radiation,
	}
	hourly_dataframe = pd.DataFrame(
		index=pd.date_range(
			start=pd.to_datetime(hourly.Time(), unit="s", utc=True),
			end=pd.to_datetime(hourly.TimeEnd(), unit="s", utc=True),
			freq=pd.Timedelta(seconds=hourly.Interval()),
			inclusive="left"
		),
		data=hourly_data,
	)

	return hourly_dataframe


def aggregate_hourly_weather_data(
		hourly_data: pd.DataFrame,
) -> pd.DataFrame:
	resampled_data = hourly_data.resample("1ME").agg({
		"temperature_2m": ["min", "max", "mean"],
		"relative_humidity_2m": ["min", "max"],
		"wind_speed_10m": "mean",
		"precipitation": "mean",
		"shortwave_radiation": "mean",
		"et0_fao_evapotranspiration": "mean",
	})

	monthly_data = pd.DataFrame.from_dict({
		"day_of_year": resampled_data.index.dayofyear,
		"air_temperature_min": resampled_data[("temperature_2m", "min")],
		"air_temperature_max": resampled_data[("temperature_2m", "max")],
		"air_temperature_mean": resampled_data[("temperature_2m", "mean")],
		"relative_humidity_min": resampled_data[("relative_humidity_2m", "min")],
		"relative_humidity_max": resampled_data[("relative_humidity_2m", "max")],
		"precipitation": resampled_data[("precipitation", "mean")],
		"wind_speed": resampled_data[("wind_speed_10m", "mean")],
		"irradiance": resampled_data[("shortwave_radiation", "mean")],
		"et0_fao_evapotranspiration": resampled_data[("et0_fao_evapotranspiration", "mean")],
	})

	return monthly_data


if __name__ == '__main__':
	latitude = 47
	longitude = 3
	start_year = 2000
	end_year = 2024
	df = download_historical_weather_data(latitude, longitude, start_year, end_year)
	monthly_df = aggregate_hourly_weather_data(df)

	et0 = compute_et0(monthly_df, latitude, longitude)
	monthly_df["et0"] = et0

	plt.plot(monthly_df["et0_fao_evapotranspiration"] * 100)
	plt.plot(monthly_df["et0"] + 5)
	plt.show()