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

Fixes regrid2 mapping output to input ordering #653

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,30 @@ def setup(self):
}
)

# source ordering is time, height, lat, lon
@pytest.mark.parametrize(
"ordering",
[
("time", "lat", "height", "lon"),
("lat", "lon", "height", "time"),
("lat", "height", "time", "lon"),
# no change in ordering
("time", "height", "lat", "lon"),
],
)
def test_ordering(self, ordering):
ds = self.coarse_4d_ds

output_grid = grid.create_gaussian_grid(4)

ds["ts"] = ds.ts.transpose(*ordering)

regridder = regrid2.Regrid2Regridder(ds, output_grid)

output_ds = regridder.horizontal("ts", ds)

assert ds.ts.dims == output_ds.ts.dims

def test_vertical_placeholder(self):
ds = fixtures.generate_dataset(
decode_times=True, cf_compliant=False, has_bounds=True
Expand Down
7 changes: 6 additions & 1 deletion xcdat/regridder/regrid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def _regrid(
other_sizes = list(other_dims.values())

data_shape = [y_length * x_length] + other_sizes

# output data is always float32 in original code
output_data = np.zeros(data_shape, dtype=np.float32)
output_mask = np.ones(data_shape, dtype=np.float32)
Expand Down Expand Up @@ -212,7 +213,11 @@ def _regrid(

output_data = output_data.reshape(output_data_shape)

output_order = [x + 2 for x in range(input_data_var.ndim - 2)] + [0, 1]
# temp dimensional ordering
temp_dims = [y_name, x_name] + list(other_dims.keys())

# map temp ordering to input ordering
output_order = [temp_dims.index(x) for x in input_data_var.dims]

output_data = output_data.transpose(output_order)

Expand Down
Loading