/
qvp_boxpol.py
843 lines (678 loc) · 27.7 KB
/
qvp_boxpol.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
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
# -*- coding: utf-8 -*-
"""
Created on Mon May 26 16:09:26 2014
@author: timo
median averaging for zh, zdr,rho, phi over 360 degrees
"""
# Todo: add wet bulb temperature
# from cosmo data
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.colors import BoundaryNorm
from matplotlib.colors import ListedColormap
import h5py
import eccodes as codes
import miub_eccodes as mecc
from scipy import stats
import glob
import os
import wradlib as wrl
import psutil
process = psutil.Process(os.getpid())
"""
-----------------------------------------------------------------
global data
-----------------------------------------------------------------
"""
# this defines start and end time
# need to be within the same day
start_time = dt.datetime(2015, 6, 22, 15, 00)
end_time = dt.datetime(2015, 6, 22, 18, 30)
date = '{0}-{1:02d}-{2:02d}'.format(start_time.year, start_time.month, start_time.day)
location = 'Bonn'
radar_path='/automount/radar/scans/{0}/{0}-{1:02}/{2}'.format(start_time.year, start_time.month, date)
output_path = '../output/Riming'
# choose scan
file_path = radar_path + '/' + 'n_ppi_280deg/'
textfile_path = output_path + '/{0}/textfiles/'.format(date)
plot_path = output_path + '/{0}/plots/'.format(date)
print(radar_path)
print(output_path)
print(textfile_path)
print(plot_path)
#exit(9)
# create paths accordingly
if not os.path.isdir(output_path):
os.makedirs(output_path)
if not os.path.isdir(textfile_path):
os.makedirs(textfile_path)
if not os.path.isdir(plot_path):
os.makedirs(plot_path)
plot_width = 9
plot_height = 7.2
offset_z = 3
offset_phi = 90
offset_zdr = 0.8
special_char = ":"
"""
functions
"""
# transforms rotated_ll to latlon and vica versa
def rotated_grid_transform(grid_in, option, SP_coor):
lon = grid_in[...,0]
lat = grid_in[...,1]
lon = np.deg2rad(lon) # Convert degrees to radians
lat = np.deg2rad(lat)
SP_lon = SP_coor[0]
SP_lat = SP_coor[1]
theta = 90 + SP_lat # Rotation around y-axis
phi = SP_lon # Rotation around z-axis
phi = np.deg2rad(phi) # Convert degrees to radians
theta = np.deg2rad(theta)
x = np.cos(lon) * np.cos(lat) # Convert from spherical to cartesian coordinates
y = np.sin(lon) * np.cos(lat)
z = np.sin(lat)
if not option: # Regular -> Rotated
x_new = np.cos(theta) * np.cos(phi) * x + np.cos(theta) * np.sin(phi) * y + np.sin(theta) * z
y_new = -np.sin(phi) * x + np.cos(phi) * y
z_new = -np.sin(theta) * np.cos(phi) * x - np.sin(theta) * np.sin(phi) * y + np.cos(theta) * z
else:
phi = -phi
theta = -theta
x_new = np.cos(theta) * np.cos(phi) * x + np.sin(phi) * y + np.sin(theta) * np.cos(phi) * z
y_new = -np.cos(theta) * np.sin(phi) * x + np.cos(phi) * y - np.sin(theta) * np.sin(phi) * z
z_new = -np.sin(theta) * x + np.cos(theta) * z
lon_new = np.arctan2(y_new, x_new) # Convert cartesian back to spherical coordinates
lat_new = np.arcsin(z_new)
# +90 added for proper presentation in europe
lon_new = np.rad2deg(lon_new) + 90 # Convert radians back to degrees
lat_new = np.rad2deg(lat_new)
return np.dstack((lon_new, lat_new))
# -----------------------------------------------------------------
# -----------------------------------------------------------------
def movingaverage(values, window, mode):
''' moving average calculation:
Parameters:
values: source array
window: number of elements for the averaging '''
weights = np.repeat(1.0, window) / window
ma = np.convolve(values, weights, mode)
return ma
def fit_line(x, y):
''' slope calculation
Parameters: x and y array '''
slope, intercept, r_value, p_value, err = stats.linregress(x, y)
return slope
def kdp_calc(p, wl=5):
''' K_DP calculation for all time steps
Parameter: p: radar matrix for all time steps '''
print(p.shape)
# get x-array over last dimension of source-array (here [2])
x = np.arange(0, p.shape[2])
# kdp = np.ones_like(p)*(-10)
kdp = np.ones_like(p) * (0)
# iterate over timesteps
for k in range(np.shape(p)[0]):
# iterate over azimuths
for j in range(np.shape(p)[1]):
# iterate along ray
for i in range(np.shape(p)[2] - wl):
# fit
slope = fit_line(x[i:i + wl - 1], p[k, j, i:i + wl - 1])
# kdp[k, j, i+wl] = slope/2.
# Durch 2 wg Def von Kdp, mal 10 da 100m Auflösung und Einheit deg/km
kdp[k, j, i + 3] = slope / 2. * 10.
return kdp
# -----------------------------------------------------------------
# -----------------------------------------------------------------
def transform(data, dmin, dmax, dformat):
"""
transforms the raw data to the dynamic range [dmin,dmax]
"""
if dformat == 'UV8':
dform = 255
else:
dform = 65535
# or even better: use numpy arrays, which removes need of for loops
t = dmin + data * (dmax - dmin) / dform
return t
# -----------------------------------------------------------------
def read_generic_hdf5(fname):
"""Reads hdf5 files according to their structure
In contrast to other file readers under wradlib.io, this function will
*not* returnreturn a two item tuple with (data, metadata). Instead, this
function returns ONE a dictionary that contains all the file contents -
both data and metadata. The keys of the output dictionary conform to the
Group/Subgroup directory branches of the original file.
Parameters
----------
fname : string (a hdf5 file path)
Returns
-------
output : a dictionary that contains both data and metadata according to the
original hdf5 file structure
"""
f = h5py.File(fname, "r")
fcontent = {}
def filldict(x, y):
# create a new container
tmp = {}
# add attributes if present
if len(y.attrs) > 0:
tmp['attrs'] = dict(y.attrs)
# add data if it is a dataset
if isinstance(y, h5py.Dataset):
tmp['data'] = np.array(y)
# only add to the dictionary, if we have something meaningful to add
if tmp != {}:
fcontent[x] = tmp
f.visititems(filldict)
f.close()
return fcontent
class boxpol(object):
"""
reads data from hdf5-file
gelesen werden die Daten zh,phi,rho,zdr mit Zeitstempel.
Mit der Rückgabe von no_file=0 wird zum Ausdruck gebracht, dass
die Datei korrekt gelesen werden konnte.
Im Fehlerfall (Rückgabe n0_file=1) werden Dummy-Daten returniert und
auf den Bildschirm der Name der nicht gefundenen Datei ausgegeben.
"""
def __init__(self, filename, **kwargs):
data = read_generic_hdf5(filename)
#print(data)
#exit(9)
if data is not None:
self._zh = transform(data['scan0/moment_10']['data'],
data['scan0/moment_10']['attrs']['dyn_range_min'],
data['scan0/moment_10']['attrs']['dyn_range_max'],
data['scan0/moment_10']['attrs']['format'])
self._phi = transform(data['scan0/moment_1']['data'],
data['scan0/moment_1']['attrs']['dyn_range_min'],
data['scan0/moment_1']['attrs']['dyn_range_max'],
data['scan0/moment_1']['attrs']['format'])
self._rho = transform(data['scan0/moment_2']['data'],
data['scan0/moment_2']['attrs']['dyn_range_min'],
data['scan0/moment_2']['attrs']['dyn_range_max'],
data['scan0/moment_2']['attrs']['format'])
self._zdr = transform(data['scan0/moment_9']['data'],
data['scan0/moment_9']['attrs']['dyn_range_min'],
data['scan0/moment_9']['attrs']['dyn_range_max'],
data['scan0/moment_9']['attrs']['format'])
self._vel = transform(data['scan0/moment_5']['data'],
data['scan0/moment_5']['attrs']['dyn_range_min'],
data['scan0/moment_5']['attrs']['dyn_range_max'],
data['scan0/moment_5']['attrs']['format'])
self._radar_height = data['where']['attrs']['height']
self._range_samples = data['scan0/how']['attrs']['range_samples']
self._range_step = data['scan0/how']['attrs']['range_step']
self._bin_count = data['scan0/how']['attrs']['bin_count']
self._elevation = data['scan0/how']['attrs']['elevation']
try:
self._date = dt.datetime.strptime(data['scan0/how']['attrs']['timestamp'].decode(), '%Y-%m-%dT%H:%M:%SZ')
except:
self._date = dt.datetime.strptime(data['scan0/how']['attrs']['timestamp'].decode(), '%Y-%m-%dT%H:%M:%S.000Z')
@property
def zh(self):
""" Returns DataSource
"""
return self._zh
@zh.setter
def zh(self, value):
self._zh = value
@property
def phi(self):
""" Returns DataSource
"""
return self._phi
@phi.setter
def phi(self, value):
self._phi = value
@property
def rho(self):
""" Returns DataSource
"""
return self._rho
@rho.setter
def rho(self, value):
self._rho = value
@property
def zdr(self):
""" Returns DataSource
"""
return self._zdr
@zdr.setter
def zdr(self, value):
self._zdr = value
@property
def date(self):
""" Returns DataSource
"""
return self._date
@property
def radar_height(self):
""" Returns DataSource
"""
return self._radar_height
@property
def range_step(self):
""" Returns DataSource
"""
return self._range_step
@property
def bin_count(self):
""" Returns DataSource
"""
return self._bin_count
@property
def range_samples(self):
""" Returns DataSource
"""
return self._range_samples
@property
def elevation(self):
""" Returns DataSource
"""
return self._elevation
def fig_ax(title, w, h):
fig = plt.figure(figsize=(w, h))
ax = fig.add_subplot(111)
ax.set_title(title)
return fig, ax
def add_contour(ax, X, Y, data, levels, **kwargs):
cs = ax.contour(X, Y, data, levels, **kwargs)
ax.clabel(cs, fmt='%2.0f', inline=True, fontsize=10)
def add_plot(mom, cfg):
ax = mom['ax']
levels = mom['levels']
if cfg['contour']:
im = ax.contourf(mom['data'], levels=levels,
colors=cfg['colors'], origin='lower', axis='equal',
extent=[cfg['dt_start'], cfg['dt_stop'],
-0.11, cfg['beam_height'][-1]])
else:
cmap = ListedColormap(cfg['colors']) # , name='')
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
im = ax.pcolormesh(cfg['X'], cfg['Y'], mom['data'], cmap=cmap, norm=norm)
ax.set_ylim(0, 13)
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('\n%H:%M'))
ax.xaxis.set_major_locator(mdates.HourLocator())
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%M'))
ax.xaxis.set_minor_locator(
mdates.MinuteLocator(byminute=(15, 30, 45, 60)))
ax.grid()
ax.set_ylabel('Height (km)')
ax.set_xlabel('Time (UTC)')
cb = mom['fig'].colorbar(im, orientation='vertical', pad=0.018, aspect=35)
cb.outline.set_visible(False)
cbarytks = plt.getp(cb.ax.axes, 'yticklines')
plt.setp(cbarytks, visible=False)
cb.set_ticks(levels[0:-1])
cb.set_ticklabels(["%.2f" % lev for lev in levels[0:-1]])
cb.set_label(mom['cb_label'])
def get_grid_from_gribfile(filename, rotated=False):
f = open(filename)
# get grib message count and create gid_list, close filehandle
msg_count = codes.codes_count_in_file(f)
gid_list = [codes.codes_grib_new_from_file(f) for i in range(msg_count)]
f.close()
print("Working on grib-file: {0}".format(filename))
print("Message Count: {0}".format(msg_count))
# read grib grid details from given gid
gid = gid_list[0]
return get_grid_from_gid(gid, rotated=rotated)
def get_grid_from_gid(gid, rotated=False):
Ni = codes.codes_get(gid, 'Ni')
Nj = codes.codes_get(gid, 'Nj')
lat_start = codes.codes_get(gid, 'latitudeOfFirstGridPointInDegrees')
lon_start = codes.codes_get(gid, 'longitudeOfFirstGridPointInDegrees')
lat_stop = codes.codes_get(gid, 'latitudeOfLastGridPointInDegrees')
lon_stop = codes.codes_get(gid, 'longitudeOfLastGridPointInDegrees')
print("LL: ({0},{1})".format(lon_start, lat_start))
print("UR: ({0},{1})".format(lon_stop, lat_stop))
lat_sp = codes.codes_get(gid, 'latitudeOfSouthernPole')
lon_sp = codes.codes_get(gid, 'longitudeOfSouthernPole')
ang_rot = codes.codes_get(gid, 'angleOfRotation')
print("SP: ({0},{1}) - Ang:{2}".format(lon_sp, lat_sp, ang_rot))
# create grid arrays from grid details
# iarr, jarr one-dimensional data
iarr = np.linspace(lon_start, lon_stop, num=Ni, endpoint=False)
jarr = np.linspace(lat_start, lat_stop, num=Nj, endpoint=False)
# converted by meshgrid to 2d-arrays
i2d, j2d = np.meshgrid(iarr, jarr)
grid_rot = np.dstack((i2d, j2d))
if not rotated:
return rotated_grid_transform(grid_rot, 1, [lon_sp, lat_sp])
else:
return grid_rot
# -----------------------------------------------------------------
def qvp_Boxpol():
"""
-----------------------------------------------------------------
main program
-----------------------------------------------------------------
"""
t1 = dt.datetime.now()
# for noise reduction: only for Bonn
# wichtig austauschen bei anderen bins
# aendern
print(file_path)
file_names = sorted(glob.glob(os.path.join(file_path, '*mvol')))
print(file_names[0])
# get some specifics of the radar data
ds0 = boxpol(file_names[0])
bin_range = ds0.range_samples * ds0.range_step
bin_count = ds0.bin_count
range_bin_dist = np.arange(bin_range/2, bin_range*bin_count+1, bin_range)
elevation = ds0.elevation
# get bins, azi from fist file
(azi, bins) = ds0.zh.shape
# try to load existing data
try:
save = np.load(output_path + '/' + location + '_' + date + '.npz')
result_data_phi = save['phi']
result_data_rho = save['rho']
result_data_zdr = save['zdr']
result_data_zh = save['zh']
radar_height = save['radar_height']
dt_src = save['dt_src']
# or create data from scratch
except IOError:
file_names = sorted(glob.glob(os.path.join(file_path, '*mvol')))
print(file_names)
file_list = []
for fname in file_names:
time = dt.datetime.strptime(os.path.splitext(os.path.basename(fname))[0], "%Y-%m-%d--%H:%M:%S,%f")
if time >= start_time and time <= end_time:
file_list.append(fname)
print(file_list)
file_names = file_list
n_files = len(file_names)
# define result arrays
result_data_zh = np.zeros((n_files, azi, bins))
result_data_phi = np.zeros((n_files, azi, bins))
result_data_rho = np.zeros((n_files, azi, bins))
result_data_zdr = np.zeros((n_files, azi, bins))
# -----------------------------------------------------------------
dt_src = [] # list for time stamps
# -----------------------------------------------------------------
# iterate over files and read data files
for n, fname in enumerate(file_names):
print("FILENAME:", fname)
dsl = boxpol(fname)
print(dsl.date)
print("ZH:", dsl.zh.shape)
print("RES:", result_data_zh.shape)
print("---------> Reading finished")
# add the offset
dsl.zh += offset_z
dsl.phi += offset_phi
dsl.zdr += offset_zdr
# noise reduction for rho_hv
# vorher -23
# vorher -21
noise_level = -23
# noise_level = -32
# aendern
snr = np.zeros((azi, bins))
# snr = np.zeros((360,1000))
# snr = np.zeros((360,60))
# snr = np.zeros((360,1100))
for i in range(azi):
snr[i, :] = dsl.zh[i, :] - 20 * np.log10(range_bin_dist * 0.001) - noise_level - \
0.033 * range_bin_dist / 1000
dsl.rho = dsl.rho * np.sqrt(1. + 1. / 10. ** (snr * 0.1))
result_data_zh[n, ...] = dsl.zh
result_data_phi[n, ...] = dsl.phi
result_data_rho[n, ...] = dsl.rho
result_data_zdr[n, ...] = dsl.zdr
dt_src.append(dsl.date)
# save data to file
np.savez(output_path + '/' + location + '_' + date, zh=result_data_zh, rho=result_data_rho,
zdr=result_data_zdr, phi=result_data_phi, dt_src=dt_src, radar_height=dsl.radar_height)
print("Result-Shape:", result_data_phi.shape)
# try top read phi and kdp from file
try:
save = np.load(output_path + '/' + location + '_' + date + '_phidp_kdp.npz')
phi = save['phi']
kdp = save['kdp']
test = save['test']
# or create from scratch
except:
import copy
phi = copy.copy(result_data_phi)
# process phidp
test = np.zeros_like(phi)
kdp = np.zeros_like(phi)
for i, v in enumerate(phi):
print("Timestep:", i)
for j, w in enumerate(v):
ma = movingaverage(w, 11, mode='same')
# window = [-1, 0, 0, 0, 1]
# slope = np.convolve(range(len(w)), window, mode='same') / np.convolve(w, window, mode='same')
# slope, intercept, r_value, p_value, std_err = stats.linregress(range(len(w)),w)
# slope = np.gradient(w)
# print(slope.shape)
# kdp[i, j, :] = slope
test[i, j, :] = ma
# test[i, j, result_data_zh[i, j, :] < -5.] = np.nan
test[i, j, result_data_zh[i, j, :] < -5.] = np.nan
# print(test[i, j, :])
first = np.argmax(test[i, j, :] >= 0.)
last = np.argmax(test[i, j, ::-1] >= 0)
# print("f:", first, test[i, j, first], test[i, j, first-1])
# print("l:", last, test[i, j, -last-1], test[i, j, -last])
if first:
test[i, j, :first + 1] = test[i, j, first]
if last:
test[i, j, -last:] = test[i, j, -last - 1]
# get kdp from phidp
# V1: kdp from convolution, maximum speed
kdp = wrl.dp.kdp_from_phidp_convolution(test, L=3, dr=1.0)
# V2: fit ala ryzhkov, see function declared above
#kdp = kdp_calc(test, wl=11)
print(kdp.shape)
# save data to file
np.savez(output_path + '/' + location + '_' + date + '_phidp_kdp', phi=phi, kdp=kdp, test=test)
# median calculation
result_data_zh = np.nanmedian(result_data_zh, axis=1).T
result_data_rho = np.nanmedian(result_data_rho, axis=1).T
result_data_zdr = np.nanmedian(result_data_zdr, axis=1).T
# mask kdp eventually,
for i in range(360):
k1 = kdp[:, i, :]
k1[result_data_zh.T < -5.] = np.nan
# print(k1.shape)
kdp[:, i, :] = k1
result_data_kdp = np.nanmedian(kdp, axis=1).T
# mask phidp eventually,
for i in range(360):
k2 = test[:, i, :]
k2[result_data_zh.T < -5.] = np.nan
# print(k1.shape)
test[:, i, :] = k2
result_data_phi = np.nanmedian(test, axis=1).T
# result_data_phi_median = stats.nanmedian(phi, axis=1).T
print("SHAPE1:", result_data_phi.shape, result_data_zdr.shape)
# -----------------------------------------------------------------
# calulate beam_height: array with 350 elements
# Elevation angle is hardcoded here
# aendern
#beam_height = (wrl.georef.beam_height_n(np.linspace(0, (bins - 1) * 100, bins), 28.0)
# + radar_height) / 1000
beam_height = (wrl.georef.beam_height_n(range_bin_dist, round(elevation,1))
+ radar_height) / 1000
# COSMO prozessing
cosmo_path = '/automount/cluma04/CNRW/CNRW_5.00_grb2/cosmooutput/' \
'out_{0}-00/'.format(date)
## read grid from gribfile
#filename = cosmo_path + 'lfff00{0}{1:02d}00'.format(16,0)
#ll_grid = get_grid_from_gribfile(filename)
# read grid from constants file
filename = cosmo_path + 'lfff00000000c'
print(filename)
rlat = mecc.get_ecc_value_from_file(filename, 'shortName', 'RLAT')
rlon = mecc.get_ecc_value_from_file(filename, 'shortName', 'RLON')
ll_grid = np.dstack((rlon, rlat))
print("rlat, rlon", rlat.shape, rlon.shape)
# calculate boxpol grid indices
boxpol_coords = (7.071663, 50.73052)
llx = np.searchsorted(ll_grid[0, :, 0], boxpol_coords[0], side='left')
lly = np.searchsorted(ll_grid[:, 0, 1], boxpol_coords[1], side='left')
print("Coords Bonn: ({0},{1})".format(llx, lly))
# read height layers from constants file
filename = cosmo_path + 'lfff00000000c'
hhl = mecc.get_ecc_value_from_file(filename,
'shortName',
'HHL')[llx, lly, ...]
# get km from meters
hhl = hhl / 1000.
# reading temperature from associated comso files
# getting count of comso files
# beware only available from 00:00 to 21:30
tcount = int(divmod((end_time - start_time).total_seconds(), 60*30)[0] + 1)
print(tcount)
# create timestamps every full 30th minute (00, 30)
cosmo_dt_arr = [(start_time + dt.timedelta(minutes=30 * i)) for i in range(tcount)]
cosmo_time_arr = [(start_time + dt.timedelta(minutes=30 * i)).time() for i in range(tcount)]
print(cosmo_dt_arr)
# create temperature array and read from grib files
temp = np.zeros((len(cosmo_time_arr), 50))
for it, t in enumerate(cosmo_time_arr):
filename = cosmo_path + 'lfff00{:%H%M%S}'.format(t)
print(filename)
temp[it, ...] = mecc.get_ecc_value_from_file(filename, 'shortName', 't')[llx,lly,...]
# we need degree celsius, not kelvins
temp = temp - 273.15
# text output temperature
fn = '{0}_output_{1}_{2}.txt'.format('temp', location, date)
header = "Temperature: {0}\tDATE: {1}\n".format(
location, date)
y_hhl = np.diff(hhl) / 2 + hhl[:-1]
print(y_hhl.shape, temp)
index, tindex = temp.T.shape
cosmo_time_arr = [(start_time + dt.timedelta(minutes=30 * i)).time() for
i in range(tindex)]
header2 = "\nBin Height/km " + ' '.join(
['{0:02d}:{1:02d}'.format(tx.hour, tx.minute) for tx in
cosmo_time_arr]) + '\n'
iarr = np.array(range(index))
fmt = ['%d', '%0.4f'] + ['%0.4f'] * tindex
print(iarr.shape, y_hhl.shape, temp.shape)
np.savetxt(textfile_path + '/' + fn,
np.vstack([iarr, y_hhl[::-1], temp[:,::-1]]).T,
fmt=fmt, delimiter=' ',
header=header + header2)
# -----------------------------------------------------------------
# result_data_... plotting
# time stamps for plotting
dt_start = mdates.date2num(dt_src[0])
dt_stop = mdates.date2num(dt_src[-1])
# contour lines
# if true plot contours, if false plot pcolormesh
contour = True
# zh Contourlevels for zh isoline overlay
contourlevels = [0, 5, 10, 20, 30]
# define the colors
colors = '#00f8f8', '#01b8fb', '#0000fa', '#00ff00', '#00d000', '#009400', \
'#ffff00', '#f0cc00', '#ff9e00', '#ff0000', '#e40000', '#cc0000', \
'#ff00ff', '#a06cd5'
# dictionaries for moments, with some configuration
zdr = {'name': 'zdr',
'data': result_data_zdr,
'levels': [-2, -1, 0, .1, .2, .3, .4, .5, .6, .8, 1.0, 1.2, 1.5,
2.0, 2.5],
'title': '$\mathrm{\mathsf{Z_{DR}}}$',
'cb_label': 'Differential Reflectivity (dB)'}
zh = {'name': 'zh',
'data': result_data_zh,
'levels': [-10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50,
60, 65],
'title': '$\mathrm{\mathsf{Z_{H}}}$',
'cb_label': 'Reflectivity (dBz)'}
phi = {'name': 'phi',
'data': result_data_phi,
'levels': [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 25, 30, 35,
40, 100],
'title': '$\mathrm{\mathsf{\phi_{DP}}}$',
'cb_label': 'Differential Phase (deg)'}
rho = {'name': 'rho',
'data': result_data_rho,
'levels': [.7, .8, .85, .9, .92, .94, .95, .96, .97, .98, .985,
.99, .995, 1, 1.1],
'title': r'$\mathrm{\mathsf{\rho_{hv}}}$',
'cb_label': 'Crosscorrelation Coefficient'}
kdp = {'name': 'kdp',
'data': result_data_kdp,
'levels': [-0.5, -0.1, 0, 0.05, 0.10, 0.20, 0.30, 0.40, 0.60, 0.80,
1.0, 2., 3., 4.],
'title': '$\mathrm{\mathsf{K_{DP}}}$',
'cb_label': 'Specific differential Phase (deg/km)'}
moments = {'zh': zh ,
'zdr': zdr,
'phi': phi,
'rho': rho,
'kdp': kdp
}
# x-y-grid for radar data
y = beam_height
x = mdates.date2num(dt_src)
X, Y = np.meshgrid(x, y)
# x-y-grid for grib data
# we use hhl, so we have to calculate the mid of the layers.
y_hhl = np.diff(hhl) / 2 + hhl[:-1]
print(y_hhl.shape, hhl.shape)
x_temp = mdates.date2num(cosmo_dt_arr)
X1, Y1 = np.meshgrid(x_temp, y_hhl)
# cfg dict
cfg = {'dt_start': dt_start,
'dt_stop': dt_stop,
'contour': contour,
'contourlevels': contourlevels,
'colors': colors,
'X': X,
'Y': Y,
'beam_height': beam_height
}
# iterate over moments dict
for k, mom in moments.items():
# create figure
mom['fig'], mom['ax'] = fig_ax(mom['title'], plot_width, plot_height)
# add zh overlay contour
add_contour(mom['ax'], X, Y, moments['zh']['data'], contourlevels,
manual='True', origin='lower', colors='k', alpha=0.8,
linewidths=1,
extent=[dt_start, dt_stop, -0.11, beam_height[-1]])
# add temperature contour
add_contour(mom['ax'], X1, Y1, temp.T, [-15, -10, -5, 0], manual='True',
origin='lower', colors='k', alpha=0.8,
linewidths=2)
# add data to images
add_plot(mom, cfg)
# save images
mom['fig'].savefig(plot_path + mom['name'] + location + '_' + date + '.png',
dpi=300, bbox_inches='tight')
# text output
fn = '{0}_output_{1}_{2}.txt'.format(mom['name'], location, date)
header = "Radar: {0}\n\n\t{1}-DATA\tDATE: {2}\n".format(
location, mom['name'].upper(), date)
index, tindex = mom['data'].shape
radar_time_arr = [(start_time + dt.timedelta(minutes=5 * i)).time() for
i in range(tindex)]
header2 = "\nBin Height/km " + ' '.join(
['{0:02d}:{1:02d}'.format(tx.hour, tx.minute) for tx in
radar_time_arr]) + '\n'
iarr = np.array(range(index))
fmt = ['%d', '%0.4f'] + ['%0.13f'] * tindex
np.savetxt(textfile_path + '/' + fn,
np.vstack([iarr, beam_height, mom['data'].T]).T, fmt=fmt,
header=header + header2)
plt.show()
t2 = dt.datetime.now()
print('Elapsed time: %12.7f seconds.' % ((t2 - t1).total_seconds()))
# =======================================================
if __name__ == '__main__':
qvp_Boxpol()