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

Dimension order shouldn't matter for periodicity #363

Open
mberdahl-uw opened this issue May 15, 2024 · 2 comments
Open

Dimension order shouldn't matter for periodicity #363

mberdahl-uw opened this issue May 15, 2024 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@mberdahl-uw
Copy link

mberdahl-uw commented May 15, 2024

Hi,

I'm trying to regrid some cmip6 models with xesmf regridder.
The operation works, but upon plotting some (not all) of the regridded models have a seam in the output at the 0 dateline.
I am using periodic=True.
Appreciate any help!

Here is a small bit of code that illustrates the issue with one model:
Data.zip

import xarray as x
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import numpy as np
import xesmf as xe

original_dataset = xr.open_dataset("ds_input.nc")
ds_target = xr.open_dataset("ds_target.zarr")

# Define the names of the datasets
dataset_names = ["original_dataset"]

# Create an empty dictionary to store the regridded datasets
regridded_datasets = {}

# Loop through each dataset name
for dataset_name in dataset_names:
    
    # Define the original dataset
    original_dataset = globals()[dataset_name]

    # Define the regridder object
    regridder = xe.Regridder(
        original_dataset, ds_target, "bilinear", periodic=True, ignore_degenerate=True,
    )  # this takes some time to calculate a weight matrix for the regridding

    # Apply regridding and store the regridded dataset in the dictionary
    regridded_datasets[dataset_name] = regridder(original_dataset.squeeze())  # this is a bit of a lazy operation

original_regridded = regridded_datasets["original_dataset"]


# for testing, just compare the original with the regridded dataset to see about the seam


fig, (axs) = plt.subplots(
    # ncols=2, nrows=11, figsize=[10, 35], subplot_kw={"projection": ccrs.Robinson()}
    ncols=2, nrows=1, figsize=[10, 3], subplot_kw={"projection": ccrs.PlateCarree()}

)

vupper = 293.15
vlower = 253.15


# plot the model data
original_dataset.tas.plot(
    ax=axs[0],
    x="x",
    y="y",
    transform=ccrs.PlateCarree(),
    cmap="RdBu_r",
    vmax = vupper,
    vmin = vlower,
    robust=True,
)

original_regridded.tas.plot(
    ax=axs[1],
    x="x",
    y="y",
    transform=ccrs.PlateCarree(),
    cmap="RdBu_r",
    vmax = vupper,
    vmin = vlower,
    robust=True,
)

axs[0].coastlines()
axs[0].set_title("Historical ACCESS_CM2")

axs[1].coastlines()
axs[1].set_title("Historical ACCESS_CM2 - regridded")

Screenshot 2024-05-15 at 10 55 46 AM (2)

image

@aulemahal
Copy link
Collaborator

Hi @mberdahl-uw, thanks for the very complete issue.

The bug is quite subtle and needs some fix as fast as possible... It has to do with the dimension order of your data. Your ds_input has a lon coordinate of dimensions x, y, while ESMF expects y, x. xESMF should have taken care of this, dimension order is not supposed to be a problem in the xarray-world. But it has failed to do so and for some reason this has made the periodic feature fail, while the rest of the regridding seems to work anyway.

On my side, I was able to produce the correct figures by adding original_dataset = original_dataset.transpose('y', 'x') before initialising the Regridder.

Please confirm if this fixes your problem, we'll work on a way to fix this.

@mberdahl-uw
Copy link
Author

Hi @aulemahal , thanks so much for looking into this.
Your fix works for me too, so that is great. I'll look out for a fix on your end when you get around to it. In the meantime I'll do this piecemeal approach for the models that require this transpose.

@aulemahal aulemahal changed the title Possible bug with regridder on some CMIP6 models - seam persists at 0deg Longitude Dimension order shouldn't matter for periodicity May 17, 2024
@aulemahal aulemahal self-assigned this May 17, 2024
@aulemahal aulemahal added the bug Something isn't working label May 17, 2024
@aulemahal aulemahal added this to the v0.9 milestone May 17, 2024
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

No branches or pull requests

2 participants