#!/usr/bin/python3
'''
For HW14 part 1, check the video at https://www.youtube.com/watch?v=xr42RGPjRbE&list=PLCFNfZsc2MICCEzpjsLaelymhUecxm4eB&index=19
for how the program should format its output, having a menu for the user
to choose what to do.
'''

'''
  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:
        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):
    numDays = 0
    meanTotal = tminTotal = tmaxTotal = 0
    
    for d in w_data:
        d_date_str = d[5:]
        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 record_temps(w_data):
    htmp = -999
    hdate = ''
    mtmp = 999
    mdate = ''
    for d, v in w_data.items():
        if v['tmax'] > htmp:
            htmp = v['tmax']
            hdate = d
        if v['tmin'] < mtmp:
            mtmp = v['tmin']
            mdate = d
    print('Record high:', hdate, htmp)
    print('Record low:', mdate, mtmp)
    
if __name__ == '__main__':
    w_data = load_weather_data('Indianapolis-Weather-Station-USW00093819-1950-2018.csv')
    record_temps(w_data)


    #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)