Skip to content

Commit ace2806

Browse files
committed
Update from_copernicusmarine iteration through standard names
1 parent f3be557 commit ace2806

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

parcels/fieldset.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -300,39 +300,64 @@ def _rename_coords_copernicusmarine(ds):
300300
def _discover_copernicusmarine_U_and_V(ds: xr.Dataset) -> xr.Dataset:
301301
# Assumes that the dataset has U and V data
302302

303-
cf_standard_name_fallbacks = {
304-
"U": [
305-
"eastward_sea_water_velocity", # GLOBAL_ANALYSISFORECAST_PHY_001_024, MEDSEA_ANALYSISFORECAST_PHY_006_013, BALTICSEA_ANALYSISFORECAST_PHY_003_006, BLKSEA_ANALYSISFORECAST_PHY_007_001, IBI_ANALYSISFORECAST_PHY_005_001, NWSHELF_ANALYSISFORECAST_PHY_004_013, MULTIOBS_GLO_PHY_MYNRT_015_003, MULTIOBS_GLO_PHY_W_3D_REP_015_007
306-
"surface_geostrophic_eastward_sea_water_velocity", # SEALEVEL_GLO_PHY_L4_MY_008_047, SEALEVEL_EUR_PHY_L4_NRT_008_060
307-
"geostrophic_eastward_sea_water_velocity", # MULTIOBS_GLO_PHY_TSUV_3D_MYNRT_015_012
308-
"sea_surface_wave_stokes_drift_x_velocity", # GLOBAL_ANALYSISFORECAST_WAV_001_027, MEDSEA_MULTIYEAR_WAV_006_012, ARCTIC_ANALYSIS_FORECAST_WAV_002_014, BLKSEA_ANALYSISFORECAST_WAV_007_003, IBI_ANALYSISFORECAST_WAV_005_005, NWSHELF_ANALYSISFORECAST_WAV_004_014
309-
"sea_water_x_velocity", # ARCTIC_ANALYSISFORECAST_PHY_002_001
310-
"eastward_sea_water_velocity_vertical_mean_over_pelagic_layer", # GLOBAL_MULTIYEAR_BGC_001_033
311-
],
312-
"V": [
303+
cf_UV_standard_name_fallbacks = [
304+
(
305+
"eastward_sea_water_velocity",
313306
"northward_sea_water_velocity",
307+
), # GLOBAL_ANALYSISFORECAST_PHY_001_024, MEDSEA_ANALYSISFORECAST_PHY_006_013, BALTICSEA_ANALYSISFORECAST_PHY_003_006, BLKSEA_ANALYSISFORECAST_PHY_007_001, IBI_ANALYSISFORECAST_PHY_005_001, NWSHELF_ANALYSISFORECAST_PHY_004_013, MULTIOBS_GLO_PHY_MYNRT_015_003, MULTIOBS_GLO_PHY_W_3D_REP_015_007
308+
(
309+
"surface_geostrophic_eastward_sea_water_velocity",
314310
"surface_geostrophic_northward_sea_water_velocity",
311+
), # SEALEVEL_GLO_PHY_L4_MY_008_047, SEALEVEL_EUR_PHY_L4_NRT_008_060
312+
(
313+
"geostrophic_eastward_sea_water_velocity",
315314
"geostrophic_northward_sea_water_velocity",
315+
), # MULTIOBS_GLO_PHY_TSUV_3D_MYNRT_015_012
316+
(
317+
"sea_surface_wave_stokes_drift_x_velocity",
316318
"sea_surface_wave_stokes_drift_y_velocity",
317-
"sea_water_y_velocity",
319+
), # GLOBAL_ANALYSISFORECAST_WAV_001_027, MEDSEA_MULTIYEAR_WAV_006_012, ARCTIC_ANALYSIS_FORECAST_WAV_002_014, BLKSEA_ANALYSISFORECAST_WAV_007_003, IBI_ANALYSISFORECAST_WAV_005_005, NWSHELF_ANALYSISFORECAST_WAV_004_014
320+
("sea_water_x_velocity", "sea_water_y_velocity"), # ARCTIC_ANALYSISFORECAST_PHY_002_001
321+
(
322+
"eastward_sea_water_velocity_vertical_mean_over_pelagic_layer",
318323
"northward_sea_water_velocity_vertical_mean_over_pelagic_layer",
319-
],
320-
}
321-
322-
for parcels_varname, fallbacks in cf_standard_name_fallbacks.items():
323-
if parcels_varname in ds:
324-
continue
324+
), # GLOBAL_MULTIYEAR_BGC_001_033
325+
]
326+
327+
if "U" in ds and "V" in ds:
328+
return ds # U and V already present
329+
elif "U" in ds or "V" in ds:
330+
raise ValueError(
331+
"Dataset has only one of the two variables 'U' and 'V'. Please rename the appropriate variable in your dataset to have both 'U' and 'V' for Parcels simulation."
332+
)
325333

326-
for cf_standard_name in fallbacks:
327-
if cf_standard_name in ds.cf.standard_names:
328-
name = ds.cf[cf_standard_name].name
329-
ds = ds.rename({name: parcels_varname})
330-
logger.info(
331-
f"Found variable {name!r} with CF standard name {cf_standard_name!r} in dataset, renamed it to {parcels_varname!r} for Parcels simulation."
334+
for cf_standard_name_U, cf_standard_name_V in cf_UV_standard_name_fallbacks:
335+
if cf_standard_name_U in ds.cf.standard_names:
336+
if cf_standard_name_V not in ds.cf.standard_names:
337+
raise ValueError(
338+
f"Dataset has variable with CF standard name {cf_standard_name_U!r}, "
339+
f"but not the matching variable with CF standard name {cf_standard_name_V!r}. "
340+
"Please rename the appropriate variables in your dataset to have both 'U' and 'V' for Parcels simulation."
332341
)
333-
break
334342
else:
335-
raise ValueError(
336-
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."
337-
)
343+
continue
344+
345+
ds = _ds_rename_using_standard_names(ds, {cf_standard_name_U: "U", cf_standard_name_V: "V"})
346+
break
347+
else:
348+
raise ValueError(
349+
f"Could not find variables 'U' and 'V' in dataset, nor any of the fallback CF standard names "
350+
f"{cf_UV_standard_name_fallbacks}. Please rename the appropriate variables to 'U' and 'V' in "
351+
"your dataset for the Parcels simulation."
352+
)
353+
return ds
354+
355+
356+
def _ds_rename_using_standard_names(ds: xr.Dataset, name_dict: dict[str, str]) -> xr.Dataset:
357+
for standard_name, rename_to in name_dict.items():
358+
name = ds.cf[standard_name].name
359+
ds = ds.rename({name: rename_to})
360+
logger.info(
361+
f"cf_xarray found variable {name!r} with CF standard name {standard_name!r} in dataset, renamed it to {rename_to!r} for Parcels simulation."
362+
)
338363
return ds

0 commit comments

Comments
 (0)