|
| 1 | +#!/bin/env python |
| 2 | +""" |
| 3 | +LINDA nowcasts |
| 4 | +============== |
| 5 | +
|
| 6 | +This example shows how to compute and plot a deterministic and ensemble LINDA |
| 7 | +nowcasts using Swiss radar data. |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +from datetime import datetime |
| 12 | +import warnings |
| 13 | + |
| 14 | +warnings.simplefilter("ignore") |
| 15 | + |
| 16 | +import matplotlib.pyplot as plt |
| 17 | + |
| 18 | +from pysteps import io, rcparams |
| 19 | +from pysteps.motion.lucaskanade import dense_lucaskanade |
| 20 | +from pysteps.nowcasts import linda, sprog, steps |
| 21 | +from pysteps.utils import conversion, dimension, transformation |
| 22 | +from pysteps.visualization import plot_precip_field |
| 23 | + |
| 24 | +############################################################################### |
| 25 | +# Read the input rain rate fields |
| 26 | +# ------------------------------- |
| 27 | + |
| 28 | +date = datetime.strptime("201701311200", "%Y%m%d%H%M") |
| 29 | +data_source = "mch" |
| 30 | + |
| 31 | +# Read the data source information from rcparams |
| 32 | +datasource_params = rcparams.data_sources[data_source] |
| 33 | + |
| 34 | +# Find the radar files in the archive |
| 35 | +fns = io.find_by_date( |
| 36 | + date, |
| 37 | + datasource_params["root_path"], |
| 38 | + datasource_params["path_fmt"], |
| 39 | + datasource_params["fn_pattern"], |
| 40 | + datasource_params["fn_ext"], |
| 41 | + datasource_params["timestep"], |
| 42 | + num_prev_files=2, |
| 43 | +) |
| 44 | + |
| 45 | +# Read the data from the archive |
| 46 | +importer = io.get_method(datasource_params["importer"], "importer") |
| 47 | +reflectivity, _, metadata = io.read_timeseries( |
| 48 | + fns, importer, **datasource_params["importer_kwargs"] |
| 49 | +) |
| 50 | + |
| 51 | +# Convert reflectivity to rain rate |
| 52 | +rainrate, metadata = conversion.to_rainrate(reflectivity, metadata) |
| 53 | + |
| 54 | +# Upscale data to 2 km to reduce computation time |
| 55 | +rainrate, metadata = dimension.aggregate_fields_space(rainrate, metadata, 2000) |
| 56 | + |
| 57 | +# Plot the most recent rain rate field |
| 58 | +plt.figure() |
| 59 | +plot_precip_field(rainrate[-1, :, :]) |
| 60 | +plt.show() |
| 61 | + |
| 62 | +############################################################################### |
| 63 | +# Estimate the advection field |
| 64 | +# ---------------------------- |
| 65 | + |
| 66 | +# The advection field is estimated using the Lucas-Kanade optical flow |
| 67 | +advection = dense_lucaskanade(rainrate, verbose=True) |
| 68 | + |
| 69 | +############################################################################### |
| 70 | +# Deterministic nowcast |
| 71 | +# --------------------- |
| 72 | + |
| 73 | +# Compute 30-minute LINDA nowcast with 8 parallel workers |
| 74 | +# Restrict the number of features to 15 to reduce computation time |
| 75 | +nowcast_linda = linda.forecast( |
| 76 | + rainrate, |
| 77 | + advection, |
| 78 | + 6, |
| 79 | + max_num_features=15, |
| 80 | + add_perturbations=False, |
| 81 | + num_workers=8, |
| 82 | + measure_time=True, |
| 83 | +)[0] |
| 84 | + |
| 85 | +# Compute S-PROG nowcast for comparison |
| 86 | +rainrate_db, _ = transformation.dB_transform( |
| 87 | + rainrate, metadata, threshold=0.1, zerovalue=-15.0 |
| 88 | +) |
| 89 | +nowcast_sprog = sprog.forecast( |
| 90 | + rainrate_db[-3:, :, :], |
| 91 | + advection, |
| 92 | + 6, |
| 93 | + n_cascade_levels=6, |
| 94 | + R_thr=-10.0, |
| 95 | +) |
| 96 | + |
| 97 | +# Convert reflectivity nowcast to rain rate |
| 98 | +nowcast_sprog = transformation.dB_transform( |
| 99 | + nowcast_sprog, threshold=-10.0, inverse=True |
| 100 | +)[0] |
| 101 | + |
| 102 | +# Plot the nowcasts |
| 103 | +fig = plt.figure(figsize=(9, 4)) |
| 104 | +ax = fig.add_subplot(1, 2, 1) |
| 105 | +plot_precip_field( |
| 106 | + nowcast_linda[-1, :, :], |
| 107 | + title="LINDA (+ 30 min)", |
| 108 | +) |
| 109 | + |
| 110 | +ax = fig.add_subplot(1, 2, 2) |
| 111 | +plot_precip_field( |
| 112 | + nowcast_sprog[-1, :, :], |
| 113 | + title="S-PROG (+ 30 min)", |
| 114 | +) |
| 115 | + |
| 116 | +plt.show() |
| 117 | + |
| 118 | +############################################################################### |
| 119 | +# The above figure shows that the filtering scheme implemented in LINDA preserves |
| 120 | +# small-scale and band-shaped features better than S-PROG. This is because the |
| 121 | +# former uses a localized elliptical convolution kernel instead of the |
| 122 | +# cascade-based autoregressive process, where the parameters are estimated over |
| 123 | +# the whole domain. |
| 124 | + |
| 125 | +############################################################################### |
| 126 | +# Probabilistic nowcast |
| 127 | +# --------------------- |
| 128 | + |
| 129 | +# Compute 30-minute LINDA nowcast ensemble with 40 members and 8 parallel workers |
| 130 | +nowcast_linda = linda.forecast( |
| 131 | + rainrate, |
| 132 | + advection, |
| 133 | + 6, |
| 134 | + max_num_features=15, |
| 135 | + add_perturbations=True, |
| 136 | + num_ens_members=40, |
| 137 | + num_workers=8, |
| 138 | + measure_time=True, |
| 139 | +)[0] |
| 140 | + |
| 141 | +# Compute 40-member STEPS nowcast for comparison |
| 142 | +nowcast_steps = steps.forecast( |
| 143 | + rainrate_db[-3:, :, :], |
| 144 | + advection, |
| 145 | + 6, |
| 146 | + 40, |
| 147 | + n_cascade_levels=6, |
| 148 | + R_thr=-10.0, |
| 149 | + mask_method="incremental", |
| 150 | + kmperpixel=2.0, |
| 151 | + timestep=datasource_params["timestep"], |
| 152 | + vel_pert_method=None, |
| 153 | +) |
| 154 | + |
| 155 | +# Convert reflectivity nowcast to rain rate |
| 156 | +nowcast_steps = transformation.dB_transform( |
| 157 | + nowcast_steps, threshold=-10.0, inverse=True |
| 158 | +)[0] |
| 159 | + |
| 160 | +# Plot two ensemble members of both nowcasts |
| 161 | +fig = plt.figure() |
| 162 | +for i in range(2): |
| 163 | + ax = fig.add_subplot(2, 2, i + 1) |
| 164 | + ax = plot_precip_field( |
| 165 | + nowcast_linda[i, -1, :, :], geodata=metadata, colorbar=False, axis="off" |
| 166 | + ) |
| 167 | + ax.set_title(f"LINDA Member {i+1}") |
| 168 | + |
| 169 | +for i in range(2): |
| 170 | + ax = fig.add_subplot(2, 2, 3 + i) |
| 171 | + ax = plot_precip_field( |
| 172 | + nowcast_steps[i, -1, :, :], geodata=metadata, colorbar=False, axis="off" |
| 173 | + ) |
| 174 | + ax.set_title(f"STEPS Member {i+1}") |
| 175 | + |
| 176 | +############################################################################### |
| 177 | +# The above figure shows the main difference between LINDA and STEPS. In |
| 178 | +# addition to the convolution kernel, another improvement in LINDA is a |
| 179 | +# localized perturbation generator using the short-space Fourier transform |
| 180 | +# (SSFT) and a spatially variable marginal distribution. As a result, the |
| 181 | +# LINDA ensemble members preserve the anisotropic and small-scale structures |
| 182 | +# considerably better than STEPS. |
| 183 | + |
| 184 | +plt.tight_layout() |
| 185 | +plt.show() |
0 commit comments