The system makes a forecast of the weather based on historical data in a region. The system uses AI in order to train the weather forecast with the actual weather results. The actual weather results are obtained using the Open Weather API.
The system has to be called inside of MATLAB, using pyrunfile, and pyrun functions to generate the model and get the weather forecast.
Here is the Python file for the weather forecasting:
import os
import numpy as np
from data_prep import scaler, df_for_training
from open_weather_api import api_data_df
from keras import models
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
model = models.load_model(os.path.join('../models', 'ottawa_model.h5'))
api_data_scaled = scaler.transform(api_data_df)
scaled_data_array = np.array([api_data_scaled])
prediction = model.predict(scaled_data_array)
prediction_copies = np.repeat(prediction, df_for_training.shape[1], axis=-1)
y_pred_future = scaler.inverse_transform(prediction_copies)
if y_pred_future[0][2] < 0:
y_pred_future[0][2] = 0.0
def get_prediction():
forcast_weather = []
for x in range(len(y_pred_future[0])):
forcast_weather.append(y_pred_future[0][x])
return forcast_weather
The Open Weather API calls are made using this Python file:
request_url = "https://api.openweathermap.org/data/2.5/weather?"
api_key = "key"
city = "Ottawa"
url = request_url + "appid=" + api_key + "&q=" + city
response = requests.get(url).json()
temperature = round(response['main']['temp'] - 273.15, 1)
humidity = response['main']['humidity']
wind_speed = round(response['wind']['speed'] * 3.6, 1)
pressure = round(response['main']['pressure'] / 10, 1)
precipitation = 0.0
try:
precipitation = response['rain']['1h']
except:
pass
data = [{'Temp (°C)': temperature, 'Rel Hum (%)': humidity, 'Precip. Amount (mm)': precipitation,
'Wind Spd (km/h)': wind_speed, 'Stn Press (kPa)': pressure}]
api_data_df = pd.DataFrame(data).astype(float)
The following MATLAB code successfully generates the AI model (using the LSTM.py file);
%generate the model
pyrunfile("LSTM.py");
%run forcast.py
pyrunfile("forcast.py");
%import the prediction function from forcast.py
pyrun("from forcast import get_prediction");
%store the get_prediction() result
result = pyrun("get_prediction()");
disp(result);
which yields the following results:
>> untitled
Epoch 1/20
2441/2441 - 6s - loss: 0.0348 - val_loss: 0.0089 - 6s/epoch - 3ms/step
Epoch 2/20
2441/2441 - 4s - loss: 0.0162 - val_loss: 0.0085 - 4s/epoch - 2ms/step
.
.
.
Epoch 20/20
2441/2441 - 4s - loss: 0.0159 - val_loss: 0.0077 - 4s/epoch - 2ms/step
Error using open_weather_api><module>
Python Error: KeyError: 'main'
Error in <string>><module> (line 7)
The KeyError: 'main' must be from the Open Weather API file. However, when testing the same file in VSCode using a test file, there is no error. The output is:
>>>Time-Series-Forecasting/Ottawa_Canada/forcast.py
1/1 [==============================] - 0s 401ms/step
[2.3040028, 63.859226, 0.0, 9.074291, 100.264404]
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Where the array [2.3040028, 63.859226, 0.0, 9.074291, 100.264404] is the expected result.
MATLAB is using the same version of Python as VSCode. Why is the MATLAB code resulting in a KeyError? Is there a fix?