Skip to content
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

All dimmensions become coordinates? #722

Closed
wilhelmsen opened this issue Jan 25, 2016 · 4 comments
Closed

All dimmensions become coordinates? #722

wilhelmsen opened this issue Jan 25, 2016 · 4 comments

Comments

@wilhelmsen
Copy link

I have a netcdf file that looks like this (ncdump):

netcdf filevYpcG2 {
dimensions:
    coor1 = 2 ;
    dim1 = 2 ;
variables:
    int coor1(coor1) ;
        coor1:_FillValue = 999999 ;
    int var1(coor1) ;
        var1:_FillValue = 999999 ;
    int var2(dim1) ;
        var2:units = "unit1" ;
        var2:_FillValue = 999999 ;
        var2:attr_name = "variable1" ;

// global attributes:
        :metadata1 = "metadata1" ;
data:

 coor1 = 1, 2 ;

 var1 = 1, 2 ;

 var2 = 3, 4 ;
}

When opening it in xray (xarray) I therefore expect dim1 to be a dimension and coor1 to be a coordinate, because coor1 is also a variable, coor1(coor1)? But when opening the dataset in xray, I get the following:

In [1]: import xray;

In [2]: filename = "/tmp/filevYpcG2"

In [3]: x = xray.open_dataset(filename)

In [4]: x
Out[4]: 
<xray.Dataset>
Dimensions:  (coor1: 2, dim1: 2)
Coordinates:
  * coor1    (coor1) float64 1.0 2.0
  * dim1     (dim1) int64 0 1
Data variables:
    var1     (coor1) float64 1.0 2.0
    var2     (dim1) float64 3.0 4.0
Attributes:
    metadata1: metadata1


In [5]: x.coords
Out[5]: 
Coordinates:
  * coor1    (coor1) float64 1.0 2.0
  * dim1     (dim1) int64 0 1

In [6]: x.dims
Out[6]: Frozen(SortedKeysDict({u'coor1': 2, u'dim1': 2}))

In [7]: x.data_vars
Out[7]: 
Data variables:
    var1     (coor1) float64 1.0 2.0
    var2     (dim1) float64 3.0 4.0

That is, both coor1 and dim1 becomes coordinate variables. I expected only coor1 to become a coordinate variable, and not dim1.

Am I wrohg, or is it meant to be like this?

@shoyer
Copy link
Member

shoyer commented Jan 25, 2016

This is definitely intended behavior: if you don't supply a coordinate to label points along a dimension, a default coordinate (equivalently to range(n)) is created for you. This is somewhat similar to pandas. Looking through the docs, though, it looks like we never state this explicitly...

@wilhelmsen
Copy link
Author

Thank you!

I was just wondering how you know then, which one is an "actual" coordinate and which one is an indexed ("default") coordinate?

xarray seem to know this.

Example:

In [1]: import xray as xr

In [2]: import numpy as np

In [3]: var1 = xr.DataArray(np.array([1, 2], np.float64), coords=[np.array([1, 2], np.int64)], dims=["coor1",], name = "var1")

In [4]: var2 = xr.DataArray(np.array([3, 4], np.float64), dims=["dim1",], name = "var2")

In [5]: ds.var1
Out[5]: 
<xray.DataArray 'var1' (coor1: 2)>
array([ 1.,  2.])
Coordinates:
  * coor1    (coor1) int64 1 2

In [6]: ds.var2
Out[6]: 
<xray.DataArray 'var2' (dim1: 2)>
array([ 3.,  4.])
Coordinates:
  * dim1     (dim1) int64 0 1

In [7]: ds.var1.coords
Out[7]: 
Coordinates:
  * coor1    (coor1) int64 1 2

In [8]: ds.var2.coords
Out[8]: 
Coordinates:
  * dim1     (dim1) int64 0 1

In [9]: ds.to_netcdf("/tmp/from_xr.nc")

And then

$ ncdump /tmp/from_xr.nc 
netcdf from_xr {
dimensions:
    coor1 = 2 ;
    dim1 = 2 ;
variables:
    int64 coor1(coor1) ;
    double var1(coor1) ;
    double var2(dim1) ;
data:

 coor1 = 1, 2 ;

 var1 = 1, 2 ;

 var2 = 3, 4 ;
}

As dim1 is not written in the file, xarray somehow knows?

@shoyer
Copy link
Member

shoyer commented Jan 30, 2016

Yes, xarray checks for "trivial indexes" that are equivalent to what it would create automatically when it writes a file to disk:
https://github.com/pydata/xarray/blob/v0.7.0/xarray/backends/common.py#L31-L47

@jhamman
Copy link
Member

jhamman commented Dec 29, 2016

I'm closing since #1017 has moved us past this question.

@jhamman jhamman closed this as completed Dec 29, 2016
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

3 participants