# -*- coding: utf-8 -*-
"""
Created on Tue Mar 29 15:29:46 2022

@author: Mark B
"""

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

df = pd.read_csv(r"~\Documents\R\Adjusted.dat","\s+")

mask = df.uah == df.uah

p = np.polyfit(df.t[mask], df["uah"][mask], 1)
pLast6 = np.polyfit(df.t[mask][-6*12:], df["uah"][mask][-6*12:], 1)
pLast6raw = np.polyfit(df.t[mask][-6*12:], df["uah.raw"][mask][-6*12:], 1)

plt.clf()
plt.plot(df.t, df["uah.raw"], '-y',
         df.t, df["uah"], '-c', 
         df.t[mask], np.polyval(p,df.t[mask]), '-b', 
         df.t[-6*12:], np.polyval(pLast6raw,df.t[-6*12:]), ':r',
         df.t[-6*12:], np.polyval(pLast6,df.t[-6*12:]), '-r')
plt.title("UAH TLT Adjusted per Foster/Rahmdorf 2011")
plt.grid(axis='both')
plt.ylabel("Anomaly (C)")
plt.xlabel("Year")
plt.legend(["UAH TLT raw", "UAH TLT adjusted", 
            "OLS fit ({0:5.2f} C/decade)".format(10*p[0]),
            "Raw trend last 6 years ({0:5.2f} C/decade)".format(10*pLast6raw[0]),
            "OLS fit last 6 years ({0:5.2f} C/decade)".format(10*pLast6raw[0])] )
plt.title("UAH TLT Adjusted per Foster/Rahmstorf 2011")