-
Notifications
You must be signed in to change notification settings - Fork 10
/
pcc_corra_rado.py
365 lines (273 loc) · 10.9 KB
/
pcc_corra_rado.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
"""
Einlesen und darstellen von GPM CORRA und Radolan Dateien
Radolanpfad:
"""
import h5py
import numpy as np
import matplotlib.pyplot as plt
import wradlib
import glob
from scipy import stats, linspace
import wradlib as wrl
from osgeo import osr
from pcc import get_time_of_gpm
from pcc import cut_the_swath
## Landgrenzenfunktion
## -------------------
from pcc import boxpol_pos
bonn_pos = boxpol_pos()
bx, by = bonn_pos['gkx_ppi'], bonn_pos['gky_ppi']
bonnlat, bonnlon = bonn_pos['lat_ppi'], bonn_pos['lon_ppi']
from pcc import plot_borders
from pcc import plot_radar
from pcc import get_miub_cmap
my_cmap = get_miub_cmap()
from pcc import get_my_cmap
my_cmap2 = get_my_cmap()
GGG = []
RRR = []
# Ref.Threshold nach RADOLAN_Goudenhoofdt_2016
TH_ref = 0.1#18#7
#zz = np.array([20141007, 20150227, 20150402, 20150427])
zz = np.array([20160607, 20160805, 20160904,20160917, 20161001, 20161024,20170113])
#zz = np.array(['20141007'])
#zz = np.array([20140609, 20140610, 20140629, 20140826, 20140921, 20141007,
# 20141016, 20150128, 20150227, 20150402, 20150427, 20160405,
# 20160607, 20160805, 20160904, 20160917, 20161001, 20161024,
# 20170113, 20170203,])
for i in range(len(zz)):
ZP = str(zz[i])
#year, m, d, ht, mt, st = ZP[0:4], ZP[4:6], ZP[6:8], ZP[8:10], ZP[10:12], ZP[12:14]
year, m, d = ZP[0:4], ZP[4:6], ZP[6:8]
ye = ZP[2:4]
## Read GPM Data
## -------------
pfad2 = ('/home/velibor/shkgpm/data/'+str(year)+str(m)+str(d)+'/corra/*.HDF5')
pfad_gpm = glob.glob(pfad2)
pfad_gpm_g = pfad_gpm[0]
gpmdpr = h5py.File(pfad_gpm_g, 'r')
gprof_lat=np.array(gpmdpr['NS']['Latitude'])
gprof_lon=np.array(gpmdpr['NS']['Longitude'])
gprof_pp=np.array(gpmdpr['NS']['surfPrecipTotRate'])
gprof_pp[gprof_pp==-9999.9]= np.nan
gpm_time = gpmdpr['NS']['ScanTime']
gpm_zeit = get_time_of_gpm(gprof_lon, gprof_lat, gpm_time)
ht, mt = gpm_zeit[14:16], str(int(round(float(gpm_zeit[17:19])/5.0)*5.0))
if mt == '0':
mt = '00'
elif mt == '5':
mt = '05'
print mt
print gpm_zeit
## Read RADOLAN Data
## -----------------
r_pro = 'rx'
pfad = ('/automount/radar/dwd/'+ r_pro +'/'+str(year)+'/'+str(year)+'-'+
str(m)+'/'+ str(year)+'-'+str(m)+'-'+str(d)+'/raa01-'+r_pro+'_10000-'+
str(ye)+str(m)+ str(d)+str(ht)+str(mt)+'-dwd---bin.gz')
pfad_radolan = pfad[:-3]
try:
rw_filename = wradlib.util.get_wradlib_data_file(pfad)
except EnvironmentError:
rw_filename = wradlib.util.get_wradlib_data_file(pfad_radolan)
rwdata, rwattrs = wradlib.io.read_RADOLAN_composite(rw_filename)
radolan_zeit = rwattrs['datetime'].strftime("%Y.%m.%d -- %H:%M:%S")
#Binaere Grid
rn = rwdata.copy()
rn[rn != -9999] = 1
rn[rn == -9999] = 0
radolan_grid_xy = wradlib.georef.get_radolan_grid(900,900)
x = radolan_grid_xy[:,:,0]
y = radolan_grid_xy[:,:,1]
rwdata = np.ma.masked_equal(rwdata, -9999) / 2 - 32.5#*12
#rwdata[rwdata < 0] = np.nan
## Cut the GPM Swath
## ------------------
blon, blat, gprof_pp_b = cut_the_swath(gprof_lon,gprof_lat,gprof_pp)
proj_stereo = wrl.georef.create_osr("dwd-radolan")
proj_wgs = osr.SpatialReference()
proj_wgs.ImportFromEPSG(4326)
gpm_x, gpm_y = wradlib.georef.reproject(blon, blat, projection_target=proj_stereo , projection_source=proj_wgs)
grid_xy = np.vstack((gpm_x.ravel(), gpm_y.ravel())).transpose()
## INTERLOLATION
## --------------
gk3 = wradlib.georef.epsg_to_osr(31467)
grid_gpm_xy = np.vstack((gpm_x.ravel(), gpm_y.ravel())).transpose()
xy = np.vstack((x.ravel(), y.ravel())).transpose()
mask = ~np.isnan(rwdata)
result = wrl.ipol.interpolate(xy, grid_gpm_xy, rwdata.reshape(900*900,1), wrl.ipol.Idw, nnearest=4)
result = np.ma.masked_invalid(result)
rrr = result.reshape(gpm_x.shape)
## Interpolation of the binary Grid
## ------------------------------
res_bin = wrl.ipol.interpolate(xy, grid_gpm_xy, rn.reshape(900*900,1), wrl.ipol.Idw, nnearest=4)
res_bin = res_bin.reshape(gpm_x.shape)
res_bin[res_bin!=0]= 1 # Randkorrektur
rand_y_unten = -4658.6447242655722
rand_y_oben = -3759.6447242655722
rand_x_rechts = 375.5378330781441
rrr[np.where(gpm_y < rand_y_unten)] = np.nan
rrr[np.where(gpm_y > rand_y_oben)] = np.nan
rrr[np.where(gpm_x > rand_x_rechts)] = np.nan
res_bin[np.where(gpm_y < rand_y_unten)] = np.nan
res_bin[np.where(gpm_y > rand_y_oben)] = np.nan
res_bin[np.where(gpm_x > rand_x_rechts)] = np.nan
res_bin[res_bin == 0] = np.nan #check nur 1 un NaN
ggg = gprof_pp_b * res_bin
## Nur Niederschlagsrelevante
Z = wradlib.trafo.idecibel(rwdata)
rwdata = wradlib.zr.z2r(Z, a=200., b=1.6)
Z2 = wradlib.trafo.idecibel(rrr)
rrr = wradlib.zr.z2r(Z2, a=200., b=1.6)
rrr[rrr < TH_ref]=np.nan
ggg[ggg < TH_ref]=np.nan
################################################################Swap!
#rrr, ggg = ggg, rrr
ff = 15
cc = 0.5
fig = plt.figure(figsize=(12,12))
ax1 = fig.add_subplot(221, aspect='equal')#------------------------------------
pm1 = plt.pcolormesh(x, y, rwdata, cmap=my_cmap, vmin=0.01, vmax=10, zorder=2)
plt.plot(gpm_x[:,0],gpm_y[:,0], color='black',lw=1)
plt.plot(gpm_x[:,-1],gpm_y[:,-1], color='black',lw=1)
cb = plt.colorbar(shrink=cc)
cb.set_label("RR (mm/h)",fontsize=ff)
cb.ax.tick_params(labelsize=ff)
plot_borders(ax1)
plot_radar(bonnlon, bonnlat, ax1, reproject=True)
plt.title('RADOLAN RR: \n'+ radolan_zeit + ' UTC',fontsize=ff)
plt.grid(color='r')
plt.tick_params(
axis='both',
which='both',
bottom='off',
top='off',
labelbottom='off',
right='off',
left='off',
labelleft='off')
plt.xlim(-420,390)
plt.ylim(-4700, -3700)
ax2 = fig.add_subplot(222, aspect='equal')#------------------------------------
pm2 = plt.pcolormesh(gpm_x, gpm_y,np.ma.masked_invalid(ggg),
cmap=my_cmap, vmin=0.01, vmax=10, zorder=2)
plt.plot(gpm_x[:,0],gpm_y[:,0], color='black',lw=1)
plt.plot(gpm_x[:,-1],gpm_y[:,-1], color='black',lw=1)
cb = plt.colorbar(shrink=cc)
cb.set_label("RR (mm/h)",fontsize=ff)
cb.ax.tick_params(labelsize=ff)
plt.title('GPM CORRA RR: \n'+ gpm_zeit + ' UTC',fontsize=ff)
plot_borders(ax2)
plot_radar(bonnlon, bonnlat, ax2, reproject=True)
plt.grid(color='r')
plt.tick_params(
axis='both',
which='both',
bottom='off',
top='off',
labelbottom='off',
right='off',
left='off',
labelleft='off')
plt.xlim(-420,390)
plt.ylim(-4700, -3700)
ax2 = fig.add_subplot(223, aspect='equal')#------------------------------------
pm3 = plt.pcolormesh(gpm_x, gpm_y,np.ma.masked_invalid(rrr),
cmap=my_cmap, vmin=0.01, vmax=10,zorder=2)
plt.plot(gpm_x[:,0],gpm_y[:,0], color='black',lw=1)
plt.plot(gpm_x[:,-1],gpm_y[:,-1], color='black',lw=1)
cb = plt.colorbar(shrink=cc)
cb.set_label("RR (mm/h)",fontsize=ff)
cb.ax.tick_params(labelsize=ff)
plt.title('RADOLAN RR Interpoliert: \n'+ radolan_zeit + ' UTC',fontsize=ff) #RW Product Polar Stereo
plot_borders(ax2)
plot_radar(bonnlon, bonnlat, ax2, reproject=True)
plt.grid(color='r')
plt.tick_params(
axis='both',
which='both',
bottom='off',
top='off',
labelbottom='off',
right='off',
left='off',
labelleft='off')
plt.xlim(-420,390)
plt.ylim(-4700, -3700)
ax4 = fig.add_subplot(224, aspect='equal')#------------------------------------
maske = ~np.isnan(ggg) & ~np.isnan(rrr)
slope, intercept, r_value, p_value, std_err = stats.linregress(ggg[maske], rrr[maske])
line = slope * ggg +intercept
from pcc import skill_score
SS = skill_score(ggg,rrr,th=TH_ref)
ax4.scatter(ggg, rrr, label='RR (mm/h)', color='grey', alpha=0.6)
r_value_s, p_value_s = stats.spearmanr(ggg[maske],rrr[maske])
text = ('f(x) = ' + str(round(slope,3)) + 'x + ' + str(round(intercept,3)) +
'\nCorr: ' + str(round(r_value,3)) + r'$\pm$: '+ str(round(std_err,3))+
'\nN: '+ str(int(SS['N']))+
'\nHit: ' + str(SS['H'])+
'\nMiss: ' + str(SS['M'])+
'\nFalse: ' + str(SS['F'])+
'\nCnegative: ' + str(SS['C'])+
'\nHR: ' + str(round(SS['HR'],3))+
'\nPOD: ' + str(round(SS['POD'],3))+
'\nFAR: ' + str(round(SS['FAR'],3))+
'\nBID: ' + str(round(SS['BID'],3))+
'\nHSS: ' + str(round(SS['HSS'],3))+
'\nBias: '+ str(round(SS['bias'],3))+
'\nRMSE: '+ str(round(SS['RMSE'],3))+
'\nCorrS:' + str(round(r_value_s,3))
)
ax4.annotate(text, xy=(0.01, 0.99), xycoords='axes fraction', fontsize=10,
horizontalalignment='left', verticalalignment='top')
t1 = linspace(0,50,50)
plt.plot(t1,t1,'k-')
plt.plot(t1, t1*slope + intercept, 'r-', lw=3 ,label='Regression')
plt.plot(t1, t1*slope + (intercept+5), 'r-.', lw=1.5 ,label=r'Reg $\pm$ 5 mdBZ')
plt.plot(t1, t1*slope + (intercept-5), 'r-.', lw=1.5 )
plt.plot(np.nanmean(ggg),np.nanmean(rrr), 'ob', lw = 4,label='Mean')
#plt.plot(np.nanmedian(ggg),np.nanmedian(rrr), 'vb', lw = 4,label='Median')
import matplotlib as mpl
mean = [ np.nanmean(ggg),np.nanmean(rrr)]
width = np.nanstd(ggg)
height = np.nanstd(rrr)
angle = 0
ell = mpl.patches.Ellipse(xy=mean, width=width, height=height,
angle=180+angle, color='blue', alpha=0.8,
fill=False, ls='--', label='Std')
ax4.add_patch(ell)
plt.legend(loc='lower right', fontsize=10, scatterpoints= 1, numpoints=1, shadow=True)
plt.xlim(0,10)
plt.ylim(0,10)
plt.xlabel('GPM CORRA RR [mm/h]',fontsize=ff)
plt.ylabel('RADOLAN RR[mm/h]',fontsize=ff)
plt.xticks(fontsize=ff)
plt.yticks(fontsize=ff)
plt.grid(color='r')
plt.tight_layout()
plt.savefig('/home/velibor/shkgpm/plot/gpm_corra_radolan_'+ZP + '.png' )
plt.close()
#plt.show()
GGG.append(ggg.reshape(ggg.shape[0]*ggg.shape[1]))
RRR.append(rrr.reshape(rrr.shape[0]*rrr.shape[1]))
G_all = np.concatenate(GGG,axis=0)
R_all = np.concatenate(RRR,axis=0)
from pcc import plot_scatter
fig = plt.figure(figsize=(12,12))
ax11 = fig.add_subplot(111, aspect='equal')
plot_scatter(G_all, R_all)
import matplotlib as mpl
mean = [ np.nanmean(G_all),np.nanmean(R_all)]
width = np.nanstd(G_all)
height = np.nanstd(R_all)
angle = 0
ell = mpl.patches.Ellipse(xy=mean, width=width, height=height,
angle=180+angle, color='blue', alpha=0.8,
fill=False, ls='--', label='Std')
ax11.add_patch(ell)
plt.xlabel(('GPM CORRA (dBZ)'))
plt.ylabel(('RADOLAN (dBZ)'))
plt.grid()
plt.savefig('/home/velibor/shkgpm/plot/all_gpm_corra_radolan_'+ZP + '.png' )
#plt.show()
plt.close()