question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pointwise indexing -- something like sel_points

See original GitHub issue

@hdail suggested that it would be useful to be able to index points in addition to indexing dimensions separately. Right now, you can do this via something like: xray.concat([ds.sel(x=x, y=y) for x, y in pts], dim='station')

This would be handy for sampling particular points out of multiple dimensions, e.g., to compare gridded and station data.

It’s also probably worth implementing in xray because it can be done quickly using numpy’s fancy indexing (I think) which would be more efficient than a loop, though I suspect the implementation is probably somewhat complex.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:19 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
wholmgrencommented, Jul 7, 2015

+1 for this proposal.

I made a slight modification to @WeatherGod’s code so that I could use string indices for the “station” coordinate, though I’m sure there is a better way to implement this. Also note the addition of a few list calls for Python 3 compat.

def grid_to_points2(grid, points, coord_names):
    if not coord_names:
        raise ValueError("No coordinate names provided")
    spat_dims = {d for n in coord_names for d in grid[n].dims}
    not_spatial = set(grid.dims) - spat_dims
    spatial_selection = {n:0 for n in not_spatial}
    spat_only = grid.isel(**spatial_selection)

    coords = bcast(spat_only, coord_names)

    kd = KDTree(list(zip(*[c.ravel() for c in coords])))
    _, indx = kd.query(list(zip(*[points[n].values for n in coord_names])))
    indx = np.unravel_index(indx, coords[0].shape)

    station_da = xray.DataArray(name='station', dims='station', data=stations.index.values)

    return xray.concat(
            (grid.isel(**{n:j for n, j in zip(spat_only.dims, i)})
             for i in zip(*indx)),
            dim=station_da)

In [97]:
stations = pd.DataFrame({'XLAT':[32.13, 32.43], 'XLONG':[-110.95, -112.02]}, index=['KTUS', 'KPHX'])
stations
Out[97]:
XLAT    XLONG
KTUS    32.13   -110.95
KPHX    32.43   -112.02

In [98]:
station_ds = grid_to_points2(ds, stations, ('XLAT', 'XLONG'))
station_ds
Out[98]:
<xray.Dataset>
Dimensions:      (Times: 1681, station: 2)
Coordinates:
  * Times        (Times) datetime64[ns] 2015-07-02T06:00:00 ...
    XLAT         (station) float32 32.1239 32.4337
  * station      (station) object 'KTUS' 'KPHX'
    west_east    (station) int64 220 164
    XLONG        (station) float32 -110.947 -112.012
    south_north  (station) int64 116 134
Data variables:
    SWDNBRH      (station, Times) float32 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
    V10          (station, Times) float32 -2.09897 -1.94047 -1.55494 ...
    V80          (station, Times) float32 0.0 -1.95921 -1.87583 -1.86289 ...
    SWDNB        (station, Times) float32 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
    U10          (station, Times) float32 2.22951 1.89406 1.39955 1.04277 ...
    SWDDNI       (station, Times) float32 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
    SWDNBC       (station, Times) float32 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
    T2           (station, Times) float32 301.419 303.905 304.155 304.296 ...
    SWDDNIRH     (station, Times) float32 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
    U80          (station, Times) float32 0.0 1.93936 1.7901 1.63011 1.69481 ...

In [100]:
station_ds.sel(station='KTUS')[['U10','V10']]
Out[100]:
<xray.Dataset>
Dimensions:      (Times: 1681)
Coordinates:
    west_east    int64 220
    south_north  int64 116
    XLONG        float32 -110.947
  * Times        (Times) datetime64[ns] 2015-07-02T06:00:00 ...
    station      object 'KTUS'
    XLAT         float32 32.1239
Data variables:
    U10          (Times) float32 2.22951 1.89406 1.39955 1.04277 1.16338 ...
    V10          (Times) float32 -2.09897 -1.94047 -1.55494 -1.34216 ...
0reactions
shoyercommented, Jul 27, 2016
Read more comments on GitHub >

github_iconTop Results From Across the Web

xarray.Dataset.sel_points — xarray 0.8.2 documentation
Returns a new dataset with each array indexed pointwise by tick labels along the specified dimension(s). In contrast to Dataset.isel_points , indexers for ......
Read more >
Indexing and selecting data — xarray 0.7.1 documentation
Indexing a DataArray directly works (mostly) just like it does for numpy arrays, ... See Pointwise indexing for how to achieve this functionality...
Read more >
Indexing and selecting data - xarray - Read the Docs
Indexing a DataArray directly works (mostly) just like it does for numpy arrays, except that the ... The following is an example of...
Read more >
Download book PDF - Springer Link
A rough idea of how the product will function and what it looks like is ... as a sequence of indexed symbols by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found