Skip to content

intial fix for geos dimension bug - #1861

Merged
dylannelson merged 8 commits into
mainfrom
GEOS-IO
Nov 20, 2026
Merged

dylannelson merged 8 commits into
mainfrom
GEOS-IO

Conversation

@dylannelson

@dylannelson dylannelson commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Closes #1835

Overview

UxDataArray.from_xarray passed the DataArray to _map_dims_to_ugrid, whose GEOS-CS branch loops over ds.data_vars. Every GEOS-CS face or node field raised AttributeError: 'DataArray' object has no attribute 'data_vars'.

The GEOS branch now uses a helper, _stack_geos_cs, that flattens one DataArray from (nf, Ydim, Xdim) to n_face and from (nf, YCdim, XCdim) to n_node, without creating a MultiIndex. A Dataset runs the existing loop on a shallow copy.

Also fixes the UxDataArray.from_xarray docstring, which said it returns a UxDataset and an instance where the ` wasn't used correctly
image

Ensures these are guaranteed within the GEOS reader:

  • face and node data map onto the grid in the right order
  • any dimension order works
  • name, attributes and other coordinates are kept
  • lazy data stays lazy

Expected Usage

import uxarray as ux
import xarray as xr

path = "test/meshfiles/geos-cs/c12/test-c12.native.nc4"
grid = ux.open_grid(path)
ds = xr.open_dataset(path)

uxda = ux.UxDataArray.from_xarray(ds["T"], uxgrid=grid)  # raised AttributeError before
print(uxda.dims)  # ('time', 'lev', 'n_face')

The problem occurs during .from_xarray hence why the data was read in with xarray first

PR Checklist

General

  • An issue is created and linked
  • Added appropriate labels (if your uxarray repo permissions allow it)
  • Filled out Overview and Expected Usage (if applicable) sections

Testing & Benchmarking

  • There is adequate test coverage of changes from this PR (add new tests if needed)
  • [N/A] If this PR could affect performance, ran ASV benchmarks and confirmed they show expected behavior (add a new benchmark if necessary)

Documentation and Examples

  • Docstrings updated with any function changes, and included in all new functions
  • [N/A] User (public) functions added to docs/api.rst; internal (private) function names start with an underscore (_)
  • [N/A] If touched any notebook files, cleared the output of all cells before committing
  • [N/A] If added new notebook files, put into appropriate directories and referenced in appropriate files

AI Disclosure

AI Usage:
Claude Code

  • I have tested and take responsibility for all AI-generated content in my PR.

@cmdupuis3 cmdupuis3 changed the title intial fix for geos dimension big intial fix for geos dimension bug Oct 28, 2026
@dylannelson dylannelson added the bug Something isn't working label Nov 18, 2026
@dylannelson
dylannelson marked this pull request as ready for review November 18, 2026 17:54

@Sevans711 Sevans711 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, I have a few thoughts but they are all nonblocking, it would be okay to merge as-is.

  1. This PR changes the UxDataset.from_xarray() behavior using the MCVE from the bug report; before this PR the result contained coordinates for "n_face", but now it does not. I think dropping the coordinates for "n_face" in this case is fine, but please confirm whether this change is intentional (either via comment in thread or by updating PR overview).
  2. see inline suggestions

Comment thread uxarray/core/dataarray.py Resolved
Comment thread test/io/test_geos.py Resolved

@cmdupuis3 cmdupuis3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor thing you could add:. from_xarray(da.isel(nf=[0, 1]), uxgrid=g) produces 288 faces and is silently paired with an 864-face grid. Main's Dataset path already has this problem; the branch makes it reachable from a DataArray too. A cheap fix: compare the stacked length against uxgrid.n_face / uxgrid.n_node and raise DimensionError.

        if isinstance(ds, xr.DataArray):
            ds = _stack_geos_cs(ds)
        else:
            # shallow copy, so the caller's Dataset keeps its original variables
            ds = ds.copy()
            for var_name in list(ds.coords) + list(ds.data_vars):
                ds[var_name] = _stack_geos_cs(ds[var_name])

        for dim, expected in (("n_face", grid.n_face), ("n_node", grid.n_node)):
            if dim in ds.dims and ds.sizes[dim] != expected:
                raise DimensionError(
                    f"GEOS-CS data flattens to {ds.sizes[dim]} elements along '{dim}', "
                    f"but the Grid has {expected}. Keep all six tiles ('nf') and the "
                    "full horizontal extent when converting."
                )

@dylannelson

Copy link
Copy Markdown
Member Author

@Sevans711
Yeah this is intended. It seems to only survive for input like the MCVE, which has no nf/Ydim/Xdim coordinates. For real GEOS-CS files, xarray was already dropping it (with a Deleting a single level of a MultiIndex” FutureWarning), so ux.open_dataset doesn't return it. Both the PR and the example should now match open_dataset.

@dylannelson
dylannelson merged commit 10522a2 into main Nov 20, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: UxDataArray.from_xarray crashes for GEOS-CS face and node fields

3 participants