Xarray: Lost coords after multiplication

Created on 11 May 2017  路  4Comments  路  Source: pydata/xarray

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.

help wanted stale

Most helpful comment

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.

All 4 comments

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.

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

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

Closing this in favor of the bigger discussion in https://github.com/pydata/xarray/issues/2217

Was this page helpful?
0 / 5 - 0 ratings