-
Notifications
You must be signed in to change notification settings - Fork 41
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
Use standard_name mapper in rename_like #165
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import inspect | ||
import itertools | ||
import warnings | ||
from collections import ChainMap | ||
from collections import ChainMap, Counter | ||
from typing import ( | ||
Any, | ||
Callable, | ||
|
@@ -1298,8 +1298,6 @@ def rename_like( | |
``other``, that variable will be renamed to match the "longitude" variable in | ||
``other``. | ||
|
||
For now, this function only matches ``("latitude", "longitude", "vertical", "time")`` | ||
|
||
Parameters | ||
---------- | ||
other: DataArray, Dataset | ||
|
@@ -1312,18 +1310,21 @@ def rename_like( | |
ourkeys = self.keys() | ||
theirkeys = other.cf.keys() | ||
|
||
good_keys = set(_COORD_NAMES) & ourkeys & theirkeys | ||
if not good_keys: | ||
raise ValueError( | ||
"No common coordinate variables between these two objects." | ||
) | ||
|
||
good_keys = ourkeys & theirkeys | ||
renamer = {} | ||
for key in good_keys: | ||
ours = _get_axis_coord_single(self._obj, key)[0] | ||
theirs = _get_axis_coord_single(other, key)[0] | ||
renamer[ours] = theirs | ||
ours = apply_mapper( | ||
(_get_axis_coord, _get_with_standard_name), self._obj, key, error=False | ||
) | ||
theirs = apply_mapper( | ||
(_get_axis_coord, _get_with_standard_name), other, key, error=False | ||
) | ||
if len(ours) > 1 or len(theirs) > 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could optionally raise a warning here if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe warn ant the end about all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might work out with staggered grids: Rename axes first, if |
||
continue | ||
renamer[ours[0]] = theirs[0] | ||
|
||
count = Counter(renamer.values()) | ||
renamer = {k: v for k, v in renamer.items() if count[v] == 1} | ||
newobj = self._obj.rename(renamer) | ||
|
||
# rename variable names in the coordinates attribute | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,10 +214,13 @@ def test_getitem_ancillary_variables(): | |
def test_rename_like(): | ||
original = popds.copy(deep=True) | ||
|
||
with pytest.raises(KeyError): | ||
popds.cf.rename_like(airds) | ||
# it'll match for axis: X (lon, nlon) and coordinate="longitude" (lon, TLONG) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used to only check "coordinates", now we check all keys... |
||
# so delete the axis attributes | ||
newair = airds.copy(deep=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would switching to fixtures for the datasets get around this? |
||
del newair.lon.attrs["axis"] | ||
del newair.lat.attrs["axis"] | ||
|
||
renamed = popds.cf["TEMP"].cf.rename_like(airds) | ||
renamed = popds.cf["TEMP"].cf.rename_like(newair) | ||
for k in ["TLONG", "TLAT"]: | ||
assert k not in renamed.coords | ||
assert k in original.coords | ||
|
@@ -227,6 +230,20 @@ def test_rename_like(): | |
assert "lat" in renamed.coords | ||
assert renamed.attrs["coordinates"] == "lon lat" | ||
|
||
# standard name matching | ||
newroms = romsds.expand_dims(latitude=[1], longitude=[1]).cf.guess_coord_axis() | ||
renamed = popds.cf["UVEL"].cf.rename_like(newroms) | ||
assert renamed.attrs["coordinates"] == "longitude latitude" | ||
assert "longitude" in renamed.coords | ||
assert "latitude" in renamed.coords | ||
assert "ULON" not in renamed.coords | ||
assert "ULAT" not in renamed.coords | ||
|
||
# should change "temp" to "TEMP" | ||
renamed = romsds.cf.rename_like(popds) | ||
assert "temp" not in renamed | ||
assert "TEMP" in renamed | ||
|
||
|
||
@pytest.mark.parametrize("obj", objects) | ||
@pytest.mark.parametrize( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we still raise an error?