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

Add lonFlip function #594

Open
brianpm opened this issue Apr 13, 2024 · 2 comments
Open

Add lonFlip function #594

brianpm opened this issue Apr 13, 2024 · 2 comments
Labels
feature a new feature that is going to be developed support Support request opened by outside user/collaborator

Comments

@brianpm
Copy link

brianpm commented Apr 13, 2024

A very common operation performed with global data is to convert the longitude convention from/to [0,360] to/from [-180, 180]. This was available in NCL as lonFlip.

There are probably many ways this could be implemented. I've written this function a bunch of times. My current version assumes global data and that the data is an xarray object, and it at least seems to work:

def lonFlip(data, lonname=None):
    # NOTE: this assumes global values
    if lonname is None:
        lonname = 'lon'
    tmplon = data[lonname]
    tmpdata = data.roll( {lonname: (len(tmplon) // 2)}, roll_coords=True)
    lonroll = tmpdata[lonname].values
    if tmplon.min() >= 0:
        # flip to -180:180
        tmpdata[lonname] = xr.DataArray(np.where(lonroll >= 180, lonroll - 360, lonroll), dims=[lonname], attrs=data[lonname].attrs)
    else:
        # flip from -180:180 to 0:360
        tmpdata[lonname] = xr.DataArray(((lonroll + 360) % 360), dims=[lonname], attrs=data[lonname].attrs)
    return tmpdata
@anissa111
Copy link
Member

Hi @brianpm! 👋

Thank you for the functionality request! We'll provide updates here of our review and implementation progress.

@anissa111 anissa111 added support Support request opened by outside user/collaborator feature a new feature that is going to be developed labels Apr 16, 2024
@kafitzgerald
Copy link
Contributor

Thanks, Brian!

Jotting down a couple of related dev notes:

This issue (pydata/xarray#7031) regarding a periodic boundary index and a related conversation on the Pangeo Discourse might be worth a look. It's a different solution and relies on some work currently under development, but may be worth keeping an eye on.

I think something using roll and updating coordinates could make sense though and while there may be overlap with the periodic boundary index option there are probably cases where one or the other might make more sense.

From a design perspective, we might also consider something a little more flexible like lonPivot which covers the lonFlip case as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a new feature that is going to be developed support Support request opened by outside user/collaborator
Projects
None yet
Development

No branches or pull requests

3 participants