|
13 | 13 | from parcels._core.utils.time import is_compatible as datetime_is_compatible |
14 | 14 | from parcels._typing import Mesh |
15 | 15 | from parcels.field import Field, VectorField |
| 16 | +from parcels.tools.loggers import logger |
16 | 17 | from parcels.xgrid import XGrid |
17 | 18 |
|
18 | 19 | if TYPE_CHECKING: |
@@ -177,7 +178,7 @@ def gridset(self) -> list[BaseGrid]: |
177 | 178 |
|
178 | 179 | def from_copernicusmarine(ds: xr.Dataset): |
179 | 180 | ds = ds.copy() |
180 | | - |
| 181 | + ds = _discover_copernicusmarine_U_and_V(ds) |
181 | 182 | expected_axes = set("XYZT") # TODO: Update after we have support for 2D spatial fields |
182 | 183 | if missing_axes := (expected_axes - set(ds.cf.axes)): |
183 | 184 | raise ValueError( |
@@ -265,3 +266,30 @@ def _rename_coords_copernicusmarine(ds): |
265 | 266 | except ValueError as e: |
266 | 267 | raise ValueError(f"Multiple coordinates found for Copernicus dataset on axis '{axis}'. Check your data.") from e |
267 | 268 | return ds |
| 269 | + |
| 270 | + |
| 271 | +def _discover_copernicusmarine_U_and_V(ds: xr.Dataset) -> xr.Dataset: |
| 272 | + # Assumes that the dataset has U and V data |
| 273 | + |
| 274 | + cf_standard_name_fallbacks = { |
| 275 | + "U": ["eastward_sea_water_velocity_due_to_ekman_drift"], |
| 276 | + "V": ["northward_sea_water_velocity_due_to_ekman_drift"], |
| 277 | + } |
| 278 | + |
| 279 | + for parcels_varname, fallbacks in cf_standard_name_fallbacks.items(): |
| 280 | + if parcels_varname in ds: |
| 281 | + continue |
| 282 | + |
| 283 | + for cf_standard_name in fallbacks: |
| 284 | + if cf_standard_name in ds.cf.standard_names: |
| 285 | + name = ds.cf[cf_standard_name].name |
| 286 | + ds = ds.rename({name: parcels_varname}) |
| 287 | + logger.info( |
| 288 | + f"Found variable {name!r} with CF standard name {cf_standard_name!r} in dataset, renamed it to {parcels_varname!r} for Parcels simulation." |
| 289 | + ) |
| 290 | + break |
| 291 | + else: |
| 292 | + raise ValueError( |
| 293 | + f"Could not find variable {parcels_varname!r} in dataset, nor any of the fallback CF standard names {fallbacks}. Please rename the appropriate variables in your dataset to {parcels_varname!r} for Parcels simulation." |
| 294 | + ) |
| 295 | + return ds |
0 commit comments