Part II. (80 pts.) ile weather.txt on Blackobard contains some unknown number of
ID: 3872163 • Letter: P
Question
Part II. (80 pts.) ile weather.txt on Blackobard contains some unknown number of lines of weather data listing a town inine trom three sites (Bangor, Orono, or Calais), date, temperature (Fahrenheit), wind direction, wind speed (mph) and name of reporter. All entries are from the same month but do not assume the data set will always be for February. Names contain at least two parts separated by a blank. Names may have multiple parts (ex. Penny Jane Thomas). You may assume the file is not empty. weather.tt Notepad File Edit Format View Bangor 82/10/17 Orono 82/10/17 Bangor 82/11/17 Calais 82/11/17 Calais 02/11/17 Bangor 02/12/17 Bangor e2/13/17 Orono 02/13/17 Orono 82/13/17 Calais 82/13/17 Orono 02/14/17 Bangor 82/14/17 4.7 Dind 23.65 NW 22.0 W 25.1 W 25.75 W 21.9 N 28.5 N 26.85 N 26.9 NE 23.15 N 28.6 E 21.75 E 3.5 Pauline Stinson 3.7 Suan Speller 3.3 Tom McBride 3.9 Penny Jane Thomas 3.1 Xang Xao 3.8 Hillary Tines 3.1 Theresa Sanborn 3.2 Owen Mark Prentiss 3.1 Isaac Short 3.7 Xang Xao 3.1 Isaac Short 3.6 Jeff Byron File averages.txt on First Class contains a historical listing of month names and average temperatures in Fahrenheit. averagestt . Notepad File Edit Format View Help anuary 24.92 February 28.70 March 45.91 April 52.8e May 68.90 June 73.55 July 78.63 August 77.59 September 68.40 October 55.7 November 45.75 December 36.33 Formula for wind chill: wnd Chill (F) = 35.74 + 0.6215.. 35.75 where T-Air Temperature E 16 + 0.4275TYTI Wind Speed (mph)Explanation / Answer
Algorithm steps:
1)Firstly we need to open the wheather.txt (any input file) file to read its data line by line
2)In each line we need to take all those values which are required for the output and we are creating a temporary line with these
3)Then we calculate the Wind Chill using the formula and inlcude this value in our temporary line.
4)parallely we also need to track all temperature values for calculating Average Temperature, Minimum temperature and maximum temperature
so we are storing temperatures in a separate list(array) which we are going to use later.
5)and also for generating reading data we are keeping track of all counts of town, means how many time a town data occured
for tracking this we are using a key value pair logic (dictionary) in which the key is town and its value is its count(reading).
6)Last data in temporary line is reporter name logic so while reading line we have taken that line in a list by using split function to split it on the
basis of whitespace from there we pick its first name first character and for last name we are taking this list last item using -1 as index value in python
which picks the last item of a list(array) and its first character and then we are capitalizing it and appending it to our temporary line.
7)Then we are placing this temporary line in one more list so that we can write this whole line at once later on in out_data.
8)then using the temperature list we are calculating the avg, min, max temperature and writing them in our output file.
9)then using the reading data using the reading dictionary and writing it to our output file.
10) then we are reading our averages.txt and writing it to our output file as it is.
11)Now for generating difference between current average temperature and historical temperature for that month we are tracking this using the date variable declared
date will have the last date of our input data, so we use it and from here we find out the month,
then we convert our historical data into a list which contain one item as one line in averages.txt, form here we read out the historical average
and finally we write it to out output file.
Working code is written in Python as (#--> is used for comments in python):
#Declared variables to be used later.
out_data = [] # for storing final output data
readings = {} # for generating reading data
date = "" #for keep tracking of month in input file
temperatures = [] #for tracking all temperatures for all average, minimum and maximum calculations
with open("wheather.txt", 'r') as f: # we open our input file in read mode
for line in f.readlines(): # reading each line of our input file
temp_data = []
temp = line.split() # this is to split each line on the basis of space to find all fields seperately
temp_data.append(temp[0].upper()+" ") # appending town in uppercase in our temp_line
if temp[0] in readings: # Parallely tracking readings
readings[temp[0]] += 1
else:
readings[temp[0]] = 1
temp_data.append(temp[1]+" ")
date = temp[1]
temp_data.append(temp[2]+" ")
temperatures.append(float(temp[2]))
temp_data.append(temp[3]+" ")
temp_data.append(temp[4]+" ")
wind_chill = 35.74 + 0.6215*(float(temp[2])) - 35.75*(float(temp[4])**0.16) + 0.4275*(float(temp[2]))*(float(temp[4])**0.16) # calculating whind Chill
wind_chill = round(wind_chill, 2) # round is used to reduce it upto 2 decimal places
temp_data.append(str(wind_chill)+" ")
reporter_name = temp[5][0]+temp[-1][0] #reporter name logic
reporter_name = reporter_name.upper()
temp_data.append(reporter_name)
out_data.append(temp_data) #generating final output data
with open("wheatherout.txt", 'wt') as f: # opened output file
f.write("Town Date Temp Direction Wind Speed Wind Chill Reporter ")
for line in out_data:
for data in line:
f.write(str(data) + " ")
f.write(" ")
f.write(" ")
avg_temperature = float(sum(temperatures))/ len(temperatures)
f.write("Average Temperature: "+ str(avg_temperature) + " ")
max_temperature = max(temperatures)
f.write("Maximum Temperature: "+ str(max_temperature) + " ")
min_temperature = min(temperatures)
f.write("Minimum Temperature: "+ str(min_temperature) + " ")
f.write(" Number of readings: ")
for reading in readings:
f.write(reading.upper() + " " + str(readings[reading] )+ " ")
f.write(" Historical Averages: ")
historical_data = []
with open('average.txt', 'r') as ff:
historical_data = ff.read()
f.write(str(historical_data))
f.write(" ")
month = date[1]
historical_data = list(historical_data.split(' '))
data = historical_data[int(month)-1]
data = data.split()
historical_temp = float(data[1])
f.write("Difference between this Februrary from historical average is " + str(round((avg_temperature - historical_temp), 2)))