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 utility for modifying pop model output to be compatible with xgcm #21

Merged
merged 19 commits into from
Dec 10, 2019
Merged

Add utility for modifying pop model output to be compatible with xgcm #21

merged 19 commits into from
Dec 10, 2019

Conversation

andersy005
Copy link
Contributor

@andersy005 andersy005 commented Sep 25, 2019

Closes #17, and towards #16

@andersy005 andersy005 marked this pull request as ready for review September 25, 2019 22:52
@andersy005
Copy link
Contributor Author

@matt-long, this is ready for another look. If you deem it complete, we can go ahead and merge it. I will add tests for it in a separate PR when test data is available on the FTP server.

y_loc_key = int(grid_loc[2])
z_loc_key = int(grid_loc[3])

x_loc = {1: 'nlon_t', 2: 'nlon_u'}[x_loc_key]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need

x_loc = {1: "nlon_t", 2: "nlon_u", 3: "nlon_u"}[x_loc_key]
y_loc = {1: "nlat_t", 2: "nlat_u", 3: "nlat_t"}[y_loc_key]

to make DZU and DZT work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Thank you, @dcherian

Can you point me to a small test dataset that I can use for testing purposes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ask Anna on that email thread. I just left work

@klindsay28
Copy link
Collaborator

When partial_bottom_cells=.true. in POP, the bottom layer doesn't always have the nominal thickness. We probably want this utility to handle that usecase, assuming that xgcm can handle it (which I think is a reasonable assumption). I think this can be detected in model output by comparing HT to something like z_w[KMT] (I might have an off-by-1 error here). If they differ in some places, then partial_bottom_cells=.true..

Copy link
Collaborator

@matt-long matt-long left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests?

What is the primary user-facing API that has been enabled?

pop_tools/modify_pop_for_xgcm.py Outdated Show resolved Hide resolved
@andersy005
Copy link
Contributor Author

Do we have tests?

Not yet. Can someone point me to some sample data to use?

What is the primary user-facing API that has been enabled?

To use this functionality the user would need to do the following:

import xarray as xr
from pop_tools.xgcm_util import relabel_pop_dims
ds = xr.open_mfdataset('pop_netcdf_files.*.nc')
ds = relabel_pop_dims(ds)

# now you can use xgcm
import xgcm
grid = xgcm.Grid(ds)

What additional user-facing functionality should we support?

@matt-long
Copy link
Collaborator

Let's wrap that in a method poptools.get_xgcm_grid(ds).

@matt-long
Copy link
Collaborator

I think tests/data/cesm_pop_monthly.T62_g17.nc
should be appropriate for tests. If it's missing key fields, we could augment.

@andersy005

This comment has been minimized.

@bradyrx
Copy link
Contributor

bradyrx commented Oct 16, 2019

Here's some examples of computations to try out in the testing: https://xgcm.readthedocs.io/en/latest/grid_metrics.html

@andersy005
Copy link
Contributor Author

This seems to be working:

>>> from pop_tools import get_xgcm_grid, DATASETS
>>> import xarray as xr
>>> fname = DATASETS.fetch("iron_tracer.nc")
>>> ds = xr.open_dataset(fname)
>>> ds
<xarray.Dataset>
Dimensions:    (nlat: 384, nlon: 320, time: 24, z_t: 60, z_w_bot: 60, z_w_top: 60)
Coordinates:
    * time       (time) object 0249-02-01 00:00:00 ... 0251-01-01 00:00:00
    * z_t        (z_t) float32 500.0 1500.0 2500.0 ... 487508.34 512502.8 537500.0
    TLAT       (nlat, nlon) float64 ...
    ULONG      (nlat, nlon) float64 ...
    TLONG      (nlat, nlon) float64 ...
    ULAT       (nlat, nlon) float64 ...
    * z_w_top    (z_w_top) float32 0.0 1000.0 2000.0 ... 500004.7 525000.94
    * z_w_bot    (z_w_bot) float32 1000.0 2000.0 3000.0 ... 525000.94 549999.06
Dimensions without coordinates: nlat, nlon
Data variables:
     UE         (time, z_t, nlat, nlon) float32 ...
     VN         (time, z_t, nlat, nlon) float32 ...
     WT         (time, z_w_top, nlat, nlon) float32 ...
     HDIFE      (time, z_t, nlat, nlon) float32 ...
     HDIFN      (time, z_t, nlat, nlon) float32 ...
     HDIFB      (time, z_w_bot, nlat, nlon) float32 ...
     DIA_IMPVF  (time, z_w_bot, nlat, nlon) float32 ...
     KPP_SRC    (time, z_t, nlat, nlon) float32 ...
     STF        (time, nlat, nlon) float32 ...
     SMS        (time, nlat, nlon) float32 ...
>>> grid = get_xgcm_grid(ds)
>>> grid
<xgcm.Grid>
Z Axis (periodic):
        * center   z_t --> left
        * right    z_w_bot --> center
        * left     z_w_top --> center
Y Axis (periodic):
        * center   nlat_t --> right
        * right    nlat_u --> center
X Axis (periodic):
        * center   nlon_t --> right
        * right    nlon_u --> center

I still haven't addressed @klindsay28's comment though.

When partial_bottom_cells=.true. in POP, the bottom layer doesn't always have the nominal thickness. We probably want this utility to handle that usecase, assuming that xgcm can handle it (which I think is a reasonable assumption). I think this can be detected in model output by comparing HT to something like z_w[KMT] (I might have an off-by-1 error here). If they differ in some places, then partial_bottom_cells=.true..

Since I am not that familiar with POP model output, it would be nice for someone to take a look at the current code in this PR, and let me know what changes need to be made to address this.

@andersy005
Copy link
Contributor Author

@dcherian, @matt-long, the gentlest of bumps on this. Have two minute to take a look at this? 😄

Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know anything about bottom cells.

"""
import xgcm

ds = relabel_pop_dims(ds)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fairly certain this doesn't work. ds isn't returned but a copy is made deep down in the call stack and all the xgcm stuff nrequires the renamed dimensions.

How about

grid, new_ds = pop_tools.to_xgcm_grid_dataset(ds)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

grid, new_ds = pop_tools.to_xgcm_grid_dataset(ds)

👍 Good catch. I've addressed this and renamed the utility function

tests/test_xgcm_util.py Show resolved Hide resolved


def _label_coord_grid_locs(ds):
grid_locs = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a dataset that tests all of these. I've emailed Anna on our previous email thread. She was looking at one that had DZU & DZT

Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I left one minor comment but LGTM otherwise.

tests/test_xgcm_util.py Show resolved Hide resolved
@andersy005
Copy link
Contributor Author

@dcherian, Thank you for reviewing this! I think this is ready to go.

@bradyrx
Copy link
Contributor

bradyrx commented Dec 9, 2019

This looks great @andersy005!

@andersy005 andersy005 merged commit 2cc1b90 into NCAR:master Dec 10, 2019
@andersy005 andersy005 deleted the pop2xgcm_grid branch December 10, 2019 17:52
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

Successfully merging this pull request may close these issues.

Add pop2xgcm_grid method
5 participants