In: Computer Science
C ++ Data File Preparation
1. Using the original AL Weather Station Data file find all records that have a bad data flag (-9999) for either the PRCP, TMAX or TMIN fields. Produce a new data file (call it Filtered_AL_Weather_Station.txt ) that omits those records with bad data flags. This new file will be used in problem 2. NOTE: The temperatures are given in tenths of a degree Celsius. e.g 83 is 8.3 degrees C.
2. Using the filtered data file from problem 1, create another file (weather_station_five_column.txt) with only the following five columns of information:
STATION NAME DATE PRCP TMAX TMIN
Separate the date fields by inserting spaces and convert the temperatures from Celsius to Fahrenheit.
IMPORTANT: You will need to use the string conversion functions to convert the string type numbers to float or double. The functions are stof and stod. To convert a string to float do this:
string s_tmax; // string type for TMAX
float tmax; // float type for TMAX
// Convert string to float
tmax = stof(s_tmax);
Here is a sample of the output. (You may left justify station name if you like.)
Preliminary program to test the new data file.
3. Using the weather_station_five_column.txt file produced by problem two above:
INTERACTIVE DATA PROCESSING
4. Using the five-column data file produced by problem 2 above write a program that gives the user five options as described by a-e below.
The program should provide a menu showing each of the five options. The user then selects one of the five options. Depending on the option the user will be prompted to enter the corresponding parameters. Then those parameters will be passed to a function to do the work.
A: Total Precipitation from all stations for a single day
User enters one day in mm dd yyyy format. Program returns total precipitation from all stations in inches for that date.
TEST CASES: March 10, March 20.
B: Total Precipitation over a range of dates.
User specifies start date and end date in mm dd yyyy format. Program returns total precipitation from all stations over the date range.
TEST CASE: 03 07 2018 through 03 12 2018
C: Total Precipitation by Station for March.
User enters station name. The program adds up the precipitation for the entire month from that station - or group of stations. (NOTE: Entering DECATUR will select all stations with DECATUR as all or part of the station name.)
TEST CASES: HUNTSVILLE, ATHENS
D: Temperature Extremes and Average by Station
User enters station name. e.g. BANKHEAD. The program finds and returns the maximum temp, minimum temp, average high temperature, and average low temperature for station during March. (Station names must match exactly.)
TEST CASES: MOBILE DOWNTOWN AIRPORT, DECATUR PRYOR FIELD
E. Temperature Extremes and Average by Station over a range of dates
User enters station name, start date and end date in mm dd yyyy format. The program computes and returns the maximum temp, minimum temp, average high temperature, and average low temperature for the specified station over the date range. (Station names must match exactly.)
TEST CASES: BIRMINGHAM AIRPORT 03 05 2018 03 15 2018
SCOTTSBORO 03 20 2018 03 31 2018
http://uahcs.org/CS121/data/AL_Weather_Station.txt
Working code implemented in C++ and appropriate comments provided for better understanding.
Source Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(void) {
ifstream weatherin;
ofstream weatherout;
string s_tmax, s_tmin, s_prcp, dataline;
int pos_tmax, pos_tmin, pos_prcp;
float f_tmax, f_tmin, f_prcp;
unsigned int i = 0, records = 0;
cout << "PROBLEM 1" << endl << endl;
cout << "Attempting to open
AL_Weather_Station.txt..." << endl << endl;
weatherin.open("C:/Temp/AL_Weather_Station.txt");
if (!weatherin) {
cout << "ERROR:
AL_Weather_Station file not found." << endl <<
endl;
system("pause");
return 1;
}
else {
cout << "AL_Weather_Station
file opened." << endl << endl;
}
cout << "Creating new file:
Filtered_AL_Weather_Station.txt..." << endl <<
endl;
weatherout.open("C:/Temp/Filtered_AL_Weather_Station.txt");
if (!weatherout) {
cout << "ERROR:
Filtered_AL_Weather_Station file not found." << endl <<
endl;
system("pause");
return 1;
}
else {
cout <<
"Filtered_AL_Weather_Station file opened." << endl <<
endl;
}
cout << "Attempting to find position of TMAX,
TMIN, and PRCP columns..." << endl << endl;
getline(weatherin, dataline);
weatherout << dataline << endl;
pos_tmax = dataline.find("TMAX");
if (pos_tmax != string::npos) {
cout << "The index of the
TMAX column is " << pos_tmax << endl;
}
else
{
cout << "Unable to find TMAX"
<< endl << endl;
system("pause");
return 2;
}
pos_tmin = dataline.find("TMIN");
if (pos_tmin != string::npos) {
cout << "The index of the
TMIN column is " << pos_tmin << endl;
}
else
{
cout << "Unable to find TMIN"
<< endl << endl;
system("pause");
return 2;
}
pos_prcp = dataline.find("PRCP");
if (pos_prcp != string::npos)
cout << "The index of the
PRCP column is " << pos_prcp << endl <<
endl;
else
{
cout << "Unable to find PRCP"
<< endl << endl;
system("pause");
return 2;
}
// Skipping formatting line
getline(weatherin, dataline);
cout << "Parsing data..." << endl << endl;
while (!weatherin.eof()) {
// Continuously scroll through each
line of data
getline(weatherin, dataline);
// Find the value for each
header
s_tmax = dataline.substr(pos_tmax,
5);
s_tmin = dataline.substr(pos_tmin,
5);
s_prcp = dataline.substr(pos_prcp,
5);
// Converting string values to
floats
f_tmax = stof(s_tmax);
f_tmin = stof(s_tmin);
f_prcp = stof(s_prcp);
if (f_tmax != -9999 &&
f_tmin != -9999 && f_prcp != -9999) {
weatherout
<< dataline << endl;
records++;
}
}
cout << "Total number of valid weather data
entries: " << records << endl << endl;
cout << "Closing files..." << endl
<< endl;
weatherin.close();
weatherout.close();
system("pause");
return 0;
}