1. The program in “proj06b.py” will produce data files which can serve as the in
ID: 675240 • Letter: 1
Question
1. The program in “proj06b.py” will produce data files which can serve as the input file for the program in Part A.
a) The program will always read from “data_full.txt” (it will not prompt the user for the name of the input file). If it is unable to open that file, the program will halt.
b) The program will prompt the user for the name of the output file. If that file does not exist, the program will create it and continue. If that file does exist, the program will discard the current contents of the file and continue.
c) The program will allow the user to select the subset of the data which is to be processed and written into the output file.
2. The file named “data_full.txt” contains monthly global mean temperature deviations. For each year, the file contains one line which identifies the year and has the global mean temperature deviation for each of the 12 months. The first four lines of that file are:
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1880 -30 -20 -18 -28 -14 -29 -23 -7 -17 -16 -19 -21 1881 -9-13 1 -3 -4-29 -6 -2 -8-19-26-15 1882 10 9 2-20-17-25-10 4 -1-22-21-24
Each value in the file is a deviation from a baseline temperature, in hundredths of a degree Celsius. That is, in January of 1880, the global mean temperature was 0.30 degrees Celsius cooler than the baseline value.
3. The program will allow the user to select the subset of lines in “data_full.txt” which are to be processed. The program will prompt the user to enter a year, and it will then prompt the user to enter an integer count. The year will identify the first year that the user wants to include in the subset, and the integer count will identify the number of years that the user wants to include in the subset. For example, if the user enters "1920" and "15", the program will use the 15 years starting with 1920 and ending with 1934 as the subset.
If the user enters an integer count which is greater than the number of years available, the program will include as many years as possible in the subset. For example, if the user enters "2010" and "90", the program will use the 5 years starting with 2010 and ending with 2014 (the last year in the data set) as the subset.
If the user enters "all" (any mix of upper and lower case letters) as the year, the program will include the entire data set as the subset to be processed; it will not prompt the user to enter an integer count.
4. The program will calculate the average temperature deviation for each year in the selected subset: it will calculate the sum of the 12 monthly temperature deviations for that year, divide by 12, and round the resulting value to 0 decimal places.
An example: the sum of the monthly temperature deviations for 1881 is -133. That value divided by 12 is -11.083333333333334; rounded to 0 decimal places, the result is -11.
Another example: the sum of the monthly temperature deviations for 1882 is -115. That value divided by 12 is -9.583333333333334; rounded to 0 decimal places, the result is -10.
The program will then write the year and average temperature deviation to the output file using the format described in Part A: each line will contain a year (4 characters), a space, and a temperature deviation (4 characters).
5. The program will display appropriate messages to inform the user about any unusual circumstances.
Explanation / Answer
import os.path
#change path here
inputfile = os.path.join(os.getcwd(), "data_full.txt")
print(inputfile);
if(os.path.exists(inputfile)):
opfile = input ("Enter the output file: ")
#opfile = os.path.join(os.getcwd(), opfile)
startY = input ("Enter starting year: ")
num = input ("Enter no of years: ")
#print(opfile, "- ", startY,"-", num);
file = open(inputfile, 'r')
output = open(opfile, "w+"); #create file if it not exist - w+
output.write("Year Average ")
lines = ( file.readlines())
count = 0
nyears = 0;
for line in lines:
#first line is header ignore it
if(count > 0):
fields = line.split();
#identify the year
if(int(fields[0]) >= int(startY) and nyears < int(num)):
#print(fields)
#calculate average
length = len(fields)
total = 0
for i in range(1,length):
total =total+ int(fields[i])
avg = total / length-1
#print(fields[0],": ", int(avg))
output.write(fields[0]+ " "+str(int(avg))+" ")
nyears = nyears+1
count = count+1
file.close();
output.close();
else:
print("File not exist");
-------------------output------------------------
D: aviChegpython>python D: aviChegpythonpython-temerature4mfile.txt
D: aviChegpythondata_full.txt
Enter the output file:
test.txt
Enter starting year:
1880
Enter no of years:
5
--------------test.txt--------------------
Year Average
1880 -19
1881 -11
1882 -9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.