Skip to content

Commit 7ae8c9d

Browse files
authored
Add NDPointIndex example (#285)
* add NDPointIndex example * add style.css * finalize example * add history.md entry * remove logo-size from style.css
1 parent 07b8908 commit 7ae8c9d

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed

docs/_static/style.css

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Added as suggested by @benbovy to fix issues with dark mode
3+
*/
4+
5+
code.literal {
6+
background-color: unset;
7+
border: unset;
8+
}
9+
10+
.xr-wrap {
11+
/* workaround when (ipy)widgets are present in output cells
12+
* https://github.com/xarray-contrib/xarray-indexes/issues/14
13+
* only rely on pydata-sphinx-theme variables here!
14+
*/
15+
--xr-font-color0: var(--pst-color-text-base);
16+
--xr-font-color2: var(--pst-color-text-base);
17+
--xr-font-color3: var(--pst-color-text-base);
18+
--xr-border-color: hsl(from var(--pst-color-text-base) h s calc(l + 40));
19+
--xr-disabled-color: hsl(from var(--pst-color-text-base) h s calc(l + 40));
20+
--xr-background-color: var(--pst-color-on-background);
21+
--xr-background-color-row-even: hsl(
22+
from var(--pst-color-on-background) h s calc(l - 5)
23+
);
24+
--xr-background-color-row-odd: hsl(
25+
from var(--pst-color-on-background) h s calc(l - 15)
26+
);
27+
28+
font-size: 0.9em;
29+
margin-left: 1.25em;
30+
}
31+
32+
html[data-theme="dark"] .xr-wrap {
33+
--xr-border-color: hsl(from var(--pst-color-text-base) h s calc(l - 40));
34+
--xr-disabled-color: hsl(from var(--pst-color-text-base) h s calc(l - 40));
35+
--xr-background-color-row-even: hsl(
36+
from var(--pst-color-on-background) h s calc(l + 5)
37+
);
38+
--xr-background-color-row-odd: hsl(
39+
from var(--pst-color-on-background) h s calc(l + 15)
40+
);
41+
}
42+
43+
.xr-array-wrap,
44+
.xr-var-data,
45+
.xr-var-preview {
46+
/* font-size: 0.9em; */
47+
}
48+
.gp {
49+
color: darkorange;
50+
}
51+
52+
/* workaround Pydata Sphinx theme using light colors for widget cell outputs in dark-mode */
53+
/* works for many widgets but not for Xarray html reprs */
54+
/* https://github.com/pydata/pydata-sphinx-theme/issues/2189 */
55+
html[data-theme="dark"] div.cell_output .text_html:has(div.xr-wrap) {
56+
background-color: var(--pst-color-on-background) !important;
57+
color: var(--pst-color-text-base) !important;
58+
}

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def _custom_edit_url(
292292
# relative to this directory. They are copied after the builtin static files,
293293
# so a file named "default.css" will overwrite the builtin "default.css".
294294
html_static_path = ["_static"]
295+
html_css_files = ["style.css"]
295296

296297
favicons = [
297298
{

docs/history.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* FIX: Prevent literal timedelta decoding for new xarray versions, fix tests, update pinnings ({pull}`278`) by [@kmuehlbauer](https://github.com/kmuehlbauer).
1212
* MNT: Package and Documentation cleanup ({issue}`273`), {pull}`284`) by [@kmuehlbauer](https://github.com/kmuehlbauer).
1313
* ADD: Support Universal Format (UF) ({issue}`275`) ({pull}`276`) by [@kmuehlbauer](https://github.com/kmuehlbauer).
14+
* ADD: NDPointIndex Notebook example ({pull}`276`) by [@kmuehlbauer](https://github.com/kmuehlbauer).
1415

1516
## 0.9.0 (2025-02-07)
1617

docs/usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ notebooks/CfRadial1_Export
4747
notebooks/Read-plot-Sigmet-data-from-AWS
4848
notebooks/plot-ppi
4949
notebooks/angle_reindexing
50+
notebooks/NDPointIndex.ipynb
5051
notebooks/Multi-Volume-Concatenation.ipynb
5152
notebooks/multiple-sweeps-into-volume-scan.ipynb
5253
notebooks/Transform
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "0",
6+
"metadata": {},
7+
"source": [
8+
"# NDPointIndex\n",
9+
"\n",
10+
"This uses one of the new indexes xarray is providing. See [xarray-indexes](https://xarray-indexes.readthedocs.io/) for more in-depth details on that.\n",
11+
"\n",
12+
"Needs latest xarray '2025.7.1'"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "1",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"import cmweather # noqa\n",
23+
"import numpy as np\n",
24+
"import xarray as xr\n",
25+
"from open_radar_data import DATASETS"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"id": "2",
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"xr.__version__"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"id": "3",
41+
"metadata": {},
42+
"source": [
43+
"# Step-by-step guide"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"id": "4",
49+
"metadata": {},
50+
"source": [
51+
"## Load radar data"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"id": "5",
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"fname = DATASETS.fetch(\"DWD-Vol-2_99999_20180601054047_00.h5\")\n",
62+
"ds = xr.open_dataset(fname, engine=\"gamic\", group=\"sweep_9\")\n",
63+
"display(ds)"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"id": "6",
69+
"metadata": {},
70+
"source": [
71+
"## Georeference\n",
72+
"\n",
73+
"Add x,y,z - 2D-coordinates."
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"id": "7",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"ds = ds.xradar.georeference()\n",
84+
"display(ds)"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"id": "8",
90+
"metadata": {},
91+
"source": [
92+
"## Add NDPointIndex\n",
93+
"\n",
94+
"This uses scipy.KDTree under the hood. See also Indexes-Section in the html-repr below."
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"id": "9",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"ds = ds.set_xindex((\"x\", \"y\"), xr.indexes.NDPointIndex)\n",
105+
"display(ds)"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"id": "10",
111+
"metadata": {},
112+
"source": [
113+
"## Plot\n",
114+
"\n",
115+
"This works as usual with the new NDPointIndex."
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"id": "11",
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"ds.DBZH.plot(\n",
126+
" x=\"x\", y=\"y\", xlim=(-10e3, 10e3), ylim=(-10e3, 10e3), cmap=\"HomeyerRainbow\", vmin=0\n",
127+
")"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"id": "12",
133+
"metadata": {},
134+
"source": [
135+
"## Nearest neighbour interpolation with NDPointIndex\n"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"id": "13",
141+
"metadata": {},
142+
"source": [
143+
"### Create 1D DataArrays for x and y selection"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": null,
149+
"id": "14",
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"y = xr.DataArray(np.arange(-100e3, 100e3, 500), dims=\"y\", name=\"y\", attrs=ds.y.attrs)\n",
154+
"x = xr.DataArray(np.arange(-100e3, 100e3, 500), dims=\"x\", name=\"x\", attrs=ds.x.attrs)"
155+
]
156+
},
157+
{
158+
"cell_type": "markdown",
159+
"id": "15",
160+
"metadata": {},
161+
"source": [
162+
"### Select with above 1D DataArrays"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": null,
168+
"id": "16",
169+
"metadata": {},
170+
"outputs": [],
171+
"source": [
172+
"actual = ds.sel(y=y, x=x, method=\"nearest\")"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"id": "17",
178+
"metadata": {},
179+
"source": [
180+
"### Assign the 1D DataArrays"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": null,
186+
"id": "18",
187+
"metadata": {},
188+
"outputs": [],
189+
"source": [
190+
"actual = actual.assign(x=x, y=y)"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": null,
196+
"id": "19",
197+
"metadata": {},
198+
"outputs": [],
199+
"source": [
200+
"display(actual)"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"id": "20",
206+
"metadata": {},
207+
"source": [
208+
"## Plot cartesian representation"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": null,
214+
"id": "21",
215+
"metadata": {},
216+
"outputs": [],
217+
"source": [
218+
"actual.DBZH.plot(xlim=(-10e3, 10e3), ylim=(-10e3, 10e3), cmap=\"HomeyerRainbow\", vmin=0)"
219+
]
220+
}
221+
],
222+
"metadata": {
223+
"language_info": {
224+
"codemirror_mode": {
225+
"name": "ipython",
226+
"version": 3
227+
},
228+
"file_extension": ".py",
229+
"mimetype": "text/x-python",
230+
"name": "python",
231+
"nbconvert_exporter": "python",
232+
"pygments_lexer": "ipython3",
233+
"version": "3.12.8"
234+
}
235+
},
236+
"nbformat": 4,
237+
"nbformat_minor": 5
238+
}

0 commit comments

Comments
 (0)