Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Singletons breaks default sort_ascending=True in swap_lon_axis() #389

Closed
tomvothecoder opened this issue Nov 11, 2022 · 0 comments · Fixed by #392
Closed

[Bug]: Singletons breaks default sort_ascending=True in swap_lon_axis() #389

tomvothecoder opened this issue Nov 11, 2022 · 0 comments · Fixed by #392
Labels
type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Comments

@tomvothecoder
Copy link
Collaborator

What happened?

Using xcdat v0.4.0, I just got an error when trying to to swap the longitude axes. I wonder if the issue is that coord_keys = get_dim_coords(ds, "X").coords.keys() on L236 of swap_lon_axis() returns not just the coordinate key for the "X" dimension but also "height" since this is a 2-meter air temperature.

Hey @mzelinka and @pochedls, this issue is related to height being a singleton coordinate. Singleton coordinate variables are included alongside other coordinate variables (refer to code example below). There is an open xarray issue regarding singletons here: pydata/xarray#6196

import xcdat as xc

fnames = "/p/css03/esgf_publish/CMIP6/CMIP/BCC/BCC-CSM2-MR/historical/r1i1p1f1/day/tasmax/gn/v20181126/*.nc"

ds = xc.open_mfdataset(fnames)

print(xc.get_dim_coords(ds, axis="X"))

# Notice how `height` is included in the `lon` coordinate var.

# <xarray.DataArray 'lon' (lon: 320)>
# array([  0.   ,   1.125,   2.25 , ..., 356.625, 357.75 , 358.875])
# Coordinates:
#   * lon      (lon) float64 0.0 1.125 2.25 3.375 4.5 ... 355.5 356.6 357.8 358.9
#     height   float64 2.0
# Attributes:
#     bounds:         lon_bnds
#     units:          degrees_east
#     axis:           X
#     long_name:      Longitude
#     standard_name:  longitude

How it affects swap_lon_axis()

In swap_lon_axis(), we are getting all of the coordinates keys using lon, which includes height.
This causes swap_lon_axis(ds, sort_ascending=True) to break on this file.

xcdat/xcdat/axis.py

Lines 235 to 236 in d5d4fd7

ds = dataset.copy()
coord_keys = get_dim_coords(ds, "X").coords.keys()

xcdat/xcdat/axis.py

Lines 259 to 260 in d5d4fd7

if sort_ascending:
ds = ds.sortby(list(coord_keys), ascending=True)

Temporary workaround:

  • Set sort_ascending=False in swap_lon_axis() and perform sorting manually

The fix for swap_lon_axis() is to ignore singleton coordinate keys.

Originally posted by @tomvothecoder in #296 (reply in thread)

What did you expect to happen?

swap_lon_axis() should disregard singletons such as height when sorting coordinates.

Minimal Complete Verifiable Example

No response

Relevant log output

No response

Anything else we need to know?

No response

Environment

xcdat=0.4.0

@tomvothecoder tomvothecoder added the type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors. label Nov 11, 2022
@tomvothecoder tomvothecoder changed the title [Bug]: Singletons break swap_lon_axis() [Bug]: Singletons breaks sort_ascend=True in swap_lon_axis() Nov 15, 2022
@tomvothecoder tomvothecoder changed the title [Bug]: Singletons breaks sort_ascend=True in swap_lon_axis() [Bug]: Singletons breaks default sort_ascending=True in swap_lon_axis() Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant