TD9991 commited on
Commit
57cfeee
1 Parent(s): 9dbc337

fix meteo api

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. APIs/meteo.py +42 -23
.gitignore CHANGED
@@ -4,4 +4,5 @@
4
  .vscode/
5
  .venv/
6
  documents/
7
- APIs\__pycache__
 
 
4
  .vscode/
5
  .venv/
6
  documents/
7
+ APIs/__pycache__
8
+ weather_data.json
APIs/meteo.py CHANGED
@@ -10,26 +10,7 @@ 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):
14
- """Function that creates a Json file containing the weather data of the location
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()
@@ -52,6 +33,8 @@ def get_info_meteo(latitude, longitude):
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
@@ -74,10 +57,13 @@ def get_info_meteo(latitude, longitude):
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()
@@ -97,18 +83,51 @@ def get_info_meteo(latitude, longitude):
97
  daily_data["daily_rain_sum_mm"] = daily_rain_sum
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
 
 
10
  openmeteo = openmeteo_requests.Client(session=retry_session)
11
 
12
 
13
+ def get_hourly_weather_data(response):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # Hourly dataframe
15
 
16
  hourly = response.Hourly()
 
33
 
34
  hourly_dataframe = pd.DataFrame(data=hourly_data)
35
 
36
+ # print('hourly df', hourly_dataframe)
37
+
38
  hourly_dataframe = pd.DataFrame(data=hourly_data)
39
 
40
  # Average hourly data per day
 
57
  med_cloud_cover = tmp2.rename(
58
  columns={'cloud_cover_%': 'med_cloud_cover_%'})
59
 
60
+ return avg_hourly_dataframe, med_cloud_cover
61
+
62
+
63
+ def get_daily_weather_data(response):
64
  # Daily dataframe
65
  daily = response.Daily()
66
  daily_temperature_2m_max = daily.Variables(0).ValuesAsNumpy()
 
67
  daily_precipitation_sum = daily.Variables(1).ValuesAsNumpy()
68
  daily_wind_speed_10m_max = daily.Variables(2).ValuesAsNumpy()
69
  daily_sunshine_duration = daily.Variables(3).ValuesAsNumpy()
 
83
  daily_data["daily_rain_sum_mm"] = daily_rain_sum
84
 
85
  daily_dataframe = pd.DataFrame(data=daily_data)
86
+ # print('meteo_daily', daily_dataframe)
87
 
88
  daily_dataframe['day_date'] = daily_dataframe['date'].dt.strftime(
89
  '%Y-%m-%d')
90
 
91
+ return daily_dataframe
92
+
93
+
94
+ def get_info_meteo(latitude, longitude):
95
+ """Function that creates a Json file containing the weather data of the location
96
+ ARGS:
97
+ latitude (Float) : latitude coordinate
98
+ longitude (Float): longitude coordinate
99
+
100
+ """
101
+ url = "https://api.open-meteo.com/v1/forecast"
102
+ params = {
103
+ "latitude": latitude,
104
+ "longitude": longitude, # "cloud_cover_low", "cloud_cover_mid","cloud_cover_high"
105
+ "hourly": ["relative_humidity_2m", "soil_temperature_0cm", "soil_moisture_0_to_1cm", "cloud_cover"],
106
+ "daily": ["temperature_2m_max", "precipitation_sum", "wind_speed_10m_max", "sunshine_duration", "rain_sum"],
107
+ "past_days": 5,
108
+ "forecast_days": 7
109
+
110
+ }
111
+ responses = openmeteo.weather_api(url, params=params)
112
+ response = responses[0]
113
+
114
+ print(f"Coordinates {response.Latitude()}°N {response.Longitude()}°E")
115
+ print(f"Elevation {response.Elevation()} m asl")
116
+ print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")
117
+ print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")
118
+
119
+ avg_hourly_dataframe, med_cloud_cover = get_hourly_weather_data(response)
120
+ daily_dataframe = get_daily_weather_data(response)
121
+
122
  total_df = pd.merge(daily_dataframe, avg_hourly_dataframe,
123
  on="day_date", how="left")
124
+ total_df = pd.merge(total_df, med_cloud_cover,
125
+ on="day_date", how="left")
126
 
127
  total_df['date'] = total_df['day_date']
128
  total_df = total_df.drop('day_date', axis=1)
129
 
130
+ # total_df.to_json("weather_data.json", orient='columns')
131
  return total_df
132
 
133