#!/usr/bin/python3
'''
  Goals
  - something interesting / exciting / important
  - read weather data from 1950-2018 for Indy airport
  - be able to lookup weather for a given date
  - be able to compute average temperature for given day
  - be able to compute average temperature for each year

  Questions
  - Has the temperature been rising or falling since 1950?
  - What is the hottest day of the year?
  - What is the coolest day of the year?
  - How much temperature variation is normal for a given day?

  More questions
  - Same types of questions but also for precipitation, snowfall

  Why does it matter
  - Should I invest in a better furnance or AC, insulation, etc.?
  - Is it getting more likely that a given location will experience
    floods, very cold weather, or very hot weather?
  - What does this say about global warming?

  Next steps
  - Look at the same type of data but for every city in the US or
    every city on earth.  Then we can get a view of how global
    climate is changing.
'''

def load_weather_data(filename):
    f = open(filename)
    all_data = f.read()
    f.close()

    lines = all_data.split('\n')

    header = ''

    w_data = {}

    for line in lines:
        if line == '': continue
        data = line.split(',')
        if data[0] == 'Date':
            header = data
            continue

        for i in range(1, 7):
            if data[i] == 'T': data[i] = 0.0001
            if data[i] == 'M': data[i] = 0

        w_data[data[0]] = {'date': data[0], 'prcp': float(data[1]),
                           'snow': float(data[2]), 'snwd': float(data[3]),
                           'tmax': float(data[4]), 'tmin': float(data[5]),
                           'mean': float(data[6])}
    return w_data

#date = input('Date to lookup (YYYY-MM-DD): ')
#if date in w_data:
#    print(w_data[date])
#else:
#    print('Date not found')


def yearsAverage(w_data, start, end):
    start = int(start)
    end = int(end)

    # init counts/totals to 0
    tempTotal = {}
    numDays = {}
    prcpTotal = {}
    for year in range(start, end+1):
        tempTotal[year] = numDays[year] = prcpTotal[year] = 0

    # for each line in the weather data, check if in this
    # range of years, and add to counts/totals
    for d in w_data:
        year = int(d[0:4])
        if year >= start and year <= end:
            mean = w_data[d]['mean']
            tempTotal[year] += mean
            prcpTotal[year] += w_data[d]['prcp']
            numDays[year] += 1

    # for each year in the range, print off total/average
    print('Year', '#Days', 'Temp', 'Prcp', sep='\t')
    for year in range(start, end+1):
        print(year, numDays[year], round(tempTotal[year] / numDays[year], 1),
              round(prcpTotal[year], 1), sep='\t')

def dayAverage(w_data, date_str):
    # date_str is something like '01-02'
    numDays = 0
    meanTotal = tminTotal = tmaxTotal = 0

    for d in w_data: # w_data is a dictionary, d will go over all the keys
        # d is a key from w_data, something like '1950-01-01'
        d_date_str = d[5:] # something like '01-01'
        if d_date_str == date_str:
            numDays += 1
            meanTotal += w_data[d]['mean']
            tmaxTotal += w_data[d]['tmax']
            tminTotal += w_data[d]['tmin']

    print('#Days', 'Mean', 'Tmax', 'Tmin', sep='\t')
    print(numDays, round(meanTotal / numDays, 1),
          round(tmaxTotal / numDays, 1), round(tminTotal / numDays, 1), sep='\t')

def allTimeRecords(w_data):
    print('Do something here')
    for date in w_data:
        print('something')
        # date is '1950-01-01'
        # wdata[date]['mean'] is the mean for that date

w_data = load_weather_data('Indianapolis-Weather-Station-USW00093819-1950-2018.csv')
#w_data = load_weather_data('small.csv')

#print(w_data)
#import sys
#sys.exit()

#year_start = input('Year to compute average starting from (YYYY): ')
#year_end = input(  'Year to compute average ending from (YYYY):   ')
#yearsAverage(w_data, year_start, year_end)

day_avg = input('Day of the year to compute average (MM-DD): ')
dayAverage(w_data, day_avg)