Skip to content

use np.expand_dims instead of np.newaxis#96

Merged
efiring merged 4 commits intoTEOS-10:masterfrom
rcaneill:fix_distance_xarray
Jun 25, 2022
Merged

use np.expand_dims instead of np.newaxis#96
efiring merged 4 commits intoTEOS-10:masterfrom
rcaneill:fix_distance_xarray

Conversation

@rcaneill
Copy link
Contributor

The use of [np.newaxis, lon] is the distance function in geostrophy.py was not compatible with xarray (see exemple below). This PR fixes this issue.

Exemple

import xarray as xr
import gsw
da = xr.DataArray([0,1])
gsw.distance(da, da)

outputs

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-59-24e6827f537c> in <module>
----> 1 gsw.distance(da, da)

~/.cache/pypoetry/virtualenvs/gsw-xarray-NsrEXKiZ-py3.8/lib/python3.8/site-packages/gsw/_utilities.py in wrapper(*args, **kw)
     60             kw['p'] = newargs.pop()
     61 
---> 62         ret = f(*newargs, **kw)
     63 
     64         if isinstance(ret, tuple):

~/.cache/pypoetry/virtualenvs/gsw-xarray-NsrEXKiZ-py3.8/lib/python3.8/site-packages/gsw/geostrophy.py in distance(lon, lat, p, axis)
    185     if lon.ndim == 1:
    186         one_d = True
--> 187         lon = lon[np.newaxis, :]
    188         lat = lat[np.newaxis, :]
    189         axis = -1

~/.cache/pypoetry/virtualenvs/gsw-xarray-NsrEXKiZ-py3.8/lib/python3.8/site-packages/xarray/core/dataarray.py in __getitem__(self, key)
    739         else:
    740             # xarray-style array indexing
--> 741             return self.isel(indexers=self._item_key_to_dict(key))
    742 
    743     def __setitem__(self, key: Any, value: Any) -> None:

~/.cache/pypoetry/virtualenvs/gsw-xarray-NsrEXKiZ-py3.8/lib/python3.8/site-packages/xarray/core/dataarray.py in _item_key_to_dict(self, key)
    703         if utils.is_dict_like(key):
    704             return key
--> 705         key = indexing.expanded_indexer(key, self.ndim)
    706         return dict(zip(self.dims, key))
    707 

~/.cache/pypoetry/virtualenvs/gsw-xarray-NsrEXKiZ-py3.8/lib/python3.8/site-packages/xarray/core/indexing.py in expanded_indexer(key, ndim)
     48             new_key.append(k)
     49     if len(new_key) > ndim:
---> 50         raise IndexError("too many indices")
     51     new_key.extend((ndim - len(new_key)) * [slice(None)])
     52     return tuple(new_key)

IndexError: too many indices

@ocefpaf ocefpaf force-pushed the fix_distance_xarray branch from 7208f0c to 3723853 Compare June 24, 2022 18:10
@ocefpaf
Copy link
Member

ocefpaf commented Jun 24, 2022

Looks like there is a regression in latest pip on Windows. Getting out of build isolation did the trick for now.

@efiring this PR LGTM. Do you mind taking a look and merging if you agree?

Copy link
Member

@efiring efiring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestions; if you disagree, it can go in as-is.

@rcaneill rcaneill requested a review from efiring June 25, 2022 09:23
@efiring efiring merged commit c447795 into TEOS-10:master Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants