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

Direction of integration for cumint? #609

Open
chrisb13 opened this issue May 30, 2023 · 2 comments
Open

Direction of integration for cumint? #609

chrisb13 opened this issue May 30, 2023 · 2 comments

Comments

@chrisb13
Copy link

For aesthetic reasons I'd like to plot a barotropic streamfunction using an integral starting at the top rather than the bottom. (For an idealised southern ocean channel model.) Looking at the method-object-docs, I couldn't see a way to do this with cumint..? I briefly played with reindexing via strides but that feels un-xgcm like (and I didn't see how to make it work!)

Thanks for a handy package!

@jbusecke
Copy link
Contributor

jbusecke commented Jun 5, 2023

Hi @chrisb13, thanks for raising this issue.

I think this is related to #337 and earlier #184.

I agree this is a needed feature, but currently you would have to flip your values (not the coordinates see #605) apply the cumsum and flip them back (which is not ideal).

Would you be interested in working on this feature at all?

@dhruvbalwada
Copy link
Contributor

Here is an easy and temporary solution using grid_ufunc feature:

ds_dens = xr.Dataset(
    coords={
        "zl": (["zl"], np.arange(26,28, 0.2)),
        "zi": (["zi"], np.arange(25.9, 28, 0.2))
    }
)

grid = Grid(ds_dens, 
            coords={"Z": {"center": 'zl', 'outer': 'zi'}},
           periodic=False)

ds_dens['vh'] = 0.*ds_dens.zl + 0.5


@as_grid_ufunc("(Z:center)->(Z:outer)")
def upward_sum(a):

    temp_zeros = np.zeros_like(a[...,0])[..., np.newaxis]

    temp = np.append(np.flip(np.cumsum(np.flip(a, axis=-1), axis=-1), axis=-1), temp_zeros, axis=-1)
    return temp

reg_cumsum = grid.cumsum(ds_dens.vh, 'Z', boundary='fill', fill_value=0.)
upward_cumsum = upward_sum(grid, ds_dens.vh, axis=['Z'])

Creates the following plot
Screen Shot 2024-03-27 at 4 03 05 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants