def test_grid_limits(): X = [11.2, 11.8, 12.2, 12.3] Y = [0.8, 1.2, 1.4, 3.1] i0, i1, j0, j1 = 10, 14, 0, 2 C = cellcount(X, Y, grid_limits=(i0, i1, j0, j1)) assert C.shape == (j1 - j0, i1 - i0) # (2, 4) assert C.sum() == len(X) - 1 # 3, one point outside assert (C == [[0, 0, 0, 0], [0, 1, 2, 0]]).all() assert C.sel(X=11, Y=1) == 1 assert C.sel(X=12, Y=1) == 2 assert C.sel(X=11, Y=0) == 0 C = cellcount(X, Y, grid_limits=(12, 3)) assert C.shape == (3, 12) assert C.sum() == 1 # Three points outside assert C.sel(X=11, Y=1) == 1 assert C.sel(X=11, Y=0) == 0
def test_default(): X = [11.2, 11.8, 12.2, 12.3] Y = [0.8, 1.2, 1.4, 3.1] C = cellcount(X, Y) assert C.shape == (3, 2) assert C.sum() == len(X) # 4 assert (C == [[1, 2], [0, 0], [0, 1]]).all() assert C.sel(X=11, Y=1) == 1 assert C.sel(X=12, Y=1) == 2 assert C.sel(X=12, Y=3) == 1 assert C.sel(X=11, Y=2) == 0
def test_weight(): X = [11.2, 11.8, 12.2, 12.3] Y = [0.8, 1.2, 1.4, 3.1] W = [1, 2, 3, 4] C = cellcount(X, Y, W) assert C.shape == (3, 2) assert C.sum() == sum(W) assert (C == [[W[0], W[1] + W[2]], [0, 0], [0, W[3]]]).all() assert C.sel(X=11, Y=1) == W[0] assert C.sel(X=12, Y=1) == W[1] + W[2] assert C.sel(X=12, Y=3) == W[3]
lat = f0.variables["lat_rho"][j0:j1, i0:i1] # Cell centers and boundaries Xcell = np.arange(i0, i1) Ycell = np.arange(j0, j1) Xb = np.arange(i0 - 0.5, i1) Yb = np.arange(j0 - 0.5, j1) # --------------------------- # Read and count particles # --------------------------- pf = ParticleFile(pfile) X = pf.X[tframe0:tframe1] Y = pf.Y[tframe0:tframe1] C = cellcount(X, Y, grid_limits=(i0, i1, j0, j1)) # ------------------------- --- # Plot particle concentration # ----------------------------- plt.set_cmap("cool") plt.pcolormesh(Xb, Yb, C) plt.colorbar() # Land mask constmap = plt.matplotlib.colors.ListedColormap([0.2, 0.6, 0.4]) M = np.ma.masked_where(M > 0, M) plt.pcolormesh(Xb, Yb, M, cmap=constmap)
import numpy as np import matplotlib.pyplot as plt from postladim import ParticleFile, cellcount pf = ParticleFile("line.nc") X, Y = pf.position(80) C = cellcount(X, Y) i0 = int(round(X.min())) j0 = int(round(Y.min())) jmax, imax = C.shape x_edges = np.arange(i0 - 0.5, i0 + imax) y_edges = np.arange(j0 - 0.5, j0 + jmax) plt.set_cmap("magma_r") plt.pcolormesh(x_edges, y_edges, C) plt.colorbar() plt.plot(X, Y, ".k") plt.show()