SCRIPTS XARRAY IndexingΒΆ

"""
This script builds on the Dataset and DataArray tutorials, showing how these
objects can be indexed, sliced, etc.

See official documentation @ http://xarray.pydata.org/en/stable/user-guide/indexing.html

"""

# get evertyhing from the previous tutorial
Dataset_Tutorial = root['SCRIPTS']['XARRAY']['Dataset'].importCode(verbose=False, do_save=False)
locals().update(vars(Dataset_Tutorial))

# indexing can be done numpy style
print('>>>>>> numpy style <<<<<<<<')
print(ne[0:10:2])

# indexing can be done using selecting specific dim values
print('>>>>>> sel <<<<<<<<')
print(ne.sel(rho=rho[0:10:2]))
print('>>>>>> sel nearest <<<<<<<<')
print(ne.sel(rho=0.511, method='nearest'))

# indexing can be done using selecting dim indexes
print('>>>>>> isel <<<<<<<<')
print(ne.isel(rho=arange(0, 10, 2)))
# this is helpful when things have multiple dimensions
# and you don't want to bother figuring out which order they are in


fig, axs = plt.subplots(2, figsize=(8, 9))
ti0.plot(ax=axs[0], ls='-')
ti0_1d = ti.sel(rho=0, method='nearest', drop=True)
ti0_1d.plot(ax=axs[1], ls='-')
fig.tight_layout()

# combine these indexing tools with the usual tricks using where, isin, etc.
# for powerful advanced indexing (see xarray.pydata.org for more)