def animate(t): global pf n, s = divmod(t, numrec) if s == 0: pf = ParticleFile(particle_files[n]) X, Y = pf.position(s) particle_dist.set_data(X, Y) timestamp.set_text(pf.time(s)) return particle_dist, timestamp
# Set up the plot area fig = plt.figure(figsize=(12, 8)) ax = plt.axes(xlim=(0, grid.imax), ylim=(0, grid.jmax), aspect="equal") # Landmask Xb = np.arange(-0.5, grid.imax) Yb = np.arange(-0.5, grid.jmax) constmap = plt.matplotlib.colors.ListedColormap([0.2, 0.6, 0.4]) M = np.ma.masked_where(grid.M > 0, grid.M) plt.pcolormesh(Xb, Yb, M, cmap=constmap) # Draw the cirular boundary T = np.linspace(0, np.pi) plt.plot(grid.X0 + grid.R * np.cos(T), grid.R * np.sin(T), color="black") # Plot initial particle distribution X, Y = pf.position(0) particle_dist, = ax.plot(X, Y, ".", color="red", markeredgewidth=0.5, lw=0.5) time0 = pf.time(0) # Save start-time timestr = "00:00" timestamp = ax.text(0.02, 0.93, timestr, fontsize=16, transform=ax.transAxes) # Update function def animate(t): X, Y = pf.position(t) particle_dist.set_data(X, Y) # Time since start in minutes # dtime = int((pf.time(t) - time0).total_seconds() / 60) dtime = (pf.time(t) - time0) / 60 dtimestr = str(dtime) timestamp.set_text(dtimestr)
# n1 = -99 for n in range(pf.num_times): if pf.time(n) < date0: continue if n0 < 0: # First time n0 = n if pf.time(n) < date1: n1 = n print("start: ", n0, pf.time(n0)) print("stop : ", n1, pf.time(n1)) first = True for n in range(n0, n1 + 1): print(n) X0, Y0 = pf.position(n) S0 = pf["super", n] A = pf["age", n] I = (ddmin <= A) & (A < ddmax) if first: X = X0[I] Y = Y0[I] S = S0[I] first = False else: X = np.concatenate((X, X0[I])) Y = np.concatenate((Y, Y0[I])) S = np.concatenate((S, S0[I])) # pf.close()
tstep = 40 # Time step to show # Output file (and type) output_file = "line_hv.png" #output_file = "line_hv.html" scale = 5 # Figure size factor # --- Data files --- ladim_dir = Path("../../") grid_file = ladim_dir / "examples/data/ocean_avg_0014.nc" particle_file = ladim_dir / "examples/line/line.nc" # --- Read particle data --- pf = ParticleFile(particle_file) X, Y = pf.position(tstep) # --- Background bathymetry data --- # Read bathymetry and land mask with xr.open_dataset(grid_file) as A: H = A.h M = A.mask_rho jmax, imax = M.shape H = H.where(M > 0) # Mask out land M = M.where(M < 1) # Mask out sea # --- Holoviews elements --- # Land image land = hv.Image(data=M, kdims=["xi_rho", "eta_rho"], group="Land")
ax.contourf(Xcell, Ycell, H, cmap=cmap, alpha=0.3) # Lon/lat lines ax.contour(Xcell, Ycell, lat, levels=range(57, 64), colors="black", linestyles=":") ax.contour(Xcell, Ycell, lon, levels=range(-4, 10, 2), colors="black", linestyles=":") # Landmask 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) # Particle distribution after 48 hours X, Y = pf.position(48) particle_dist, = ax.plot(X, Y, ".", color="red", markeredgewidth=0, lw=0.5) plt.xticks([]) plt.yticks([]) plt.savefig("logo.png", dpi=200, bbox_inches="tight") plt.show()
# ROMS grid, plot domain # Slight overkill to use roppy, could be more stand alone f0 = Dataset(grid_file) g = roppy.SGrid(f0, subgrid=(i0, i1, j0, j1)) # particle_file pf = ParticleFile(particle_file) fig = plt.figure(figsize=(12, 10)) ax = fig.add_subplot(1, 1, 1) # Make background map cmap = plt.get_cmap("Blues") h = ax.contourf(g.X, g.Y, g.h, cmap=cmap, alpha=0.3) roppy.mpl_util.landmask(g, color=(0.6, 0.8, 0.0)) ax.contour(g.X, g.Y, g.lat_rho, levels=range(57, 64), colors="black", linestyles=":") ax.contour(g.X, g.Y, g.lon_rho, levels=range(-4, 10, 2), colors="black", linestyles=":") # Plot particle distribution X, Y = pf.position(time=t) timestring = pf.time(t) ax.plot(X, Y, ".", color="red", markeredgewidth=0, lw=0.5) ax.set_title(timestring) # Show the results plt.axis("image") plt.axis((i0 + 1, i1 - 1, j0 + 1, j1 - 1)) plt.show()
ax = fig.add_subplot(1, 1, 1) # Make background map cmap = plt.get_cmap("Blues") ax.contourf(g.X, g.Y, g.h, cmap=cmap, alpha=0.3) roppy.mpl_util.landmask(g, (0.6, 0.8, 0.0)) ax.contour(g.X, g.Y, g.lat_rho, levels=range(57, 64), colors="black", linestyles=":") ax.contour(g.X, g.Y, g.lon_rho, levels=range(-4, 10, 2), colors="black", linestyles=":") # Plot initial particle distribution X, Y = pf.position(t) timestring = pf.time(t) ax.plot(X, Y, ".", color="red", markeredgewidth=0, lw=0.5) ax.set_title(timestring) # Show the results plt.axis("image") plt.axis((i0 + 1, i1 - 1, j0 + 1, j1 - 1)) plt.show()
# ---------------- # particle_file pf0 = ParticleFile(unsplit_file) pf1 = ParticleFile(split_file) pf2 = ParticleFile(restarted_file) fig = plt.figure(figsize=(12, 10)) ax = fig.add_subplot(1, 1, 1) # Make bathymetry background with Dataset(grid_file) as f0: H = f0.variables["h"][j0:j1, i0:i1] cmap = plt.get_cmap("Blues") ax.contourf(range(i0, i1), range(j0, j1), H, cmap=cmap, alpha=0.3) # Plot initial particle distribution X0, Y0 = pf0.position(t0) X1, Y1 = pf1.position(t1) X2, Y2 = pf2.position(t2) ax.plot(X0, Y0, "o", color="green", label="unsplit") ax.plot(X1, Y1, "o", color="red", label="split") ax.plot(X2, Y2, "o", color="blue", label="restart") # Show the results plt.legend() plt.axis("image") plt.axis((i0 + 1, i1 - 1, j0 + 1, j1 - 1)) plt.show()
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()