import agate

"""
Explanation: Import agate, a Python-based data analysis library
End of explanation
"""


column_fix = agate.TypeTester(force={
    'date': agate.Date(),
    'neb_notes': agate.Text()
})

funds = agate.Table.from_csv('e911_fund.csv', column_types=column_fix)

"""
Explanation: Create agate Table of csv file from Nebraska E911 fund data from 2001 to 2015
End of explanation
"""


print(funds)

"""
Explanation: Check out column names & types from funds table
End of explanation
"""


def calculateNebCellPhones(row):
    return row['neb_balance'] / row['neb_surcharge']

cell_phones = funds.compute([
        ('cell_phones', agate.Formula(agate.Number(), calculateNebCellPhones))
    ])

"""
Explanation: Calculate the number of Nebraska cell phones by dividing the e911 fund by the surcharge amount
End of explanation
"""


cell_phones.print_table()

"""
Explanation: Let's see the new table
End of explanation
"""


from decimal import Decimal

def round_cell_phones(row):
    return row['cell_phones'].quantize(Decimal('1'))

rounded_cell_phones = cell_phones.compute([
        ('cell_phones_round', agate.Formula(agate.Number(), round_cell_phones))
    ])

"""
Explanation: Now, I'll round those awkward cell phone estimates
End of explanation
"""


rounded_cell_phones.print_table()

"""
Explanation: Let's see the nebraska e911 fund table
End of explanation
"""


column_names = ['date', 'ia_balance', 'ia_surcharge']
column_types = [agate.Date(), agate.Number(), agate.Number()]
iowa_phones = agate.Table.from_csv('e911_fund_iowa.csv', column_names, column_types)

print(iowa_phones)

"""
Explanation: Add the Iowa e911 fund information
End of explanation
"""


def calculateIACellPhones(row):
    return row['ia_balance'] / row['ia_surcharge']

ia_cell_phones = iowa_phones.compute([
        ('ia_cell_phones', agate.Formula(agate.Number(), calculateIACellPhones))
    ])

ia_cell_phones.print_table()

"""
Explanation: Calculate IA cell phones from e911 balance and surcharge amounts
End of explanation
"""


from decimal import Decimal

def round_cell_phones_ia(row):
    return row['ia_cell_phones'].quantize(Decimal('1'))

rounded_cell_phones_ia = ia_cell_phones.compute([
        ('ia_cell_phones_round', agate.Formula(agate.Number(), round_cell_phones_ia))
    ])

rounded_cell_phones_ia.print_table()

"""
Explanation: Round those cell phone amounts
End of explanation
"""


total_cell_phones = rounded_cell_phones.join(rounded_cell_phones_ia, 'date', 'date')

total_cell_phones.print_table()

"""
Explanation: Join together nebraska and iowa tables
End of explanation
"""


ia_phones_2015 = total_cell_phones.columns['ia_cell_phones_round'][0]
ia_phones_2006 = total_cell_phones.columns['ia_cell_phones_round'][9]
ia_pc_change = ( ia_phones_2015 - ia_phones_2006)/ia_phones_2006

print("Iowa percent change in cell phones for 2015, {}, to 2006 {} is {}".format(ia_phones_2015, ia_phones_2006, ia_pc_change))

neb_phones_2015 = total_cell_phones.columns['cell_phones_round'][0]
neb_phones_2006 = total_cell_phones.columns['cell_phones_round'][9]
neb_pc_change = (neb_phones_2015 - neb_phones_2006)/neb_phones_2006

print("Neb percent change in cell phones for 2015, {}, to 2006 {} is {}".format(neb_phones_2015, neb_phones_2006, neb_pc_change))

neb_phones_2001 = total_cell_phones.columns['cell_phones_round'][14]
neb_pc_change_total = (neb_phones_2015 - neb_phones_2001)/neb_phones_2001

print("Neb percent change in cell phones for 2015, {}, to 2001 {} is {}".format(total_cell_phones.columns['cell_phones_round'][0], total_cell_phones.columns['cell_phones_round'][14], neb_pc_change_total))

"""
Explanation: I wanted to calculate some stats by just grabing the amounts from the table
End of explanation
"""


neb_pop_15 = 1896190
neb_pop_06 = 1796619
ia_pop_15 = 3123899
ia_pop_06 = 2982644

neb_pc_cell15 = (neb_phones_2015/neb_pop_15)
neb_pc_cell06 = (neb_phones_2006/neb_pop_06)
ia_pc_cell15 = (ia_phones_2015/ia_pop_15)
ia_pc_cell06 = (ia_phones_2006/ia_pop_06)

print('Neb cell 15 {}, neb cell 06 {}, ia cell 15 {}, ia cell 06 {}'.format(neb_pc_cell15, neb_pc_cell06, ia_pc_cell15, ia_pc_cell06))

total_cell_phones.to_csv('clean_cell_phone_fund.csv')

json_table = total_cell_phones.select(['date', 'cell_phones_round', 'ia_cell_phones_round'])
json_table.to_json('cellphones.js')

"""
Explanation: Calculate the percent cell phone numbers with previously estimated cell phones and US census pop estimates
End of explanation
"""


