#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  ploty.py
#  
#  Copyright 2016 rob <rob@Novu>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

import numpy as np
import matplotlib
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from pylab import text
import wikipedia
from pylab import savefig, rcParams


'''some plot globals'''
bbox = dict(boxstyle="round,pad=0.3", fc="white", ec="k", lw=0.1)
matplotlib.style.use("ggplot")

rcParams.update({'figure.autolayout': True})
matplotlib.rc('font', family='DejaVu Sans')



def main(data_list):    
    """WorldClim.org now with 20km resolution / 1 km would be possible """
    fig = plt.figure(figsize = (15,7), facecolor="0.92", edgecolor='k')
    #ax = fig.add_subplot(111)
    font = 10
    Title = ("Improved RothC SOC Turnover Model // Baseline // Ferralsol Novo Progresseo")    
    fig.suptitle(Title, fontsize=11, y=1 )
    
    ax1 = plt.subplot2grid((2,4), (0, 0), rowspan=1, colspan=1)
    ax2 = plt.subplot2grid((2,4), (1, 0), rowspan=1, colspan=1)
    ax3 = plt.subplot2grid((2,4), (0, 1), rowspan=1, colspan=1)
    ax4 = plt.subplot2grid((2,4), (1, 1), rowspan=1, colspan=1)
    ax5 = plt.subplot2grid((2,4), (0, 2), rowspan=1, colspan=2)
    ax6 = plt.subplot2grid((2,4), (1, 2), rowspan=1, colspan=2)

    plt.subplots_adjust(bottom=0.85)

    #make empty links to append the y data
    rpm_list = []
    dpm_list = []
    bio_list = []
    hum_list = []
    co2_list = []
    x_list = [] 
    
    # walk through the data and append the data
    value = "Y_"
    for i in data_list: 
        if i["pool"] == "rpm":
            rpm_list.append(i[value])
        if i["pool"] == "dpm":
            dpm_list.append(i[value])
        if i["pool"] == "bio":
            bio_list.append(i[value])
        if i["pool"] == "hum":
            hum_list.append(i[value])
        if i["pool"] == "co2":
            co2_list.append(i[value])
            x_list.append(np.datetime64(i["datetime"]).astype(datetime))    
    
    
    # sump up all lists to get total SOC
    full_list = [sum(x) for x in zip(rpm_list, dpm_list, hum_list, bio_list)]
    
    ax1.plot(x_list, rpm_list, "#616161")
    ax1.set_ylabel((r'Mg SOC ha$^-$' +  r'$^1$' ))
    ax1.set_title("RPM pool", fontsize = font)

    ax2.plot(x_list , dpm_list, "#424242")
    ax2.set_title("DPM pool", fontsize = font)
    ax2.set_ylabel((r'Mg SOC ha$^-$' +  r'$^1$' ))
    ax2.set_xlabel("year")
    
    ax3.plot(x_list, hum_list, "#424242")
    ax3.set_title("HUM pool", fontsize = font)
    
    ax4.plot(x_list, bio_list, "#616161")
    ax4.set_title("BIO pool", fontsize = font)
    ax4.set_xlabel("year")

    ax5.plot(x_list, full_list, "#616161")
    ax5.set_title("SOC combined", fontsize = font)

    ax6.plot(x_list, co2_list, "#424242")
    ax6.set_title("C02", fontsize = font)
    ax6.set_xlabel("year")
    
    plt.show()
    savefig("fig.png", bbox_inches="tight")

    return 0

if __name__ == '__main__':
	main()

