Skip to content

Commit 7c7f400

Browse files
Preserve legacy Xarray data_vars settings (#325)
1 parent f90e4ab commit 7c7f400

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

e3sm_to_cmip/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@
2424
# Path to the directory where MPAS handlers are defined.
2525
# TODO: MPAS handlers should eventually be refactored to `handlers.yaml` too.
2626
MPAS_HANDLER_DIR_PATH = f"{ROOT_HANDLERS_DIR}/mpas_vars"
27+
28+
# Settings to preserve legacy Xarray behavior when opening multiple files with
29+
# xarray.open_mfdataset(). These settings are automatically set when using
30+
# xcdat>=0.10.1.
31+
# See https://xarray.pydata.org/en/stable/user-guide/io.html#combining-multiple-files
32+
# and https://xarray.pydata.org/en/stable/whats-new.html#id14
33+
LEGACY_XARRAY_MERGE_SETTINGS = {
34+
# "override", "exact" are the new defaults as of Xarray v2025.08.0
35+
"compat": "no_conflicts",
36+
"join": "outer",
37+
}

e3sm_to_cmip/cmor_handlers/handler.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import xcdat as xc
1010
import yaml
1111

12+
from e3sm_to_cmip import LEGACY_XARRAY_MERGE_SETTINGS
1213
from e3sm_to_cmip._logger import _setup_child_logger
1314
from e3sm_to_cmip.cmor_handlers import FILL_VALUE, _formulas
1415
from e3sm_to_cmip.util import _get_table_for_non_monthly_freq
@@ -390,12 +391,7 @@ def _get_mfdataset(
390391
combine="nested",
391392
data_vars="minimal",
392393
coords="minimal",
393-
# NOTE: Preserve legacy Xarray behavior by setting compat="no_conflicts"
394-
# and join="outer" ("override" and "exact" are the new defaults as of
395-
# Xarray v2025.08.0). These settings are automatically set when
396-
# using xcdat>=0.10.1.
397-
compat="no_conflicts",
398-
join="outer",
394+
**LEGACY_XARRAY_MERGE_SETTINGS,
399395
)
400396

401397
# If the output CMIP variable has an alternative time dimension name (e.g.,

e3sm_to_cmip/cmor_handlers/mpas_vars/pbo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import xarray
66

7-
from e3sm_to_cmip import mpas, util
7+
from e3sm_to_cmip import LEGACY_XARRAY_MERGE_SETTINGS, mpas, util
88
from e3sm_to_cmip._logger import _setup_child_logger
99
from e3sm_to_cmip.util import print_message
1010

@@ -88,11 +88,11 @@ def handle(infiles, tables, user_input_path, cmor_log_dir, **kwargs):
8888

8989
ds = mpas.remap(ds, "mpasocean", mappingFileName)
9090

91-
# NOTE: Preserve legacy Xarray behavior by setting compat="no_conflicts"
92-
# and join="outer" ("override" and "exact" are the new defaults as of
93-
# Xarray v2025.08.0)
9491
with xarray.open_mfdataset(
95-
pslFileNames, concat_dim="time", compat="no_conflicts", join="outer"
92+
pslFileNames,
93+
concat_dim="time",
94+
data_vars="all",
95+
**LEGACY_XARRAY_MERGE_SETTINGS, # type: ignore
9696
) as dsIn:
9797
ds[VAR_NAME] = ds[VAR_NAME] + dsIn.PSL.values
9898

e3sm_to_cmip/cmor_handlers/mpas_vars/pso.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import xarray
66

7-
from e3sm_to_cmip import mpas, util
7+
from e3sm_to_cmip import LEGACY_XARRAY_MERGE_SETTINGS, mpas, util
88
from e3sm_to_cmip._logger import _setup_child_logger
99
from e3sm_to_cmip.util import print_message
1010

@@ -85,11 +85,11 @@ def handle(infiles, tables, user_input_path, cmor_log_dir, **kwargs):
8585

8686
ds = mpas.remap(ds, "mpasocean", mappingFileName)
8787

88-
# NOTE: Preserve legacy Xarray behavior by setting compat="no_conflicts"
89-
# and join="outer" ("override" and "exact" are the new defaults as of
90-
# Xarray v2025.08.0)
9188
with xarray.open_mfdataset(
92-
pslFileNames, concat_dim="time", compat="no_conflicts", join="exact"
89+
pslFileNames,
90+
concat_dim="time",
91+
data_vars="all",
92+
**LEGACY_XARRAY_MERGE_SETTINGS, # type: ignore
9393
) as dsIn:
9494
ds[VAR_NAME] = ds[VAR_NAME] + dsIn.PSL.values
9595

e3sm_to_cmip/mpas.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import xarray
2121
from dask.diagnostics import ProgressBar
2222

23+
from e3sm_to_cmip import LEGACY_XARRAY_MERGE_SETTINGS
2324
from e3sm_to_cmip._logger import _setup_child_logger
2425

2526
logger = _setup_child_logger(__name__)
@@ -77,13 +78,9 @@ def remap_seaice_sgs(inFileName, outFileName, mappingFileName, renorm_threshold=
7778
# With data_vars='minimal', only data variables in which the dimension already appears are included.
7879
ds_out_all = xarray.open_mfdataset(
7980
f"{outFilePath}/temp_out*nc",
80-
data_vars="minimal",
8181
decode_times=False,
82-
# NOTE: Preserve legacy Xarray behavior by setting compat="no_conflicts"
83-
# and join="outer" ("override" and "exact" are the new defaults as of
84-
# Xarray v2025.08.0)
85-
compat="no_conflicts",
86-
join="outer",
82+
data_vars="minimal",
83+
**LEGACY_XARRAY_MERGE_SETTINGS, # type: ignore
8784
)
8885
ds_out_all = ds_out_all.drop("timeMonthly_avg_iceAreaCell")
8986
ds_out_all.to_netcdf(outFileName)
@@ -333,11 +330,8 @@ def open_mfdataset(
333330
concat_dim="Time",
334331
mask_and_scale=False,
335332
chunks=chunks,
336-
# NOTE: Preserve legacy Xarray behavior by setting compat="no_conflicts"
337-
# and join="outer" ("override" and "exact" are the new defaults as of
338-
# Xarray v2025.08.0).
339-
compat="no_conflicts",
340-
join="outer",
333+
data_vars="all",
334+
**LEGACY_XARRAY_MERGE_SETTINGS, # type: ignore
341335
)
342336

343337
if variableList is not None:

0 commit comments

Comments
 (0)