Spaces:
Runtime error
Runtime error
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files- compute_et0_adjusted.py +70 -0
- data_pipelines/historical_weather_data.py +73 -38
- docs/agro_indicators.py +1 -1
- requirements.txt +2 -1
compute_et0_adjusted.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import docs.agro_indicators as agro_indicators
|
2 |
+
import datetime
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
|
7 |
+
def compute_et0(
|
8 |
+
df: pd.DataFrame,
|
9 |
+
latitude: float,
|
10 |
+
longitude: float
|
11 |
+
):
|
12 |
+
"""
|
13 |
+
Compute reference evapotranspiration.
|
14 |
+
|
15 |
+
Parameters
|
16 |
+
----------
|
17 |
+
df : DataFrame
|
18 |
+
The input dataframe containing sensor data.
|
19 |
+
|
20 |
+
latitude : float
|
21 |
+
Latitude of the location.
|
22 |
+
longitude : float
|
23 |
+
Longitude of the location
|
24 |
+
|
25 |
+
Returns
|
26 |
+
-------
|
27 |
+
arraylike
|
28 |
+
Daily reference evapotranspiration.
|
29 |
+
"""
|
30 |
+
|
31 |
+
irradiance = df.irradiance
|
32 |
+
Tmin = df.air_temperature_min
|
33 |
+
Tmax = df.air_temperature_max
|
34 |
+
T = (Tmin + Tmin) / 2
|
35 |
+
RHmin = df.relative_humidity_min
|
36 |
+
RHmax = df.relative_humidity_max
|
37 |
+
WS = df.wind_speed
|
38 |
+
JJulien = df.day_of_year
|
39 |
+
|
40 |
+
l = agro_indicators.et0(
|
41 |
+
irradiance,
|
42 |
+
T,
|
43 |
+
Tmax,
|
44 |
+
Tmin,
|
45 |
+
RHmin,
|
46 |
+
RHmax,
|
47 |
+
WS,
|
48 |
+
JJulien,
|
49 |
+
latitude,
|
50 |
+
longitude,
|
51 |
+
)
|
52 |
+
|
53 |
+
return l
|
54 |
+
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
data_test = pd.DataFrame()
|
58 |
+
data_test["irradiance"] = [20, 30, 40]
|
59 |
+
data_test["air_temperature_min"] = [10, 15, 20]
|
60 |
+
data_test["air_temperature_max"] = [20, 25, 30]
|
61 |
+
data_test["relative_humidity_min"] = [50, 60, 70]
|
62 |
+
data_test["relative_humidity_max"] = [50, 60, 70]
|
63 |
+
data_test["wind_speed"] = [5, 10, 15]
|
64 |
+
data_test["day_of_year"] = [1, 32, 60]
|
65 |
+
|
66 |
+
latitude = 40.7128
|
67 |
+
longitude = 74.0060
|
68 |
+
|
69 |
+
et0 = compute_et0(data_test, latitude, longitude)
|
70 |
+
print(et0)
|
data_pipelines/historical_weather_data.py
CHANGED
@@ -4,17 +4,19 @@ import requests_cache
|
|
4 |
import pandas as pd
|
5 |
from retry_requests import retry
|
6 |
|
|
|
|
|
7 |
|
8 |
def download_historical_weather_data(
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
) -> pd.DataFrame:
|
14 |
# Setup the Open-Meteo API client with cache and retry on error
|
15 |
-
cache_session = requests_cache.CachedSession('.cache', expire_after
|
16 |
-
retry_session = retry(cache_session, retries
|
17 |
-
openmeteo = openmeteo_requests.Client(session
|
18 |
|
19 |
# Make sure all required weather variables are listed here
|
20 |
# The order of variables in hourly or daily is important to assign them correctly below
|
@@ -22,12 +24,11 @@ def download_historical_weather_data(
|
|
22 |
params = {
|
23 |
"latitude": latitude,
|
24 |
"longitude": longitude,
|
25 |
-
"start_date": f"{start_year}-
|
26 |
-
"end_date": f"{end_year}-
|
27 |
-
"
|
28 |
-
|
29 |
-
|
30 |
-
"et0_fao_evapotranspiration"],
|
31 |
"timezone": "GMT"
|
32 |
}
|
33 |
responses = openmeteo.weather_api(url, params=params)
|
@@ -39,32 +40,66 @@ def download_historical_weather_data(
|
|
39 |
print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")
|
40 |
print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")
|
41 |
|
42 |
-
# Process
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
"
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
inclusive="left"
|
58 |
),
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
4 |
import pandas as pd
|
5 |
from retry_requests import retry
|
6 |
|
7 |
+
from compute_et0_adjusted import compute_et0
|
8 |
+
|
9 |
|
10 |
def download_historical_weather_data(
|
11 |
+
latitude: float,
|
12 |
+
longitude: float,
|
13 |
+
start_year: int,
|
14 |
+
end_year: int,
|
15 |
) -> pd.DataFrame:
|
16 |
# Setup the Open-Meteo API client with cache and retry on error
|
17 |
+
cache_session = requests_cache.CachedSession('.cache', expire_after=-1)
|
18 |
+
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
|
19 |
+
openmeteo = openmeteo_requests.Client(session=retry_session)
|
20 |
|
21 |
# Make sure all required weather variables are listed here
|
22 |
# The order of variables in hourly or daily is important to assign them correctly below
|
|
|
24 |
params = {
|
25 |
"latitude": latitude,
|
26 |
"longitude": longitude,
|
27 |
+
"start_date": f"{start_year}-02-08",
|
28 |
+
"end_date": f"{end_year}-02-22",
|
29 |
+
"hourly": ["temperature_2m", "relative_humidity_2m",
|
30 |
+
"precipitation", "et0_fao_evapotranspiration",
|
31 |
+
"wind_speed_10m", "shortwave_radiation"],
|
|
|
32 |
"timezone": "GMT"
|
33 |
}
|
34 |
responses = openmeteo.weather_api(url, params=params)
|
|
|
40 |
print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")
|
41 |
print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")
|
42 |
|
43 |
+
# Process hourly data. The order of variables needs to be the same as requested.
|
44 |
+
hourly = response.Hourly()
|
45 |
+
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
|
46 |
+
hourly_relative_humidity_2m = hourly.Variables(1).ValuesAsNumpy()
|
47 |
+
hourly_precipitation = hourly.Variables(2).ValuesAsNumpy()
|
48 |
+
hourly_et0_fao_evapotranspiration = hourly.Variables(3).ValuesAsNumpy()
|
49 |
+
hourly_wind_speed_10m = hourly.Variables(4).ValuesAsNumpy()
|
50 |
+
hourly_shortwave_radiation = hourly.Variables(5).ValuesAsNumpy()
|
51 |
+
|
52 |
+
hourly_data = {
|
53 |
+
"temperature_2m": hourly_temperature_2m,
|
54 |
+
"relative_humidity_2m": hourly_relative_humidity_2m,
|
55 |
+
"precipitation": hourly_precipitation,
|
56 |
+
"et0_fao_evapotranspiration": hourly_et0_fao_evapotranspiration,
|
57 |
+
"wind_speed_10m": hourly_wind_speed_10m,
|
58 |
+
"shortwave_radiation": hourly_shortwave_radiation,
|
59 |
+
}
|
60 |
+
hourly_dataframe = pd.DataFrame(
|
61 |
+
index=pd.date_range(
|
62 |
+
start=pd.to_datetime(hourly.Time(), unit="s", utc=True),
|
63 |
+
end=pd.to_datetime(hourly.TimeEnd(), unit="s", utc=True),
|
64 |
+
freq=pd.Timedelta(seconds=hourly.Interval()),
|
65 |
inclusive="left"
|
66 |
),
|
67 |
+
data=hourly_data,
|
68 |
+
)
|
69 |
+
|
70 |
+
return hourly_dataframe
|
71 |
+
|
72 |
+
|
73 |
+
def aggregate_hourly_weather_data(
|
74 |
+
hourly_data: pd.DataFrame,
|
75 |
+
) -> pd.DataFrame:
|
76 |
+
resampled_data = hourly_data.resample("1ME").agg({
|
77 |
+
"temperature_2m": ["min", "max"],
|
78 |
+
"relative_humidity_2m": ["min", "max"],
|
79 |
+
"wind_speed_10m": "mean",
|
80 |
+
"shortwave_radiation": "mean",
|
81 |
+
})
|
82 |
+
|
83 |
+
monthly_data = pd.DataFrame.from_dict({
|
84 |
+
"day_of_year": resampled_data.index.dayofyear,
|
85 |
+
"air_temperature_min": resampled_data[("temperature_2m", "min")],
|
86 |
+
"air_temperature_max": resampled_data[("temperature_2m", "max")],
|
87 |
+
"relative_humidity_min": resampled_data[("relative_humidity_2m", "min")],
|
88 |
+
"relative_humidity_max": resampled_data[("relative_humidity_2m", "max")],
|
89 |
+
"wind_speed": resampled_data[("wind_speed_10m", "mean")],
|
90 |
+
"irradiance": resampled_data[("shortwave_radiation", "mean")],
|
91 |
+
})
|
92 |
+
|
93 |
+
return monthly_data
|
94 |
+
|
95 |
|
96 |
+
if __name__ == '__main__':
|
97 |
+
latitude = 47
|
98 |
+
longitude = 3
|
99 |
+
start_year = 2020
|
100 |
+
end_year = 2021
|
101 |
+
df = download_historical_weather_data(latitude, longitude, start_year, end_year)
|
102 |
+
monthly_df = aggregate_hourly_weather_data(df)
|
103 |
|
104 |
+
et0 = compute_et0(monthly_df, latitude, longitude)
|
105 |
+
print(et0)
|
docs/agro_indicators.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import numpy as np
|
2 |
from pvlib.location import lookup_altitude
|
3 |
-
from
|
4 |
|
5 |
def et0(irradiance, T, Tmax, Tmin, RHmin, RHmax, WS, JJulien, latitude, longitude):
|
6 |
"""
|
|
|
1 |
import numpy as np
|
2 |
from pvlib.location import lookup_altitude
|
3 |
+
from docs.pyeto import fao
|
4 |
|
5 |
def et0(irradiance, T, Tmax, Tmin, RHmin, RHmax, WS, JJulien, latitude, longitude):
|
6 |
"""
|
requirements.txt
CHANGED
@@ -16,4 +16,5 @@ openmeteo_requests
|
|
16 |
requests_cache
|
17 |
retry_requests
|
18 |
fuzzywuzzy
|
19 |
-
plotly
|
|
|
|
16 |
requests_cache
|
17 |
retry_requests
|
18 |
fuzzywuzzy
|
19 |
+
plotly
|
20 |
+
pvlib
|