TD9991 commited on
Commit
603824f
1 Parent(s): 27ccaa7

fix temp and humidity

Browse files
Files changed (2) hide show
  1. APIs/meteo.py +46 -31
  2. utils.py +2 -2
APIs/meteo.py CHANGED
@@ -5,9 +5,9 @@ from retry_requests import retry
5
 
6
 
7
  # Setup the Open-Meteo API client with cache and retry on error
8
- cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
9
- retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
10
- openmeteo = openmeteo_requests.Client(session = retry_session)
11
 
12
 
13
  def get_info_meteo(latitude, longitude):
@@ -15,22 +15,22 @@ def get_info_meteo(latitude, longitude):
15
  ARGS:
16
  latitude (Float) : latitude coordinate
17
  longitude (Float): longitude coordinate
18
-
19
  """
20
  url = "https://api.open-meteo.com/v1/forecast"
21
  params = {
22
- "latitude": latitude,
23
- "longitude": longitude, #"cloud_cover_low", "cloud_cover_mid","cloud_cover_high"
24
  "hourly": {"relative_humidity_2m", "soil_temperature_0cm", "soil_moisture_0_to_1cm", "cloud_cover"},
25
- "daily": {"temperature_2m_max","precipitation_sum", "wind_speed_10m_max", "sunshine_duration", "rain_sum"},
26
- "past_days" : 5,
27
- "forecast_days" : 7
28
-
29
- }
30
  responses = openmeteo.weather_api(url, params=params)
31
  response = responses[0]
32
 
33
- #Hourly dataframe
34
 
35
  hourly = response.Hourly()
36
  hourly_relative_humidity_2m = hourly.Variables(0).ValuesAsNumpy()
@@ -39,10 +39,10 @@ def get_info_meteo(latitude, longitude):
39
  hourly_cloud_cover = hourly.Variables(3).ValuesAsNumpy()
40
 
41
  hourly_data = {"date": pd.date_range(
42
- start = pd.to_datetime(hourly.Time(), unit = "s", utc = True),
43
- end = pd.to_datetime(hourly.TimeEnd(), unit = "s", utc = True),
44
- freq = pd.Timedelta(seconds = hourly.Interval()),
45
- inclusive = "left"
46
  )}
47
 
48
  hourly_data["relative_humidity_2m_%"] = hourly_relative_humidity_2m
@@ -50,26 +50,34 @@ def get_info_meteo(latitude, longitude):
50
  hourly_data["soil_moisture_0_to_1cm_m³"] = hourly_soil_moisture_0_to_1cm
51
  hourly_data["cloud_cover_%"] = hourly_cloud_cover
52
 
53
- hourly_dataframe = pd.DataFrame(data = hourly_data)
54
-
55
 
56
- hourly_dataframe = pd.DataFrame(data = hourly_data)
57
 
58
- #Average hourly data per day
59
- hourly_dataframe['day_date'] = hourly_dataframe['date'].dt.strftime('%Y-%m-%d')
 
60
 
61
- tmp1 = hourly_dataframe.groupby('day_date')[['relative_humidity_2m_%', 'soil_temperature_0cm_C', 'soil_moisture_0_to_1cm_m³']].mean()
 
62
 
63
- avg_hourly_dataframe = tmp1.rename(columns={'relative_humidity_2m_%': 'avg_hourly_relative_humidity_2m_%', 'soil_temperature_0cm_C': 'avg_hourly_soil_temperature_0cm_C', 'soil_moisture_0_to_1cm_m³': 'avg_hourly_soil_moisture_0_to_1cm_m³'})
 
 
 
 
 
64
 
65
- #med cloud couverture
66
  tmp2 = hourly_dataframe.groupby('day_date')[['cloud_cover_%']].median()
67
 
68
- med_cloud_cover = tmp2.rename(columns={'cloud_cover_%': 'med_cloud_cover_%'})
 
69
 
70
- #Daily dataframe
71
  daily = response.Daily()
72
  daily_temperature_2m_max = daily.Variables(0).ValuesAsNumpy()
 
73
  daily_precipitation_sum = daily.Variables(1).ValuesAsNumpy()
74
  daily_wind_speed_10m_max = daily.Variables(2).ValuesAsNumpy()
75
  daily_sunshine_duration = daily.Variables(3).ValuesAsNumpy()
@@ -78,7 +86,7 @@ def get_info_meteo(latitude, longitude):
78
  daily_data = {"date": pd.date_range(
79
  start=pd.to_datetime(daily.Time(), unit="s", utc=True),
80
  end=pd.to_datetime(daily.TimeEnd(), unit="s", utc=True),
81
- freq='D',
82
  inclusive="left"
83
  )}
84
 
@@ -90,13 +98,20 @@ def get_info_meteo(latitude, longitude):
90
 
91
  daily_dataframe = pd.DataFrame(data=daily_data)
92
 
93
- daily_dataframe['day_date'] = daily_dataframe['date'].dt.strftime('%Y-%m-%d')
 
94
 
95
- total_df = pd.merge(daily_dataframe, avg_hourly_dataframe, on="day_date", how="left")
 
96
  total_df = pd.merge(total_df, med_cloud_cover, on="day_date", how="left")
97
 
98
  total_df['date'] = total_df['day_date']
99
  total_df = total_df.drop('day_date', axis=1)
100
-
 
101
  return total_df
102
- #total_df.to_json("weather_data.json", orient='columns')
 
 
 
 
 
5
 
6
 
7
  # Setup the Open-Meteo API client with cache and retry on error
8
+ cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
9
+ retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
10
+ openmeteo = openmeteo_requests.Client(session=retry_session)
11
 
12
 
13
  def get_info_meteo(latitude, longitude):
 
15
  ARGS:
16
  latitude (Float) : latitude coordinate
17
  longitude (Float): longitude coordinate
18
+
19
  """
20
  url = "https://api.open-meteo.com/v1/forecast"
21
  params = {
22
+ "latitude": latitude,
23
+ "longitude": longitude, # "cloud_cover_low", "cloud_cover_mid","cloud_cover_high"
24
  "hourly": {"relative_humidity_2m", "soil_temperature_0cm", "soil_moisture_0_to_1cm", "cloud_cover"},
25
+ "daily": {"temperature_2m_max", "precipitation_sum", "wind_speed_10m_max", "sunshine_duration", "rain_sum"},
26
+ "past_days": 5,
27
+ "forecast_days": 7
28
+
29
+ }
30
  responses = openmeteo.weather_api(url, params=params)
31
  response = responses[0]
32
 
33
+ # Hourly dataframe
34
 
35
  hourly = response.Hourly()
36
  hourly_relative_humidity_2m = hourly.Variables(0).ValuesAsNumpy()
 
39
  hourly_cloud_cover = hourly.Variables(3).ValuesAsNumpy()
40
 
41
  hourly_data = {"date": pd.date_range(
42
+ start=pd.to_datetime(hourly.Time(), unit="s", utc=True),
43
+ end=pd.to_datetime(hourly.TimeEnd(), unit="s", utc=True),
44
+ freq=pd.Timedelta(seconds=hourly.Interval()),
45
+ inclusive="left"
46
  )}
47
 
48
  hourly_data["relative_humidity_2m_%"] = hourly_relative_humidity_2m
 
50
  hourly_data["soil_moisture_0_to_1cm_m³"] = hourly_soil_moisture_0_to_1cm
51
  hourly_data["cloud_cover_%"] = hourly_cloud_cover
52
 
53
+ hourly_dataframe = pd.DataFrame(data=hourly_data)
 
54
 
55
+ hourly_dataframe = pd.DataFrame(data=hourly_data)
56
 
57
+ # Average hourly data per day
58
+ hourly_dataframe['day_date'] = hourly_dataframe['date'].dt.strftime(
59
+ '%Y-%m-%d')
60
 
61
+ tmp1 = hourly_dataframe.groupby('day_date')[
62
+ ['relative_humidity_2m_%', 'soil_temperature_0cm_C', 'soil_moisture_0_to_1cm_m³']].mean()
63
 
64
+ avg_hourly_dataframe = tmp1.rename(columns={
65
+ 'relative_humidity_2m_%': 'avg_hourly_relative_humidity_2m_%',
66
+ 'soil_temperature_0cm_C': 'avg_hourly_soil_temperature_0cm_C',
67
+ 'soil_moisture_0_to_1cm_m³': 'avg_hourly_soil_moisture_0_to_1cm_m³'
68
+ }
69
+ )
70
 
71
+ # med cloud couverture
72
  tmp2 = hourly_dataframe.groupby('day_date')[['cloud_cover_%']].median()
73
 
74
+ med_cloud_cover = tmp2.rename(
75
+ columns={'cloud_cover_%': 'med_cloud_cover_%'})
76
 
77
+ # Daily dataframe
78
  daily = response.Daily()
79
  daily_temperature_2m_max = daily.Variables(0).ValuesAsNumpy()
80
+ print(daily_temperature_2m_max)
81
  daily_precipitation_sum = daily.Variables(1).ValuesAsNumpy()
82
  daily_wind_speed_10m_max = daily.Variables(2).ValuesAsNumpy()
83
  daily_sunshine_duration = daily.Variables(3).ValuesAsNumpy()
 
86
  daily_data = {"date": pd.date_range(
87
  start=pd.to_datetime(daily.Time(), unit="s", utc=True),
88
  end=pd.to_datetime(daily.TimeEnd(), unit="s", utc=True),
89
+ freq='D',
90
  inclusive="left"
91
  )}
92
 
 
98
 
99
  daily_dataframe = pd.DataFrame(data=daily_data)
100
 
101
+ daily_dataframe['day_date'] = daily_dataframe['date'].dt.strftime(
102
+ '%Y-%m-%d')
103
 
104
+ total_df = pd.merge(daily_dataframe, avg_hourly_dataframe,
105
+ on="day_date", how="left")
106
  total_df = pd.merge(total_df, med_cloud_cover, on="day_date", how="left")
107
 
108
  total_df['date'] = total_df['day_date']
109
  total_df = total_df.drop('day_date', axis=1)
110
+
111
+ total_df.to_json("weather_data.json", orient='columns')
112
  return total_df
113
+
114
+
115
+ if __name__ == "__main__":
116
+ df = get_info_meteo(48.832, 2.286)
117
+ print(df)
utils.py CHANGED
@@ -32,8 +32,8 @@ query_engine = index.as_query_engine(similarity_top_k=15)
32
  def on_init(lat, lon, address):
33
  map_html, fig = show_map(lat, lon, address)
34
  df = get_info_meteo(lat, lon)
35
- avg_temp = df.loc[:, 'daily_temperature_2m_max_C'].mean()
36
- avg_humidity = df.loc[:, 'avg_hourly_soil_moisture_0_to_1cm_m³'].mean()
37
  # avg_snowfall = df.loc[:, 'daily_precipitation_sum_mm'].mean()
38
  avg_cloud_cover = df.loc[:, 'med_cloud_cover_%'].mean()
39
  avg_precipitations = df.loc[:, 'daily_rain_sum_mm'].mean()
 
32
  def on_init(lat, lon, address):
33
  map_html, fig = show_map(lat, lon, address)
34
  df = get_info_meteo(lat, lon)
35
+ avg_temp = df.loc[:, 'avg_hourly_soil_temperature_0cm_C'].mean()
36
+ avg_humidity = df.loc[:, 'avg_hourly_relative_humidity_2m_%'].mean()
37
  # avg_snowfall = df.loc[:, 'daily_precipitation_sum_mm'].mean()
38
  avg_cloud_cover = df.loc[:, 'med_cloud_cover_%'].mean()
39
  avg_precipitations = df.loc[:, 'daily_rain_sum_mm'].mean()