Newer
Older
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime
class AnalysisTool:
"""Compare data from different cropsites by croptype"""
def __init__(self):
self.data = {}
def load_cropsite(self, cropsite_object, cropsite_name):
self.data[cropsite_name] = cropsite_object.data
"""Plot relative deltas of different cropsites for a give croptype"""
colors = ['#134e6f', '#ffa822', '#ff6150', '#1ac0c6']
data = pd.DataFrame(columns=['Date', 'Month', 'Cropsite', 'Delta'])
start_month = 12
all_months = {}
for cropsite in self.data.keys():
if croptype in self.data[cropsite]['cropwise'].keys():
for season in self.data[cropsite]['cropwise'][croptype]:
dates = self.data[cropsite]['cropwise'][croptype][season]['dates']
deltas = self.data[cropsite]['cropwise'][croptype][season]['deltas_rel']['MODIS']
months = []
cropsites = []
first_month = dates[0].month
if first_month < start_month:
start_month = first_month
for date in dates:
months.append(date.strftime('%b'))
cropsites.append(cropsite)
df = pd.DataFrame(list(zip(dates, months, cropsites, np.array(deltas)*100)), columns=['Date', 'Month', 'Cropsite', 'Delta'])
data = pd.concat([data, df])
months = []
for i in range(start_month, start_month + 12):
if i > 12:
i = i - 12
month = datetime.strptime(str(i), '%m').strftime('%b')
if month in data.values:
months.append(month)
axs = sns.boxplot(x='Month', y='Delta', hue='Cropsite', palette=colors, data=data, order=months)
axs.set_title(croptype)
axs.set_ylabel('Relative Difference in Latent Heat Flux [%]')
try:
axs.axvline(months.index('Jan'), color='lightgrey', linestyle='--')
except ValueError:
pass
axs.axhline(0, color='black', linestyle='-')
def scatterplot(self, croptype, cropsite='all', combine=True, save=False):
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Plot latent heat values: MODIS vs FLUXNET"""
colors = ['#134e6f', '#ffa822', '#ff6150', '#1ac0c6']
fig, axs = plt.subplots(figsize=(10, 10))
if cropsite != 'all':
if cropsite in self.data.keys():
if croptype in self.data[cropsite]['cropwise'].keys():
data = self.data[cropsite]['cropwise'][croptype]
if combine == True:
x, y = [], []
for season in data.keys():
x += data[season]['values']['MODIS']
y += data[season]['values']['FLUXNET']
axs.scatter(x, y, alpha=0.1, color=colors[0])
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x), color=colors[0], label=cropsite + ' ' + str(list(data.keys())))
else:
for season in data.keys():
x = data[season]['values']['MODIS']
y = data[season]['values']['FLUXNET']
axs.scatter(x, y, alpha=0.1)
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x), label=cropsite + ' ' + str(season))
else:
raise ValueError('Invalid cropsite')
else:
for n, cropsite in enumerate(self.data.keys()):
if croptype in self.data[cropsite]['cropwise'].keys():
data = self.data[cropsite]['cropwise'][croptype]
if combine == True:
x, y = [], []
for season in data.keys():
x += data[season]['values']['MODIS']
y += data[season]['values']['FLUXNET']
axs.scatter(x, y, alpha=0.1, color=colors[n])
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x), color=colors[n], label=cropsite + ' ' + str(list(data.keys())))
else:
for season in data.keys():
x = data[season]['values']['MODIS']
y = data[season]['values']['FLUXNET']
axs.scatter(x, y, alpha=0.1)
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x), color=colors[n], label=cropsite + ' ' + str(season))
axs.set_title(croptype)
axs.set_xlabel('MODIS Latent Heat Flux [W m-2]')
axs.set_ylabel('FLUXNET Latent Heat Flux [W m-2]')
plt.legend()