Skip to content

Commit 44fc779

Browse files
Merge pull request #16 from CMCC-Foundation/plot_update
Adding wind quiver and further adjustments
2 parents 19eb8b7 + 52a1072 commit 44fc779

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

src/plot/plot_mdk3.py

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
4444
# read coasline
4545
land = gpd.read_file(self.config["input_files"]["dtm"]["coastline_path"])
4646

47+
4748
# read output netcdf in concentration
4849
ds_particles = xr.open_dataset(self.concentration_path)
4950

@@ -58,20 +59,40 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
5859
# opening currents netcdf
5960
curr = xr.open_mfdataset(f"{self.root_directory}/oce_files/*.nc")
6061

62+
# opening wind netcdf
63+
wind = xr.open_mfdataset(f"{self.root_directory}/met_files/*.nc").transpose("time", "lon", "lat")
64+
65+
# gravity center
66+
lon_gravity_center = ds_particles["lon_gravity_center"].values
67+
lat_gravity_center = ds_particles["lat_gravity_center"].values
68+
6169
# ensuring date index is correct
6270
try:
6371
curr["time"] = curr.indexes["time"].to_datetimeindex()
72+
wind["time"] = wind.indexes["time"].to_datetimeindex()
6473
except (KeyError, AttributeError):
6574
pass
66-
75+
76+
# Resample current data to hourly intervals and interpolate
6777
curr = curr.resample(time="1h").interpolate("linear")
78+
6879
# selecting simulation date
6980
curr = curr.sel(time=slice(inidate, enddate))
81+
82+
# selecting surface current
7083
curr = curr.isel(depth=0)
7184

72-
# defining plot boundaries
73-
# lon_min, lon_max = self.config["plot_options"]["plot_lon"]
74-
# lat_min, lat_max = self.config["plot_options"]["plot_lat"]
85+
# Resample wind data to hourly intervals and interpolate
86+
wind = wind.resample(time="1h").interpolate("linear")
87+
88+
# Select wind data within the simulation time period
89+
wind = wind.sel(time=slice(inidate, enddate))
90+
91+
# Check and correct longitude bounds
92+
if lon_min > lon_max:
93+
lon_min, lon_max = lon_max, lon_min
94+
if lat_min > lat_max:
95+
lat_min, lat_max = lat_max, lat_min
7596

7697
# cropping coastline to area of interest
7798
rec = land.cx[lon_min:lon_max, lat_min:lat_max]
@@ -110,10 +131,35 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
110131

111132
# loop for ploting
112133
for t in range(0, len(ds_particles.time)):
113-
134+
135+
target_lon = lon_gravity_center[t] # Replace with your target latitude
136+
target_lat = lat_gravity_center[t] # Replace with your target longitude
137+
138+
# From wind dataset find nearest lon/lat points to the gravity center
139+
nearest_lon = wind.lon.sel(lon=target_lon, method="nearest")
140+
nearest_lat = wind.lat.sel(lat=target_lat, method="nearest")
141+
142+
# Using nearest available lat/lon
143+
subset_wind = wind.sel(lon=nearest_lon, lat=nearest_lat)
144+
145+
# Make sure that lon/lat are preserved as dimensions
146+
subset_wind = subset_wind.expand_dims(["lat", "lon"])
147+
114148
# select the iteration timestep
115149
ds_p = ds_particles.isel(time=t)
116150
plot_curr = curr.isel(time=t)
151+
plot_wind = subset_wind.isel(time=t)
152+
153+
# Extract U10M and V10M values at the nearest position
154+
u_wind_raw = plot_wind.sel(lon=nearest_lon, lat=nearest_lat).U10M.values
155+
v_wind_raw = plot_wind.sel(lon=nearest_lon, lat=nearest_lat).V10M.values
156+
157+
# Check if the data is NaN
158+
if np.isnan(u_wind_raw) or np.isnan(v_wind_raw):
159+
print(
160+
f"plotting - NaN wind data found at timestep {t}. Skipping wind interpolation."
161+
)
162+
continue # Skip interpolation if raw data is invalid
117163

118164
fig = plt.figure(figsize=(10, 8))
119165
gs = plt.GridSpec(
@@ -211,6 +257,20 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
211257
markersize=14,
212258
)
213259

260+
# Add wind vector (quiver) at the gravity center
261+
ax1.quiver(
262+
target_lon, # Plot at center of gravity
263+
target_lat, # Plot at center of gravity
264+
u_wind_raw, # Use wind from gravity center
265+
v_wind_raw, # Use wind from gravity center
266+
scale=50, # Adjust the scale of the wind vector
267+
color="red", # "#1f77b4"
268+
zorder=2000,
269+
width=0.003, # Width of the arrow
270+
headwidth=3,
271+
headlength=4,
272+
)
273+
214274
current_time = (
215275
inidate + pd.to_timedelta(ds_particles.time.values[t]) - pd.Timedelta(hours=1)
216276
).strftime("%Y-%m-%d %H:%M")
@@ -224,10 +284,10 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
224284
ax1.set_title(
225285
f"Surface Oil Concentration\n{current_time}", fontsize=18, pad=20
226286
)
227-
ax1.set_xlabel(lon_label, fontsize=14)
228-
ax1.set_ylabel(lat_label, fontsize=14)
229-
ax1.tick_params(axis='both', which='major', labelsize=12)
230-
ax1.tick_params(axis='both', which='minor', labelsize=12)
287+
ax1.set_xlabel(lon_label, fontsize=12)
288+
ax1.set_ylabel(lat_label, fontsize=12)
289+
ax1.tick_params(axis='both', which='major', labelsize=10)
290+
ax1.tick_params(axis='both', which='minor', labelsize=10)
231291
ax1.set_xlim(lon_min, lon_max)
232292
ax1.set_ylim(lat_min, lat_max)
233293
plt.grid()
@@ -241,7 +301,7 @@ def plot_matplotlib(self,lon_min,lon_max,lat_min,lat_max):
241301
cbar = plt.colorbar(
242302
c, cax=cbar_ax, orientation="horizontal", ticks=extended_levels[:-1]
243303
)
244-
cbar.set_label(r"tons km$^{-2}$", fontsize=13)
304+
cbar.set_label(r"tons km$^{-2}$", fontsize=11)
245305

246306
# Format the colorbar ticks
247307
cbar.ax.xaxis.set_major_formatter(FormatStrFormatter(format_string))

0 commit comments

Comments
 (0)