Comp 110 Weather Stats

In this problem set you will process public data provided by the National Oceanic and Atmospheric Administration. The data you will be working with, specifically, is weather data from Raleigh-Durham International Airport for the past 30 years. You will write code to answer questions like:

"How much did it rain?"

"How many days did it snow?"

"What was the coldest day on record?"

This problem set makes use of all of the concepts we've learned in COMP110 thus far: classes, arrays, loops, conditionals, boolean expressions, variables, and parameter-driven functions that return values.

Part 0. Starting the Dev Environment

As with in lecture, begin by opening up VSCode, ensuring your project is still open, and then running the following two commands in the VSCode Integrated Terminal:

  1. npm run pull
  2. npm run start

The first command "pulls" the latest files needed for COMP110 into your repo, if any. The second command starts the development environment and will open your web browser to the local server.

Setting up the App

  1. Right click on the "src" folder
  2. Select "New Folder"
  3. Name the new folder: ps04-weather-stats
    1. Note: capitalization and punctuation are important!
  4. Right click on the folder you just created
  5. Select "New File"
  6. Name the new file: index-app.ts
    1. Note: capitalization and punctuation are important!
    2. Note: the i in index is lowercase!

Honor Code Header

All problem sets begin with an Honor Code pledge. Notice this header is contained within block comment tags discussed in Lecture 1. You can copy/paste the pledge below and fill in your name and ONYEN.

/**  
 * Author:  
 * ONYEN:  
 * UNC Honor Pledge: I certify that no unauthorized assistance has been received  
 * or given in the completion of this work. I certify that I understand and
 * could now rewrite on my own, without assistance from course staff, 
 * the problem set code I am submitting.
 */

Imports

The next step after ensuring the honor code header in starting most projects this semester is importing the functions needed from the introcs library. The first line of code to add to your app is the following:

import { print, csvToArray } from "introcs";

Exported Functions and Classes

In this problem set and future problem sets, you will be asked to implement exported functions and classes. By exporting your functions and classes, we are able to test them. Defining an exported function is the same as any other function, except that it begins with the keyword export. For example:

export let main = async () => {    
  // main's function body goes here
};
 
main();

If you do not export a function, it will appear to the grader that you did not write the function at all!

Remember to call main as the last line of your program.

Part 2. Defining a Custom Data Type

First, we need a custom type to model each row in our weather data spreadsheet. We'll name it WeatherRow and give it a property to match each column of data available.

Requirement 0. Define an exported class with the following properties:

0.0 - Name: WeatherRow

0.1 - Properties of type string: date

0.1.1 - The default value of the date property must be an empty string: ""

0.2 - Properties of type number: precipitationsnowtempHightempLow

0.2.1 - The default value of each number property must be 0.

Be sure you assigned a default value to each property before continuing

Part 3. Loading a CSV File

In this part, you will setup the necessary functions to begin your program by prompting the user to select a CSV file and a function to receive the CSV file's data once loaded.

3.0 In the definition of main, you are ready to prompt the user to select a CSV file to process. To do so, establish a variable to hold your data and assign it the result of awaiting a call to the csvToArray function covered in Lecture 10. Remember, you must await the user's selection on csvToArray. For reference, see the documentation on csvToArray below:

function csvToArray(prompt: string, class: Class): Class[]
Parameters:
 - prompt: The text you want to prompt the user with.
 - class: The class (i.e. WeatherRow) that each row of the CSV corresponds to.
Reminder: You must await the return value of this function:
 example: let data = await csvToArray("Select Data CSV", WeatherRow);

After establishing your variable, try printing its contents to get a sense of the data we're working with in this problem set. Three sample CSV data sets are provided in the data/weather folder of your work space. As you are working through the problem set, try testing with each.

  • fall-2016.csv
  • spring-2017.csv
  • since-1985.csv

Once you have selected a CSV file from the file chooser in your browser, the array of WeatherRow objects read in from the file will be assigned to the variable you declared. You should be able to see the data you're working with. You can now remove the print statement displaying your complete data set.

Seeing errors? Try refreshing your web browser and making sure you selected the correct data file. After that, be sure that your WeatherRow class has default values assigned to all of its properties as described in Requirement 0.

Part 4. Data Processing Functions

From the main function, you will call the functions you write below. An example target output for the file "fall-2016.csv" is:

Total precipitation: 14.190000000000003
Snow days: 0
Nice days (60F-80F): 15
Maximum temperature: 99
Coldest day: 2016-12-10
Days above 80F: 46
Days above 90F: 22

An example target output for the file "spring-2017.csv" is:

Total precipitation: 17.61
Snow days: 2
Nice days (60F-80F): 3
Maximum temperature: 89
Coldest day: 2017-01-09
Days above 80F: 23
Days above 90F: 0

We will not test the exact formatting of your main function's output. However, we will test the correctness of each of the numerical / date values returned by the functions you must implement below.

*** You should test your work by calling the below functions from the main function and printing their return values. UTAs will not help you with an implementation of a function below unless you can demonstrate that you have correctly defined it and are successfully calling it from the main function. Ideally, using string concatenation and function calls, you should be able to replicate the output shown above as you move through the problem set. ***

4.1 totalPrecipitation

Define and export a function named totalPrecipitation. It should accept an array of WeatherRow objects as a parameter. It should return the sum of the precipitation property for each element in the WeatherRow array parameter. Test by calling this function from the main function and printing the returned value.

Expected results from data sets:
fall-2016: 14.190000000000003
spring-2017: 17.61
since-1985: 1450.8899999999826

4.2 snowDays

Define and export a function named snowDays. It should take an array of WeatherRow objects as its parameter. It should return the number of WeatherRow elements (days) where snow was recorded. Call this function from the main function and print the returned value.

Expected results from data sets:
fall-2016: 0
spring-2017: 2
since-1985: 111

4.3 niceDays

Define and export a function named niceDays. Its only parameter is an array of WeatherRow objects. It should count and return the number of WeatherRow elements where the low temperature was at least 60 and the high temperature at most 80 degrees. You should make use of this function from the main function like the earlier functions.

Expected results from data sets:
fall-2016: 15
spring-2017: 3
since-1985: 643

4.4 maximumTemperature

Define and export a function named maximumTemperature. It, also, will take as an input an array of WeatherRow objects. It should find and return the maximum high temperature in the input array of WeatherRows. If the input array is empty, return 0. Test your maximum temperature algorithm from the main function.

Expected results from data sets:
fall-2016: 99
spring-2017: 89
since-1985: 105

4.5 coldestDay

Define and export a function named coldestDay. It takes an array of WeatherRow objects as a parameter. It should find and return the date (string) of the coldest temperature on record. 

If more than one WeatherRow shares the same low temperature, return the most recent of the dates. If the data set is empty, return "N/A". (Hint: the largest number available in the programming language is Number.MAX_SAFE_INTEGER. You could also choose a temperature that is unreasonably large given our data set, such as, 1,000,000 degrees F.)

You can assume that the array of WeatherRow objects is always sorted chronologically. Like the earlier functions, test from the main function.

Expected results from data sets:
fall-2016: "2016-12-10"
spring-2017: "2017-01-09"
since-1985: "1996-02-05"

4.6 daysAbove

Define and export a function named daysAbove. Unlike the previous functions, this function needs two parameters! The first parameter should be an array of WeatherRow objects. The second parameter should be a temperature value. The function should return the count of the number of days in the array where the high temperature is greater than the temperature parameter. 

Expected results from data sets when the temperature parameter is given an argument of 80:
fall-2016: 46
spring-2017: 23
since-1985: 4304

Expected results from data sets when the temperature parameter is given an argument of 90:
fall-2016: 22
spring-2017: 0
since-1985: 1362