#!/usr/bin/env python
# coding: utf-8

import numpy as np
import matplotlib.pyplot as plt
import pandas

def no2(filename, resample='hourly'):

    ts = pandas.DataFrame.from_csv(filename, sep='\t')

    # mask first day of simulations (spin-up)
    mask = ('2015-01-01' <= ts.index) & (ts.index < '2015-01-02')
    mask |= ('2015-07-01' <= ts.index) & (ts.index < '2015-07-02')
    ts[mask] = np.nan

    if resample == 'daily':
        ts = ts.resample('D').asfreq()
    elif resample == 'overpass':
        ts = ts[ts.index.hour == 11]

    ax = (ts/1e15).plot(y='NO2_BG_column_mean', label='background')
    ax = (ts/1e15).plot(y='NO2_TOT_column_mean', ax=ax, label='anthropogenic')

    ax.set_ylabel('NO$_2$ columns (10$^{15}$ molecules per cm$^2$)')

    if resample == 'daily':
        ax.set_ylim(0,8)
    else:
        ax.set_ylim(0,14)

    plt.grid(True)
    plt.tight_layout()
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/no2_columns_%s.png' % resample)
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/no2_columns_%s.pdf' % resample)


def co(filename, resample='hourly'):

    ts = pandas.DataFrame.from_csv(filename, sep='\t')

    # mask first day of simulations (spin-up)
    mask = ('2015-01-01' <= ts.index) & (ts.index < '2015-01-02')
    mask |= ('2015-07-01' <= ts.index) & (ts.index < '2015-07-02')
    ts[mask] = np.nan

    if resample == 'daily':
        ts = ts.resample('D').asfreq()
    elif resample == 'overpass':
        ts = ts[ts.index.hour == 11]



    ax = (ts/1e18).plot(y='CO_BG_column_mean', label='background')
    ax.set_ylabel('CO columns (10$^{18}$ molecules per cm$^2$)')
    ax.set_ylim(1,2.4)

    plt.grid(True)
    plt.tight_layout()
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/co_bg_%s.png' % resample)
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/co_bg_%s.pdf' % resample)


    ax = (ts/1e18).plot(y='CO_TOT_column_mean', label='anthropogenic')
    ax.set_ylabel('CO columns (10$^{18}$ molecules per cm$^2$)')
    ax.set_ylim(0,0.2)

    plt.grid(True)
    plt.tight_layout()
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/co_anthr_%s.png' % resample)
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/co_anthr_%s.pdf' % resample)



def xco2(filename, resample='hourly'):

    ts = pandas.DataFrame.from_csv(filename, sep='\t')

    if resample == 'daily':
        ts = ts.resample('D').asfreq()
    elif resample == 'overpass':
        ts = ts[ts.index.hour == 11]

    ax = ts.plot(y='XCO2_BG_mean', label='XCO$_2$ background')

    ax.set_ylabel('XCO$_2$ (ppm)')
    ax.set_ylim(390,408)

    plt.grid(True)
    plt.tight_layout()
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/xco2_bg_%s.png' % resample)
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/xco2_bg_%s.pdf' % resample)


    # XCO2 signals
    plt.figure()

    gpp = ts.ix[:,int(np.argwhere(ts.columns == 'XCO2_GPP_mean'))]
    gpp[gpp.index > '2015-10-12'] = np.nan

    ra = ts.ix[:,int(np.argwhere(ts.columns == 'XCO2_RA_mean'))]
    tot = ts.ix[:,int(np.argwhere(ts.columns == 'XCO2_TOT_mean'))]

    ax = tot.plot(label='anthropogenic')
    ax = (ra-gpp).plot(label='biospheric', ax=ax)


    ax.set_ylabel('XCO$_2$ signal (ppm)')
    ax.set_ylim(-2,2)

    plt.legend()
    plt.grid(True)
    plt.tight_layout()
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/xco2_signals_%s.png' % resample)
    plt.savefig('/users/gkuhl/projects/smartcarb/figures/ts/xco2_signals_%s.pdf' % resample)


if __name__ == '__main__':
    pass
