COSIMA / cosima-recipes

A cookbook of recipes (i.e., examples) for analysing ocean and sea ice model output
https://cosima-recipes.readthedocs.io
Apache License 2.0
44 stars 65 forks source link

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

Open navidcy opened 1 year ago

navidcy commented 1 year ago

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

cc @janjaapmeijer

rbeucher commented 1 year ago

Maybe relevant?

https://earth-env-data-science.github.io/lectures/working_with_gcm_data.html

adele-morrison commented 1 year ago

Agreed this would be good.

We currently have:

aekiss commented 1 year ago

@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 commented 1 year ago

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,

Is this clarifying enough?

aekiss commented 1 year ago

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

navidcy commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Sorry for confusion and thanks for clarification. I’ve only used xgcm before in the vertical (where it does do regridding) not the horizontal.

On Mon, Jan 23, 2023 at 12:18 PM, Andrew Kiss @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/COSIMA/cosima-recipes/issues/164#issuecomment-1399673854, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACA44UYWPHZJKEMZYBYMSZ3WTXL7TANCNFSM6AAAAAARVWZUVA . You are receiving this because you were mentioned.Message ID: @.***>

Thomas-Moore-Creative commented 1 year ago

[ 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 commented 1 year ago

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 commented 1 year ago

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

navidcy commented 1 year ago

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 commented 1 year ago

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