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

A Documented Example on how to use xgcm with cosima output is wanted #164

Open
navidcy opened this issue Nov 3, 2022 · 13 comments · May be fixed by #220
Open

A Documented Example on how to use xgcm with cosima output is wanted #164

navidcy opened this issue Nov 3, 2022 · 13 comments · May be fixed by #220

Comments

@navidcy
Copy link
Collaborator

navidcy commented Nov 3, 2022

That's coming out from the discussion in Cosima workshop 2022.

cc @janjaapmeijer

@navidcy navidcy added the 🧜🏽‍♀️ enhancement New feature or request label Nov 3, 2022
@rbeucher
Copy link

@navidcy navidcy added the 💻 hackathon 2.0 like the 1.0 but better label Nov 24, 2022
@adele-morrison
Copy link
Collaborator

Agreed this would be good.

We currently have:

  • a Regridding notebook using xesmf to do horizontal regridding. Though I've failed to get this to work when I've tried previously. Do we want both xesmf and xgcm horizontal regridding methods? Possibly we could also combine these with the Model Resolution Comparison notebook (i.e. interpolate the different model resolution grids onto each other so bias maps could be produced)? We could rename this notebook Horizontal Regridding perhaps, given that vertical regridding is dealt with in a separate notebook?
  • a Binning Transformation from Depth to Potential Density notebook that (amongst other methods) uses xgcm to do vertical conservative regridding onto isopycnal layers. I think this is sufficient - no more needed here?

@aekiss
Copy link
Contributor

aekiss commented Jan 23, 2023

@adele157 re. your first point, I thought xesmf and xgcm are designed for different purposes: xesmf for fast regridding between different grids, and xgcm for Arakawa-grid-aware calculations (div, grad, curl, etc) on a given grid, and also vertical coordinate transformation. As far as I can see xgcm doesn't do horizontal regridding (happy to be corrected, but even if it can I expect xesmf would be much faster at high resolution).

@navidcy
Copy link
Collaborator Author

navidcy commented Jan 23, 2023

xesmf actually regrids the variables and interpolates to some other grid of your liking

xgcm simply takes into account that some of your variables live on a slightly different locations on the same grid than others, e.g. u lives elsewhere from T, and it gives you the opportunity to compute quantities like ∂x( u T ) without you having to think which variable you need to interpolate first onto which grid before you take the product and how do you take the derivative and what not.

That is,

  • xgcm does interpolate but only within cells to bring the variable from the u-grid to the t-grid and things like that....
  • xesmf can be used to interpolate a 1/10 tripolar grid onto a 1/2 degree lat-log grid!

Is this clarifying enough?

@aekiss
Copy link
Contributor

aekiss commented Jan 23, 2023

Yep, thanks, that's what I thought! So we need both xesmf and xgcm.

@navidcy
Copy link
Collaborator Author

navidcy commented Jan 23, 2023

I made a regridding one with xesmf at some point: let's revisit that --> https://cosima-recipes.readthedocs.io/en/latest/documented_examples/Regridding.html#gallery-documented-examples-regridding-ipynb

@aekiss
Copy link
Contributor

aekiss commented Jan 23, 2023

Thanks, I've used xesmf a lot, but not xgcm. I'd just been confused by Adele's suggestion of xgcm for horizontal regridding, but it turns out that's not a thing.

@adele-morrison
Copy link
Collaborator

adele-morrison commented Jan 23, 2023 via email

@Thomas-Moore-Creative
Copy link
Collaborator

[ waves ] Hi folks. I'm a 100% COSIMA Cookbook noob - with all of 1 hour under my belt.

I'm wondering if there is a CC "key" to pull in all the grid_spec details for a particular model configuration?

@navidcy
Copy link
Collaborator Author

navidcy commented Jan 23, 2023

like the grid specs that xgcm requires? no there isn't... it could be something that we add after tomorrow.

but there are few examples in the recipes that use xgcm; see, e.g., the relative vorticity example
or the vertical coords transformation example.

@aekiss
Copy link
Contributor

aekiss commented Jan 23, 2023

There have been some proposals to store grid info in the cookbook database, but none implemented as yet - e.g. see COSIMA/cosima-cookbook#191 and #190

@navidcy
Copy link
Collaborator Author

navidcy commented Jan 23, 2023

xgcm now works with grid metrics (I tried a million times to make it work back in the day with no success)

folder = '/g/data/hh5/tmp/cosima/access-om2-025/025deg_jra55v13_iaf_gmredi6/output000/ocean/'
grid = xr.open_mfdataset(folder + 'ocean_grid.nc', combine='by_coords')

ds = xr.merge([u, v, grid])
ds.coords['xt_ocean'].attrs.update(axis='X')
ds.coords['xu_ocean'].attrs.update(axis='X', c_grid_axis_shift=0.5)
ds.coords['yt_ocean'].attrs.update(axis='Y')
ds.coords['yu_ocean'].attrs.update(axis='Y', c_grid_axis_shift=0.5)

metrics = {
    ('X',): ['dxt', 'dxu'], # X distances
    ('Y',): ['dyt', 'dyu'], # Y distances
    ('X', 'Y'): ['area_t', 'area_u'] # Areas
}

grid = xgcm.Grid(ds, periodic=['X'], metrics=metrics)

ζ_xgcm = grid.interp(grid.derivative(v, 'X'), 'Y', boundary='extend') - grid.interp(grid.derivative(u, 'Y'), 'X')
ζ_xgcm

Screenshot 2023-01-23 at 5 36 17 pm

Screenshot 2023-01-23 at 5 37 39 pm

We need to update the relative vorticity example. Another job for the Hackathon v2.0.

@navidcy
Copy link
Collaborator Author

navidcy commented Jan 23, 2023

#151 is definitely related here (if we'll update the relative vorticity example)

@navidcy navidcy linked a pull request Jan 24, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment