#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 10/21/2022 DS 2000 Lecture 13 - functions and program design, HW 5 help A program to read and analyze some small weather data """ import data_utils import matplotlib.pyplot as plt WEATHER_DATA = "weather_sample.csv" def read_data(filename, skip_header = False): """ Reads in the data in a given file and stores the values in a list of lists of strings. Assumes that commas separate row items in the given file. Parameters ---------- filename : string name of the file skip_header: boolean, optional whether or not to skip a header row. Default to False. Returns ------- data : list of lists list of lists of string values for all lines in the file """ file = open(filename, "r") # before we start processing each line, # we need to create an empty list to hold everything data = [] # do we need to skip the first row? if skip_header: file.readline() # processing each line for line in file: pieces = line.strip().split(",") # replace empty strings with 0s (for all columns) # we know empty strings happen in this column # use a for loop if you want to do this for all columns! if pieces[2] == "": pieces[2] = "0.0" # change temp to ints pieces[1] = int(pieces[1]) # change precip to floats pieces[2] = float(pieces[2]) # change temp to celcius # C = 5/9(F-32) pieces[1] = (5/9) * (pieces[1] - 32) # make sure I only have the data that I want? # build a new list for this row # I don't care about the "cloudy" column, # so I just won't put it in the new row row = [pieces[0], pieces[1], pieces[2]] data.append(row) return data def my_minimum(data, col_index): # option 2 - pretend that min() doesn't exist # None is a special value in python that indicates # nothingness or something we've never set min_temp = None # float("inf") # min_temp = weather_data[0][1] for row in data: # if I didn't yet set min_temp or min_temp is larger # than what I'm currently looking at if min_temp is None or min_temp > row[col_index]: min_temp = row[col_index] return min_temp # For functions that only print things def display_day(day_list): print("date:", day_list[0]) print("temp:", day_list[1]) # return None def main(): print("fake HW 5/Weather data analysis") weather_data = read_data(WEATHER_DATA, skip_header=True) print(weather_data) # 2) calculates the minimum temperature # similar to the strategy for find_most_similar_planet my_min_temp = my_minimum(weather_data, 1) print("my min temp", my_min_temp) my_min_precip = my_minimum(weather_data, 2) print("my min precip", my_min_precip) # option 1 - put temps into a list temps = [] for row in weather_data: temps.append(row[1]) min_temp = min(temps) print("min temp", min_temp) # 3) graphs the temperature-per-day temps = data_utils.get_column(weather_data, 1) print(temps) days = data_utils.get_column(weather_data, 0) print(days) day_nums = [] for i in range(len(temps)): day_nums.append(len(temps) - i) plt.plot(day_nums, temps) # plt.xticks(days) display_day(weather_data[0]) main()