Update dimension ordering to match NetCDF-CF conventions
Discussed in https://github.com/google/Xee/discussions/196
Originally posted by tylere December 13, 2024 Most Python libraries that plot raster images use a (y, x) or (lat, lon) ordering of dimensions. This is the convention used by Matplotlib's pyplot with images, and many libraries that utilize it (Xarray, rasterio).
(lat, lon) ordering is also used for higher dimensional data that follows the netCDF-CF conventions, which are a standard for climate forecast data. Specifically:
https://ferret.pmel.noaa.gov/Ferret/documentation/coards-netcdf-conventions Order of dimensions: If any or all of the dimensions of a variable have the interpretations of "date or time" (a.k.a. "T"), "height or depth"(a.k.a. "Z"), "latitude" (a.k.a. "Y"), or "longitude" (a.k.a. "X") then those dimensions should appear in the relative order T, then Z, then Y, then X in the CDL definition corresponding to the file.
By contrast Xee currently creates an Xarray dataset with dimensions in the order (time, lon, lat) in which the lon and lat coordinates are swapped. This requires the user to process (transpose) the data before using Matplotlib or similar plotting libraries.
This convention difference may have contributed to several the the Xee issues that have been raised (#45, #171, #174).
I think it may be worth changing Xee's coordinate ordering to (time, lat, lon) in order to match the convention used by netCDF-CF and Matplotlib. I realize this will be a breaking change for some users, but it will make the library easier to use for future users.
Thoughts?
Additionally,
From @tylere...
The coordinate ordering logic appears in several places in Xee:
Line 245 in 5ce8db6
x_dim_name, y_dim_name = self.dimension_names
Line 390 in 5ce8db6
x_dim_name, y_dim_name = self.dimension_names
Line 570 in 5ce8db6
dimensions = [self.primary_dim_name, x_dim_name, y_dim_name]
Line 717 in 5ce8db6
x_dim_name, y_dim_name = self.dimension_names
Line 724 in 5ce8db6
(x_dim_name, xarray.Variable(x_dim_name, width_coord)),
...and https://github.com/google/Xee/issues/230 also requests the change.