Skip to content

Lost coords after multiplication #1403

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

Closed
Yefee opened this issue May 11, 2017 · 4 comments
Closed

Lost coords after multiplication #1403

Yefee opened this issue May 11, 2017 · 4 comments

Comments

@Yefee
Copy link

Yefee commented May 11, 2017

Recently, I occurred a bug: multiplication discards coords of dsarray.

In [1]: import xarray as xr

In [2]: xr.__version__
Out[2]: '0.9.5'

In [3]: tarea = xr.open_dataarray('tarea.nc')

In [4]: tarea
Out[4]:
<xarray.DataArray 'TAREA' (nlat: 384, nlon: 320)>
[122880 values with dtype=float64]
Coordinates:
    TLAT     (nlat, nlon) float64 -79.22 -79.22 -79.22 -79.22 -79.22 -79.22 ...
    TLONG    (nlat, nlon) float64 320.6 321.7 322.8 323.9 325.1 326.2 327.3 ...
Dimensions without coordinates: nlat, nlon
Attributes:
    long_name:  area of T cells
    units:      centimeter^2

In [6]: advt = xr.open_dataarray('advt.nc')

In [7]: advt
Out[7]:
<xarray.DataArray 'ADVT' (nlat: 384, nlon: 320)>
[122880 values with dtype=float64]
Coordinates:
    TLAT     (nlat, nlon) float64 -79.22 -79.22 -79.22 -79.22 -79.22 -79.22 ...
    TLONG    (nlat, nlon) float64 320.6 321.7 322.8 323.9 325.1 326.2 327.3 ...
Dimensions without coordinates: nlat, nlon

In [8]: advt * tarea
Out[8]:
<xarray.DataArray (nlat: 384, nlon: 320)>
array([[             nan,              nan,              nan, ...,
                     nan,              nan,              nan],
       [             nan,              nan,              nan, ...,
                     nan,              nan,              nan],
       [  8091417.091781,  15948194.682816, -49201736.790674, ...,
                     nan,              nan,              nan],
       ...,
       [             nan,              nan,              nan, ...,
                     nan,              nan,              nan],
       [             nan,              nan,              nan, ...,
                     nan,              nan,              nan],
       [             nan,              nan,              nan, ...,
                     nan,              nan,              nan]])
Dimensions without coordinates: nlat, nlon

TLAT and TLONG are gone. Any suggestion?

Here I provide my test data.

@shoyer
Copy link
Member

shoyer commented May 11, 2017

The problem here is that your coordinates differ slightly on each file:

In [14]: abs(advt.TLAT - tarea.TLAT).max()
Out[14]:
<xarray.DataArray 'TLAT' ()>
array(4.547473508864641e-13)

This does come up with some frequency, but unfortunately, it's hard to know a priori whether differences in floating point values are meaningful or just due to loss of numerical precision.

One way to fix this would be to add a tolerance keyword argument of some sort to xarray.Variable.equals(), which we could allow setting via xarray.set_options() for arithmetic. Then you could write something like:

with xarray.set_options(float_tolerance=1e-8):
    result = advt * tarea

and still preserve coordinates.

@Yefee
Copy link
Author

Yefee commented May 11, 2017

Thanks, @shoyer.
This confuses me for a long time. It solves my problem to some extent.

@stale
Copy link

stale bot commented Apr 11, 2019

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Apr 11, 2019
@shoyer
Copy link
Member

shoyer commented Apr 11, 2019

Closing this in favor of the bigger discussion in #2217

@shoyer shoyer closed this as completed Apr 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants