From 40f00504225a032b95ccbc04225c4ccc619bd062 Mon Sep 17 00:00:00 2001 From: EricKeenan Date: Fri, 18 Dec 2020 13:56:50 -0700 Subject: [PATCH 1/9] Add example to sel docstring #4630 --- xarray/core/dataarray.py | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 6316afd57f5..309a3ba5216 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1195,6 +1195,55 @@ def sel( Dataset.sel DataArray.isel + Examples + -------- + >>> ds = xr.tutorial.open_dataset("air_temperature") + >>> ds + + Dimensions: (lat: 25, lon: 53, time: 2920) + Coordinates: + * lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0 + * lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0 + * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00 + Data variables: + air (time, lat, lon) float32 ... + Attributes: + Conventions: COARDS + title: 4x daily NMC reanalysis (1948) + description: Data is from NMC initialized reanalysis\n(4x/day). These a... + platform: Model + references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly... + + >>> tgt_lat = xr.DataArray(np.linspace(40, 45, num=6), dims="points") + >>> tgt_lon = xr.DataArray(np.linspace(200, 205, num=6), dims="points") + >>> da = ds['air'].sel(lon=tgt_lon, lat=tgt_lat, method='nearest') + >>> da + + array([[284.6 , 284.6 , 283.19998, 283.19998, 280.19998, 280.19998], + [283.29 , 283.29 , 282.79 , 282.79 , 280.79 , 280.79 ], + [282. , 282. , 280.79 , 280.79 , 280. , 280. ], + ..., + [282.49 , 282.49 , 281.29 , 281.29 , 280.49 , 280.49 ], + [282.09 , 282.09 , 280.38998, 280.38998, 279.49 , 279.49 ], + [282.09 , 282.09 , 280.59 , 280.59 , 279.19 , 279.19 ]], + dtype=float32) + Coordinates: + lat (points) float32 40.0 40.0 42.5 42.5 45.0 45.0 + lon (points) float32 200.0 200.0 202.5 202.5 205.0 205.0 + * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00 + Dimensions without coordinates: points + Attributes: + long_name: 4xDaily Air temperature at sigma level 995 + units: degK + precision: 2 + GRIB_id: 11 + GRIB_name: TMP + var_desc: Air temperature + dataset: NMC Reanalysis + level_desc: Surface + statistic: Individual Obs + parent_stat: Other + actual_range: [185.16 322.1 ] """ ds = self._to_temp_dataset().sel( indexers=indexers, From 942dc17e545b1f80458494cedd93817cf1ca4fd7 Mon Sep 17 00:00:00 2001 From: EricKeenan Date: Fri, 18 Dec 2020 14:57:30 -0700 Subject: [PATCH 2/9] Add vectorized indexing example to narrative docs (#4630) --- doc/indexing.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/indexing.rst b/doc/indexing.rst index 58064582354..27071583a2a 100644 --- a/doc/indexing.rst +++ b/doc/indexing.rst @@ -395,6 +395,22 @@ These methods may also be applied to ``Dataset`` objects ds = da.to_dataset(name="bar") ds.isel(x=xr.DataArray([0, 1, 2], dims=["points"])) +Vectorized indexing may be used to extract information from the nearest +grid cells of interest, for example, the nearest climate model grid +cells to a collection specified weather station latitudes and longitudes. + +.. ipython:: python + + ds = xr.tutorial.open_dataset("air_temperature") + + # Define target latitude and longitude (where weather stations might be) + tgt_lat = xr.DataArray(np.linspace(40, 45, num=6), dims="points") + tgt_lon = xr.DataArray(np.linspace(200, 205, num=6), dims="points") + + # Retrieve data at the grid cells nearest to the target latitudes and longitudes + da = ds['air'].sel(lon=tgt_lon, lat=tgt_lat, method='nearest') + da + .. tip:: If you are lazily loading your data from disk, not every form of vectorized From 33c6216e99cf628ab2a2f6876a1c5c0fd5117cf5 Mon Sep 17 00:00:00 2001 From: EricKeenan Date: Tue, 5 Jan 2021 13:00:33 -0700 Subject: [PATCH 3/9] Update example to use a dataarray that is made with np.arange --- xarray/core/dataarray.py | 65 ++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 309a3ba5216..bc1c0b8aad4 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1197,53 +1197,32 @@ def sel( Examples -------- - >>> ds = xr.tutorial.open_dataset("air_temperature") - >>> ds - - Dimensions: (lat: 25, lon: 53, time: 2920) + >>> da = xr.DataArray( + np.arange(25).reshape(5, 5), + coords={"x": np.arange(5), "y": np.arange(5)}, + dims=("x", "y") + ) + >>> da + + 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]]) Coordinates: - * lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0 - * lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0 - * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00 - Data variables: - air (time, lat, lon) float32 ... - Attributes: - Conventions: COARDS - title: 4x daily NMC reanalysis (1948) - description: Data is from NMC initialized reanalysis\n(4x/day). These a... - platform: Model - references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly... - - >>> tgt_lat = xr.DataArray(np.linspace(40, 45, num=6), dims="points") - >>> tgt_lon = xr.DataArray(np.linspace(200, 205, num=6), dims="points") - >>> da = ds['air'].sel(lon=tgt_lon, lat=tgt_lat, method='nearest') + * x (x) int64 0 1 2 3 4 + * y (y) int64 0 1 2 3 4 + + >>> tgt_x = xr.DataArray(np.linspace(0, 4, num=5), dims="points") + >>> tgt_y = xr.DataArray(np.linspace(0, 4, num=5), dims="points") + >>> da = da.sel(x=tgt_x, y=tgt_y, method='nearest') >>> da - - array([[284.6 , 284.6 , 283.19998, 283.19998, 280.19998, 280.19998], - [283.29 , 283.29 , 282.79 , 282.79 , 280.79 , 280.79 ], - [282. , 282. , 280.79 , 280.79 , 280. , 280. ], - ..., - [282.49 , 282.49 , 281.29 , 281.29 , 280.49 , 280.49 ], - [282.09 , 282.09 , 280.38998, 280.38998, 279.49 , 279.49 ], - [282.09 , 282.09 , 280.59 , 280.59 , 279.19 , 279.19 ]], - dtype=float32) + + array([ 0, 6, 12, 18, 24]) Coordinates: - lat (points) float32 40.0 40.0 42.5 42.5 45.0 45.0 - lon (points) float32 200.0 200.0 202.5 202.5 205.0 205.0 - * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00 + x (points) int64 0 1 2 3 4 + y (points) int64 0 1 2 3 4 Dimensions without coordinates: points - Attributes: - long_name: 4xDaily Air temperature at sigma level 995 - units: degK - precision: 2 - GRIB_id: 11 - GRIB_name: TMP - var_desc: Air temperature - dataset: NMC Reanalysis - level_desc: Surface - statistic: Individual Obs - parent_stat: Other - actual_range: [185.16 322.1 ] """ ds = self._to_temp_dataset().sel( indexers=indexers, From 5b94de5d6ef9bcc6c8ecd832bef8ed219fc16312 Mon Sep 17 00:00:00 2001 From: EricKeenan Date: Tue, 5 Jan 2021 14:19:11 -0700 Subject: [PATCH 4/9] Add dataarray.isel example --- xarray/core/dataarray.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index bc1c0b8aad4..4984ef9e380 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1087,6 +1087,29 @@ def isel( -------- Dataset.isel DataArray.sel + + Examples + -------- + >>> da = xr.DataArray( + np.arange(25).reshape(5, 5), + dims=("x", "y") + ) + >>> da + + 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]]) + Dimensions without coordinates: x, y + + >>> tgt_x = xr.DataArray(np.arange(0, 5), dims="points") + >>> tgt_y = xr.DataArray(np.arange(0, 5), dims="points") + >>> da = da.isel(x=tgt_x, y=tgt_y) + >>> da + + array([ 0, 6, 12, 18, 24]) + Dimensions without coordinates: points """ indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel") From 404f1f1e6151e6aaca38220d1b5d9cfc115c26d4 Mon Sep 17 00:00:00 2001 From: EricKeenan Date: Tue, 5 Jan 2021 14:58:53 -0700 Subject: [PATCH 5/9] Attempting to fix a formatting error --- xarray/core/dataarray.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 4984ef9e380..5d37ebb2512 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1101,15 +1101,15 @@ def isel( [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) - Dimensions without coordinates: x, y + Dimensions without coordinates: x, y >>> tgt_x = xr.DataArray(np.arange(0, 5), dims="points") >>> tgt_y = xr.DataArray(np.arange(0, 5), dims="points") >>> da = da.isel(x=tgt_x, y=tgt_y) >>> da - array([ 0, 6, 12, 18, 24]) - Dimensions without coordinates: points + array([ 0, 6, 12, 18, 24]) + Dimensions without coordinates: points """ indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel") From 33ee0f9ed21e076bd731ec63b06db8fdc992e5e4 Mon Sep 17 00:00:00 2001 From: EricKeenan Date: Wed, 13 Jan 2021 13:35:18 -0700 Subject: [PATCH 6/9] Add prompts to example --- xarray/core/dataarray.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 5d37ebb2512..f12cac35026 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1091,9 +1091,9 @@ def isel( Examples -------- >>> da = xr.DataArray( - np.arange(25).reshape(5, 5), - dims=("x", "y") - ) + ... np.arange(25).reshape(5, 5), + ... dims=("x", "y") + ... ) >>> da array([[ 0, 1, 2, 3, 4], From 79497447e3f4e12ccd48e07cf0961112be1d1b42 Mon Sep 17 00:00:00 2001 From: EricKeenan Date: Wed, 13 Jan 2021 13:48:01 -0700 Subject: [PATCH 7/9] Fix: Forgot to add prompts to sel example. --- xarray/core/dataarray.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index f12cac35026..4df38379fbd 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1221,10 +1221,10 @@ def sel( Examples -------- >>> da = xr.DataArray( - np.arange(25).reshape(5, 5), - coords={"x": np.arange(5), "y": np.arange(5)}, - dims=("x", "y") - ) + ... np.arange(25).reshape(5, 5), + ... coords={"x": np.arange(5), "y": np.arange(5)}, + ... dims=("x", "y") + ... ) >>> da array([[ 0, 1, 2, 3, 4], From b374a177323d966c7d4abd857b320c637eb14c8c Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Tue, 16 Feb 2021 23:08:30 +0100 Subject: [PATCH 8/9] Apply suggestions from code review --- doc/indexing.rst | 2 +- xarray/core/dataarray.py | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/doc/indexing.rst b/doc/indexing.rst index 27071583a2a..01a348f8b94 100644 --- a/doc/indexing.rst +++ b/doc/indexing.rst @@ -408,7 +408,7 @@ cells to a collection specified weather station latitudes and longitudes. tgt_lon = xr.DataArray(np.linspace(200, 205, num=6), dims="points") # Retrieve data at the grid cells nearest to the target latitudes and longitudes - da = ds['air'].sel(lon=tgt_lon, lat=tgt_lat, method='nearest') + da = ds["air"].sel(lon=tgt_lon, lat=tgt_lat, method="nearest") da .. tip:: diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 4df38379fbd..de244dec0a4 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1090,10 +1090,7 @@ def isel( Examples -------- - >>> da = xr.DataArray( - ... np.arange(25).reshape(5, 5), - ... dims=("x", "y") - ... ) + >>> da = xr.DataArray(np.arange(25).reshape(5, 5), dims=("x", "y")) >>> da array([[ 0, 1, 2, 3, 4], @@ -1101,7 +1098,7 @@ def isel( [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) - Dimensions without coordinates: x, y + Dimensions without coordinates: x, y >>> tgt_x = xr.DataArray(np.arange(0, 5), dims="points") >>> tgt_y = xr.DataArray(np.arange(0, 5), dims="points") @@ -1223,7 +1220,7 @@ def sel( >>> da = xr.DataArray( ... np.arange(25).reshape(5, 5), ... coords={"x": np.arange(5), "y": np.arange(5)}, - ... dims=("x", "y") + ... dims=("x", "y"), ... ) >>> da @@ -1238,7 +1235,7 @@ def sel( >>> tgt_x = xr.DataArray(np.linspace(0, 4, num=5), dims="points") >>> tgt_y = xr.DataArray(np.linspace(0, 4, num=5), dims="points") - >>> da = da.sel(x=tgt_x, y=tgt_y, method='nearest') + >>> da = da.sel(x=tgt_x, y=tgt_y, method="nearest") >>> da array([ 0, 6, 12, 18, 24]) From 1ad0c04355d9d538d850d5c493e43b342ec7bf76 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Tue, 16 Feb 2021 23:28:27 +0100 Subject: [PATCH 9/9] simplify example --- doc/indexing.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/indexing.rst b/doc/indexing.rst index a8dd2860fa3..14af176c428 100644 --- a/doc/indexing.rst +++ b/doc/indexing.rst @@ -396,19 +396,19 @@ These methods may also be applied to ``Dataset`` objects ds.isel(x=xr.DataArray([0, 1, 2], dims=["points"])) Vectorized indexing may be used to extract information from the nearest -grid cells of interest, for example, the nearest climate model grid -cells to a collection specified weather station latitudes and longitudes. +grid cells of interest, for example, the nearest climate model grid cells +to a collection specified weather station latitudes and longitudes. .. ipython:: python ds = xr.tutorial.open_dataset("air_temperature") # Define target latitude and longitude (where weather stations might be) - tgt_lat = xr.DataArray(np.linspace(40, 45, num=6), dims="points") - tgt_lon = xr.DataArray(np.linspace(200, 205, num=6), dims="points") + target_lon = xr.DataArray([200, 201, 202, 205], dims="points") + target_lat = xr.DataArray([31, 41, 42, 42], dims="points") # Retrieve data at the grid cells nearest to the target latitudes and longitudes - da = ds["air"].sel(lon=tgt_lon, lat=tgt_lat, method="nearest") + da = ds["air"].sel(lon=target_lon, lat=target_lat, method="nearest") da .. tip::