-
Notifications
You must be signed in to change notification settings - Fork 0
/
bav_lib.py
248 lines (206 loc) · 7.61 KB
/
bav_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 17 14:03:28 2019
@author: bav
"""
#%matplotlib qt
import numpy as np
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.axes_grid1 import make_axes_locatable
import pandas as pd
from scipy.stats import gaussian_kde
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score
import datetime
import matplotlib.dates as mdates
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
years_fmt = mdates.DateFormatter('%Y')
from math import radians, cos, sin, asin, sqrt
def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
"""
slightly modified version: of http://stackoverflow.com/a/29546836/2901002
Calculate the great circle distance between two points
on the earth (specified in decimal degrees or in radians)
All (lat, lon) coordinates must have numeric dtypes and be of equal length.
"""
if to_radians:
lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
a = np.sin((lat2-lat1)/2.0)**2 + \
np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
return earth_radius * 2 * np.arcsin(np.sqrt(a))
def stat_title(x,y,ax):
ind = np.logical_and(pd.notnull(x),pd.notnull(y))
x = x[ind]
y=y[ind]
x = x.values.reshape(-1,1)
y = y.values.reshape(-1,1)
lr = linear_model.LinearRegression()
lr.fit(x,y)
# print('Coefficients: \n', lr.coef_)
preds = lr.predict(x)
ax.set_title('R2=%.3f\nRMSE=%.2f\nN=%.0f' % (r2_score(y,preds),
mean_squared_error(y,preds),
len(x)))
return ax
#%%
def OutlookRaster(var,title):
l,b,r,t = var.bounds
res = var.res
x = np.arange(l,r, res[0])
y = np.arange(t,b, -res[0])
z=var.read(1)
nan_col = ~np.all(np.isnan(z), axis=0)
z=z[:,nan_col]
x=x[nan_col]
nan_row = ~np.all(np.isnan(z), axis=1)
z=z[nan_row,:]
y=y[nan_row]
fig = go.Figure(
data=go.Heatmap(x=x,
y=y,
z=z,
type = 'heatmap',
colorscale='Jet',
colorbar=dict(title=title),
showscale=True))
fig.update_layout(
autosize=False,
width=500,
height=500)
fig.show()
# fig.write_image(title+".jpeg")
return x,y,z
#%%
def heatmap(var, title='', col_lim=(np.nan, np.nan) ,cmap_in='gnuplot'):
if np.isnan(col_lim[0]):
col_lim=(np.nanmin(var), np.nanmax(var))
z=var
nan_col = ~np.all(np.isnan(z), axis=0)
z=z[:,nan_col]
nan_row = ~np.all(np.isnan(z), axis=1)
z=z[nan_row,:]
cmap = plt.get_cmap(cmap_in)
cmap.set_bad(color='gray')
# fig,ax = plt.subplots()
im = plt.imshow(z, cmap=cmap, interpolation='nearest',vmin=col_lim[0], vmax=col_lim[1])
cb= plt.colorbar(im)
cb.ax.set_ylabel(title)
# fig.show()
# fig.write_image(title+".jpeg")
return z
#%%
def heatmap_discrete(var, title='', col_lim=(np.nan, np.nan) ,cmap_in='gnuplot'):
if np.isnan(col_lim[0]):
col_lim=(np.nanmin(var), np.nanmax(var))
z=var
nan_col = ~np.all(np.isnan(z), axis=0)
z=z[:,nan_col]
nan_row = ~np.all(np.isnan(z), axis=1)
z=z[nan_row,:]
cmap = plt.get_cmap(cmap_in)
cmap.set_bad(color='gray')
bounds = np.unique(var)[np.logical_not(np.isnan(np.unique(var)))]
bounds = np.append(bounds, bounds[len(bounds)-1]+1)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N+1)
fig,ax = plt.subplots(figsize=(10,15))
im = ax.imshow(z+1e-6, cmap=cmap, norm = norm, interpolation='Nearest')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%" , pad= 0.1)
cb = mpl.colorbar.ColorbarBase(ax=cax, cmap=cmap, norm=norm)
tic_locs =bounds[0:len(bounds)-1] - (bounds[0:len(bounds)-1]-bounds[1:len(bounds)])/2
cb.set_ticks(tic_locs)
cb.ax.set_yticklabels(bounds[0:len(bounds)-1])
cb.ax.set_ylabel(title)
fig.show()
# fig.write_image(title+".jpeg")
return fig,ax
#%% Density scatter
def density_scatter(x,y,ax,ss):
if isinstance(x, pd.core.series.Series)==False:
x = pd.DataFrame(x)
x = x[0]
if isinstance(y, pd.core.series.Series)==False:
y = pd.DataFrame(y)
y=y[y.columns[0]]
y=y[~np.isnan(x)]
x=x[~np.isnan(x)]
x=x[~np.isnan(y)]
y=y[~np.isnan(y)]
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)
# Sort the points by density, so that the densest points are plotted last
idx = z.argsort()
x, y, z = x.iloc[idx], y.iloc[idx], z[idx]
ax.scatter(x, y, c=z, s=ss)
return ax
# =============================================================================
#
# =============================================================================
#%% Plotting top of atmosphere refletance
#%matplotlib qt
#%matplotlib inline
def mosaic_albedo_fit(df, Rad_in):
fig, ax = plt.subplots(7,3, sharex='col', sharey='row',figsize=(15, 15))
fig.subplots_adjust(hspace=0, wspace=0)
if Rad_in=='Rt':
fig.text(0.5, 0.05, 'Top of atmosphere OLCI reflectance', ha='center', size = 20)
elif Rad_in=='Rb':
fig.text(0.5, 0.05, 'Bottom of atmosphere OLCI reflectance', ha='center', size = 20)
fig.text(0.05, 0.5, 'PROMICE albedo', va='center', rotation='vertical', size = 20)
# axes are in a two-dimensional array, indexed by [row, col]
count=0
ss=5
for i in range(7):
for j in range(3):
# Calculate the point density
count = count+1
R=df[Rad_in+str(count)]
alb = df['PROMICE alb']
alb=alb[~np.isnan(R)]
R=R[~np.isnan(R)]
alb = alb[~(R>1.1)]
R = R[~(R>1.1)]
input_name= Rad_in+str(count)
density_scatter(R,alb,ax[i, j],ss)
R=R.values
alb=alb.values
R=R.reshape(-1,1)
alb=alb.reshape(-1,1)
lr = linear_model.LinearRegression()
lr.fit(R, alb)
print(Rad_in+str(count))
print('Coefficients: \n', lr.coef_)
preds = lr.predict(R)
tmp = np.array([np.min(R), np.max(R)])
tmp = tmp.reshape(-1,1)
ax[i, j].plot(tmp, lr.predict(tmp))
# x_ticks = np.linspace(0.25,1,4)
# ax[i, j].set_xticklabels(x_ticks,fontsize=20)
# ax[i, j].set_yticklabels(x_ticks,fontsize=20)
ax[i, j].text(0.08, 0.93,
'%s' % input_name,
fontsize=18,
color='black',
fontweight='bold',
horizontalalignment='left',
verticalalignment='top',
bbox=dict(boxstyle="square",
ec='lightskyblue',
alpha=0.8))
ax[i, j].text(0.7, 0.5,
'R2=%.3f\nRMSE=%.2f\nN=%.0f' % (r2_score(alb,preds), mean_squared_error(alb,preds),len(R)),
fontsize=13,
color='white',
fontweight='bold',
horizontalalignment='left',
verticalalignment='top',
bbox=dict(boxstyle="square",
ec='lightskyblue',
alpha=0.8))
ax[i, j].set_xlim([0.01, 1.05])
ax[i, j].set_ylim([0.01, 1.05])
ax[i, j].grid(True)
fig.savefig('./output/linear_'+Rad_in+'oa.png',bbox_inches='tight')