#!/usr/bin/env python
# encoding: utf-8
"""
Title: SIMO demo cash flow model implementations

*deterministic_prices.py*


Copyright (c) 2007 Simosol Oy. Distributed under the GNU General Public License 2.0.
"""

import math
import numpy


# CONSTANTS
# cash flow table classifiers for timber (species, assortment)
PINELOG = (1,1)
SPRUCELOG = (2,1)
BIRCHLOG1 = (3,1)
BIRCHLOG2 = (4,1)
PINEPULP = (1,2)
SPRUCEPULP = (2,2)
BIRCHPULP1 = (3,2)
BIRCHPULP2 = (4,2)
# cash flow table classifiers for biomass (species, bm_assortment)
PINESTUMP_ROOTS = (1,1)
PINEBRANCH = (1,2)
PINEFOLIAGE = (1,3)
SPRUCESTUMP_ROOTS = (2,1)
SPRUCEBRANCH = (2,2)
SPRUCEFOLIAGE = (2,3)
BIRCHSTUMP_ROOTS = (3,1)
BIRCHBRANCH = (3,2)
BIRCHFOLIAGE = (3,3)
BIRCH2STUMP_ROOTS = (4,1)
BIRCH2BRANCH = (4,2)
BIRCH2FOLIAGE = (4,3)


def long_term_timber_prices(table, date, iteration, variables, parameters,
                            errors):
    """Calculate long term predictions for timber and biomass prices"""
    
    ret = True
    
    # model has been fitted so that independent variable is years since 1986
    x = date.year - 1986

    if 'bm_assortment' in table.classifiers:
        #prices of bm_assortments
        #for all the biomass assortments price development (increasing price)
        #same as for pine log
        index = (0,1)

        # pine stump and roots
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, PINESTUMP_ROOTS)
        table.set_values(index, PINESTUMP_ROOTS, A)
        
        # pine branch
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, PINEBRANCH)
        table.set_values(index, PINEBRANCH, A)
        
        # pine foliage
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, PINEFOLIAGE)
        table.set_values(index, PINEFOLIAGE, A)
        
        # spruce stump and roots
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, SPRUCESTUMP_ROOTS)
        table.set_values(index, SPRUCESTUMP_ROOTS, A)
        
        # spruce branch
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, SPRUCEBRANCH)
        table.set_values(index, SPRUCEBRANCH, A)
        
        # spruce foliage
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, SPRUCEFOLIAGE)
        table.set_values(index, SPRUCEFOLIAGE, A)
        
        # birch (pendula) stump and roots
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, BIRCHSTUMP_ROOTS)
        table.set_values(index, BIRCHSTUMP_ROOTS, A)
        
        # birch (pendula) branch
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, BIRCHBRANCH)
        table.set_values(index, BIRCHBRANCH, A)
        
        # birch (pendula) foliage
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, BIRCHFOLIAGE)
        table.set_values(index, BIRCHFOLIAGE, A)
        
        # birch (pubescens) stump and roots
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, BIRCH2STUMP_ROOTS)
        table.set_values(index, BIRCH2STUMP_ROOTS, A)
        
        # birch (pubescens) branch
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, BIRCH2BRANCH)
        table.set_values(index, BIRCH2BRANCH, A)
        
        # birch (pubescens) foliage
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, BIRCH2FOLIAGE)
        table.set_values(index, BIRCH2FOLIAGE, A)
        
    else:    
        #prices of timber assortments
        index = (1,2)
    
        # pine log
        A = (13.093 * math.log(x) + 73.823) / 100 * table.get_values(index, PINELOG)
        table.set_values(index, PINELOG, A)
    
        # spruce log
        A = (20.932 * math.log(x) + 43.205) / 100 * table.get_values(index, SPRUCELOG)
        table.set_values(index, SPRUCELOG, A)
    
        # birch log (pendula)
        A = (4.4173 * math.log(x) + 93.344) / 100 * table.get_values(index, BIRCHLOG1)
        table.set_values(index, BIRCHLOG1, A)
    
        # birch log (pubescens)
        A = (4.4173 * math.log(x) + 93.344) / 100 * table.get_values(index, BIRCHLOG2)
        table.set_values(index, BIRCHLOG2, A)
    
        # pine pulp
        A = (-7.2846 * math.log(x) + 132.3) / 100 * table.get_values(index, PINEPULP)
        table.set_values(index, PINEPULP, A)
    
        # spruce pulp
        A = (5.7557 * math.log(x) + 86.9) / 100 * table.get_values(index, SPRUCEPULP)
        table.set_values(index, SPRUCEPULP, A)
    
        # birch pulp (pendula)
        A = (-1.8117 * math.log(x) + 120.91) / 100 * table.get_values(index, BIRCHPULP1)
        table.set_values(index, BIRCHPULP1, A)
    
        # birch pulp (pubescens)
        A = (-1.8117 * math.log(x) + 120.91) / 100 * table.get_values(index, BIRCHPULP2)
        table.set_values(index, BIRCHPULP2, A)

    return ret


def main():
    pass

if __name__ == '__main__':
    main()

