You are on page 1of 5

COMP1551 Core Programming

Coursework 4 Task
The United States Geological Surveys Earthquake Hazards Program publishes data for recent earthquakes at http://earthquake.usgs.gov/earthquakes/feed/. Data feeds are available in various formats, including CSV (comma-separated values), which we will use here. Your task is to write a program that reads earthquake data from a CSV le obtained via one of these feeds. Your program must do some processing of these data. A full solution will also generate a Google Map of earthquake locations, as shown in Figure 1.

Figure 1: Example of program output (full solution). Your program can be written in either Python or C++, as you prefer. Choose only one of these! Your solution must be object-oriented. It must include a class called Quake, respresenting the details of a single earthquake, and a class called QuakeData, representing an entire earthquake datasetessentially, a collection of Quake objects. Further details are given in the sections that follow. For both languages, there is a basic solution worth 65% of the marks. If you are reasonably condent and are seeking a good mark for this assignment, you should extend the basic solution with the Google Maps feature. This will earn you up to 85% of the marks available. For the very keen seeking a greater challenge, there are various advanced features that can be implemented for the remaining marks.

Preparation
If you havent yet completed the problem sheets on OOP in Python and C++, you are strongly advised to do so before attempting this assignment. Please also read the rest of this coursework specication carefully before you choose the implementation language and begin any coding. Download cwk4.zip from http://bit.ly/comp1551. This archive contains various les that can be used as the basis of your solution, plus some data that can be used to test it and some sample output. It includes a makele that simplies compilation of C++ solutions. This makele can also be used to prepare your Python or C++ code for submission. Note: for the makele to work properly, you must name your les exactly as specied here!

Python
A full Python solution satisfying all of the requirements given below can be written in under 150 lines, including docstrings, comments and whitespace for neat layout. This gure is intended as a rough guide to the scale of the problem; other good solutions might be shorter or longer.

Basic Solution (24 Marks)


1. In a le named quakes.py, create a class named Quake. Give this class a constructor that creates elds representing the latitude, longitude, depth and magnitude of an earthquake. 2. Add to Quake a __str__ method that generates a string representation of an earthquake with a format like this: M6.5, 28.9 km, lat -6.653 lon 148.155 You can use \N{DEGREE SIGN} to generate the degrees symbol here. 3. Add to quakes.py a new class named QuakeData. Give this class a constructor that takes a lename as its only parameter (besides self). Your constructor should create a eld that can act as a container for Quake objects. It should open the le with the given name, read CSV data from this le, create Quake objects from these data and store them in the container. The standard librarys csv module will simplify handling of CSV datafor more information, see http://bit.ly/csvdocs (the Examples section, in particular). Keep in mind that the values extracted from each record by the csv module are strings; you will need to convert them to float values before creating each Quake object. 4. Add __len__ and __getitem__ methods to QuakeData. The former should return the size of the dataset and the latter should return an individual Quake object, given an integer index into the underlying list that holds the Quake objects. 5. Add a method to QuakeData that computes and returns the mean depth of the stored earthquakes. Add another method that computes and returns the mean magnitude. 6. In a le named quakeinfo.py, write a program that creates a QuakeData object using a lename given as a command line argument. Your program should then display the number of earthquakes contained in the dataset, their mean depth (to two decimal places) and their mean magnitude (to one decimal place).

Google Maps Feature (8 Marks)


1. You have been given a le map-example.html. This shows how Google Maps can be used to visualise earthquake locations. Open this le in a browser to see how the map works, then examine the HTML in a text editor. 2. Add to your QuakeData class a method that generates an HTML le like map-example.html. The method should take a name for that le as its only parameter. Hint: map-example.html can be divided into three segments: a top segment that never changes, a middle segment that is dependent on the dataset and a bottom segment that never changes. The top and bottom segments could be represented in your code as multiline strings. 3. Modify quakeinfo.py so that it uses the new method to create an earthquake map in a le named map.html.

Advanced Options (6 Marks)


If you have the time and the inclination, implement one of the following for the remaining marks. Both will require some investigation and experimentation on your part. Do not attempt them if you struggled with implementing the basic solution or the Google Maps feature.

Modify QuakeData so that the CSV data come directly from the USGS data feed, rather than a manually-downloaded CSV le. You will need to change quakeinfo.py so it uses different command line argumentstime period and quake severity, for example (check the feed URLs for how these parameters are specied). The standard library will help here: see http://bit.ly/urlrequest and the examples therein. You may also nd the information at http://bit.ly/iomodule useful (particularly the section on StringIO). Add support to Quake and QuakeData for plotting of earthquake locations using Pythons turtle module. We have provided you with background.gif for use as a background image. Scaling longitudes and latitudes by a factor of 3 will give you x and y coordinates suitable for plotting on top of this background. Add code to quakeinfo.py that generates the plot. When run, your program should produce output looking something like Figure 2.

Figure 2: Earthquakes plotted using the turtle module.

C++
A full C++ solution, incorporating all of the features discussed below can be written in around 200 lines, including comments and whitespace for neat layout. This gure is intended as a rough guide to the scale of the problem; other good solutions might be shorter or longer.

Basic Solution (24 Marks)


1. In a pair of les named quakes.hpp and quakes.cpp, dene and implement a class named Quake. Dene elds to represent the latitude, longitude, depth and magnitude of an earthquake. (Examining the sample data will help you determine suitable data types here.) Write a constructor to initialise these elds and write a getter method for each eld. 2. Add to Quake a method named str that returns a string object representing an earthquake. The returned string should have a format like this: M6.5, 28.9 km, lat -6.653 lon 148.155 Review the lecture examples for guidance on implementing this method. Check that quakes.cpp compiles properly by entering make quakes.o. Fix any errors before proceeding further. 3. Add to quakes.hpp and quakes.cpp a new class named QuakeData. Give this class a eld that can act as a container for Quake objects. Then write a constructor that takes a lename as its only parameter. Your constructor should open the le with this name, read CSV data from this le, create Quake objects from these data and store them in the container.

Reading CSV data using the relatively low-level features of the IOStreams library is tricky, so we have provided you with a string parsing library that can help. You can access this by adding #include "strtk.hpp" to quakes.cpp. Look at csvdemo.cpp for examples of how this library is used with CSV data. A suitable approach would be to repeatedly read a line with getline and parse it using parse_columns, stopping when the latter returns false. Check your work by entering make quakes.o and x any compiler errors. 4. Add a method that returns the size of the dataset. Then add a method that returns a Quake object, given an integer index into the underlying container that holds the Quake objects. 5. Add a method to QuakeData that computes and returns the mean depth of the stored earthquakes. Add another method that computes and returns the mean magnitude. 6. In a le quakeinfo.cpp, write a program that creates a QuakeData object using a lename given as a command line argument. Your program should then display the number of earthquakes contained in the dataset, their mean depth (to two decimal places) and their mean magnitude (to one decimal place).

Google Maps Feature (8 Marks)


Read the description of this feature given earlier for the Python solution and make equivalent alterations to quakes.hpp, quakes.cpp and quakeinfo.cpp. You can represent the static parts of the page using multiline strings, just as you can in Python. The syntax is shown by the following example: const string multiline = R"( This is a string that can span multiple lines. )"; See http://bit.ly/cpprawstrings for more on these raw string literals. This is a relatively new feature of C++; you will need the --std=c++0x compiler option to enable it on our Linux machines. The supplied makele is already set up to use this option.

Advanced Options (6 Marks)


If you have the time and the inclination, implement any two of the following for the remaining marks. Make sure you add code to quakeinfo.cpp that demonstrates their use. We recommend that you do not attempt these tasks if you have struggled with the basic solution or with the Google Maps feature. Overload stream insertion operator << for Quake objects so that a Quake object can be written to a stream such as cout in the usual way. Overload the less than comparison, <, for Quake objects in such a way that a collection of Quake objects will be sorted rst by magnitude and then by depth. Overload the [] operator for QuakeData so that the standard square brackets syntax can be used to access a Quake object; i.e., if quakes is a QuakeData object, it should be possible to access the rst Quake object in the dataset as quakes[0].

Submission
Submit your code as a single Zip archive. You can use the supplied makele to create this archive; simply enter make pyzip for Python solutions or make cppzip for C++ solutions. These commands generate a le called submission.zip, which you should submit using the link provided in the VLE. The deadline for submissions is 10 am on Friday 26 April. The standard university penalty of 5% of available marks per day will apply to late work, unless an extension has been arranged due to genuine extenuating circumstances.

Marking
This assignment is worth 25% of your overall module grade. Submissions will be marked out of 40, with the marks breaking down as follows: Aspect Basic Quake class Basic QuakeData class Basic quakeinfo program Correct output of earthquake statistics Implementation of Google Maps feature Generation of a correct map Implementation of advanced features Correct execution of advanced features Coding style and comments Marks 6 10 6 2 6 2 4 2 2

You might also like