Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python: Starter code: Find the last and first frost dates for each year, and compute the average last and first frost dates.

In Python:

image text in transcribedimage text in transcribed

image text in transcribed

Starter code:

""" Find the last and first frost dates for each year, and compute the average last and first frost dates. """

import sys from typing import Tuple, List, Dict from datetime import date, timedelta

Weather_Datum = Tuple[date,float,int,int] Weather_List = List[Weather_Datum] Frost_Datum = Tuple[int,date,date] Frost_List = List[Frost_Datum]

def read_weather_history(weather_file_name: str) -> Weather_List: """ Read a .csv file of weather history. Each line has a date, precip, low, high.

Return: a list of tuples: each tuple is one day's datum. """ weather_history: Weather_List = []

return weather_history

def split_by_year(weather_history: Weather_List) -> List[Weather_List]: """ Split weather history into one-year chunks. Each chunk is a list, with one year's data. """ all_years_data: List[Weather_List] = []

return all_years_data

def get_frost_dates(one_years_data: Weather_List) -> Frost_Datum: """ Given one year's data, get the first and last frost dates. """

year:int = 2000 # REPLACE THE FOLLOWING LINE! return (year, date(year, 5, 1), date(year, 10, 1))

def save_frosts(frosts:Frost_List, frost_file_name:str) -> None:

""" Save the frost dates into a .csv file. """

return

def print_average_frosts(frosts:Frost_List) -> None: """ Given a list of frost dates over many years, compute the average date of Spring frost, and the average date of the Fall frost, and the season length. Print all three. """

return

def main(): if len(sys.argv) != 3: print("Usage:") print(" python frost.py weather-file.csv frost_file.csv") sys.exit(1) weather_history: Weather_List = read_weather_history(sys.argv[1]) weather_years: List[Weather_List] = split_by_year(weather_history)

frost_data: Frost_List = [] for year_data in weather_years: frost_datum: Frost_Datum = get_frost_dates(year_data) frost_data.append(frost_datum) save_frosts(frost_data, sys.argv[2]) print_average_frosts(frost_data)

if __name__ == '__main__': main()

You should create a program that reads a file with historical weather information, and writes a file with frost date information. The input file is a .csv table, which lists the precipitation (precip), high temperature (tmax), and low temp (tmin) for many days, over possibly more than 100 years. The output file will also be a . csv file, with one line per year. That line will contain the year, the day of the last Spring frost, the day of the first Fall frost, and the length of the growing season. In addition, the program should print () three lines: - the average month/day of the last spring frost, - the average month/day of the first fall frost, and - the average length of the growing season (the number of days between those two dates). I am providing some starting code, in frosts. py. You should modify this file, and submit your work. Do not add any other files. The program should be run thus: python frosts.py 99709-weather-history.csv 99709-frost.csv The first argument is the (input) historical data file, and the second is the (output) frost file. The input file is a plain text file. The first line is a descriptive header; you should ignore it All the other lines may contain four values, separated by commas: date, precipitation in inches, high temperature, and low temperature (both in degrees F). In the Alaska data, the date format is always CCYY-MM-DD (century, year, month, day, which is the ISO standard format). In the Durham data, the format of the date varies. It may be in ISO format, or MM/DD/YY (which has a "Y2K" bug). In the latter case, the century is missing. Thus these two lines may occur in the file (very far apart from each other, of course): - 1/15/64,6,33,17 (January 15, 1904) - 1/15/64,,5,2 (January 15, 2004) Even though the date format is ambiguous, the very first date in the Durham data is stored using the unambiguous ISO format, and the days are stored consecutively, so it's possible to keep track of the century as you read each line: whenever the month goes from 12 to 1 , a new year begins, and you should check whether the century has changed. Sometimes the meteorologist on duty did not report all the data. Thus these lines may occur: - 3/14/27, , ,43 (Bad data, missing low temp) Do not use bad data lines in your statistics (conti nue the loop, skipping to the next line). Output Data File Your output file should be a plain text file. The first line should be this header: Year, Last Spring Frost, First Fall Frost, Season Length All the other lines should have four fields, separated by commas. The first is the year, a 4-digit number. The second and third are YYYY-MM-DD dates, in ISO format. The last one is an int. Implementation Details You may write the program any way you want, as along as it produces the correct output. However, here are some suggestions: 1. Keep two lists of dates: all the spring frost dates, and all the fall frost dates. 2. As you process each line of input, remember the previous line's info: - the previous year - the previous month - the previous low 3. When the current tow is above 32 , and the previous low is 32 or less, and it's before July, we have a potential last spring frost. Save the previous date. 4. When the current low is 32 or below, and the previous low is above 32 , and it's after July, we have a potential first fall frost. Save the current date. 5. When you reach December 31 st, save the last spring and first fall frost dates, by writing another row to the output file. 6. When you are done, go through all the outputs you saved, and compute the average last spring frost and first fall frost. Date Handting The date time module includes the date class. If you know the year, month, and day, you can create a date: the_date = datetime, date (year, month, day) If you have a date, you can get its month and day: the_month: int = the_date. month the_day: int = the_date. day You can get the number of days between two dates: \[ \text { num_days:int }=(\text { date } 1-\text { date } 2) \text {.days } \] You can also get a date n days after another date: later_date: date = first_date + datetime.delta(n)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students also viewed these Databases questions

Question

Do you prefer working with others or by yourself?

Answered: 1 week ago

Question

Draw Turing machine accepted of language (01*0)

Answered: 1 week ago