Weather by MATLAB

Stormy Attaway, Boston University, Mechanical Engineering
Author Profile


This assignment reinforces material on using API keys to scrape data from web sites. In this case, temperature data is acquired for a given set of cities. The temperatures must then be plotted in a manner that is easy to understand.

Learning Goals

There are two main concepts that are learned from this activity: scraping data from web sites (and the issues that will arise such as the code timing out), and communicating data effectively. Students are very motivated when they learn that it is so easy using the webread function in MATLAB to obtain interesting data sets. However, they do find that data can be messy. In this case, the structure of the data that is returned is not consistent for all cities. Students must learn on their own how to use try-catch to account for this. Additionally, they must learn on their own how to customize aspects of the plot (such as the x-tick labels) to make it easier to understand.

Context for Use

This is a homework assignment given approximately half-way through an engineering computation course for freshmen. Students may work in pairs or in teams of three. At this point in the course, students have already seen material on API keys and the webread function in their text and in the course materials, and have completed a MATLAB Grader problem that uses an API key to get stock data. This reinforces their ability to communicate graphically. If any temperature data appears to be an outlier, they will have to deal with that (in a similar homework assignment, students must receive stock data including Berkshire Hathaway).

Description and Teaching Materials

SERC Activity: Weather by MATLAB

In Section 9.4, you were introduced to the idea of Application Programming Interfaces (API's). API's are an important backbone piece for most applications you use; whether it is a mobile application such as Lyft or a web application like Facebook, API's are used everywhere.

For this homework, we will be using the OpenWeatherMap's API to access real time weather metrics. Your job will be to create an application to access, analyze, and present these metrics. Before getting started, we recommend familiarizing yourself with the API's documentation available here:

When requesting data from an API, there will usually be a query parameter or series of query parameters that are needed to access information. For example, on the Stock API question on MATLAB Grader, this API had a request parameter of 'symbol' with the code that followed. For the OpenWeatherMap API, two query parameters are needed: 'APPID' which has a key value that we will provide for you, and 'q' which is the City and Country code as a character vector, placed together in this exact format: 'Boston,us'

For the APPID, please use the following key provided.
Overall, the url passed into the API should look like this:[CITY],[COUNTRY]&APPID=[KEY]
Where [CITY] is the desired city, [COUNTRY] is the two-letter country code, and [KEY] is the key.

Make a couple of practice requests to the API to familiarize yourself with the format of the data returned by the API call. A successful request from the API returns a single data structure containing all the information you will need for data analysis. You are responsible for appropriately indexing into the structure to access the information needed for the assignment. *Hint: When indexing into the structure to find the temperature data, you may realize that the organization of the structure is not always the same from city to city; and thus, different organizations of the structures will require different methods of indexing. To remedy this, we recommend using a try/catch statement. See the Mathworks website to learn about how to use try/catch to solve this problem.

The Assignment
We are providing you with the cell array of cities that you will be responsible to get weather data for. This should be placed into your script file as shown here.

'Rio de Janeiro','Accra','Longyearbyen','Khartoum'};

If you investigate the return of the API request, you can see a successful request provides you with the 5-day temperature data for each specific city, over periods of 3-hour intervals.

What is Required
- One plot that has all of the individual city's temperature data plotted as a line graph.
- A scatterplot where each point on the plot is a specific city's minimum temperature from the 5-day period.

Runtime, User Experience, and Coding Style
One feature about this API that you should note is that it is rather slow. To improve the user experience, let the user know why the program is taking a bit of time to run. For example, you could print the following statements while the code is executing (note the cities below may be different than the cities we provided):

Also, if you experience trouble with the code timing out, you can use the weboptions function to modify the Timeout key to 30 instead of 5 before calling webread.

User Experience
Keeping in mind user experience, all plots should be easy to read and interpret. This will involve, at minimum, changing axis properties and line properties.

Coding Style
This program should be modular (script that calls multiple functions). Additionally, your code should be as efficient as possible and follow the guidelines you've been learning about good coding style (descriptive variable names, vectorizing code, comments, etc.)

To Turn In:

This is what your output should look like (Note: the cities being used in this example are NOT the cities we are asking you to use):

Like past assignments, you should turn in:
(1) A hard copy of all of your code (including screenshots of your main script and all functions)
(2) Printed screenshots of your code running (plots and any messages that may appear in your Command Window)
(3) An electronic .zip file including your main script (.m file), all functions (.m file), and screenshots (.jpg)
Weather Activity Desctiption (Microsoft Word 2007 (.docx) 358kB Aug14 19)
Weather Activity Solution Code (Microsoft Word 2007 (.docx) 118kB Aug14 19)

Teaching Notes and Tips

This should take approximately 5-8 hours to complete.



Coding Style(4 points)

Modularity: _________ /2

Comments: _________ /2

Data Collection(12 points)

Correct Use of API :

city/country code ________ /2

Key ________ /2

Webread ________ /2

Notifies User of Delay During Data Collection ________ /3

Find Min Temp

Find min temp for all cities ________ /2

Plotting(11 points)

Plot 1 Min Temp of Cities

All cities included ________ /1

Title ________ /1

Axes Labels ________ /2

Axes range ________ /1

Plot 2 Hourly Temp Plot

Title ________ /1

Axes Labels ________ /2

Legends ________ /1

Axes range ________ /1

All cities included ________ /1

Handed in all components on time(4 points) _________/4

TOTAL: _________ /30

References and Resources

Attaway "MATLAB: A Practical Approach to Programming and Problem Solving, Fifth Edition"

Weather Activity Solution Code (Microsoft Word 2007 (.docx) 118kB Aug14 19)