# -*- Mode: Python -*-

# historical return on equities of 11%

# convert annual yield to monthly yield
def per_month (p):
    return ((1 + p)**(1/12.))-1

eq_per_year = 0.11
eq_per_month = per_month (eq_per_year)

# according to this page: http://www.sungevity.com/solar-cost-savings
# california rates historically go up by 6.7% per year
rate_rise_per_year = 0.067
rate_rise_per_month = per_month (rate_rise_per_year)

def tiers_month (kwh):
    # given the pg&e tiers, how much will <kwh> cost.
    tiers = [
        (12, .11531),
        (3.6, .13109),
        (8.4, .25974),
        (12, .37866),
        (1000, .44098),
        ]
    total = 0.0
    for diem, cost in tiers:
        chunk = min (diem*30, kwh)
        total += cost * chunk
        kwh -= chunk
    return total

class scenario:
    def __init__ (self, name, base, cost, bill):
        # we start out with this amount of money
        self.base = base
        # cost of the solar system
        self.cost = cost
        # we're worth this much
        self.worth = base - cost
        # monthly bill is this
        self.bill = bill
        
    def one_month (self):
        # increase net worth by market rate
        self.worth += self.worth * eq_per_month
        # subtract the electric bill
        self.worth -= self.bill
        # adjust bill for rate rises
        self.bill += self.bill * rate_rise_per_month

    def pprint (self):
        print '%10.2f' % (self.worth,)

    def go (self, years=10):
        r = []
        for i in range (years):
            for j in range (12):
                self.one_month()
            r.append (self.worth)
        return r

# base is a made-up number to reflect a pool of money
#  that is either used to pay electric bills or pay
#  for a solar installation.

scenarios = [
    # name      base    cost   bill
    ('none',     300000,    0,  650),
    ('SP 100%',  300000, 89843,  40),
    ('SP 50%',   300000, 67000, 130),
    ('SP 43%',   300000, 38769, 300),
    ('SP 25%',   300000, 22461, 432),
    # the freewatt system, running 24hr/day, *ignoring the heat generated*!!
    # generates 836kW in a month, which would lower my electric bill to $282.
    # the gas would cost $160/mo (@$1.20/therm).
    ('freewatt', 300000,  6000, 282 + 160),
    ]

def go():
    import sys

    years = 10
    if len(sys.argv) > 1:
        years = int (sys.argv[1])

    results = []
    for s in scenarios:
        x = scenario (*s)
        results.append (x.go (years))

    sys.stdout.write ('yr ')
    for j in range (len (scenarios)):
        sys.stdout.write ('%10s ' % (scenarios[j][0]))
    sys.stdout.write ('\n')
    for i in range (len (results[0])):
        sys.stdout.write ('%2d ' % (i,))
        for j in range (len (scenarios)):
            sys.stdout.write ('%10.0f ' % (results[j][i]))
        sys.stdout.write ('\n')
        
go()
