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
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) return particle_dist, timestamp
f.close() jmax, imax = H.shape # --------------------- # Read particle file # --------------------- pf = ParticleFile(particle_file) # Find record numbers n0 = -99 # 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]
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) # Plot border of submodel # plt.plot([135, 172, 172, 135, 135], # [42, 42, 81, 81, 42], # color='black') # Plot initial particle distribution X, Y = pf.position(0) particle_dist, = ax.plot(X, Y, ".", color="red", markeredgewidth=0, lw=0.5) # title = ax.set_title(pf.time(0)) timestamp = ax.text(0.01, 0.97, pf.time(0), fontsize=15, transform=ax.transAxes) # Update function def animate(t): X, Y = pf.position(t) particle_dist.set_data(X, Y) timestamp.set_text(pf.time(t)) return particle_dist, timestamp anim_running = True
ax.contourf(Xcell, Ycell, H, cmap=cmap, alpha=0.3) # Lon/lat lines ax.contour(Xcell, Ycell, lat, levels=range(56, 83, 2), colors="black", linestyles=":") ax.contour(Xcell, Ycell, lon, levels=range(-4, 40, 5), 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) # Plot initial particle distribution X, Y = pf.position(0) particle_dist, = ax.plot(X, Y, ".", color="red", markeredgewidth=0, lw=0.5) # title = ax.set_title(pf.time(0)) timestamp = ax.text(0.01, 0.92, pf.time(0), fontsize=15, transform=ax.transAxes) # Update function def animate(t): X, Y = pf.position(t) particle_dist.set_data(X, Y) timestamp.set_text(pf.time(t)) return particle_dist, timestamp # Do the animation anim = FuncAnimation( fig, animate, frames=num_times,
# 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()
cmap = plt.get_cmap("Blues") h = ax.contourf(Xcell, Ycell, H, cmap=cmap, alpha=0.3) # 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) # Graticule ax.contour(Xcell, Ycell, lat, levels=range(55, 64), colors="black", linestyles=":") ax.contour(Xcell, Ycell, lon, levels=range(-4, 10, 2), colors="black", linestyles=":") # Plot particle distribution ax.plot(X, Y, ".", color="red", markeredgewidth=0, lw=0.5) ax.set_title(pf.time(t)) # Show the results plt.axis("image") plt.axis((i0 + 1, i1 - 1, j0 + 1, j1 - 1)) plt.show()
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) # particle_files particle_files = glob.glob(f"{particle_file[:-3]}*.nc") particle_files.sort() # Inital particle distribution pf = ParticleFile(particle_files[0]) X, Y = pf.position(0) timestr = pf.time(0) pf.close() # Suppose all files (except possible last) has same number of records numrec = pf.num_times num_times = (len(particle_files) - 1) * numrec # Add the records from the last file pf = ParticleFile(particle_files[-1]) num_times += pf.num_times # Set up the plot area fig = plt.figure(figsize=(12, 10)) ax = plt.axes(xlim=(i0 + 1, i1 - 1), ylim=(j0 + 1, j1 - 1), aspect="equal") # Background bathymetry cmap = plt.get_cmap("Blues") ax.contourf(Xcell, Ycell, H, cmap=cmap, alpha=0.3)