Weather Station Data Python 3.4 Please Help You’ve been asked to write a program
ID: 675011 • Letter: W
Question
Weather Station Data Python 3.4 Please Help
You’ve been asked to write a program to read daily precipitation from a file and create a new file that has the monthly totals. There are 10 years of data in the precipitation file, with data for each day of the month. Some days there were errors in recording the data and NA has been entered. You should use functions wisely to make your code more modular and testable.
The Input Format
The input file is precipitation.csv. CSV stands for comma separated value. Each column is delimited by a comma. It is a very common format used to export and import data. You can open it easily with Excel to view the rows and columns of data. Many of these will have a row at the top with the names of the columns separated by commas.
There are only 2 columns in this file the date, and the amount of rainfall in mm. The date information is in YYYYMMDD format. Four characters for the year and 2 for the month and 2 for the day. For example the algorithm is due on 20151025.
precipitation.csv
20091006,5.8
20091007,0.0
20091008,16.0
20091014,NA
20091015,3.0
20091016,0.0
This excerpt from the file shows 5.9 mm on October 6th, 2009. Notice on the 14th of October 2009 no data was recorded.
The Output Format
You will create a new output file that has each year month precipitation amounts totaled. Since our sample file has data from 20000101 to 20091231, the output file will have a total precipitation for each month from 2000 to 2009. The output file will also be a CSV file with 2 columns. A date column and a total precipitation column. The date column won’t require the day and will be in the format YYYYMM.
200001,9.4
200002,57.2
200003,67.6
200004,9.8
200005,68.7
200006,189.2
200007,149.79999999999998
The totals above show that the month of January in 2000 had a total of 9.4 mm of precipitation for the total month.
Month Totals Averaged
The final part of your program will find the averages of total precipitation for each month. You will output the average for the total rainfall in Jan, Feb, etc.
Requirements
Ask the user for the precipitation file to read. If the file does not exist, show the error to the user and ask them to choose a new file.
Ask the user for the file to output to. If the file cannot be opened in write mode for any reason then warn the user and them to choose another file. One reason you may not be able to write to a file is if you give the name of a directory.
Any row from the input file that has NA or other non float values should be ignored. Useful Modules and functions
.split() method is useful for splitting a string by a certain delimiter. In this case a comma.
The csv module can be used to help read csv files. It will automatically handle spliting the lines by the comma.
Example
>>>================================RESTART================================
>>>
Enter the file with precipitation data==>notfound.csv
The file specified could not be found
Enter the file with precipitation data==>precipitation.csv
Enter the monthly data file to save to.==>test
The file specified had an IOError
Enter the monthly data file to save to.==>precip_monthly.csv
Monthly Total Averages
Jan 35.3500 Feb 52.3100 March 84.7300 April 95.1900 May 135.1300 June 157.2400 July 123.7800 Aug 122.1900 Sept 88.2500 Oct 98.0000 Nov 40.3200 Dec 45.8444Explanation / Answer
csvfile = input ("Enter the file with precipitation data: ")
output = {}
mCount = {}
import os.path
if(os.path.exists(csvfile)):
print("The file specified found")
outputfile = input ("Enter the monthly data file to save to: ")
import csv
with open(csvfile, 'r') as f:
reader = csv.reader(f)
for row in reader:
# print (row)
#for col in row:
#col - 1 is YYYYMMDD format
if ( row and row[0] and row[0].strip()) :
ym = int ( int(row[0].strip()) / 100)
#month = ym % 1000;
#add all records in a month
if(row[1] and row[1].strip()and row[1].strip() != 'NA'):
if(output.get(ym) == None):
output[ym] = 0.0
output[ym] = output[ym] + float(row[1].strip())
#close the file
# csvfile.close()
# sort hash keys
import collections
orderOutput = collections.OrderedDict(sorted(output.items()))
hkeys = orderOutput.keys()
#write the order list into output file
with open(outputfile , 'a') as f:
writer = csv.writer(f)
for key, value in orderOutput.items():
writer.writerow([key, value])
#print(orderOutput)
else:
print("The file specified could not be found")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.