Search code examples
pythonselectpython-xarrayindices

xarray - select the data at specific x AND y coordinates


When selecting data with xarray at x,y locations, I get data for any pair of x,y. I would like to have a 1-D array not a 2-D array from the selection. Is there an efficient way to do this? (For now I am doing it with a for-loop...)

x = [x1,x2,x3,x4] y = [y1,y2,y3,y4]

DS = 2-D array

subset = Dataset.sel(longitude=x, latitude=y, method='nearest')

To rephrase, I would like to have the dataset at [x1,y1],[x2,y2],[x3,y3],[x4,y4] not at other location i.e. [x1,y2].


Solution

  • A list of points can be selected along multiple indices if the indexers are DataArrays with a common dimension. This will result in the array being reindexed along the indexers' common dimension.

    Straight from the docs on More Advanced Indexing:

    In [78]: da = xr.DataArray(np.arange(56).reshape((7, 8)), dims=['x', 'y'])
    
    In [79]: da
    Out[79]: 
    <xarray.DataArray (x: 7, y: 8)>
    array([[ 0,  1,  2,  3,  4,  5,  6,  7],
           [ 8,  9, 10, 11, 12, 13, 14, 15],
           [16, 17, 18, 19, 20, 21, 22, 23],
           [24, 25, 26, 27, 28, 29, 30, 31],
           [32, 33, 34, 35, 36, 37, 38, 39],
           [40, 41, 42, 43, 44, 45, 46, 47],
           [48, 49, 50, 51, 52, 53, 54, 55]])
    Dimensions without coordinates: x, y
    
    In [80]: da.isel(x=xr.DataArray([0, 1, 6], dims='z'),
       ....:         y=xr.DataArray([0, 1, 0], dims='z'))
       ....: 
    Out[80]: 
    <xarray.DataArray (z: 3)>
    array([ 0,  9, 48])
    Dimensions without coordinates: z
    

    The indexing array can also be easily pulled out of a pandas DataFrame, with something like da.sel(longitude=df.longitude.to_xarray(), latitude=df.latitude.to_xarray()), which will result in the DataArray being reindexed by the DataFrame's index.

    So in your case, rather than selecting with the lists or arrays x, y, turn them into DataArrays with a common dim - let's call it location:

    x = xr.DataArray([x1,x2,x3,x4], dims=['location'])
    y = xr.DataArray([y1,y2,y3,y4], dims=['location'])
    

    Now your selection will work as you hope:

    ds.sel(longitude=x, latitude=y, method='nearest')