-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_numerical_diffusion_spectra.py
executable file
·135 lines (111 loc) · 4.97 KB
/
plot_numerical_diffusion_spectra.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
#!/bin/env python
"""
Script to verify numerical diffusion of deterministic advection nowcasts.
"""
from datetime import datetime, timedelta
import matplotlib.pylab as plt
from matplotlib.pyplot import cm
import numpy as np
import sys
import pysteps as stp
from pysteps import rcparams
# Import or define precip events
sys.path.insert(0,'..')
import precipevents
domain = "mch_hdf5" #"mch_hdf5"
if domain == "fmi":
# all events
precipevents = precipevents.fmi
# only one event (for paper)
precipevents = [("201609281600", "201609281700")]
if domain == "mch_hdf5":
# all events
precipevents = precipevents.mch
# only 2 events
precipevents = [("201701311000", "201701311000"),
("201607111300", "201607111300")]
# Forecast parameters
timestep_run = 240
n_lead_times = 24
num_prev_files = 9 # Only applies for darts
# Experiment parameters
of_methods = ["darts", "lucaskanade", "vet"]
# Figure parameters
plot_leadtimes = [11, 23]
linestyles_leadtimes = ['-', '--']
colors_of = ['C0', 'C1', 'C2']
wavelength_ticks = [512,256,128,64,32,16,8,4,2]
fmt = 'pdf'
# Loop over events
datasource = rcparams.data_sources[domain]
root_path = datasource["root_path"]
importer = stp.io.get_method(datasource["importer"], "importer")
for pei,pe in enumerate(precipevents):
curdate = datetime.strptime(pe[0], "%Y%m%d%H%M")
enddate = datetime.strptime(pe[1], "%Y%m%d%H%M")
print('Analyzing event start , end', curdate,',',enddate)
while curdate <= enddate:
print('Analyzing', curdate)
## read two consecutive radar fields
fns = stp.io.archive.find_by_date(curdate, root_path, datasource["path_fmt"],
datasource["fn_pattern"], datasource["fn_ext"],
datasource["timestep"], num_prev_files=num_prev_files)
R,_,metadata = stp.io.readers.read_timeseries(fns, importer, **datasource["importer_kwargs"])
## convert to mm/h
R, metadata = stp.utils.to_rainrate(R, metadata)
## threshold the data
R[R<0.1] = 0.0
metadata["threshold"] = 0.1
## copy the original data
R_ = R.copy()
## set NaN equal to zero
R_[~np.isfinite(R_)] = 0.0
## transform to dBR
R_, metadata_dbr = stp.utils.dB_transform(R_,metadata)
# Plot Fourier spectrum of observations
plt.figure()
ax = plt.subplot(111)
R_obs_spectrum, fft_freq = stp.utils.rapsd(stp.utils.remove_rain_norain_discontinuity(R_[-1,:,:]), np.fft, d=1.0, return_freq=True)
stp.plt.plot_spectrum1d(fft_freq, R_obs_spectrum, x_units='km', y_units='dBR', label='Observation', wavelength_ticks=wavelength_ticks, color='k', lw=1.0, ax=ax)
c=0
for of_method in of_methods:
# Compute motion field
oflow_method = stp.motion.get_method(of_method)
if of_method == "darts":
UV = oflow_method(R_)
else:
UV = oflow_method(R_[-2:,:,:])
# Simple advection nowcast
adv_method_init, adv_method = stp.extrapolation.get_method("semilagrangian")
extrapolator = adv_method_init(shape=R_[-1,:,:].shape)
R_fct = adv_method(extrapolator, R_[-1,:,:], UV, n_lead_times, verbose=True)
R_fct[np.isnan(R_fct)] = metadata_dbr["zerovalue"]
# Plot Fourier spectra of nowcasts
if of_method == "darts":
of_method_txt = "DARTS"
if of_method == "lucaskanade":
of_method_txt = "Lucas-Kanade"
if of_method == "vet":
of_method_txt = "VET"
l = 0
for t in plot_leadtimes:
R_fct_shift = stp.utils.remove_rain_norain_discontinuity(R_fct[t,:,:])
R_fct_spectrum, fft_freq = stp.utils.rapsd(R_fct_shift, np.fft, d=1.0, return_freq=True)
stp.plt.plot_spectrum1d(fft_freq, R_fct_spectrum, color=colors_of[c], linestyle=linestyles_leadtimes[l], lw=1.0, ax=ax, label=of_method_txt + ' +' + str(int((t+1)*metadata["accutime"])) + ' min')
l+=1
c+=1
# Decorate plot
plt.legend(loc="lower left")
ax.set_title("Simple advection", fontsize=18)
ax.set_ylim([-10,60])
plt.setp(ax.get_xticklabels(), fontsize=14)
plt.setp(ax.get_yticklabels(), fontsize=14)
ax.xaxis.label.set_size(16)
ax.yaxis.label.set_size(16)
ax.grid()
# Savefig
figname = 'figures/' + domain[0:3] + '_' + curdate.strftime("%Y%m%d%H%M") + '_numerical_diffusion_spectra.' + fmt
plt.savefig(figname, bbox_inches="tight", dpi=200)
print(figname, 'saved.')
curdate += timedelta(minutes=timestep_run)
print("Finished!")