xarray-contrib / xoak

xarray extension that provides tree-based indexes used for selecting irregular, n-dimensional data.
https://xoak.readthedocs.io
MIT License
57 stars 4 forks source link

Is it required to chunk selections? #40

Closed andhuang-CLGX closed 3 years ago

andhuang-CLGX commented 3 years ago
import xarray as xr
import xoak
ds = xr.tutorial.open_dataset('rasm').isel(time=0)

ds.xoak.set_index(['yc', 'xc'], "scipy_kdtree")

ds.xoak.sel(yc=16.91, xc=27.51)

AttributeError: 'float' object has no attribute 'chunks'

willirath commented 3 years ago

Currently, selections are only possible with xarray.DataArrays. These don't need to be chunked, but they do always have a degenerate single chunk:

print(xarray.DataArray([16.91, ]).chunks)
None

So you should be able to do something like:

import xarray as xr
import xoak
ds = xr.tutorial.open_dataset('rasm').isel(time=0)

ds.xoak.set_index(['yc', 'xc'], "scipy_kdtree")

print(ds.xoak.sel(yc=xr.DataArray([16.91, ]), xc=xr.DataArray([27.51, ])))
<xarray.Dataset>
Dimensions:  (dim_0: 1)
Coordinates:
    time     object 1980-09-16 12:00:00
    xc       (dim_0) float64 16.91
    yc       (dim_0) float64 27.51
Dimensions without coordinates: dim_0
Data variables:
    Tair     (dim_0) float64 ...
Attributes:
    title:                     /workspace/jhamman/processed/R1002RBRxaaa01a/l...
    institution:               U.W.
    source:                    RACM R1002RBRxaaa01a
    output_frequency:          daily
    output_mode:               averaged
    convention:                CF-1.4
    references:                Based on the initial model of Liang et al., 19...
    comment:                   Output from the Variable Infiltration Capacity...
    nco_openmp_thread_number:  1
    NCO:                       netCDF Operators version 4.7.9 (Homepage = htt...
    history:                   Fri Aug  7 17:57:38 2020: ncatted -a bounds,,d...
andhuang-CLGX commented 3 years ago

Do you think xoak should automatically do that internally to mimic xarray.Dataset.sel() behavior?

andhuang-CLGX commented 3 years ago

Oh I guess duplicate of https://github.com/xarray-contrib/xoak/issues/37