Skip to content

Commit a259e5a

Browse files
Plot only last step
1 parent 3a20dc7 commit a259e5a

File tree

1 file changed

+69
-103
lines changed

1 file changed

+69
-103
lines changed

src/plot/plot_mdk3.py

Lines changed: 69 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -149,68 +149,58 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
149149
magnitude - 1
150150
)
151151

152-
# loop for ploting
153-
for t in range(0, len(ds_particles.time)):
152+
# Use only the last time step
153+
t = len(ds_particles.time) - 1
154154

155-
target_lon = lon_gravity_center[t] # Replace with your target latitude
156-
target_lat = lat_gravity_center[t] # Replace with your target longitude
155+
target_lon = lon_gravity_center[t] # Replace with your target longitude
156+
target_lat = lat_gravity_center[t] # Replace with your target latitude
157157

158-
# From wind dataset find nearest lon/lat points to the gravity center
159-
nearest_lon = wind.lon.sel(lon=target_lon, method="nearest")
160-
nearest_lat = wind.lat.sel(lat=target_lat, method="nearest")
158+
# From wind dataset find nearest lon/lat points to the gravity center
159+
nearest_lon = wind.lon.sel(lon=target_lon, method="nearest")
160+
nearest_lat = wind.lat.sel(lat=target_lat, method="nearest")
161161

162-
# Using nearest available lat/lon
163-
subset_wind = wind.sel(lon=nearest_lon, lat=nearest_lat)
162+
# Using nearest available lat/lon
163+
subset_wind = wind.sel(lon=nearest_lon, lat=nearest_lat)
164+
subset_wind = subset_wind.expand_dims(["lat", "lon"]) # Ensure lon/lat dimensions
164165

165-
# Make sure that lon/lat are preserved as dimensions
166-
subset_wind = subset_wind.expand_dims(["lat", "lon"])
166+
# Select the iteration timestep for particles, currents, and wind
167+
ds_p = ds_particles.isel(time=t)
168+
plot_curr = curr.isel(time=t)
169+
plot_wind = subset_wind.isel(time=t)
167170

168-
# select the iteration timestep
169-
ds_p = ds_particles.isel(time=t)
170-
plot_curr = curr.isel(time=t)
171-
plot_wind = subset_wind.isel(time=t)
171+
# Extract U10M and V10M values at the nearest position
172+
u_wind_raw = plot_wind.sel(lon=nearest_lon, lat=nearest_lat).U10M.values
173+
v_wind_raw = plot_wind.sel(lon=nearest_lon, lat=nearest_lat).V10M.values
172174

173-
# Extract U10M and V10M values at the nearest position
174-
u_wind_raw = plot_wind.sel(lon=nearest_lon, lat=nearest_lat).U10M.values
175-
v_wind_raw = plot_wind.sel(lon=nearest_lon, lat=nearest_lat).V10M.values
176-
177-
# Check if the data is NaN
178-
if np.isnan(u_wind_raw) or np.isnan(v_wind_raw):
179-
print(
180-
f"plotting - NaN wind data found at timestep {t}. Skipping wind interpolation."
181-
)
182-
continue # Skip interpolation if raw data is invalid
183-
184-
fig = plt.figure(figsize=(10, 8))
185-
gs = plt.GridSpec(
186-
3, 1, height_ratios=[20, 0.3, 1], width_ratios=[1]
175+
# Check if the data is NaN
176+
if np.isnan(u_wind_raw) or np.isnan(v_wind_raw):
177+
print(
178+
f"plotting - NaN wind data found at timestep {t}. Skipping wind interpolation."
187179
)
180+
else:
181+
fig = plt.figure(figsize=(10, 8))
182+
gs = plt.GridSpec(3, 1, height_ratios=[20, 0.3, 1], width_ratios=[1])
188183
ax1 = fig.add_subplot(gs[0, :])
189184
ax1.set_facecolor("white")
190185

191-
# Ploting coastline
186+
# Plot coastline
192187
rec.plot(ax=ax1, color="#FFFDD0", edgecolor="black", zorder=1000, aspect=1)
193188

194189
colors = [
195-
(0, 0, 1), # Blue
196-
(0, 1, 1), # Aqua
197-
(0, 1, 0), # Green
198-
(1, 1, 0), # Yellow
190+
(0, 0, 1), # Blue
191+
(0, 1, 1), # Aqua
192+
(0, 1, 0), # Green
193+
(1, 1, 0), # Yellow
199194
(1, 0.5, 0), # Orange
200-
(1, 0, 0), # Red
201-
(0.5, 0, 0.5), # Violet
195+
(1, 0, 0), # Red
196+
(0.5, 0, 0.5) # Violet
202197
]
203-
204198
colors.append((0.5, 0, 0.5)) # Violet for the final section
205199
cmap_name = "custom_BlAqGrYeOrReVi"
206200
custom_cmap = LinearSegmentedColormap.from_list(cmap_name, colors, N=200)
207201

208-
# ploting concentration (conversion to tons/km^2)
209-
ds_c1 = xr.where(
210-
ds_p.concentration * 1000 > 0.001, ds_p.concentration * 1000, np.nan
211-
)
212-
# ds_c1.plot(ax=ax, cbar_kwargs={"label": r"Concentration (tons $km^{-2}$)"})
213-
202+
# Plot concentration (converted to tons/km^2)
203+
ds_c1 = xr.where(ds_p.concentration * 1000 > 0.001, ds_p.concentration * 1000, np.nan)
214204
c = ds_c1.plot(
215205
ax=ax1,
216206
levels=extended_levels,
@@ -221,54 +211,30 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
221211
extend="max",
222212
)
223213

224-
# regularly spaced grid spanning the domain of x and y
225-
xi = np.linspace(
226-
plot_curr.lon.values.min(),
227-
plot_curr.lon.values.max(),
228-
plot_curr.lon.values.shape[0],
229-
)
230-
yi = np.linspace(
231-
plot_curr.lat.values.min(),
232-
plot_curr.lat.values.max(),
233-
plot_curr.lat.values.shape[0],
234-
)
235-
236-
# bicubic interpolation
214+
# Create a regularly spaced grid
215+
xi = np.linspace(plot_curr.lon.values.min(), plot_curr.lon.values.max(), plot_curr.lon.values.shape[0])
216+
yi = np.linspace(plot_curr.lat.values.min(), plot_curr.lat.values.max(), plot_curr.lat.values.shape[0])
237217
x, y = np.meshgrid(xi, yi, indexing="xy", sparse=False)
238218

239-
# Vector components
219+
# Vector components for currents
240220
u = plot_curr.uo.values
241221
v = plot_curr.vo.values
242-
243222
velovect(
244223
ax1,
245224
x,
246225
y,
247226
u,
248227
v,
249-
density=3, # Adjust for density of vectors
250-
linewidth=0.6, # Adjust for vector line width
251-
color="black", # Single color or array for multicolor
252-
arrowsize=0.6, # Adjust arrow size
228+
density=3,
229+
linewidth=0.6,
230+
color="black",
231+
arrowsize=0.6,
253232
grains=16,
254-
# integration_direction='forward',
255-
broken_streamlines=True, # Allow streamlines to break
233+
broken_streamlines=True,
256234
zorder=1500,
257235
)
258236

259-
260-
# plotting current vectors
261-
# ax.quiver(
262-
# plot_curr.lon.values,
263-
# plot_curr.lat.values,
264-
# plot_curr.uo.values,
265-
# plot_curr.vo.values,
266-
# scale=10,
267-
# width=0.0025,
268-
# color="gray",
269-
# )
270-
271-
# plotting release point or center of mass
237+
# Plot the release point or center of mass
272238
ax1.plot(
273239
self.config["simulation"]["spill_lon"],
274240
self.config["simulation"]["spill_lat"],
@@ -277,16 +243,16 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
277243
markersize=14,
278244
)
279245

280-
# Add wind vector (quiver) at the gravity center
246+
# Add wind vector at the gravity center
281247
ax1.quiver(
282-
target_lon, # Plot at center of gravity
283-
target_lat, # Plot at center of gravity
284-
u_wind_raw, # Use wind from gravity center
285-
v_wind_raw, # Use wind from gravity center
286-
scale=50, # Adjust the scale of the wind vector
287-
color="red", # "#1f77b4"
248+
target_lon,
249+
target_lat,
250+
u_wind_raw,
251+
v_wind_raw,
252+
scale=50,
253+
color="red",
288254
zorder=2000,
289-
width=0.003, # Width of the arrow
255+
width=0.003,
290256
headwidth=3,
291257
headlength=4,
292258
)
@@ -297,43 +263,43 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
297263

298264
plt.xlim(lon_min, lon_max)
299265
plt.ylim(lat_min, lat_max)
300-
301266
lon_label = "Longitude (°E)" if lon_min >= 0 else "Longitude (°W)"
302267
lat_label = "Latitude (°N)" if lat_min >= 0 else "Latitude (°S)"
303-
304-
ax1.set_title(
305-
f"Surface Oil Concentration\n{current_time}", fontsize=18, pad=20
306-
)
268+
ax1.set_title(f"Surface Oil Concentration\n{current_time}", fontsize=18, pad=20)
307269
ax1.set_xlabel(lon_label, fontsize=12)
308270
ax1.set_ylabel(lat_label, fontsize=12)
309271
ax1.tick_params(axis='both', which='major', labelsize=10)
310272
ax1.tick_params(axis='both', which='minor', labelsize=10)
311-
ax1.set_xlim(lon_min, lon_max)
312-
ax1.set_ylim(lat_min, lat_max)
313273
plt.grid()
314274

315-
# Blank space row (above colorbar)
275+
# Setup colorbar
316276
ax_blank1 = fig.add_subplot(gs[1, :])
317277
ax_blank1.set_visible(False)
318-
319-
# Colorbar row
320278
cbar_ax = fig.add_subplot(gs[2, :])
321-
cbar = plt.colorbar(
322-
c, cax=cbar_ax, orientation="horizontal", ticks=extended_levels[:-1]
323-
)
279+
cbar = plt.colorbar(c, cax=cbar_ax, orientation="horizontal", ticks=extended_levels[:-1])
324280
cbar.set_label(r"tons km$^{-2}$", fontsize=11)
325-
326-
# Format the colorbar ticks
327281
cbar.ax.xaxis.set_major_formatter(FormatStrFormatter(format_string))
328282

283+
# Save the figure using a filename indicating it's the last time step
329284
plt.savefig(
330-
self.out_figures
331-
+ f"/surf_oil_concentration_{self.config['simulation']['name']}_{t+1:03d}.png",
285+
self.out_figures + f"/surf_oil_concentration_{self.config['simulation']['name']}_last.png",
332286
dpi=200,
333287
)
334-
335288
plt.close()
336289

290+
291+
# plotting current vectors
292+
# ax.quiver(
293+
# plot_curr.lon.values,
294+
# plot_curr.lat.values,
295+
# plot_curr.uo.values,
296+
# plot_curr.vo.values,
297+
# scale=10,
298+
# width=0.0025,
299+
# color="gray",
300+
# )
301+
302+
337303
def plot_mass_balance(self):
338304

339305
path = self.out_directory

0 commit comments

Comments
 (0)