Weather Forecast

Weather Forecast

Getting the Current Weather Conditions

First, we will start off by using the Weather API to get the current weather conditions. This data will be obtained in a json format and read with the webread function.

%% Get the current weather conditions
% Assemble the URL
% http://api.wunderground.com/api/{key}/conditions/q/CA/San_Francisco.json

CallURL_current = ['http://api.wunderground.com/api/', key,...
 '/conditions/q/', State, '/' City, '.json'];

% Grab the data from the API
Current_Data = webread(CallURL_current, options);

The webread function will return the data in a structure format. The current weather conditions are stored as strings and numbers, so some formatting is required:

%% Display the current information
disp(Current_Data.current_observation.display_location.full);
disp('Current Conditions');
disp([num2str(Current_Data.current_observation.temp_f, 3) char(176) 'F, ',...
 Current_Data.current_observation.relative_humidity ' Humidity, ',...
 Current_Data.current_observation.weather]);
disp(['Wind: ' Current_Data.current_observation.wind_string, char(10)]);

The previous code will write the current weather conditions to the display window in the following style:

Houghton, MI
Current Conditions
21.5°F, 88% Humidity, Snow
Wind: From the South at 7.0 MPH Gusting to 11.0 MPH

Getting the Forecast Conditions

Next, we will acquire forecasted weather conditions using the same API. The call url for the forecast will be slightly different from previously. This API will provide the weather forecast for the current plus next three days:

%% Get the forecast
% http://api.wunderground.com/api/{key}/forecast/q/CA/San_Francisco.json
CallURL_forecast = ['http://api.wunderground.com/api/', key,...
'/forecast/q/', State, '/' City, '.json'];

% Grab the data from the API
Forecast_Data = webread(CallURL_forecast, options);

disp('Forecast');
% Display Forecast
for ii = 1:8
disp(Forecast_Data.forecast.txt_forecast.forecastday(ii).title);
disp([strrep(Forecast_Data.forecast.txt_forecast.forecastday(ii).fcttext,...
'. ', char(10)) char(10)])
end

The previous code will provide the following output style:

Forecast
Wednesday
Occasional snow showers
High 27F
Winds SSW at 5 to 10 mph
Chance of snow 70%.

Wednesday Night
Snow this evening will transition to snow showers overnight
Low 26F
Winds W at 20 to 30 mph
Chance of snow 70%
About one inch of snow expected.

...

Saturday Night
Mainly cloudy with snow showers around before midnight
Low 14F
Winds WNW at 10 to 20 mph
Chance of snow 40%.

Current Radar Map

Now it is time to acquire the animated radar map. The map can be obtained in either a *.png, *.gif, or *.swf format. For this tutorial we will request the *.gif format so that we do not need to concern ourselves with making the animation. Matlab can play gif videos using the implay function. However, I find it easier to simply open the image with a web browser using the web function.

%% Get the radar data
CallURL_radar = ['http://api.wunderground.com/api/', key,...
 '/animatedradar/q/', State, '/' City,...
 '.gif?newmaps=1&timelabel=1&timelabel.y=10&num=5&delay=50',...
 '&width=600&height=480'];
web(CallURL_radar,'-new','-notoolbar');

The obtained radar for Houghton, MI on December 28th 2016 is:

 

Plotting the Forecast Conditions

Finally, using the forecast weather results, I will plot the high and low temperature as well as the icon describing the weather for each day. The forecast data structure contains a field for each day containing an icon name for each day. The Weather Underground API documentation contains a detailed list of the icon sets available and how to use them: https://www.wunderground.com/weather/api/d/docs?d=resources/icon-sets

I will not display the code to generate this plot.

Download the Code

I’ve uploaded the code for this work to the MATLAB file exchange. You can access the upload here: https://www.mathworks.com/matlabcentral/fileexchange/60935-get-current-weather-conditions

Comments are closed.