Skip to content

Commit

Permalink
Added function to alphabetize xarray datasets
Browse files Browse the repository at this point in the history
Variables within an xarray datasets are not guaranteed
to be in any particular dataset. This can be problematic
for datasets with many variables. This commit adds a
function to alphabetize the variable names in a dataset.
  • Loading branch information
jkrasting committed Feb 24, 2025
1 parent a525cec commit 7ad6294
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/momlevel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from momlevel import trend

__all__ = [
"alphabetize_dataset",
"annual_average",
"annual_cycle",
"default_coords",
Expand All @@ -28,6 +29,23 @@
]


def alphabetize_dataset(ds):
"""This function reorders the variables in an xarray dataset
according to alphabetical order
Parameters
----------
ds : xarray.core.dataset.Dataset
Input xarray datset
Returns
-------
xarray.core.dataset.Dataset
"""

return ds[sorted(ds.variables.keys())]


def annual_average(xobj, tcoord="time"):
"""Function to calculate annual averages
Expand Down

0 comments on commit 7ad6294

Please sign in to comment.