ajdawson / windspharm

A Python library for spherical harmonic computations on vector winds.
http://ajdawson.github.io/windspharm
MIT License
82 stars 38 forks source link

xarray.VectorWind gives strange error when u- and v- components have different extent #71

Open shoyer opened 8 years ago

shoyer commented 8 years ago

Adapted from the sfvp example:

>>> VectorWind(uwnd, vwnd[:, :, :10])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-80b64ca64fa5> in <module>()
----> 1 VectorWind(uwnd, vwnd[:, :, :10])

/Users/shoyer/conda/envs/windspharm/lib/python3.5/site-packages/windspharm/xarray.py in __init__(self, u, v, rsphere)
     69             msg = 'u and v must have the same dimension coordinates'
     70             raise ValueError(msg)
---> 71         if not all([(uc == vc).all() for uc, vc in zip(ucoords, vcoords)]):
     72             msg = 'u and v must have the same dimension coordinate values'
     73             raise ValueError(msg)

/Users/shoyer/conda/envs/windspharm/lib/python3.5/site-packages/windspharm/xarray.py in <listcomp>(.0)
     69             msg = 'u and v must have the same dimension coordinates'
     70             raise ValueError(msg)
---> 71         if not all([(uc == vc).all() for uc, vc in zip(ucoords, vcoords)]):
     72             msg = 'u and v must have the same dimension coordinate values'
     73             raise ValueError(msg)

AttributeError: 'bool' object has no attribute 'all'
ajdawson commented 8 years ago

This test looks wrong to me, I don't think the .all() should be there. You'd know more about comparing xarray coordinates than me, what is the best way to write this? I don't really use xarray much, and I implemented this whilst still learning how xarray works, errors and areas for improvement are inevitable.

shoyer commented 8 years ago

I think this is actually a pretty reasonable way to compare coordinates. And, if my guess is correct, this is actually a subtle NumPy bug/issue: arrays of different length compare as False instead of raising TypeError when compared.

I would simply replace (uc == vc).all() with np.all(uc == vc). That should fix the bool issue.