From d077bfabb8001e00db6426d2602f262d13890ee1 Mon Sep 17 00:00:00 2001 From: John Krasting Date: Mon, 24 Feb 2025 10:12:29 -0500 Subject: [PATCH] Added function to alphabetize xarray datasets 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. --- src/momlevel/util.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/momlevel/util.py b/src/momlevel/util.py index 333fd90..1b56c6a 100644 --- a/src/momlevel/util.py +++ b/src/momlevel/util.py @@ -11,6 +11,7 @@ from momlevel import trend __all__ = [ + "alphabetize_dataset", "annual_average", "annual_cycle", "default_coords", @@ -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