Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
################################################################################
# Notice that the default motion field can be significantly slower (more than 10%
# slower) because of the presence of wrong stationary motion vectors at the edges.
#
# Forecast skill
# --------------
#
# We are now going to evaluate the benefit of buffering the radar mask by computing
# the forecast skill in terms of the Spearman correlation coefficient.
# The extrapolation forecasts are computed using the dense UV motion fields
# estimted above.
# Get the advection routine and extrapolate the last radar frame by 12 time steps
# (i.e., 1 hour lead time)
extrapolate = nowcasts.get_method("extrapolation")
R[~np.isfinite(R)] = metadata["zerovalue"]
R_f1 = extrapolate(R[-1], UV1, 12)
R_f2 = extrapolate(R[-1], UV2, 12)
# Back-transform to rain rate
R_f1 = transformation.dB_transform(R_f1, threshold=-10.0, inverse=True)[0]
R_f2 = transformation.dB_transform(R_f2, threshold=-10.0, inverse=True)[0]
# Find the veriyfing observations in the archive
fns = io.archive.find_by_date(
date,
root_path,
path_fmt,
fn_pattern,
fn_ext,
timestep=5,
R[~np.isfinite(R)] = -15.0
# Nicely print the metadata
pprint(metadata)
###############################################################################
# Forecast
# --------
#
# We use the STEPS approach to produce a ensemble nowcast of precipitation fields.
# Estimate the motion field
V = dense_lucaskanade(R)
# The STEPES nowcast
nowcast_method = nowcasts.get_method("steps")
R_f = nowcast_method(
R[-3:, :, :],
V,
n_leadtimes,
n_ens_members,
n_cascade_levels=6,
R_thr=-10.0,
kmperpixel=2.0,
timestep=timestep,
decomp_method="fft",
bandpass_filter_method="gaussian",
noise_method="nonparametric",
vel_pert_method="bps",
mask_method="incremental",
seed=seed,
)
def export(X):
## convert to mm/h
X,_ = converter(X, metadata)
# readjust to initial domain shape
X,_ = reshaper(X, metadata, inverse=True)
# export to netcdf
stp.io.export_forecast_dataset(X, exporter)
## initialize netcdf file
incremental = "timestep" if p["nwc_method"].lower() == "steps" else None
exporter = stp.io.initialize_forecast_exporter_netcdf(outfn, startdate,
ds.timestep, p["n_lead_times"], metadata0["shape"],
p["n_ens_members"], metadata0, incremental=incremental)
## start the nowcast
nwc_method = stp.nowcasts.get_method(p["nwc_method"])
R_fct = nwc_method(R, UV, p["n_lead_times"], p["n_ens_members"],
p["n_cascade_levels"], kmperpixel=metadata["xpixelsize"]/1000,
timestep=ds.timestep, R_thr=metadata["threshold"],
extrap_method=p["adv_method"],
decomp_method=p["decomp_method"],
bandpass_filter_method=p["bandpass_filter"],
noise_method=p["noise_method"],
noise_stddev_adj=p["noise_adjustment"],
ar_order=p["ar_order"],conditional=p["conditional"],
mask_method=p["mask_method"], callback=export,
return_output=False, seed=p["seed"])
## save results
stp.io.close_forecast_file(exporter)
R_fct = None
# variance of the original observed field.
# However, certain applications demand that the forecast retain the same
# statistical properties of the observations. In such cases, the S-PROG
# forecasts are of limited use and a stochatic approach might be of more
# interest.
###############################################################################
# Stochastic nowcast with STEPS
# -----------------------------
#
# The S-PROG approach is extended to include a stochastic term which represents
# the variance associated to the unpredictable development of precipitation. This
# approach is known as STEPS (short-term ensemble prediction system).
# The STEPES nowcast
nowcast_method = nowcasts.get_method("steps")
R_f = nowcast_method(
R[-3:, :, :],
V,
n_leadtimes,
n_ens_members,
n_cascade_levels=6,
R_thr=-10.0,
kmperpixel=2.0,
timestep=timestep,
decomp_method="fft",
bandpass_filter_method="gaussian",
noise_method="nonparametric",
vel_pert_method="bps",
mask_method="incremental",
seed=seed,
)
converter = stp.utils.get_method(unit)
R, metadata = converter(R, metadata)
## transform the data
transformer = stp.utils.get_method(transformation)
R, metadata = transformer(R, metadata)
## set NaN equal to zero
R[~np.isfinite(R)] = metadata["zerovalue"]
# Compute motion field
oflow_method = stp.motion.get_method(oflow_method)
UV = oflow_method(R)
# Perform the nowcast
nwc_method = stp.nowcasts.get_method(nwc_method)
R_fct = nwc_method(R, UV, n_lead_times, n_ens_members,
n_cascade_levels, kmperpixel=metadata["xpixelsize"]/1000,
timestep=ds.timestep, R_thr=metadata["threshold"],
extrap_method=adv_method, decomp_method=decomp_method,
bandpass_filter_method=bandpass_filter,
noise_method=noise_method, noise_stddev_adj=adjust_noise,
ar_order=ar_order, conditional=conditional,
mask_method=mask_method, seed=seed)
## if necessary, transform back all data
R_fct, _ = transformer(R_fct, metadata, inverse=True)
R, metadata = transformer(R, metadata, inverse=True)
## convert all data to mm/h
converter = stp.utils.get_method("mm/h")
R_fct, _ = converter(R_fct, metadata)
###############################################################################
# Deterministic nowcast with S-PROG
# ---------------------------------
#
# First, the motiong field is estimated using a local tracking approach based
# on the Lucas-Kanade optical flow.
# The motion field can then be used to generate a deterministic nowcast with
# the S-PROG model, which implements a scale filtering appraoch in order to
# progressively remove the unpredictable spatial scales during the forecast.
# Estimate the motion field
V = dense_lucaskanade(R)
# The S-PROG nowcast
nowcast_method = nowcasts.get_method("sprog")
R_f = nowcast_method(
R[-3:, :, :],
V,
n_leadtimes,
n_cascade_levels=8,
R_thr=-10.0,
decomp_method="fft",
bandpass_filter_method="gaussian",
probmatching_method="mean",
)
# Back-transform to rain rate
R_f = transformation.dB_transform(R_f, threshold=-10.0, inverse=True)[0]
# Plot the S-PROG forecast
plot_precip_field(
###############################################################################
# Compute the nowcast
# -------------------
#
# The extrapolation nowcast is based on the estimation of the motion field,
# which is here performed using a local tacking approach (Lucas-Kanade).
# The most recent radar rainfall field is then simply advected along this motion
# field in oder to produce an extrapolation forecast.
# Estimate the motion field with Lucas-Kanade
oflow_method = motion.get_method("LK")
V = oflow_method(R[-3:, :, :])
# Extrapolate the last radar observation
extrapolate = nowcasts.get_method("extrapolation")
R[~np.isfinite(R)] = metadata["zerovalue"]
R_f = extrapolate(R[-1, :, :], V, n_leadtimes)
# Back-transform to rain rate
R_f = transformation.dB_transform(R_f, threshold=-10.0, inverse=True)[0]
# Plot the motion field
plot_precip_field(R_, geodata=metadata)
quiver(V, geodata=metadata, step=50)
###############################################################################
# Verify with FSS
# ---------------
#
# The fractions skill score (FSS) provides an intuitive assessment of the
# dependency of skill on spatial scale and intensity, which makes it an ideal