Skip to content

Commit 3121cbf

Browse files
authored
Update RainFARM example following #311 (#367)
* Update RainFARM example following #311
1 parent 9d4e169 commit 3121cbf

File tree

1 file changed

+107
-61
lines changed

1 file changed

+107
-61
lines changed

Diff for: examples/rainfarm_downscale.py

+107-61
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,71 @@
1010
al. (2006). The method can represent the realistic small-scale variability of the
1111
downscaled precipitation field by means of Gaussian random fields.
1212
13+
Steps:
14+
1. Read the input precipitation data.
15+
2. Upscale the precipitation field.
16+
3. Downscale the field to its original resolution using RainFARM with defaults.
17+
4. Downscale with smoothing.
18+
5. Downscale with spectral fusion.
19+
6. Downscale with smoothing and spectral fusion.
20+
21+
References:
22+
23+
Rebora, N., L. Ferraris, J. von Hardenberg, and A. Provenzale, 2006: RainFARM:
24+
Rainfall downscaling by a filtered autoregressive model. J. Hydrometeor., 7,
25+
724–738.
26+
27+
D D'Onofrio, E Palazzi, J von Hardenberg, A Provenzale, and S Calmanti, 2014:
28+
Stochastic rainfall downscaling of climate models. J. Hydrometeorol., 15(2):830–843.
1329
"""
1430

1531
import matplotlib.pyplot as plt
1632
import numpy as np
1733
import os
1834
from pprint import pprint
35+
import logging
1936

2037
from pysteps import io, rcparams
2138
from pysteps.utils import aggregate_fields_space, square_domain, to_rainrate
2239
from pysteps.downscaling import rainfarm
2340
from pysteps.visualization import plot_precip_field
2441

42+
# Configure logging
43+
logging.basicConfig(
44+
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
45+
)
46+
2547
###############################################################################
2648
# Read the input data
2749
# -------------------
2850
#
2951
# As first step, we need to import the precipitation field that we are going
3052
# to use in this example.
3153

54+
55+
def read_precipitation_data(file_path):
56+
"""Read and process precipitation data from a file."""
57+
precip, _, metadata = io.import_mch_gif(
58+
file_path, product="AQC", unit="mm", accutime=5.0
59+
)
60+
precip, metadata = to_rainrate(precip, metadata)
61+
precip, metadata = square_domain(precip, metadata, "crop")
62+
return precip, metadata
63+
64+
3265
# Import the example radar composite
3366
root_path = rcparams.data_sources["mch"]["root_path"]
3467
filename = os.path.join(root_path, "20160711", "AQC161932100V_00005.801.gif")
35-
precip, _, metadata = io.import_mch_gif(
36-
filename, product="AQC", unit="mm", accutime=5.0
37-
)
38-
39-
# Convert to mm/h
40-
precip, metadata = to_rainrate(precip, metadata)
4168

42-
# Reduce to a square domain
43-
precip, metadata = square_domain(precip, metadata, "crop")
69+
# Read and process data
70+
precip, metadata = read_precipitation_data(filename)
4471

4572
# Nicely print the metadata
4673
pprint(metadata)
4774

4875
# Plot the original rainfall field
4976
plot_precip_field(precip, geodata=metadata)
77+
plt.title("Original Rainfall Field")
5078
plt.show()
5179

5280
# Assign the fill value to all the Nans
@@ -61,60 +89,87 @@
6189
# create a lower resolution field to apply our downscaling method.
6290
# We are going to use a factor of 16 x.
6391

92+
93+
def upscale_field(precip, metadata, scale_factor):
94+
"""Upscale the precipitation field by a given scale factor."""
95+
upscaled_resolution = metadata["xpixelsize"] * scale_factor
96+
precip_lr, metadata_lr = aggregate_fields_space(
97+
precip, metadata, upscaled_resolution
98+
)
99+
return precip_lr, metadata_lr
100+
101+
64102
scale_factor = 16
65-
upscaled_resolution = (
66-
metadata["xpixelsize"] * scale_factor
67-
) # upscaled resolution : 16 km
68-
precip_lr, metadata_lr = aggregate_fields_space(precip, metadata, upscaled_resolution)
103+
precip_lr, metadata_lr = upscale_field(precip, metadata, scale_factor)
69104

70105
# Plot the upscaled rainfall field
71106
plt.figure()
72107
plot_precip_field(precip_lr, geodata=metadata_lr)
108+
plt.title("Upscaled Rainfall Field")
109+
plt.show()
73110

74111
###############################################################################
75112
# Downscale the field
76113
# -------------------
77114
#
78-
# We can now use RainFARM to generate stochastic realizations of the downscaled
79-
# precipitation field.
80-
81-
fig = plt.figure(figsize=(5, 8))
82-
# Set the number of stochastic realizations
83-
num_realizations = 5
84-
85-
# Per realization, generate a stochastically downscaled precipitation field
86-
# and plot it.
87-
# The first time, the spectral slope alpha needs to be estimated. To illustrate
88-
# the sensitivity of this parameter, we are going to plot some realizations with
89-
# half or double the estimated slope.
90-
alpha = None
91-
for n in range(num_realizations):
92-
# Spectral slope estimated from the upscaled field
93-
precip_hr, alpha = rainfarm.downscale(
94-
precip_lr, ds_factor=scale_factor, alpha=alpha, return_alpha=True
95-
)
96-
plt.subplot(num_realizations, 3, n * 3 + 2)
97-
plot_precip_field(precip_hr, geodata=metadata, axis="off", colorbar=False)
98-
if n == 0:
99-
plt.title(f"alpha={alpha:.1f}")
100-
101-
# Half the estimated slope
102-
precip_hr = rainfarm.downscale(precip_lr, ds_factor=scale_factor, alpha=alpha * 0.5)
103-
plt.subplot(num_realizations, 3, n * 3 + 1)
104-
plot_precip_field(precip_hr, geodata=metadata, axis="off", colorbar=False)
105-
if n == 0:
106-
plt.title(f"alpha={alpha * 0.5:.1f}")
107-
108-
# Double the estimated slope
109-
precip_hr = rainfarm.downscale(precip_lr, ds_factor=scale_factor, alpha=alpha * 2)
110-
plt.subplot(num_realizations, 3, n * 3 + 3)
111-
plot_precip_field(precip_hr, geodata=metadata, axis="off", colorbar=False)
112-
if n == 0:
113-
plt.title(f"alpha={alpha * 2:.1f}")
114-
115-
plt.subplots_adjust(wspace=0, hspace=0)
116-
117-
plt.tight_layout()
115+
# We can now use RainFARM to downscale the precipitation field.
116+
117+
# Basic downscaling
118+
precip_hr = rainfarm.downscale(precip_lr, ds_factor=scale_factor)
119+
120+
# Plot the downscaled rainfall field
121+
plt.figure()
122+
plot_precip_field(precip_hr, geodata=metadata)
123+
plt.title("Downscaled Rainfall Field")
124+
plt.show()
125+
126+
###############################################################################
127+
# Downscale with smoothing
128+
# ------------------------
129+
#
130+
# Add smoothing with a Gaussian kernel during the downscaling process.
131+
132+
precip_hr_smooth = rainfarm.downscale(
133+
precip_lr, ds_factor=scale_factor, kernel_type="gaussian"
134+
)
135+
136+
# Plot the downscaled rainfall field with smoothing
137+
plt.figure()
138+
plot_precip_field(precip_hr_smooth, geodata=metadata)
139+
plt.title("Downscaled Rainfall Field with Gaussian Smoothing")
140+
plt.show()
141+
142+
###############################################################################
143+
# Downscale with spectral fusion
144+
# ------------------------------
145+
#
146+
# Apply spectral merging as described in D'Onofrio et al. (2014).
147+
148+
precip_hr_fusion = rainfarm.downscale(
149+
precip_lr, ds_factor=scale_factor, spectral_fusion=True
150+
)
151+
152+
# Plot the downscaled rainfall field with spectral fusion
153+
plt.figure()
154+
plot_precip_field(precip_hr_fusion, geodata=metadata)
155+
plt.title("Downscaled Rainfall Field with Spectral Fusion")
156+
plt.show()
157+
158+
###############################################################################
159+
# Combined Downscale with smoothing and spectral fusion
160+
# -----------------------------------------------------
161+
#
162+
# Apply both smoothing with a Gaussian kernel and spectral fusion during the
163+
# downscaling process to observe the combined effect.
164+
165+
precip_hr_combined = rainfarm.downscale(
166+
precip_lr, ds_factor=scale_factor, kernel_type="gaussian", spectral_fusion=True
167+
)
168+
169+
# Plot the downscaled rainfall field with smoothing and spectral fusion
170+
plt.figure()
171+
plot_precip_field(precip_hr_combined, geodata=metadata)
172+
plt.title("Downscaled Rainfall Field with Gaussian Smoothing and Spectral Fusion")
118173
plt.show()
119174

120175
###############################################################################
@@ -126,13 +181,4 @@
126181
# the original algorithm from Rebora et al. (2006), it cannot downscale the temporal
127182
# dimension.
128183

129-
130-
###############################################################################
131-
# References
132-
# ----------
133-
#
134-
# Rebora, N., L. Ferraris, J. von Hardenberg, and A. Provenzale, 2006: RainFARM:
135-
# Rainfall downscaling by a filtered autoregressive model. J. Hydrometeor., 7,
136-
# 724–738.
137-
138184
# sphinx_gallery_thumbnail_number = 2

0 commit comments

Comments
 (0)