Skip to content
Pheipp edited this page Feb 1, 2019 · 4 revisions

Dokumentation

Foreword

The idea behind the radiation index was to display the average, the minimum and the maximum value for all the capitol cities of the G20 states. Because of several outliers in the data, a more statistic approach was used in form of the median and whiskers. The documentation will cover a short insight which files are created/altered and what are they used for.

Extracting the data

To get the data out of the database, 3 scripts are needed. All these files are currently within the scripts folder.

Radiation_index_export

This script contains all the needed SQL-Queries. One for each city. The query selects all measurements located 5000 meters (depending on the amount of data, some cities have a bigger, and some a lesser distance) within a radius of the coordinates of the city center and exports these into separate csv files.

Calculate_radiation_index

Calculate_radiation_index is a Ruby script with the main task to read all the csv files, get rid of not needed information and save all the information needed in a new csv file. Because every city has a separate csv file, every file needs to be read. For that, the city_names – array is iterated. This array stores all the names of the cities that will be displayed later. In every iteration, two arrays are needed. One array is the data-array. This array can store three additional arrays, one of them is for the average, one for the minimal and one for the maximal values. The other array (city_values) contains all the values that were read from the latest csv file. This array is different for each iteration, while the data-array stays the same, just with additional data each time. To calculate the data for the three arrays inside the data-array, the calculate_data method is called. First, the average value is calculated. As mentioned earlier, for better statistical evaluation, the median is calculated and not the exact average.

Before the minimum and maximum values are also stored in their representative arrays, outliers need to be removed. For that, the Whiskers of a Box plot where calculated. To calculate the Whiskers, the values for the IQR, the first and the third quartile are needed. After obtaining the value for the first and third quartile, which are calculated similar to the median, the value for of the first quartile need to be subtracted from the value of the third quartile to get the IQR. The mathematical rule is that every value, that is bigger than the value for the third quartile plus the IQR1.5, is an outlier. Mirroring that, every value smaller than the value for the first quartile minus the IQR1.5 is also an outlier. These outliers are removed from the city_values-array. After that, the minimal and maximal value is calculated from the city_values-array, using a standard Ruby Method and is stored in their representative data-sub-arrays.

After all csv file are read and all the needed data is calculated, a new csv file needs to be created. This is done inside the create_g20_csv method, where a new csv file is created, and every sub array of the data-array is stored as a separate line.

The resulting csv file has 4 different lines:

  1. Header with with the city names
  2. Average Value of all the cities
  3. Minimal Value of all the cities
  4. Maximal Value of all the cities

Radiation_index_export

This shell script is used for executing the other two scripts. First, the radiation_index_export.sql script is executed. After that, the ruby script calculate_radiation_index is executed. If both scripts are finished, the resulting g20.csv file need to be copied to another location and the server need to be resynchronized.

Displaying the Data

Controller

The controller (radiation_index_controller.rb) has one major job: to read one row of the g20.csv file, depending on the HTML-request parameter and converting the row into a sorted Hash. If the city has a nil value, a value of -1 is set to prevent a runtime error and is used for later validation.

View

The view (radiation_index.erb) contains 3 parts: a navigation menu, the time of the last update and a table for all the data. The navigation menu is handled by the Helper class. The parameter for the menu is the name that should be displayed. The time display for the last update is done by reading the last update time from the file itself.
The iteration over the Hash is done with an index to display the rank. There, the value is also checked for -1. If that’s the case, instead of -1 “Not enough data” is displayed.

Helper

The Helper Class (radiation_index_helper.rb) is used for the navigation menu. There, the right HTML-request parameter is set, and the button is set active for visual feedback.

Other

There are two smaller additions to existing files:

  • Routes.rb -> routing
  • Application.html.rb -> Link in the application navigation menu

Adding a new City

To add a new city, only the coordinates and the name of the file must be changed. The first value for the coordinates is the longitude and second is the latitude. The other change need to be done in the calculate_radiation_index script. There, The name of the city needs to be added into the city_names-array.

Clone this wiki locally