-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_relation_plots_trends.py
424 lines (298 loc) · 15.4 KB
/
merge_relation_plots_trends.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 31 11:00:01 2019
This part of the code will create
trends plots for each product,
LAI, LST, EVAPO, ALBEDO pairing each dependency
library seaborn
@author: moparthys
"""
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from pandas.plotting import scatter_matrix
import cartopy.crs as ccrs
from mpl_toolkits.basemap import Basemap
import xarray as xr
import matplotlib as mpl
import h5py
from scipy.stats import gaussian_kde
from matplotlib.colors import Normalize
from matplotlib import cm
from matplotlib import gridspec
from scipy import stats
'''Merged albedo MDAL NRT, ICARE, MDAL_R '''
savefile='./output_plots/mergertrends/merger_trends.png'
cdpzn_2004='/cnrm/vegeo/SAT/CODES_suman/From_Suman/clean_trends/output_cci_chunks/process_weights_two_2004'
#cdpzn_2005='/cnrm/vegeo/SAT/CODES/From_Suman/prepare_seviri_projection/process_weights_two_2005'
cdpzn_2018='/cnrm/vegeo/SAT/CODES_suman/From_Suman/clean_trends/output_cci_chunks/process_weights_two_2018'
nc_output=Dataset(cdpzn_2004+'/Merger_cci_scores_2004_new.nc','r')
trend_bb_2004=nc_output.variables['chunk_scores_bb'][:]
trend_needle_2004=nc_output.variables['chunk_scores_needle'][:]
nc_output.close()
trend_bb_2004[trend_bb_2004==0]=np.NaN
trend_needle_2004[trend_needle_2004==0]=np.NaN
nc_output=Dataset(cdpzn_2018+'/Merger_cci_scores_2018.nc','r')
trend_bb_2018=nc_output.variables['chunk_scores_bb'][:]
trend_needle_2018=nc_output.variables['chunk_scores_needle'][:]
nc_output.close()
trend_bb_2018[trend_bb_2018==0]=np.NaN
trend_needle_2018[trend_needle_2018==0]=np.NaN
diff_needle=trend_needle_2004-trend_needle_2018
diff_bb=trend_bb_2004-trend_bb_2018
'''Load LAT and LON here '''
work_dir_msg='./input_ancillary/'
# Reading lat from MSG disk
latmsg=h5py.File(work_dir_msg+'HDF5_LSASAF_MSG_LAT_MSG-Disk_4bytesPrecision','r')
lat_MSG=latmsg['LAT'][:]
lat_MSG=np.array(lat_MSG,dtype='float')
lat_MSG[lat_MSG==910000]=np.nan
lat_MSG=lat_MSG*0.0001
lonmsg=h5py.File(work_dir_msg+'HDF5_LSASAF_MSG_LON_MSG-Disk_4bytesPrecision','r')
lon_MSG=lonmsg['LON'][:]
lon_MSG=np.array(lon_MSG,dtype='float')
lon_MSG[lon_MSG==910000]=np.nan
lon_MSG=lon_MSG*0.0001
lon_MSG[np.isnan(lon_MSG)]=91
lat_MSG[np.isnan(lat_MSG)]=91
Dataset_trends=np.empty([3712*3712,4])
'''Albedo trends '''
'''start with 50% change and slowly decrease it till you get atleast 1000 points for comparison '''
nc_output=Dataset('./trends_together/Merger_TRND_ALBEDO_Final.nc','r')
trend_pval=nc_output.variables['chunk_scores_p_val'][:]
trend_zval=nc_output.variables['chunk_scores_z_val'][:]
trend_len=nc_output.variables['chunk_scores_length'][:]
trend_sn=nc_output.variables['chunk_scores_Sn_val'][:]
nc_output.close()
trend=trend_sn.copy()
trend[trend==999]=np.NaN
trend[trend_pval>0.05]=np.NaN
#trend_copy=trend[440:475,1825:1850].copy() # Landes Forest
trend_copy=trend.copy() # Landes Forest
#trend_copy[indx_cci_plot]=np.NaN
#Dataset_trends=np.empty([trend_copy.shape[0]*trend_copy.shape[1],4])
trend_copy=np.reshape(trend_copy,[trend_copy.shape[0]*trend_copy.shape[1]])
Dataset_trends[:,0]=trend_copy*365
'''LAI Daily trends'''
nc_output=Dataset('./trends_together/Merger_TRND_LAI_Final.nc','r')
trend_pval=nc_output.variables['chunk_scores_p_val'][:]
trend_zval=nc_output.variables['chunk_scores_z_val'][:]
trend_len=nc_output.variables['chunk_scores_length'][:]
trend_sn=nc_output.variables['chunk_scores_Sn_val'][:]
nc_output.close()
trend=trend_sn.copy()
trend[trend==999]=np.NaN
trend[trend_pval>0.05]=np.NaN
#trend_copy=trend[440:475,1825:1850].copy() # Landes Forest
trend_copy=trend.copy() # Landes Forest
#trend_copy[indx_cci_plot]=np.NaN
trend_copy=np.reshape(trend_copy,[trend_copy.shape[0]*trend_copy.shape[1]])
Dataset_trends[:,1]=trend_copy*365
'''LST 13UTC trends'''
nc_output=Dataset('./trends_together/Merger_TRND_LST_Final.nc','r')
'''Replace this file by LST; hourly vise computed files '''
trend_pval=nc_output.variables['chunk_scores_p_val'][:]
trend_zval=nc_output.variables['chunk_scores_z_val'][:]
trend_len=nc_output.variables['chunk_scores_length'][:]
trend_sn=nc_output.variables['chunk_scores_Sn_val'][:]
nc_output.close()
trend=trend_sn.copy()
trend[trend==999]=np.NaN
trend[trend_pval>0.05]=np.NaN
#trend_copy=trend[440:475,1825:1850].copy() # Landes Forest
trend_copy=trend.copy() # Landes Forest
#trend_copy[indx_cci_plot]=np.NaN
trend_copy=np.reshape(trend_copy,[trend_copy.shape[0]*trend_copy.shape[1]])
Dataset_trends[:,2]=trend_copy*365
'''Evapotranspiration trends'''
nc_output=Dataset('./trends_together/Merger_TRND_EVAPO_Final.nc','r')
trend_pval=nc_output.variables['chunk_scores_p_val'][:]
trend_zval=nc_output.variables['chunk_scores_z_val'][:]
trend_len=nc_output.variables['chunk_scores_length'][:]
trend_sn=nc_output.variables['chunk_scores_Sn_val'][:]
nc_output.close()
trend=trend_sn.copy()
trend[trend==999]=np.NaN
trend[trend_pval>0.05]=np.NaN
#trend_copy=trend[440:475,1825:1850].copy() # Landes Forest
trend_copy=trend.copy() # Landes Forest
#trend_copy[indx_cci_plot]=np.NaN
trend_copy=np.reshape(trend_copy,[trend_copy.shape[0]*trend_copy.shape[1]])
Dataset_trends[:,3]=trend_copy*365
'''Relation plots here '''
Dataset_trends_cci_based=Dataset_trends.copy()
#diff_cci=diff_bb.copy()[440:475,1825:1850]
diff_cci=diff_needle.copy()
#diff_cci=np.absolute(diff_bb)
diff_cci=np.reshape(diff_cci,[3712*3712])
#diff_cci=np.reshape(diff_cci,[35*25])
indx_cci_loss=np.where(diff_cci>=30)
trend_cci_2004=np.reshape(trend_needle_2004,[3712*3712])
trend_cci_2018=np.reshape(trend_needle_2018,[3712*3712])
trend_cci_concate_loss = np.array([trend_cci_2004[indx_cci_loss], trend_cci_2018[indx_cci_loss]])
average_cci_loss=np.nanmean(trend_cci_concate_loss, axis=0)
indx_cci_gain=np.where(diff_cci<=-30)
trend_cci_concate_gain = np.array([trend_cci_2004[indx_cci_gain], trend_cci_2018[indx_cci_gain]])
average_cci_gain=np.nanmean(trend_cci_concate_gain, axis=0)
#Dataset_trends_cci_based[indx_cci_plot[0],:]=np.NaN
hlw=h5py.File('./input_ancillary/HDF5_LSASAF_USGS-IGBP_LWMASK_MSG-Disk_201610171300','r')
#lwmsk=hlw['LWMASK'][:][440:475,1825:1850]
lwmsk=hlw['LWMASK'][:]
lwmsk=np.reshape(lwmsk,[3712*3712])
#lwmsk=np.reshape(lwmsk,[35*25])
find_ocean=np.where(lwmsk==0)
find_land=np.where(lwmsk==1)
Dataset_trends_cci_based[find_ocean[0],:]=np.NaN
hangle_view_zen=h5py.File('./input_ancillary/GEO_L1B-ANGLES-MSG2_2012-06-15T13-00-00_V1-00.hdf5','r')
#view_zenith=hangle_view_zen['View_Zenith'][:][440:475,1825:1850]
view_zenith=hangle_view_zen['View_Zenith'][:]
view_zenith=np.array(view_zenith,dtype='float')
view_zenith[view_zenith==65535]=np.NaN
view_zenith=view_zenith*0.01
view_zenith=np.reshape(view_zenith,[3712*3712])
#view_zenith=np.reshape(view_zenith,[35*25])
Dataset_trends_cci_based[view_zenith>75,:]=np.NaN
lat_MSG=np.reshape(lat_MSG,[3712*3712])
#lon_MSG=np.reshape(lon_MSG,[3712*3712])
#indx_lat_lon=np.where(np.logical_and(np.logical_or(lat_MSG<43,lat_MSG>45),np.logical_or(lon_MSG<-1,lon_MSG>1.3)))
#indx_lat=np.where(np.logical_or(lat_MSG<42.5,lat_MSG>51))
#indx_lon=np.where(np.logical_or(lon_MSG<-4.5,lon_MSG>8))
#Dataset_trends_cci_based[indx_lat,:]=np.NaN
#Dataset_trends_cci_based[indx_lon,:]=np.NaN
#Dataset_trends_cci_based[lat_MSG<=60,:]=np.NaN
'''high light this when points or pixels needed lat > 60 '''
Dataset_trends_cci_based[lat_MSG>60,:]=np.NaN
'''high light this when points or pixels needed lat <=60 '''
df=pd.DataFrame(Dataset_trends_cci_based,columns=['AL-BB-DH','LAI','LST','Evapotranspiration'])
fig = plt.figure(figsize=(32, 24) )
gs = gridspec.GridSpec(4, 4, width_ratios=[6, 6, 6, 6])
positions_subplot=np.array([0,4,8,12])
for i in range(4):
for j in range(4):
'''plot relation between 4 variables and close the subplot; use different colors for cci class '''
ax = plt.subplot(gs[i,j])
x=df.values[:,i]
y=df.values[:,j]
labelx=df.columns[i]
labely=df.columns[j]
# ax.plot(x,y,'.k',markersize=1.5)
# ax.plot(x[indx_cci_gain],y[indx_cci_gain],'s',markersize=8.0)
# ax.plot(x[indx_cci_loss],y[indx_cci_loss],'o',markersize=8.0)
# c1=ax.scatter(x[indx_cci_gain],y[indx_cci_gain],marker='s', edgecolors='b',c=average_cci_gain, alpha=1, s=20, cmap='jet',vmin=30, vmax=100)
# c2=ax.scatter(x[indx_cci_loss],y[indx_cci_loss],marker='o', edgecolors='r',c=average_cci_loss, alpha=1, s=20, cmap='jet',vmin=30, vmax=100)
c1=ax.scatter(x[indx_cci_gain],y[indx_cci_gain], marker='s', edgecolors='cyan', s=30, c=average_cci_gain, cmap='jet', vmin=30, vmax=100)
# c1.set_facecolor('none')
# closs= plt.cm.jet(average_cci_loss)
c2=ax.scatter(x[indx_cci_loss],y[indx_cci_loss], marker='o', s=30, c=average_cci_loss, cmap='jet', vmin=30, vmax=100)
c2.set_facecolor('none')
# c1=ax.plot(x[indx_cci_gain],y[indx_cci_gain],marker='s', markeredgecolor=average_cci_gain,markerfacecolor='cyan', alpha=1, markersize=10, cmap='jet',vmin=30, vmax=100)
# c2=ax.plot(x[indx_cci_loss],y[indx_cci_loss],marker='o', markeredgecolor=average_cci_loss,markerfacecolor='None', alpha=1, markersize=10, cmap='jet',vmin=30, vmax=100)
# ax.colorbar(c1)
ax.set_xlabel(labelx,fontsize=14)
ax.set_ylabel(labely,fontsize=14)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.axhline(0, color='k')
ax.axvline(0, color='k')
if df.columns[i]=='AL-BB-DH':
ax.set_xlim([-0.005,0.005])
if df.columns[j]=='AL-BB-DH':
ax.set_ylim([-0.005,0.005])
vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg2=np.arange(-0.005,0.005+0.0005,0.0005)
if df.columns[j]=='LST':
ax.set_ylim([-1,1])
vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg2=np.linspace(-1,1,len(vals_reg1))
if df.columns[j]=='LAI':
ax.set_ylim([-0.1,0.1])
vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg2=np.linspace(-0.1,0.1,len(vals_reg1))
if df.columns[j]=='Evapotranspiration':
# ax.set_ylim([-0.005,0.005])
vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
# vals_reg2=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg2=np.linspace(np.nanmin(y),np.nanmax(y)+0.0005,len(vals_reg1))
if df.columns[i]=='LST':
ax.set_xlim([-1,1])
if df.columns[j]=='AL-BB-DH':
ax.set_ylim([-0.005,0.005])
vals_reg1=np.arange(-1,1+0.1,0.1)
vals_reg2=np.linspace(-0.005,0.005,len(vals_reg1))
if df.columns[j]=='LST':
ax.set_ylim([-1,1])
vals_reg1=np.arange(-1,1.1,0.1)
vals_reg2=np.arange(-1,1.1,0.1)
if df.columns[j]=='LAI':
ax.set_ylim([-0.1,0.1])
vals_reg1=np.arange(-1,1.1,0.1)
vals_reg2=np.linspace(-0.1,0.1,len(vals_reg1))
if df.columns[j]=='Evapotranspiration':
# ax.set_ylim([-0.005,0.005])
vals_reg1=np.arange(-1,1.1,0.1)
# vals_reg2=np.linspace(-0.005,0.005,len(vals_reg1))
vals_reg2=np.linspace(np.nanmin(y),np.nanmax(y)+0.0005,len(vals_reg1))
if df.columns[i]=='LAI':
ax.set_xlim([-0.1,0.1])
if df.columns[j]=='AL-BB-DH':
ax.set_ylim([-0.005,0.005])
vals_reg1=np.arange(-0.1,0.1+0.01,0.01)
vals_reg2=np.linspace(-0.005,0.005,len(vals_reg1))
if df.columns[j]=='LST':
ax.set_ylim([-1,1])
vals_reg1=np.arange(-0.1,0.1+0.01,0.01)
vals_reg2=np.linspace(-1,1,len(vals_reg1))
if df.columns[j]=='LAI':
ax.set_ylim([-0.1,0.1])
vals_reg1=np.arange(-0.1,0.1+0.01,0.01)
vals_reg2=np.arange(-0.1,0.1+0.01,0.01)
if df.columns[j]=='Evapotranspiration':
# ax.set_ylim([-0.005,0.005])
vals_reg1=np.arange(-0.1,0.1+0.01,0.01)
# vals_reg2=np.linspace(-0.005,0.005,len(vals_reg1))
vals_reg2=np.linspace(np.nanmin(y),np.nanmax(y)+0.0005,len(vals_reg1))
if df.columns[i]=='Evapotranspiration':
# ax.set_xlim([-0.005,0.005])
if df.columns[j]=='AL-BB-DH':
ax.set_ylim([-0.005,0.005])
# vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg1=np.arange(np.nanmin(x),np.nanmax(x)+0.0005,0.0005)
# vals_reg2=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg2=np.linspace(-0.005,0.005+0.0005,len(vals_reg1))
if df.columns[j]=='LST':
ax.set_ylim([-1,1])
# vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg1=np.arange(np.nanmin(x),np.nanmax(x)+0.0005,0.0005)
vals_reg2=np.linspace(-1,1,len(vals_reg1))
if df.columns[j]=='LAI':
ax.set_ylim([-0.1,0.1])
# vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg1=np.arange(np.nanmin(x),np.nanmax(x)+0.0005,0.0005)
vals_reg2=np.linspace(-0.1,0.1,len(vals_reg1))
if df.columns[j]=='Evapotranspiration':
# ax.set_ylim([-0.005,0.005])
# vals_reg1=np.arange(-0.005,0.005+0.0005,0.0005)
# vals_reg2=np.arange(-0.005,0.005+0.0005,0.0005)
vals_reg1=np.arange(np.nanmin(y),np.nanmax(y)+0.0005,0.0005)
vals_reg2=np.arange(np.nanmin(y),np.nanmax(y)+0.0005,0.0005)
nanindx=np.where(np.logical_and(np.isnan(x[indx_cci_gain])!=1,np.isnan(y[indx_cci_gain])!=1))
xxgain=x[indx_cci_gain][nanindx]
yygain=y[indx_cci_gain][nanindx]
slope_CI, intercept_CI, r_value_CI, p_value_CI, std_err_CI = stats.linregress(xxgain, yygain)
ax.plot(vals_reg1, slope_CI*vals_reg1+intercept_CI,'b-',linewidth=2.0)
nanindx=np.where(np.logical_and(np.isnan(x[indx_cci_loss])!=1,np.isnan(y[indx_cci_loss])!=1))
xxloss=x[indx_cci_loss][nanindx]
yyloss=y[indx_cci_loss][nanindx]
slope_CI, intercept_CI, r_value_CI, p_value_CI, std_err_CI = stats.linregress(xxloss, yyloss)
ax.plot(vals_reg1, slope_CI*vals_reg1+intercept_CI,'r-',linewidth=2.0)
ax.plot(vals_reg1,vals_reg2,'g--',linewidth=2.0) # Draw a reference line from (0,0) to (1,1)
ax.tick_params(axis='both', which='minor', labelsize=8)
# sm=scatter_matrix(df, alpha = 0.2, figsize = (12, 12), diagonal='kde',color="black")
# [s.xaxis.label.set_rotation(45) for s in sm.reshape(-1)]
# [s.yaxis.label.set_rotation(0) for s in sm.reshape(-1)]
# [s.get_yaxis().set_label_coords(-0.3,0.5) for s in sm.reshape(-1)]
plt.savefig(savefile,dpi=300)
#plt.savefig('/cnrm/vegeo/SAT/CODES/From_Suman/Trends_and_Time_series/trend_relation_merger_needle_leaves_cci_10_scatter_no_lat',dpi=300)
plt.close()