|
10 | 10 | al. (2006). The method can represent the realistic small-scale variability of the
|
11 | 11 | downscaled precipitation field by means of Gaussian random fields.
|
12 | 12 |
|
| 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. |
13 | 29 | """
|
14 | 30 |
|
15 | 31 | import matplotlib.pyplot as plt
|
16 | 32 | import numpy as np
|
17 | 33 | import os
|
18 | 34 | from pprint import pprint
|
| 35 | +import logging |
19 | 36 |
|
20 | 37 | from pysteps import io, rcparams
|
21 | 38 | from pysteps.utils import aggregate_fields_space, square_domain, to_rainrate
|
22 | 39 | from pysteps.downscaling import rainfarm
|
23 | 40 | from pysteps.visualization import plot_precip_field
|
24 | 41 |
|
| 42 | +# Configure logging |
| 43 | +logging.basicConfig( |
| 44 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 45 | +) |
| 46 | + |
25 | 47 | ###############################################################################
|
26 | 48 | # Read the input data
|
27 | 49 | # -------------------
|
28 | 50 | #
|
29 | 51 | # As first step, we need to import the precipitation field that we are going
|
30 | 52 | # to use in this example.
|
31 | 53 |
|
| 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 | + |
32 | 65 | # Import the example radar composite
|
33 | 66 | root_path = rcparams.data_sources["mch"]["root_path"]
|
34 | 67 | 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) |
41 | 68 |
|
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) |
44 | 71 |
|
45 | 72 | # Nicely print the metadata
|
46 | 73 | pprint(metadata)
|
47 | 74 |
|
48 | 75 | # Plot the original rainfall field
|
49 | 76 | plot_precip_field(precip, geodata=metadata)
|
| 77 | +plt.title("Original Rainfall Field") |
50 | 78 | plt.show()
|
51 | 79 |
|
52 | 80 | # Assign the fill value to all the Nans
|
|
61 | 89 | # create a lower resolution field to apply our downscaling method.
|
62 | 90 | # We are going to use a factor of 16 x.
|
63 | 91 |
|
| 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 | + |
64 | 102 | 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) |
69 | 104 |
|
70 | 105 | # Plot the upscaled rainfall field
|
71 | 106 | plt.figure()
|
72 | 107 | plot_precip_field(precip_lr, geodata=metadata_lr)
|
| 108 | +plt.title("Upscaled Rainfall Field") |
| 109 | +plt.show() |
73 | 110 |
|
74 | 111 | ###############################################################################
|
75 | 112 | # Downscale the field
|
76 | 113 | # -------------------
|
77 | 114 | #
|
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") |
118 | 173 | plt.show()
|
119 | 174 |
|
120 | 175 | ###############################################################################
|
|
126 | 181 | # the original algorithm from Rebora et al. (2006), it cannot downscale the temporal
|
127 | 182 | # dimension.
|
128 | 183 |
|
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 |
| - |
138 | 184 | # sphinx_gallery_thumbnail_number = 2
|
0 commit comments