Skip to content

Commit b17a98c

Browse files
committed
Update tutorials
1 parent 14e8aa8 commit b17a98c

File tree

3 files changed

+49
-68
lines changed

3 files changed

+49
-68
lines changed

examples/plot_noise_generators.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333

3434
# Import the example radar composite
3535
root_path = rcparams.data_sources["mch"]["root_path"]
36-
filename = os.path.join(
37-
root_path, "20160711", "AQC161932100V_00005.801.gif"
38-
)
36+
filename = os.path.join(root_path, "20160711", "AQC161932100V_00005.801.gif")
3937
R, _, metadata = io.import_mch_gif(filename, product="AQC", unit="mm", accutime=5.0)
4038

4139
# Convert to mm/h
@@ -158,16 +156,16 @@
158156

159157
###############################################################################
160158
# The above figure highlights the main limitation of the parametric approach
161-
# (top row), that is, the assumption of an isotropic power law scaling
162-
# relationship, meaning that anisotropic structures such as rainfall bands
163-
# cannot be represented.
159+
# (top row), that is, the assumption of an isotropic power law scaling
160+
# relationship, meaning that anisotropic structures such as rainfall bands
161+
# cannot be represented.
164162
#
165-
# Instead, the nonparametric approach (bottom row) allows generating
166-
# perturbation fields with anisotropic structures, but it also requires a
167-
# larger sample size and is sensitive to the quality of the input data, e.g.
163+
# Instead, the nonparametric approach (bottom row) allows generating
164+
# perturbation fields with anisotropic structures, but it also requires a
165+
# larger sample size and is sensitive to the quality of the input data, e.g.
168166
# the presence of residual clutter in the radar image.
169167
#
170-
# In addition, both techniques assume spatial stationarity of the covariance
168+
# In addition, both techniques assume spatial stationarity of the covariance
171169
# structure of the field.
172170

173-
# sphinx_gallery_thumbnail_number = 3
171+
# sphinx_gallery_thumbnail_number = 3

examples/plot_optical_flow.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
Optical flow
44
============
55
6-
This tutorial offers a short overview to the optical flow routines available in
6+
This tutorial offers a short overview of the optical flow routines available in
77
pysteps and it will cover how to compute and plot the motion field from a
88
sequence of radar images.
99
1010
"""
1111

1212
from datetime import datetime
13-
from matplotlib import pyplot
1413
import numpy as np
1514
from pprint import pprint
1615
from pysteps import io, motion, rcparams
@@ -21,20 +20,25 @@
2120
# Read the radar input images
2221
# ---------------------------
2322
#
24-
# First thing, the sequence of radar composites is imported, converted and
23+
# First thing, the sequence of radar composites is imported, converted and
2524
# transformed into units of dBR.
2625

2726
date = datetime.strptime("201505151630", "%Y%m%d%H%M")
2827
data_source = "mch"
28+
29+
# Load data source config
2930
root_path = rcparams.data_sources[data_source]["root_path"]
3031
path_fmt = rcparams.data_sources[data_source]["path_fmt"]
3132
fn_pattern = rcparams.data_sources[data_source]["fn_pattern"]
3233
fn_ext = rcparams.data_sources[data_source]["fn_ext"]
3334
importer_name = rcparams.data_sources[data_source]["importer"]
3435
importer_kwargs = rcparams.data_sources[data_source]["importer_kwargs"]
36+
timestep = rcparams.data_sources[data_source]["timestep"]
3537

3638
# Find the input files from the archive
37-
fns = io.archive.find_by_date(date, root_path, path_fmt, fn_pattern, fn_ext, timestep=5, num_prev_files=9)
39+
fns = io.archive.find_by_date(
40+
date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=9
41+
)
3842

3943
# Read the radar composites
4044
importer = io.get_method(importer_name, "importer")
@@ -44,7 +48,7 @@
4448
R, metadata = conversion.to_rainrate(R, metadata)
4549

4650
# Store the last frame for polotting it later later
47-
R_ = R[-1, : , :].copy()
51+
R_ = R[-1, :, :].copy()
4852

4953
# Log-transform the data
5054
R, metadata = transformation.dB_transform(R, metadata, threshold=0.1, zerovalue=-15.0)
@@ -57,53 +61,53 @@
5761
# -----------------
5862
#
5963
# The Lucas-Kanade optical flow method implemented in pysteps is a local
60-
# tracking apporach that relies on the OpenCV package.
64+
# tracking approach that relies on the OpenCV package.
6165
# Local features are tracked in a sequence of two or more radar images. The
62-
# scheme includes a final interpolation step in order to produce a smooth
66+
# scheme includes a final interpolation step in order to produce a smooth
6367
# field of motion vectors.
6468

6569
oflow_method = motion.get_method("LK")
6670
V1 = oflow_method(R[-3:, :, :])
6771

6872
# Plot the motion field
69-
plot_precip_field(R_, geodata=metadata, title="Lucas-Kanade")
73+
plot_precip_field(R_, geodata=metadata, title="LK")
7074
quiver(V1, geodata=metadata, step=25)
7175

7276
###############################################################################
7377
# Variational echo tracking (VET)
7478
# -------------------------------
7579
#
76-
# This module implements the VET algorithm presented
77-
# by Laroche and Zawadzki (1995) and used in the McGill Algorithm for
78-
# Prediction by Lagrangian Extrapolation (MAPLE) described in
80+
# This module implements the VET algorithm presented
81+
# by Laroche and Zawadzki (1995) and used in the McGill Algorithm for
82+
# Prediction by Lagrangian Extrapolation (MAPLE) described in
7983
# Germann and Zawadzki (2002).
8084
# The approach essentially consists of a global optimization routine that seeks
81-
# at minimizing a cost function between the displaced and the reference image.
85+
# at minimizing a cost function between the displaced and the reference image.
8286

8387
oflow_method = motion.get_method("VET")
8488
V2 = oflow_method(R[-3:, :, :])
8589

8690
# Plot the motion field
87-
plot_precip_field(R_, geodata=metadata, title="Variational echo tracking")
91+
plot_precip_field(R_, geodata=metadata, title="VET")
8892
quiver(V2, geodata=metadata, step=25)
8993

9094
###############################################################################
9195
# Dynamic and adaptive radar tracking of storms (DARTS)
9296
# -----------------------------------------------------
9397
#
9498
# DARTS uses a spectral approach to optical flow that is based on the discrete
95-
# Fourier transform (DFT) of a temporal sequence of radar fields.
96-
# The level of truncation of the DFT coefficients controls the degree of
97-
# smoothness of the estimated motion field, allowing for an efficient
98-
# motion estimation. DARTS requires a longer sequence of radar fields for
99+
# Fourier transform (DFT) of a temporal sequence of radar fields.
100+
# The level of truncation of the DFT coefficients controls the degree of
101+
# smoothness of the estimated motion field, allowing for an efficient
102+
# motion estimation. DARTS requires a longer sequence of radar fields for
99103
# estimating the motion, here we are going to use all the available 10 fields.
100104

101105
oflow_method = motion.get_method("DARTS")
102106
R[~np.isfinite(R)] = metadata["zerovalue"]
103-
V3 = oflow_method(R) # needs longer training sequence
107+
V3 = oflow_method(R) # needs longer training sequence
104108

105109
# Plot the motion field
106110
plot_precip_field(R_, geodata=metadata, title="DARTS")
107111
quiver(V3, geodata=metadata, step=25)
108112

109-
# sphinx_gallery_thumbnail_number = 1
113+
# sphinx_gallery_thumbnail_number = 1

examples/plot_steps_nowcast.py

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,28 @@
2020
from pysteps.visualization import plot_precip_field
2121

2222
# Set nowcast parameters
23-
date = datetime.strptime("201609281600", "%Y%m%d%H%M")
2423
n_ens_members = 12
24+
n_leadtimes = 12
2525
seed = 24
2626

2727
###############################################################################
2828
# Read precipitation field
2929
# ------------------------
3030
#
31-
# First thing, the sequence of Finnish radar composites is imported, converted and
31+
# First thing, the sequence of Finnish radar composites is imported, converted and
3232
# transformed into units of dBR.
3333

34+
date = datetime.strptime("201701311200", "%Y%m%d%H%M")
35+
data_source = "mch"
36+
3437
# Load data source config
35-
root_path = rcparams.data_sources["fmi"]["root_path"]
36-
path_fmt = rcparams.data_sources["fmi"]["path_fmt"]
37-
fn_pattern = rcparams.data_sources["fmi"]["fn_pattern"]
38-
fn_ext = rcparams.data_sources["fmi"]["fn_ext"]
39-
timestep = rcparams.data_sources["fmi"]["timestep"]
38+
root_path = rcparams.data_sources[data_source]["root_path"]
39+
path_fmt = rcparams.data_sources[data_source]["path_fmt"]
40+
fn_pattern = rcparams.data_sources[data_source]["fn_pattern"]
41+
fn_ext = rcparams.data_sources[data_source]["fn_ext"]
42+
importer_name = rcparams.data_sources[data_source]["importer"]
43+
importer_kwargs = rcparams.data_sources[data_source]["importer_kwargs"]
44+
timestep = rcparams.data_sources[data_source]["timestep"]
4045

4146
# Find the radar files in the archive
4247
inputfns = find_by_date(
@@ -73,7 +78,7 @@
7378
R_f = nowcast_method(
7479
R[-3:, :, :],
7580
V,
76-
12,
81+
n_leadtimes,
7782
n_cascade_levels=8,
7883
R_thr=-10.0,
7984
decomp_method="fft",
@@ -86,15 +91,7 @@
8691

8792
# Plot the S-PROG forecast
8893
figure()
89-
bm = plot_precip_field(
90-
R_f[-1, :, :],
91-
map="basemap",
92-
geodata=metadata,
93-
drawlonlatlines=False,
94-
basemap_resolution="h",
95-
basemap_scale_args=[30.0, 58.5, 30.2, 58.5, 120],
96-
title="S-PROG",
97-
)
94+
bm = plot_precip_field(R_f[-1, :, :], geodata=metadata, title="S-PROG")
9895

9996
###############################################################################
10097
# As we can see from the figure above, the forecast produced by S-PROG is a
@@ -140,35 +137,20 @@
140137
# Plot the ensemble mean
141138
R_f_mean = np.mean(R_f[:, -1, :, :], axis=0)
142139
figure()
143-
bm = plot_precip_field(
144-
R_f_mean,
145-
map="basemap",
146-
geodata=metadata,
147-
drawlonlatlines=False,
148-
basemap_resolution="h",
149-
basemap_scale_args=[30.0, 58.5, 30.2, 58.5, 120],
150-
title="Ensemble mean",
151-
)
140+
bm = plot_precip_field(R_f_mean, geodata=metadata, title="Ensemble mean")
152141

153142
###############################################################################
154143
# The mean of the ensemble displays similar properties as the S-PROG
155144
# forecast seen above, although the degree of smoothing strongly depends on
156-
# the ensemble size. In this sense, the S-PROG forecast can be seen as
145+
# the ensemble size. In this sense, the S-PROG forecast can be seen as
157146
# the mean forecast from an ensemble of infinite size.
158147

159148
# Plot the first two realizations
160149
fig = figure()
161150
for i in range(2):
162151
ax = fig.add_subplot(121 + i)
163152
ax.set_title("Member %02d" % i)
164-
bm = plot_precip_field(
165-
R_f[i, -1, :, :],
166-
map="basemap",
167-
geodata=metadata,
168-
drawlonlatlines=False,
169-
basemap_resolution="h",
170-
basemap_scale_args=[30.0, 58.5, 30.2, 58.5, 120],
171-
)
153+
bm = plot_precip_field(R_f[i, -1, :, :], geodata=metadata)
172154
tight_layout()
173155

174156
###############################################################################
@@ -183,11 +165,8 @@
183165
figure()
184166
bm = plot_precip_field(
185167
P,
186-
map="basemap",
187168
geodata=metadata,
188169
drawlonlatlines=False,
189-
basemap_resolution="h",
190-
basemap_scale_args=[30.0, 58.5, 30.2, 58.5, 120],
191170
type="prob",
192171
units="mm/h",
193172
probthr=0.5,

0 commit comments

Comments
 (0)