From 737d593ba1ecfb8b1e4b92bd70cbc61d166f183e Mon Sep 17 00:00:00 2001 From: Lars Buntemeyer Date: Sat, 22 Jun 2024 16:54:04 +0200 Subject: [PATCH] rename `CORDEX_domain` to `domain_id` argument (#252) * rename CORDEX_domain to domain_id argument * added deprecation warning * update whats new --- cordex/cmor/cmor.py | 65 ++++++++++++++++++++++++++------------------- docs/whats_new.rst | 1 + tests/test_cmor.py | 2 +- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/cordex/cmor/cmor.py b/cordex/cmor/cmor.py index 9f70ff2..3c7975f 100644 --- a/cordex/cmor/cmor.py +++ b/cordex/cmor/cmor.py @@ -145,8 +145,8 @@ def _get_time_axis_name(time_cell_method): def _define_axes(ds, table_id): - if "CORDEX_domain" in ds.attrs: - grid = cordex_domain(ds.attrs["CORDEX_domain"], bounds=True) + if "domain_id" in ds.attrs: + grid = cordex_domain(ds.attrs["domain_id"], bounds=True) lon_vertices = grid.lon_vertices.to_numpy() lat_vertices = grid.lat_vertices.to_numpy() else: @@ -458,7 +458,7 @@ def prepare_variable( allow_units_convert=False, allow_resample=False, input_freq=None, - CORDEX_domain=None, + domain_id=None, time_units=None, rewrite_time_axis=False, use_cftime=False, @@ -500,11 +500,12 @@ def prepare_variable( # remove point coordinates, e.g, height2m if squeeze is True: var_ds = var_ds.squeeze(drop=True) - if CORDEX_domain is not None: - var_ds.attrs["CORDEX_domain"] = CORDEX_domain - var_ds = _crop_to_cordex_domain(var_ds, CORDEX_domain) + if domain_id is not None: + var_ds.attrs["domain_id"] = domain_id + var_ds = _crop_to_cordex_domain(var_ds, domain_id) if replace_coords is True: - grid = cordex_domain(CORDEX_domain, bounds=True) + domain_id = domain_id or var_ds.cx.domain_id + grid = cordex_domain(domain_id, bounds=True) var_ds = var_ds.assign_coords(rlon=grid.rlon, rlat=grid.rlat) var_ds = var_ds.assign_coords(lon=grid.lon, lat=grid.lat) var_ds = var_ds.assign_coords( @@ -533,8 +534,8 @@ def prepare_variable( try: mapping = ds.cf["grid_mapping"] # _get_pole(ds) except KeyError: - warn(f"adding pole from archive specs: {CORDEX_domain}") - mapping = _get_cordex_pole(CORDEX_domain) + warn(f"adding pole from archive specs: {domain_id}") + mapping = _get_cordex_pole(domain_id) if "time" in mapping.coords: raise Exception("grid_mapping variable should have no time coordinate!") @@ -556,7 +557,7 @@ def cmorize_variable( allow_units_convert=False, allow_resample=False, input_freq=None, - CORDEX_domain=None, + domain_id=None, time_units=None, rewrite_time_axis=False, outpath=None, @@ -599,8 +600,8 @@ def cmorize_variable( The frequency of the input dataset in pandas notation. It ``None`` and the dataset contains a time axis, the frequency will be determined automatically using ``pandas.infer_freq`` if possible. - CORDEX_domain: str - Cordex domain short name. If ``None``, the domain will be determined by the ``CORDEX_domain`` + domain_id: str + Cordex domain short name. If ``None``, the domain will be determined by the ``domain_id`` global attribute if available. time_units: str Time units of the cmorized dataset (``ISO 8601``). @@ -622,15 +623,25 @@ def cmorize_variable( """ ds = ds.copy() - if CORDEX_domain is None: + if "CORDEX_domain" in kwargs: + warn( + "'CORDEX_domain' keyword is deprecated, please use the 'domain_id' keyword instead", + DeprecationWarning, + stacklevel=2, + ) + domain_id = kwargs["CORDEX_domain"] + del kwargs["CORDEX_domain"] + + if domain_id is None: try: - CORDEX_domain = ds.CORDEX_domain - except Exception: + domain_id = ds.cx.domain_id + except Exception as e: + warn(e) warn( - "could not identify CORDEX domain, try to set the 'CORDEX_domain' argument" + "could not identify CORDEX domain, try to set the 'domain_id' argument" ) - elif "CORDEX_domain" not in ds.attrs: - ds.attrs["CORDEX_domain"] = CORDEX_domain + elif "domain_id" not in ds.attrs: + ds.attrs["domain_id"] = domain_id if inpath is None: inpath = os.path.dirname(cmor_table) @@ -645,7 +656,7 @@ def cmorize_variable( ds, out_name, cmor_table, - CORDEX_domain=CORDEX_domain, + domain_id=domain_id, mapping_table=mapping_table, replace_coords=replace_coords, input_freq=input_freq, @@ -677,7 +688,7 @@ def __init__( allow_units_convert=False, allow_resample=False, input_freq=None, - CORDEX_domain=None, + domain_id=None, time_units=None, rewrite_time_axis=False, outpath=None, @@ -712,8 +723,8 @@ def __init__( The frequency of the input dataset in pandas notation. It ``None`` and the dataset contains a time axis, the frequency will be determined automatically using ``pandas.infer_freq`` if possible. - CORDEX_domain: str - Cordex domain short name. If ``None``, the domain will be determined by the ``CORDEX_domain`` + domain_id: str + Cordex domain short name. If ``None``, the domain will be determined by the ``domain_id`` global attribute if available. time_units: str Time units of the cmorized dataset (``ISO 8601``). @@ -734,7 +745,7 @@ def __init__( self.replace_coords = replace_coords self.allow_units_convert = allow_units_convert self.allow_resample = allow_resample - self.CORDEX_domain = CORDEX_domain + self.domain_id = domain_id self.time_units = time_units self.rewrite_time_axis = rewrite_time_axis self.outpath = outpath @@ -754,7 +765,7 @@ def preprocess( allow_units_convert=False, allow_resample=False, input_freq=None, - CORDEX_domain=None, + domain_id=None, time_units=None, rewrite_time_axis=False, use_cftime=False, @@ -770,7 +781,7 @@ def preprocess( allow_units_convert=allow_units_convert or self.allow_units_convert, allow_resample=allow_resample or self.allow_resample, input_freq=input_freq, - CORDEX_domain=CORDEX_domain or self.CORDEX_domain, + domain_id=domain_id or self.domain_id, time_units=time_units or self.time_units, rewrite_time_axis=rewrite_time_axis or self.rewrite_time_axis, use_cftime=use_cftime, @@ -820,8 +831,8 @@ def cmorize(self, ds, out_name, cmor_table): The frequency of the input dataset in pandas notation. It ``None`` and the dataset contains a time axis, the frequency will be determined automatically using ``pandas.infer_freq`` if possible. - CORDEX_domain: str - Cordex domain short name. If ``None``, the domain will be determined by the ``CORDEX_domain`` + domain_id: str + Cordex domain short name. If ``None``, the domain will be determined by the ``domain_id`` global attribute if available. time_units: str Time units of the cmorized dataset (``ISO 8601``). diff --git a/docs/whats_new.rst b/docs/whats_new.rst index d7d7bd7..31286ae 100644 --- a/docs/whats_new.rst +++ b/docs/whats_new.rst @@ -10,6 +10,7 @@ Internal Changes ~~~~~~~~~~~~~~~~ - Pin ``pint!=0.24`` due to breaking changes that affect ``cf_xarray`` (:pull:`249`). +- Changed ``CORDEX_domain`` keyword to ``domain_id`` in :py:meth:`cmor.cmorize_variable`, added deprecation warning (:pull:`252`). v0.7.1 (4 June 2024) -------------------- diff --git a/tests/test_cmor.py b/tests/test_cmor.py index 94011fc..c520592 100644 --- a/tests/test_cmor.py +++ b/tests/test_cmor.py @@ -180,7 +180,7 @@ def run_cmorizer(ds, out_name, domain_id, table_id, dataset_table=None, **kwargs cmor_table=cordex_cmor_table(f"{table_prefix}_{table_id}"), dataset_table=dataset_table, grids_table=cordex_cmor_table(f"{table_prefix}_grids"), - CORDEX_domain=domain_id, + domain_id=domain_id, replace_coords=True, allow_units_convert=True, allow_resample=True,