Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
-------
R : array-like
Array of any shape containing the converted units.
metadata : dict
The metadata with updated attributes.
"""
R = R.copy()
metadata = metadata.copy()
if metadata["transform"] is not None:
if metadata["transform"] == "dB":
R, metadata = transformation.dB_transform(R, metadata,
inverse=True)
elif metadata["transform"] in ["BoxCox", "log"]:
R, metadata = transformation.boxcox_transform(R, metadata,
inverse=True)
elif metadata["transform"] == "NQT":
R, metadata = transformation.NQ_transform(R, metadata,
inverse=True)
elif metadata["transform"] == "sqrt":
R, metadata = transformation.sqrt_transform(R, metadata,
inverse=True)
# 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
# skill score for high-resolution precipitation forecasts.
# Find observations in the data archive
fns = io.archive.find_by_date(
date,
# image processing methods
methods_objects["shitomasi"] = images.ShiTomasi_detection
methods_objects["morph_opening"] = images.morph_opening
# interpolation methods
methods_objects["rbfinterp2d"] = interpolate.rbfinterp2d
# spectral methods
methods_objects["rapsd"] = spectral.rapsd
methods_objects["rm_rdisc"] = spectral.remove_rain_norain_discontinuity
# transformation methods
methods_objects["boxcox"] = transformation.boxcox_transform
methods_objects["box-cox"] = transformation.boxcox_transform
methods_objects["db"] = transformation.dB_transform
methods_objects["decibel"] = transformation.dB_transform
methods_objects["log"] = transformation.boxcox_transform
methods_objects["nqt"] = transformation.NQ_transform
methods_objects["sqrt"] = transformation.sqrt_transform
# FFT methods
if name in ["numpy", "pyfftw", "scipy"]:
if "shape" not in kwargs.keys():
raise KeyError("mandatory keyword argument shape not given")
return _get_fft_method(name, **kwargs)
else:
try:
return methods_objects[name]
except KeyError as e:
raise ValueError(
"Unknown method %s\n" % e
fns = io.archive.find_by_date(
date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=9
)
# Read the radar composites
importer = io.get_method(importer_name, "importer")
R, _, metadata = io.read_timeseries(fns, importer, **importer_kwargs)
# Convert to mm/h
R, metadata = conversion.to_rainrate(R, metadata)
# Store the last frame for polotting it later later
R_ = R[-1, :, :].copy()
# Log-transform the data
R, metadata = transformation.dB_transform(R, metadata, threshold=0.1, zerovalue=-15.0)
# Nicely print the metadata
pprint(metadata)
###############################################################################
# Lucas-Kanade (LK)
# -----------------
#
# The Lucas-Kanade optical flow method implemented in pysteps is a local
# tracking approach that relies on the OpenCV package.
# Local features are tracked in a sequence of two or more radar images. The
# scheme includes a final interpolation step in order to produce a smooth
# field of motion vectors.
oflow_method = motion.get_method("LK")
V1 = oflow_method(R[-3:, :, :])
vsf = 60.0 / datasource["timestep"] * metadata["xpixelsize"] / 1000.0
missing_data = False
for i in range(R.shape[0]):
if not np.any(np.isfinite(R[i, :, :])):
missing_data = True
break
if missing_data:
curdate += timedelta(minutes=datasource["timestep"])
continue
R[~np.isfinite(R)] = metadata["zerovalue"]
if use_precip_mask:
MASK = np.any(R < R_min, axis=0)
R = transformation.dB_transform(R)[0]
if args.oflow == "vet":
R_ = R[-2:, :, :]
else:
R_ = R
V = oflow(R_) * vsf
if use_precip_mask:
V[0, :, :][MASK] = np.nan
V[1, :, :][MASK] = np.nan
motionfields[curdate] = V.astype(np.float32)
curdate += timedelta(minutes=datasource["timestep"])
# compare initial and future motion fields
# ----------------------------------------