Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename CORDEX_domain to domain_id argument #252

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions cordex/cmor/cmor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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!")
Expand All @@ -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,
Expand Down Expand Up @@ -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``).
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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``).
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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``).
Expand Down
1 change: 1 addition & 0 deletions docs/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
--------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading