def test_decompose_recompose(): """Tests cascade decomposition.""" pytest.importorskip("netCDF4") root_path = pysteps.rcparams.data_sources["bom"]["root_path"] rel_path = os.path.join("prcp-cscn", "2", "2018", "06", "16") filename = os.path.join(root_path, rel_path, "2_20180616_120000.prcp-cscn.nc") precip, _, metadata = pysteps.io.import_bom_rf3(filename) # Convert to rain rate from mm precip, metadata = pysteps.utils.to_rainrate(precip, metadata) # Log-transform the data precip, metadata = pysteps.utils.dB_transform( precip, metadata, threshold=0.1, zerovalue=-15.0 ) # Set Nans as the fill value precip[~np.isfinite(precip)] = metadata["zerovalue"] # Set number of cascade levels num_cascade_levels = 9 # Construct the Gaussian bandpass filters _filter = filter_gaussian(precip.shape, num_cascade_levels) # Decompose precip decomp = decomposition_fft(precip, _filter) # Recomposed precip from decomp recomposed = recompose_fft(decomp) # Assert assert_array_almost_equal(recomposed.squeeze(), precip)
cmap = cm.RdBu_r vmin = -3 vmax = 3 fn = io.archive.find_by_date(date, root_path, "%Y%m%d", fn_pattern, fn_ext, 5) R, _, metadata = io.read_timeseries(fn, import_fmi_pgm, gzipped=True) R = R.squeeze() R[R < 10.0] = 5.0 R[~np.isfinite(R)] = 5.0 R = (R - np.mean(R)) / np.std(R) filter = filter_gaussian(R.shape, 8) decomp = decomposition_fft(R, filter) for i in range(8): mu = decomp["means"][i] sigma = decomp["stds"][i] decomp["cascade_levels"][i] = (decomp["cascade_levels"][i] - mu) / sigma fig, ax = pyplot.subplots(nrows=2, ncols=4) im = ax[0, 0].imshow(R, cmap=cmap, vmin=vmin, vmax=vmax) ax[0, 1].imshow(decomp["cascade_levels"][0], cmap=cmap, vmin=vmin, vmax=vmax, rasterized=True) ax[0, 2].imshow(decomp["cascade_levels"][1],
) ax.set_xlim(1, L / 2) ax.set_ylim(0, 1) xt = np.hstack([[1.0], filter["central_wavenumbers"][1:]]) ax.set_xticks(xt) ax.set_xticklabels(["%.2f" % cf for cf in filter["central_wavenumbers"]]) ax.set_xlabel("Radial wavenumber $|\mathbf{k}|$") ax.set_ylabel("Normalized weight") ax.set_title("Bandpass filter weights") plt.show() ############################################################################### # Finally, apply the 2D Gaussian filters to decompose the radar rainfall field # into a set of cascade levels of decreasing spatial scale and plot them. decomp = decomposition_fft(R, filter, compute_stats=True) # Plot the normalized cascade levels for i in range(num_cascade_levels): mu = decomp["means"][i] sigma = decomp["stds"][i] decomp["cascade_levels"][i] = (decomp["cascade_levels"][i] - mu) / sigma fig, ax = plt.subplots(nrows=2, ncols=4) ax[0, 0].imshow(R, cmap=cm.RdBu_r, vmin=-5, vmax=5) ax[0, 1].imshow(decomp["cascade_levels"][0], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[0, 2].imshow(decomp["cascade_levels"][1], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[0, 3].imshow(decomp["cascade_levels"][2], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[1, 0].imshow(decomp["cascade_levels"][3], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[1, 1].imshow(decomp["cascade_levels"][4], cmap=cm.RdBu_r, vmin=-3, vmax=3)
root_path, "%Y%m%d", fn_pattern, fn_ext, 5, num_prev_files=9) # read the radar composites and apply thresholding Z, _, metadata = io.read_timeseries(fns, import_fmi_pgm, gzipped=True) R = conversion.to_rainrate(Z, metadata, 223.0, 1.53)[0] R = transformation.dB_transform(R, threshold=0.1, zerovalue=-15.0)[0] R[~np.isfinite(R)] = -15.0 # construct bandpass filter and apply the cascade decomposition filter = filter_gaussian(R.shape[1:], 7) decomp = decomposition_fft(R[-1, :, :], filter) # plot the normalized cascade levels for i in range(7): mu = decomp["means"][i] sigma = decomp["stds"][i] decomp["cascade_levels"][i] = (decomp["cascade_levels"][i] - mu) / sigma fig, ax = pyplot.subplots(nrows=2, ncols=4) ax[0, 0].imshow(R[-1, :, :], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[0, 1].imshow(decomp["cascade_levels"][0], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[0, 2].imshow(decomp["cascade_levels"][1], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[0, 3].imshow(decomp["cascade_levels"][2], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[1, 0].imshow(decomp["cascade_levels"][3], cmap=cm.RdBu_r, vmin=-3, vmax=3) ax[1, 1].imshow(decomp["cascade_levels"][4], cmap=cm.RdBu_r, vmin=-3, vmax=3)