PlasmaCalcs.tools.xarray_tools.xarray_indexing.xarray_argsort

PlasmaCalcs.tools.xarray_tools.xarray_indexing.xarray_argsort(array, dim, newname='{dim}_sort', *, ascending=True, nkeep=None, promote_dims_if_needed=True, squeeze=True, kind=None)
argsort for xarray.DataArray; returns indexes along dim which would sort array along dim.
Drops original coords along dim, because the output along dim is now the indexes;
result[i] corresponds to coord value array[dim][result[i]], not array[dim][i].
Good examples to consider (where result=xarray_argsort(array, dim)):
(1) sortarr = array.isel(dim=result) to get sorted array,
which will have dim as a multidimensional coordinate with same dims as argsort result,
and values telling original values of dim, sorted appropriately.
(2) sortarr = array.isel(dim=result.isel(newdim=slice(0, N)) to get sorted array,
similar to example (1), but only keeping first N lowest values
(or N highest values if ascending=False when getting argsort result).
array: xarray.DataArray
array to argsort.
dim: str
dimension along which to sort.
newname: str
result dim will be replaced by newname.format(dim=dim). Default: ‘{dim}_sort’.
ascending: bool
result sorted in ascending order if True, descending if False.
nkeep: None or int
number of sorted values to keep. E.g. nkeep=2 –> keep only the first 2 values.
equivalent to using result.isel(newdim=(0, nkeep)), where newdim=newname.format(dim=dim).
promote_dims_if_needed: bool
whether to promote non-dimension coords to dimensions.
if False, raise DimensionKeyError if any relevant coord is not already a dimension.
squeeze: bool
whether to drop redundant dims for argsort result.
This means any dims with np.all(array==array.isel(dim=0)). (This includes dims with len=1.)
kind: None or str from {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}
sorting algorithm. Passed directly to numpy.argsort; see help(numpy.argsort) for details.