-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJulian_Entropy_CFADs_codes.py
More file actions
864 lines (620 loc) · 33.3 KB
/
Julian_Entropy_CFADs_codes.py
File metadata and controls
864 lines (620 loc) · 33.3 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
def Entropy_timesteps_over_azimuth_different_vars_schneller(Polari_variable_List):
'''
Function to calculate the information Entropy, to estimate the homogenity from a sector PPi or the whole 360° PPi
for each timestep
'''
n_az = 360
Variable_List_new_zhlin = (Polari_variable_List.zhlin/(Polari_variable_List.zhlin.sum(("azimuth"),skipna=True)))
entropy_zhlin = - ((Variable_List_new_zhlin * np.log10(Variable_List_new_zhlin)).sum("azimuth"))/np.log10(n_az)
entropy_zhlin = entropy_zhlin.rename("entropy_Z")
Variable_List_new_zdrlin = (Polari_variable_List.zdrlin/(Polari_variable_List.zdrlin.sum(("azimuth"),skipna=True)))
entropy_zdrlin = - ((Variable_List_new_zdrlin * np.log10(Variable_List_new_zdrlin)).sum("azimuth"))/np.log10(n_az)
entropy_zdrlin = entropy_zdrlin.rename("entropy_zdrlin")
Variable_List_new_RHOHV = (Polari_variable_List.RHOHV_NC/(Polari_variable_List.RHOHV_NC.sum(("azimuth"),skipna=True)))
entropy_RHOHV = - ((Variable_List_new_RHOHV * np.log10(Variable_List_new_RHOHV)).sum("azimuth"))/np.log10(n_az)
entropy_RHOHV = entropy_RHOHV.rename("entropy_RHOHV")
if "KDP_ML_corrected" in Polari_variable_List.data_vars:
Variable_List_new_KDP = (Polari_variable_List.KDP_ML_corrected/(Polari_variable_List.KDP_ML_corrected.sum(("azimuth"),skipna=True)))
entropy_KDP = - ((Variable_List_new_KDP * np.log10(Variable_List_new_KDP)).sum("azimuth"))/np.log10(n_az)
entropy_KDP = entropy_KDP.rename("entropy_KDP")
else:
Variable_List_new_KDP = (Polari_variable_List.KDP_CONV/(Polari_variable_List.KDP_CONV.sum(("azimuth"),skipna=True)))
entropy_KDP = - ((Variable_List_new_KDP * np.log10(Variable_List_new_KDP)).sum("azimuth"))/np.log10(n_az)
entropy_KDP = entropy_KDP.rename("entropy_KDP")
entropy_all_xr = xr.merge([entropy_zhlin, entropy_zdrlin, entropy_RHOHV, entropy_KDP ])
return entropy_all_xr
######### Example how to calculate the min over the entropy calculated over the polarimetric variables
Entropy = Entropy_timesteps_over_azimuth_different_vars_schneller(ds_PPi)
strati = xr.concat((Entropy.entropy_zdrlin, Entropy.entropy_Z, Entropy.entropy_RHOHV, Entropy.entropy_KDP),"entropy")
min_trst_strati = strati.min("entropy")
ds["min_entropy"] = min_trst_strati
########## Melting Layer after Wolfensberger et al 2016 but refined for PHIDP correction using silkes Style from Tömel 2019
#### Functions
def normalise(da, dim):
damin = da.min(dim=dim, skipna=True, keep_attrs=True)
damax = da.max(dim=dim, skipna=True, keep_attrs=True)
da = (da - damin) / (damax - damin)
return da
def sobel(da, dim):
axis = da.get_axis_num(dim)
func = lambda x, y: ndimage.sobel(x, y)
return xr.apply_ufunc(func, da, axis, dask='parallelized')
def ml_normalise(ds, moments=dict(DBZH=(10., 60.), RHOHV=(0.65, 1.)), dim='height'):
assign = {}
for m, span in moments.items():
v = ds[m]
v = v.where((v >= span[0]) & (v <= span[1]))
v = v.pipe(normalise, dim=dim)
assign.update({f'{m}_norm': v})
return xr.Dataset(assign)
def ml_clean(ds, zh, dim='height'):
# removing profiles with too few data (>93% of array is nan)
good = ds[zh+"_norm"].count(dim=dim) / ds[zh+"_norm"].sizes[dim] * 100
return ds.where(good > 7)
def ml_combine(ds, zh, rho, dim='height'):
comb = (1 - ds[rho+"_norm"]) * ds[zh+"_norm"]
return comb
def ml_gradient(ds, dim='height', ywin=None):
assign = {}
for m in ds.data_vars:
# step 3 sobel filter
dy = ds[m].pipe(sobel, dim)
# step 3 smoothing
# currently smoothes only in y-direction (height)
dy = dy.rolling({dim: ywin}, center=True).mean()
assign.update({f'{m}_dy': dy})
return ds.assign(assign)
def ml_noise_reduction(ds, dim='height', thres=0.02):
assign = {}
for m in ds.data_vars:
if '_dy' in m:
dy = ds[m].where((ds[m] > thres) | (ds[m] < -thres))
assign.update({f"{m}": dy})
return ds.assign(assign)
def ml_height_bottom_new(ds, moment='comb_dy', dim='height', skipna=True, drop=True):
hgt = ds[moment].idxmax(dim=dim, skipna=skipna, fill_value=np.nan).load()
ds = ds.assign(dict(
mlh_bottom=hgt))
return ds
def ml_height_top_new(ds, moment='comb_dy', dim='height',skipna=True, drop=True, thrs = 2000):#, max_height=3000): 2000 für 16.11.2014
hgt = ds[moment].idxmin(dim=dim, skipna=skipna, fill_value=np.nan).load()
ds = ds.assign(dict(
mlh_top=hgt))
return ds
def melting_layer_qvp_X_new(ds, moments=dict(DBZH=(10., 60.), RHOHV=(0.65, 1.), PHIDP=(-90.,-70.)), dim='height', thres=0.02, xwin=5, ywin=5, fmlh=0.3, all_data=False):
'''
Function to detect the melting layer based on wolfensberger et all and refined for delta bump removing by K. Mühlbauer and refined by T. Scharbach
'''
zh = [k for k in moments if ("zh" in k.lower()) or ("th" in k.lower())][0]
rho = [k for k in moments if "rho" in k.lower()][0]
phi = [k for k in moments if "phi" in k.lower()][0]
# step 1
ds0 = ml_normalise(ds, moments=moments, dim=dim)
# step 1a
# removing profiles with too few data (>93% of array is nan)
good = ml_clean(ds0, zh, dim=dim)
# step 2
comb = ml_combine(good, zh, rho, dim=dim)
ds0 = ds0.assign(dict(comb=comb))
# step 3 (and 8)
ds0 = ml_gradient(ds0, dim=dim, ywin=ywin)
# step 4
ds1 = ml_noise_reduction(ds0, dim=dim, thres=thres)
#display(ds1)
# step 5
ds2 = ml_height_bottom_new(ds1, dim=dim, drop=False)
ds2 = ml_height_top_new(ds2, dim=dim, drop=False)
# step 6
med_mlh_bot = ds2.mlh_bottom.rolling(time=xwin, min_periods=xwin//2, center=True).median(skipna=True) #min_periods=xwin//2
med_mlh_top = ds2.mlh_top.rolling(time=xwin, min_periods=xwin//2, center=True).median(skipna=True) # min_periods=xwin//2
# step 7 (step 5 again)
above = (1 + fmlh) * med_mlh_top
below = (1 - fmlh) * med_mlh_bot
ds3 = ds1.where((ds1.height >= below) & (ds1.height <= above), drop=False)
ds3 = ml_height_bottom_new(ds3, dim=dim, drop=False)
ds3 = ml_height_top_new(ds3, dim=dim, drop=False)
# ds3.mlh_bottom and ds3.ml_bottom derived according Wolfensberger et. al.
# step 8 already done at step 3
# step 9
# ml top refinement
# cut below ml_top
ds4 = ds0.where((ds0.height > ds3.mlh_top))# & (ds0.height < above))
# cut above first local maxima (via differentiation and difference of signs)
########## rechunk, weil plötzlich nicht mehr geht, warum auch immer naja, neue Version????
ds4 = ds4.chunk(-1)
p4 = np.sign(ds4[zh+"_norm_dy"].differentiate(dim)).diff(dim).idxmin(dim, skipna=True)
ds5 = ds4.where(ds4.height < p4)
ds5 = ml_height_top_new(ds5, moment=phi+"_norm", dim=dim, drop=False)
# ds5.mlh_top,ds5.ml_top, derived according Wolfensberger et. al. but used PHIDP_norm instead of DBZH_norm
# ml bottom refinement
ds6 = ds0.where((ds0.height < ds3.mlh_bottom) & (ds0.height > below))
# cut below first local maxima (via differentiation and difference of signs)
ds6 = ds6.chunk(-1)
p4a = np.sign(ds6[phi+"_norm_dy"].differentiate(dim)).diff(dim).sortby(dim, ascending=False).idxmin(dim, skipna=True)
ds7 = ds6.where(ds6.height > p4a)
#idx = ds7[phi+"_norm"].argmin(dim=dim, skipna=True)
#idx[np.isnan(idx)] = 0
hgt = ds7[phi+"_norm"].idxmin(dim=dim, skipna=True, fill_value=np.nan)
#hgt = ds7.isel({dim: idx.load()}, drop=False).height
#for i in range(0,len(idx)):
# if idx[i].values == 0:
# hgt[idx].values = np.nan
ds7 = ds7.assign(dict(
mlh_bottom=hgt))
# ds7.mlh_bottom ds7.ml_bottom, derived similar to Wolfensberger et. Al. but for bottom
# uses PHIDP_norm
ds = ds.assign(dict(mlh_top=ds5.mlh_top,
mlh_bottom=ds7.mlh_bottom,
),
)
if all_data is True:
ds = ds.assign({"comb": ds0.comb,
rho+"_norm": ds0[rho+"_norm"],
zh+"_norm": ds0[zh+"_norm"],
phi+"_norm": ds0[phi+"_norm"],
rho+"_norm_dy": ds0[rho+"_norm_dy"],
zh+"_norm_dy": ds0[zh+"_norm_dy"],
phi+"_norm_dy": ds0[phi+"_norm_dy"],
})
ds = ds.assign_coords({'height_idx': (['height'], np.arange(len(ds.height)))})
return ds
################# Example usage of melting layer detection and PHIDP processing
X_ZH = "DBTH"
X_ZDR = "UZDR"
X_RHOHV = "RHOHV"
X_PHIDP = "PHIDP"
######### Processing PHIDP for BoXPol
phi = ds[X_PHIDP].where((ds[X_RHOHV+"_NC"]>=0.9) & (ds[X_ZH+"_OC"]>=0))
start_range, off = phi.pipe(phase_offset, rng=3000)
fix_range = 750
phi_fix = ds[X_PHIDP].copy()
off_fix = off.broadcast_like(phi_fix)
phi_fix = phi_fix.where(phi_fix.range >= start_range + fix_range).fillna(off_fix) - off
window = 11
window2 = None
phi_median = phi_fix.pipe(xr_rolling, window, window2=window2, method='median', skipna=True, min_periods=3)
phi_masked = phi_median.where((ds[X_RHOHV+"_NC"] >= 0.95) & (ds[X_ZH+"_OC"] >= 0.))
dr = phi_masked.range.diff('range').median('range').values / 1000.
winlen = 31 # windowlen
min_periods = 3 # min number of vaid bins
kdp = kdp_from_phidp(phi_masked, winlen, min_periods=3)
kdp1 = kdp.interpolate_na(dim='range')
winlen = 31
phidp = phidp_from_kdp(kdp1, winlen)
assign = {X_PHIDP+"_OC_SMOOTH": phi_median.assign_attrs(ds[X_PHIDP].attrs),
X_PHIDP+"_OC_MASKED": phi_masked.assign_attrs(ds[X_PHIDP].attrs),
"KDP_CONV": kdp.assign_attrs(ds.KDP.attrs),
"PHI_CONV": phidp.assign_attrs(ds[X_PHIDP].attrs),
X_PHIDP+"_OFFSET": off.assign_attrs(ds[X_PHIDP].attrs),
X_PHIDP+"_OC": phi_fix.assign_attrs(ds[X_PHIDP].attrs)}
ds = ds.assign(assign)
ds_qvp_ra = ds.median("azimuth")
moments={X_ZH+"_OC": (10., 60.), X_RHOHV+"_NC": (0.65, 1.), X_PHIDP+"_OC": (-0.5, 360)}
dim = 'height'
thres = 0.02
limit = None
xwin = 5
ywin = 5
fmlh = 0.3
ml_qvp = melting_layer_qvp_X_new(ds_qvp_ra, moments=moments,
dim=dim, thres=thres, xwin=xwin, ywin=ywin, fmlh=fmlh, all_data=True)
ds_qvp_ra = ds_qvp_ra.assign_coords({'height_ml': ml_qvp.mlh_top})
ds_qvp_ra = ds_qvp_ra.assign_coords({'height_ml_bottom': ml_qvp.mlh_bottom})
ml_qvp = ml_qvp.where(ml_qvp.mlh_top<10000)
nan = np.isnan(ds_plus_entropy.PHIDP_OC_MASKED)
phi2 = ds_plus_entropy["PHIDP_OC_MASKED"].where((ds_plus_entropy.z < ml_qvp.mlh_bottom) | (ds_plus_entropy.z > ml_qvp.mlh_top))#.interpolate_na(dim='range',dask_gufunc_kwargs = "allow_rechunk")
phi2 = phi2.interpolate_na(dim='range', method=interpolation_method_ML)
phi2 = xr.where(nan, np.nan, phi2)
dr = phi2.range.diff('range').median('range').values / 1000.
print("range res [km]:", dr)
# winlen in gates
# todo window length in m
winlen = 31
min_periods = 3
kdp_ml = kdp_from_phidp(phi2, winlen, min_periods=3)
ds = ds.assign({"KDP_ML_corrected": (["time", "azimuth", "range"], kdp_ml.values, ml_qvp.KDP.attrs)})
ds["KDP_ML_corrected"] = ds.KDP_ML_corrected.where((ds.KDP_ML_corrected >= 0.01) & (ds.KDP_ML_corrected <= 3)) #### maybe you dont need this filtering here
ds = ds.assign_coords({'height': ds.z})
ds = ds.assign_coords({'height_ml': ml_qvp.mlh_top})
ds = ds.assign_coords({'height_ml_bottom': ml_qvp.mlh_bottom})
#################### Giagrande refinment
cut_above = ds_qvp_ra.where(ds_qvp_ra.height<ds_qvp_ra.height_ml)
cut_above = cut_above.where(ds_qvp_ra.height>ds_qvp_ra.height_ml_bottom)
#test_above = cut_above.where((cut_above.rho >=0.7)&(cut_above.rho <0.98))
min_height_ML = cut_above.rho.idxmin(dim="height")
new_cut_below_min_ML = ds_qvp_ra.where(ds_qvp_ra.height > min_height_ML)
new_cut_above_min_ML = ds_qvp_ra.where(ds_qvp_ra.height < min_height_ML)
new_cut_below_min_ML_filter = new_cut_below_min_ML.rho.where((new_cut_below_min_ML.rho>=0.97)&(new_cut_below_min_ML.rho<=1))
new_cut_above_min_ML_filter = new_cut_above_min_ML.rho.where((new_cut_above_min_ML.rho>=0.97)&(new_cut_above_min_ML.rho<=1))
import pandas as pd
######### ML TOP Giagrande refinement
panda_below_min = new_cut_below_min_ML_filter.to_pandas()
first_valid_height_after_ml = pd.DataFrame(panda_below_min).apply(pd.Series.first_valid_index)
first_valid_height_after_ml = first_valid_height_after_ml.to_xarray()
######### ML BOTTOM Giagrande refinement
panda_above_min = new_cut_above_min_ML_filter.to_pandas()
last_valid_height = pd.DataFrame(panda_above_min).apply(pd.Series.last_valid_index)
last_valid_height = last_valid_height.to_xarray()
ds_qvp_ra = ds_qvp_ra.assign_coords(height_ml_new_gia = ("time",first_valid_height_after_ml.data))
ds_qvp_ra = ds_qvp_ra.assign_coords(height_ml_bottom_new_gia = ("time", last_valid_height.data))
ds = ds.assign_coords(height_ml_new_gia = ("time",first_valid_height_after_ml.data))
ds = ds.assign_coords(height_ml_bottom_new_gia = ("time", last_valid_height.data))
#################################### CFADs
KDP_DGL_max_test = []
ZH = []
ZDR = []
KDP = []
RHO = []
TT = []
E = []
ML = []
valid_values = []
ZH_ML_max = []
ZDR_ML_max = []
RHO_ML_min = []
KDP_ML_mean = []
MLTH_ML = []
BETA = []
ZH_DGL = []
ZDR_DGL = []
RHO_DGL = []
KDP_DGL = []
ZH_DGL_09 = []
ZDR_DGL_09 = []
KDP_DGL_09 = []
KDP_DGL_max= []
RHO_DGL_min= []
ZH_DGL_max= []
ZDR_DGL_max= []
# ZH_snow_list= []
# ZDR_snow_list= []
# ZH_rain_list= []
# ZDR_rain_list= []
ZH_snow = []
ZDR_snow = []
ZH_rain = []
ZDR_rain = []
ZH_sfc = []
ZDR_sfc = []
Dm_ZDR_KDP_Z_max = []
Nt_ZDR_KDP_Z_max = []
Nt_ZDR_KDP_Z_max_log = []
IWC_ZDR_KDP_Z_max = []
Dm_ZDR_KDP_Z = []
Nt_ZDR_KDP_Z_log = []
Nt_ZDR_KDP_Z = []
IWC_ZDR_KDP_Z = []
Dm_ZDR_KDP_Z_DGL_09 = []
Nt_ZDR_KDP_Z_DGL_09 = []
IWC_ZDR_KDP_Z_DGL_09 = []
Dm_ZDR_KDP_Z_DGL = []
Nt_ZDR_KDP_Z_DGL = []
IWC_ZDR_KDP_Z_DGL = []
#.where(~np.isnan(ds.height_ml))
# .where(ds.height_ml>height_ml_bottom, drop=True)
for i in range(len(qvp_nc_files)):
#print(qvp_nc_files[i])
#try:
ds = xr.open_dataset(qvp_nc_files[i], chunks='auto')
ds = ds.swap_dims({"index_new":"time"})
ml_height_thres = ~np.isnan(ds.height_ml_new_gia)
ds_thrs = ds.where(ml_height_thres == True, drop=True)
ml_bottom_thres = ~np.isnan(ds_thrs.height_ml_bottom_new_gia)
ds_thrs = ds_thrs.where(ml_bottom_thres == True, drop=True)
ds_thrs = ds_thrs.where(ds_thrs.height_ml_new_gia > ds_thrs.height_ml_bottom_new_gia)
ds = ds_thrs
ds["Nt_ZDR_KDP_Z_log"] = np.log10(ds.Nt_ZDR_KDP_Z/1000)
ds["Nt_ZDR_KDP_Z"] = ds.Nt_ZDR_KDP_Z/1000
ds_stat_ML = ds.where(ds.min_entropy>=0.8, drop=True)
ds_stat_ML = ds_stat_ML.where((ds_stat_ML.height<ds_stat_ML.height_ml_new_gia) & (ds_stat_ML.height>ds_stat_ML.height_ml_bottom_new_gia))
ds_stat_ML = ds_stat_ML.where((ds_stat_ML.min_entropy>=0.8)&(ds_stat_ML.DBTH_OC > 0 )&(ds_stat_ML.KDP_ML_corrected > 0.01)&(ds_stat_ML.RHOHV_NC > 0.7)&(ds_stat_ML.UZDR_OC > -1), drop=True)
### ML statistics
if ds_stat_ML.time.shape[0]!=0:
ZH_ML_max.append(ds_stat_ML.DBTH_OC.max(dim="height").values.flatten())
ZDR_ML_max.append(ds_stat_ML.UZDR_OC.max(dim="height").values.flatten())
RHO_ML_min.append(ds_stat_ML.RHOHV_NC.min(dim="height").values.flatten())
KDP_ML_mean.append(ds_stat_ML.KDP_ML_corrected.mean(dim="height").values.flatten())
mlth_ML = ds_stat_ML.height_ml_new_gia - ds_stat_ML.height_ml_bottom_new_gia
MLTH_ML.append(mlth_ML.values.flatten())
### Silke Style
Gradient_silke = ds.where(ds.min_entropy>=0.8)
Gradient_silke = Gradient_silke.where((Gradient_silke.min_entropy>=0.8)&(Gradient_silke.DBTH_OC > 0 )&(Gradient_silke.KDP_ML_corrected > 0.01)&(Gradient_silke.RHOHV_NC > 0.7)&(Gradient_silke.UZDR_OC > -1))
DBTH_OC_gradient_Silke_ML = Gradient_silke.DBTH_OC.sel(height = Gradient_silke.height_ml_new, method="nearest")
DBTH_OC_gradient_Silke_ML_plus_2_km = Gradient_silke.DBTH_OC.sel(height = DBTH_OC_gradient_Silke_ML.height+2000, method="nearest")
DBTH_OC_gradient_Final = (DBTH_OC_gradient_Silke_ML_plus_2_km - DBTH_OC_gradient_Silke_ML)/2
BETA.append(DBTH_OC_gradient_Final)
### DGL statistics
ds_stat_DGL = ds.where((ds.temp_coord>=-20)&(ds.temp_coord<=-10), drop=True)
ds_stat_DGL = ds_stat_DGL.where(ds_stat_DGL.min_entropy>=0.8, drop=True)
ds_stat_DGL = ds_stat_DGL.where((ds_stat_DGL.min_entropy>=0.8)&(ds_stat_DGL.DBTH_OC > 0 )&(ds_stat_DGL.KDP_ML_corrected > 0.01)&(ds_stat_DGL.RHOHV_NC > 0.7)&(ds_stat_DGL.UZDR_OC > -1), drop=True)
if ds_stat_DGL.time.shape[0]!=0:
ZH_DGL.append(ds_stat_DGL.DBTH_OC.values.flatten())
ZDR_DGL.append(ds_stat_DGL.UZDR_OC.values.flatten())
KDP_DGL_max.append(ds_stat_DGL.KDP_ML_corrected.max(dim="height").values.flatten())
KDP_DGL_max_test.append(np.asarray(ds_stat_DGL.KDP_ML_corrected.max(dim="height").stack(dim=["time"]).data))
RHO_DGL_min.append(ds_stat_DGL.RHOHV_NC.min(dim="height").values.flatten())
ZH_DGL_max.append(ds_stat_DGL.DBTH_OC.max(dim="height").values.flatten())
ZDR_DGL_max.append(ds_stat_DGL.UZDR_OC.max(dim="height").values.flatten())
ZDR_DGL_09.append(ds_stat_DGL.UZDR_OC.quantile(0.9, dim="height").values.flatten())
ZH_DGL_09.append(ds_stat_DGL.DBTH_OC.quantile(0.9, dim="height").values.flatten())
RHO_DGL.append(ds_stat_DGL.RHOHV_NC.values.flatten())
KDP_DGL.append(ds_stat_DGL.KDP_ML_corrected.values.flatten())
KDP_DGL_09.append(ds_stat_DGL.KDP_ML_corrected.quantile(0.9, dim="height").values.flatten())
#ds_stat_ML_list_KDP_ML_corrected.append(np.asarray(ds.KDP_ML_corrected.max(dim="height").stack(dim=["height","time"]).data))
### other statistics
ds_other1 = ds.where(ds.min_entropy>=0.8)
ds_other1 = ds_other1.where((ds_other1.min_entropy>=0.8)&(ds_other1.DBTH_OC > 0 )&(ds_other1.KDP_ML_corrected > 0.01)&(ds_other1.RHOHV_NC > 0.7)&(ds_other1.UZDR_OC > -1))
ZH_sfc.append(ds_other1.DBTH_OC.swap_dims({"height":"range"}).isel(range = 7).values.flatten())
ZDR_sfc.append(ds_other1.UZDR_OC.swap_dims({"height":"range"}).isel(range = 7).values.flatten())
# ZH_snow = ds_other.DBTH_OC.sel(height = ds_other.height_ml_new, method="nearest")
# ZDR_snow = ds_other.UZDR_OC.sel(height = ds_other.height_ml_new, method="nearest")
# ZH_rain = ds_other.DBTH_OC.sel(height = ds_other.height_ml_bottom_new_gia, method="nearest")
# ZDR_rain = ds_other.UZDR_OC.sel(height = ds_other.height_ml_bottom_new_gia, method="nearest")
# ds_other = ds.where(ds.min_entropy>=0.8, drop=True)
# ds_other = ds_other.where((ds_other.min_entropy>=0.8)&(ds_other.DBTH_OC > 0 )&(ds_other.KDP_ML_corrected > 0.001)&(ds_other.RHOHV_NC > 0.7)&(ds_other.UZDR_OC > -1), drop=True)
ZH_snow.append(ds_other1.DBTH_OC.sel(height = ds_other1.height_ml_new, method="nearest").values.flatten())
ZDR_snow.append(ds_other1.UZDR_OC.sel(height = ds_other1.height_ml_new, method="nearest").values.flatten())
ZH_rain.append(ds_other1.DBTH_OC.sel(height = ds_other1.height_ml_bottom_new_gia, method="nearest").values.flatten())
ZDR_rain.append(ds_other1.UZDR_OC.sel(height = ds_other1.height_ml_bottom_new_gia, method="nearest").values.flatten())
### IWC, Nt, Dm statistics hier ist weiteres Filtern eher nicht gut i would say
ds_microphysical = ds.where(ds.min_entropy>=0.8, drop=True)
ds_microphysical = ds_microphysical.where((ds_microphysical.temp_coord>=-20)&(ds_microphysical.temp_coord<=-10), drop=True)
Dm_ZDR_KDP_Z_DGL_09.append(ds_microphysical.Dm_ZDR_KDP_Z.quantile(0.9, dim="height").values.flatten())
Nt_ZDR_KDP_Z_DGL_09.append(ds_microphysical.Nt_ZDR_KDP_Z.quantile(0.9, dim="height").values.flatten())
IWC_ZDR_KDP_Z_DGL_09.append(ds_microphysical.IWC_ZDR_KDP_Z.quantile(0.9, dim="height").values.flatten())
Dm_ZDR_KDP_Z_DGL.append(ds_microphysical.Dm_ZDR_KDP_Z.values.flatten())
Nt_ZDR_KDP_Z_DGL.append(ds_microphysical.Nt_ZDR_KDP_Z.values.flatten())
IWC_ZDR_KDP_Z_DGL.append(ds_microphysical.IWC_ZDR_KDP_Z.values.flatten())
Dm_ZDR_KDP_Z_max.append(ds_microphysical.Dm_ZDR_KDP_Z.max(dim="height").values.flatten())
Nt_ZDR_KDP_Z_max_log.append(ds_microphysical.Nt_ZDR_KDP_Z_log.max(dim="height").values.flatten())
Nt_ZDR_KDP_Z_max.append(ds_microphysical.Nt_ZDR_KDP_Z.max(dim="height").values.flatten())
IWC_ZDR_KDP_Z_max.append(ds_microphysical.IWC_ZDR_KDP_Z.max(dim="height").values.flatten())
# ZH_snow_list.append(np.asarray(ZH_snow.squeeze().stack(dim=["height","time"]).data))
# ZDR_snow_list.append(np.asarray(ZDR_snow.squeeze().stack(dim=["height","time"]).data))
# ZH_rain_list.append(np.asarray(ZH_rain.squeeze().stack(dim=["height","time"]).data))
# ZDR_rain_list.append(np.asarray(ZDR_rain.squeeze().stack(dim=["height","time"]).data))
ZH.append(np.asarray(ds.DBTH_OC.squeeze().stack(dim=["height","time"]).data))
ZDR.append(np.asarray(ds.UZDR_OC.squeeze().stack(dim=["height","time"]).data))
RHO.append(np.asarray(ds.RHOHV_NC.squeeze().stack(dim=["height","time"]).data))
KDP.append(np.asarray(ds.KDP_ML_corrected.squeeze().stack(dim=["height","time"]).data))
E.append(np.asarray(ds.min_entropy.squeeze().stack(dim=["height","time"]).data))
valid_values.append(np.asarray(ds.valid_values_min_120.squeeze().stack(dim=["height","time"]).data))
TT.append(np.asarray((ds.temp_coord).squeeze().stack(dim=["height","time"]).data))
Dm_ZDR_KDP_Z.append(np.asarray(ds.Dm_ZDR_KDP_Z.squeeze().stack(dim=["height","time"]).data))
Nt_ZDR_KDP_Z.append(np.asarray(ds.Nt_ZDR_KDP_Z.squeeze().stack(dim=["height","time"]).data))
Nt_ZDR_KDP_Z_log.append(np.asarray(ds.Nt_ZDR_KDP_Z_log.squeeze().stack(dim=["height","time"]).data))
IWC_ZDR_KDP_Z.append(np.asarray(ds.IWC_ZDR_KDP_Z.squeeze().stack(dim=["height","time"]).data))
# ZH.append(ds.DBTH_OC.values.flatten())
# ZDR.append(ds.UZDR_OC.values.flatten())
# RHO.append(ds.RHOHV_NC.values.flatten())
# KDP.append(ds.KDP_ML_corrected.values.flatten())
# TT.append(ds.temp_coord.values.flatten())
# E.append(ds.min_entropy.values.flatten())
#except:
#print('Error')
ZH = np.concatenate(ZH)
ZDR = np.concatenate(ZDR)
KDP = np.concatenate(KDP)
RHO = np.concatenate(RHO)
TT = np.concatenate(TT)
E = np.concatenate(E)
valid_values = np.concatenate(valid_values)
RHO_ML_min = np.concatenate(RHO_ML_min)
ZDR_ML_max = np.concatenate(ZDR_ML_max)
ZH_ML_max = np.concatenate(ZH_ML_max)
MLTH_ML = np.concatenate(MLTH_ML)
BETA = np.concatenate(BETA)
KDP_ML_mean = np.concatenate(KDP_ML_mean)
ZH_DGL = np.concatenate(ZH_DGL)
ZDR_DGL = np.concatenate(ZDR_DGL)
RHO_DGL = np.concatenate(RHO_DGL)
KDP_DGL = np.concatenate(KDP_DGL)
ZDR_DGL_09 = np.concatenate(ZDR_DGL_09)
KDP_DGL_09 = np.concatenate(KDP_DGL_09)
Dm_ZDR_KDP_Z_DGL_09= np.concatenate(Dm_ZDR_KDP_Z_DGL_09)
Nt_ZDR_KDP_Z_DGL_09= np.concatenate(Nt_ZDR_KDP_Z_DGL_09)
IWC_ZDR_KDP_Z_DGL_09= np.concatenate(IWC_ZDR_KDP_Z_DGL_09)
Dm_ZDR_KDP_Z_DGL= np.concatenate(Dm_ZDR_KDP_Z_DGL)
Nt_ZDR_KDP_Z_DGL= np.concatenate(Nt_ZDR_KDP_Z_DGL)
IWC_ZDR_KDP_Z_DGL= np.concatenate(IWC_ZDR_KDP_Z_DGL)
Dm_ZDR_KDP_Z_max= np.concatenate(Dm_ZDR_KDP_Z_max)
Nt_ZDR_KDP_Z_max= np.concatenate(Nt_ZDR_KDP_Z_max)
Nt_ZDR_KDP_Z_max_log= np.concatenate(Nt_ZDR_KDP_Z_max_log)
IWC_ZDR_KDP_Z_max= np.concatenate(IWC_ZDR_KDP_Z_max)
KDP_DGL_max_test = np.concatenate(KDP_DGL_max_test)
KDP_DGL_max= np.concatenate(KDP_DGL_max)
RHO_DGL_min= np.concatenate(RHO_DGL_min)
ZH_DGL_max= np.concatenate(ZH_DGL_max)
ZDR_DGL_max= np.concatenate(ZDR_DGL_max)
Dm_ZDR_KDP_Z = np.concatenate(Dm_ZDR_KDP_Z)
Nt_ZDR_KDP_Z = np.concatenate(Nt_ZDR_KDP_Z)
IWC_ZDR_KDP_Z = np.concatenate(IWC_ZDR_KDP_Z)
Nt_ZDR_KDP_Z_log = np.concatenate(Nt_ZDR_KDP_Z_log)
ZH_snow = np.concatenate(ZH_snow)
ZDR_snow = np.concatenate(ZDR_snow)
ZH_rain = np.concatenate(ZH_rain)
ZDR_rain = np.concatenate(ZDR_rain)
ZH_sfc = np.concatenate(ZH_sfc)
ZDR_sfc = np.concatenate(ZDR_sfc)
def hist2d(ax, PX, PY, binsx=[], binsy=[], mode='rel_all', cb_mode=True, qq=0.2, cmap='turbo', colsteps=10, mini=0, fsize=13, fcolor='black', mincounts=500, cblim=[0,26], N=False):
"""
Plots the 2-dimensional distribution of two Parameters
# Input:
# ------
PX = Parameter x-axis
PY = Parameter y-axis
binsx = [start, stop, step]
binsy = [start, stop, step]
mode = 'rel_all', 'abs' or 'rel_y'
rel_all : Relative Dist. to all pixels
rel_y : Relative Dist. to all pixels
to y-axis.
abs : Absolute Dist.
qq = parentile [0-1]
# Output
# ------
Plot of 2-dimensional distribution
"""
import matplotlib
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
matplotlib.rc('axes',edgecolor='black')
# discret cmap
cmap = plt.cm.get_cmap(cmap, colsteps)
# Defin bins arange
bins_px = np.arange(binsx[0], binsx[1], binsx[2])
bins_py = np.arange(binsy[0], binsy[1], binsy[2])
# Hist 2d
H, xe, ye = np.histogram2d(PX, PY, bins = (bins_px, bins_py))
# Calc mean x and y
mx =0.5*(xe[0:-1]+xe[1:len(xe)])
my =0.5*(ye[0:-1]+ye[1:len(ye)])
# Calculate Percentil
var_mean = []
var_med = []
var_qq1 = []
var_qq2 = []
var_count = []
for i in bins_py:
var_med.append(np.nanmedian(PX[(PY>i)&(PY<=i+binsy[2])]))
var_mean.append(np.nanmean(PX[(PY>i)&(PY<=i+binsy[2])]))
var_qq1.append(np.nanquantile(PX[(PY>i)&(PY<=i+binsy[2])], qq))
var_qq2.append(np.nanquantile(PX[(PY>i)&(PY<=i+binsy[2])], 1-qq))
var_count.append(len(PX[(PY>i)&(PY<=i+binsy[2])]))
var_med = np.array(var_med)
var_mean = np.array(var_mean)
var_qq1 = np.array(var_qq1)
var_qq2 = np.array(var_qq2)
var_count = np.array(var_count)
var_med[var_count<mincounts]=np.nan
var_mean[var_count<mincounts]=np.nan
var_qq1[var_count<mincounts]=np.nan
var_qq2[var_count<mincounts]=np.nan
# overall sum for relative distribution
if mode=='rel_all':
allsum = np.nansum(H)
allsum[allsum<mincounts]=np.nan
relHa = H.T/allsum
elif mode=='rel_y':
allsum = np.nansum(H, axis=0)
allsum[allsum<mincounts]=np.nan
relHa = (H/allsum).T
elif mode=='abs':
relHa = H
else:
print('Wrong mode parameter used! Please use mode="rel_all", mode="abs" or mode="rel_y"!')
RES = 100*relHa
RES[RES<mini]=np.nan
img = ax.pcolormesh(mx, my ,RES , cmap=cmap, vmin=cblim[0], vmax=cblim[1], shading="gouraud")
ax.plot(var_mean, bins_py, color='red', lw=2)
ax.plot(var_qq1, bins_py, color='red', linestyle='-.', lw=2)
ax.plot(var_qq2, bins_py, color='red', linestyle='-.', lw=2)
if cb_mode==True:
#cax = plt.axes([0.055, -0.01, 0.93, 0.01]) #Left,bottom, length, width
cax = plt.axes([0.11, 0.27, 0.85, 0.01]) #Left, bottom, length, width
cb=plt.colorbar(img, cax=cax, pad=0.001, ticks=np.linspace(cblim[0],cblim[1], colsteps+1),extend ='max',orientation="horizontal" )
if mode=='abs':
cb.ax.set_title('#', color=fcolor)
if mode!='abs':
cb.ax.set_title('%', color=fcolor)
cb.ax.tick_params(labelsize=fsize, color=fcolor)
cbar_yticks = plt.getp(cb.ax.axes, 'yticklabels')
plt.setp(cbar_yticks, color=fcolor)
ax.grid(color=fcolor, linestyle='-.', lw=0.5, alpha=0.9)
ax.xaxis.label.set_color(fcolor)
ax.tick_params(axis='both', colors=fcolor)
if N==True:
ax2 = ax.twiny()
ax2.plot(var_count, bins_py, color='cornflowerblue', linestyle='-', lw=2)
#ax2.set_xlim(0,2500)
ax2.xaxis.label.set_color('cornflowerblue')
ax2.yaxis.label.set_color('cornflowerblue')
ax2.tick_params(axis='both', colors='cornflowerblue')
xticks = ax2.xaxis.get_major_ticks()
xticks[0].label1.set_visible(False)
xticks[-1].label1.set_visible(False)
if cb_mode==True:
#cax = plt.axes([0.055, -0.01, 0.93, 0.01]) #Left,bottom, length, width
cax = plt.axes([0.11, 0.27, 0.85, 0.01]) #Left, bottom, length, width
cb=plt.colorbar(img, cax=cax, pad=0.001, ticks=np.linspace(cblim[0],cblim[1], colsteps+1),extend ='max',orientation="horizontal" )
if mode=='abs':
cb.ax.set_title('#', color=fcolor)
if mode!='abs':
cb.ax.set_title('%', color=fcolor, fontsize=30)
cb.ax.tick_params(labelsize=fsize, color=fcolor)
cbar_yticks = plt.getp(cb.ax.axes, 'yticklabels')
plt.setp(cbar_yticks, color=fcolor)
return ax2
####### Plotting example how to use the function above
mask = (Dm_ZDR_KDP_Z>0) & (IWC_ZDR_KDP_Z>0) & (E>=0.8)& (TT<=0.5) &(TT>=-20)
smask = (Dm_syn>0.01) & (IWC_syn>0) & (sE_retrievals>=0.8)& (sTMP_retrievals<=0.5) &(sTMP_retrievals>=-20)& (~np.isnan(Nt_syn_log))
#smask = (sE_retrievals>=0.8)#& (sTMP_retrievals<=0.5) &(sTMP_retrievals>=-20)
# Remove Color for bins with th
mth = 0
# Temp bins
tb=1#0.7
# Min counts per Temp layer
mincounts=200
#Colorbar limits
cblim=[0,10]
fig, ax = plt.subplots(3, 2, sharey=True, figsize=(20,24))#, gridspec_kw={'width_ratios': [0.47,0.53]})
hist2d(ax[0,0], Dm_ZDR_KDP_Z[mask], TT[mask], binsx=[0,10,0.1], binsy=[ytlim,0.5,tb],
mode='rel_y', qq=0.2,cb_mode=False,cmap=cm, colsteps=10, mini=mth, fsize=30,
mincounts=mincounts, cblim=cblim)
ax[0,0].set_ylim(0.5,ytlim)
ax[0,0].set_ylabel('Temperature [°C]', fontsize=30, color='black')
ax[0,0].set_xlabel("$D_{m}$ [$mm$]", fontsize=30, color='black')
ax[0,0].text(0.05, 0.95, 'a)', horizontalalignment='center', verticalalignment='center', color='black', fontsize=40, transform=ax[0,0].transAxes)
ax[0,0].set_title('Observations \n '+'N: '+str(len(Dm_ZDR_KDP_Z[mask])), fontsize=30, color='black', loc='left')
hist2d(ax[1,0], Nt_ZDR_KDP_Z_log[mask], TT[mask], binsx=[-2,2,0.1], binsy=[ytlim,0.5,tb],
mode='rel_y', qq=0.2,cb_mode=False,cmap=cm, colsteps=10, mini=mth, fsize=30,
mincounts=mincounts, cblim=cblim)
ax[1,0].set_ylim(0.5,ytlim)
ax[1,0].set_ylabel('Temperature [°C]', fontsize=30, color='black')
ax[1,0].set_xlabel("$N_{t}$ [log$_{10}$($L^{-1}$)]", fontsize=30, color='black')
ax[1,0].xaxis.set_ticks(np.arange(-1.5, 2, 0.5))
ax[1,0].text(0.05, 0.95, 'c)', horizontalalignment='center', verticalalignment='center', color='black', fontsize=40, transform=ax[1,0].transAxes)
ax2 =hist2d(ax[2,0], IWC_ZDR_KDP_Z[mask], TT[mask], binsx=[0,0.7,0.01], binsy=[ytlim,0.5,tb],
mode='rel_y', qq=0.2,cb_mode=False,cmap=cm, colsteps=10, mini=mth, fsize=30,
mincounts=mincounts, cblim=cblim, N=True)
ax[2,0].set_ylim(0.5,ytlim)
ax[2,0].set_ylabel('Temperature [°C]', fontsize=30, color='black')
ax[2,0].set_xlabel("IWC [$g/m^3$]", fontsize=30, color='black')
ax2.set_xlim(200,95000)
ax[2,0].text(0.05, 0.95, 'e)', horizontalalignment='center', verticalalignment='center', color='black', fontsize=40, transform=ax[2,0].transAxes)
hist2d(ax[0,1], Dm_syn[smask], sTMP_retrievals[smask], binsx=[0,10,0.1], binsy=[ytlim,0.5,tb],
mode='rel_y', qq=0.2,cb_mode=False,cmap=cm, colsteps=10,mini=mth, fsize=30,
mincounts=mincounts, cblim=cblim)
ax[0,1].set_ylim(0.5,ytlim)
ax[0,1].set_xlabel("$D_{m}$ [$mm$]", fontsize=30, color='black')
ax[0,1].text(0.05, 0.95, 'b)', horizontalalignment='center', verticalalignment='center', color='black', fontsize=40, transform=ax[0,1].transAxes)
ax[0,1].set_title('Simulations \n '+'N: '+str(len(Dm_syn[smask])), fontsize=30, color='black', loc='left')
hist2d(ax[1,1], Nt_syn_log[smask], sTMP_retrievals[smask], binsx=[-2,2,0.1], binsy=[ytlim,0.5,tb],
mode='rel_y', qq=0.2,cb_mode=False,cmap=cm, colsteps=10, mini=mth, fsize=30,
mincounts=mincounts, cblim=cblim)
ax[1,1].set_ylim(0.5,ytlim)
ax[1,1].set_xlabel("$N_{t}$ [log$_{10}$($L^{-1}$)]", fontsize=30, color='black')
ax[1,1].xaxis.set_ticks(np.arange(-1.5, 2, 0.5))
ax[1,1].text(0.05, 0.95, 'd)', horizontalalignment='center', verticalalignment='center', color='black', fontsize=40, transform=ax[1,1].transAxes)
ax2 =hist2d(ax[2,1], IWC_syn[smask], sTMP_retrievals[smask], binsx=[0,0.7,0.01], binsy=[ytlim,0.5,tb],
mode='rel_y', qq=0.2,cmap=cm, colsteps=10, mini=mth, fsize=30,
mincounts=mincounts, cblim=cblim, N=True)
ax[2,1].set_ylim(0.5,ytlim)
ax[2,1].text(0.05, 0.95, 'f)', horizontalalignment='center', verticalalignment='center', color='black', fontsize=40, transform=ax[2,1].transAxes)
legend = ax[2,1].legend(title ="Sample size", loc = "upper center", labelcolor = 'cornflowerblue', frameon=False,)
plt.setp(legend.get_title(), color='cornflowerblue',fontsize=30)
ax[2,1].set_xlabel("IWC [$g/m^3$]", fontsize=30, color='black')
ax2.set_xlim(200, 11500)
box1 = ax[0,0].get_position()
box2 = ax[1,0].get_position()
box3 = ax[2,0].get_position()
box4 = ax[0,1].get_position()
box5 = ax[1,1].get_position()
box6 = ax[2,1].get_position()
ax[0,0].set_position([box1.x0, box1.y0 + box1.height * 1, box1.width * 1.15, box1.height * 1])
ax[0,1].set_position([box4.x0, box4.y0 + box4.height * 1, box4.width* 1.15, box4.height * 1])
ax[1,0].set_position([box2.x0, box2.y0 + box2.height *1., box2.width* 1.15, box2.height * 1])
ax[1,1].set_position([box5.x0, box5.y0 + box5.height *1., box5.width* 1.15, box5.height * 1])
ax[2,0].set_position([box3.x0, box3.y0 + box3.height * 0.9, box3.width* 1.15, box3.height * 1])
ax[2,1].set_position([box6.x0, box6.y0 + box6.height * 0.9, box6.width* 1.15, box6.height * 1])