-
Notifications
You must be signed in to change notification settings - Fork 37
reimplementation of pole_kind option #415
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1007,3 +1007,66 @@ def test_densify_polys(): | |||||
| poly = Polygon([(-80, -40), (80, -40), (80, 40), (-80, 40)]) # Large poly | ||||||
| with pytest.warns(UserWarning): | ||||||
| xe.SpatialAverager(ds_in, [poly]) | ||||||
|
|
||||||
|
|
||||||
| def test_regrid_polekind(): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I suggest writing the test files in a temporary path (see https://docs.pytest.org/en/7.1.x/how-to/tmp_path.html). As a bonus, you don't even need to clean them up at the end. |
||||||
|
|
||||||
| import pathlib | ||||||
| import tarfile | ||||||
| import urllib.request | ||||||
|
|
||||||
| # get file from figshare with sample OM4 (tripolar) data | ||||||
| url_om4_sample = 'https://figshare.com/ndownloader/files/52228691' | ||||||
| fname = 'xesmf_testing_OM4.tar.gz' | ||||||
| urllib.request.urlretrieve(url_om4_sample, fname) | ||||||
|
|
||||||
| # extract and read data | ||||||
| tar = tarfile.open(fname, 'r:gz') | ||||||
| tar.extractall() | ||||||
| tar.close() | ||||||
|
|
||||||
| ds_in = xr.open_dataset('OM4_sample_sst.nc') | ||||||
|
|
||||||
| # Open output grid specification | ||||||
| ds_out = xe.util.grid_global(1, 1, cf=False, lon1=180) | ||||||
|
|
||||||
| # Create regridder without specifying pole kind | ||||||
| base_regrid = xe.Regridder(ds_in, ds_out, 'bilinear', ignore_degenerate=True, periodic=True) | ||||||
| base_result = base_regrid(ds_in['sst']) | ||||||
|
|
||||||
| # Add monopole grid information. 1 denotes monopole, 2 bipole | ||||||
| ds_in['pole_kind'] = np.array([1, 1]) | ||||||
| ds_out['pole_kind'] = np.array([1, 1]) | ||||||
|
|
||||||
| monopole_regrid = xe.Regridder(ds_in, ds_out, 'bilinear', ignore_degenerate=True, periodic=True) | ||||||
| monopole_result = monopole_regrid(ds_in['sst']) | ||||||
|
|
||||||
| # Check behavior unchanged | ||||||
| assert monopole_result.equals(base_result) | ||||||
|
|
||||||
| # Add bipole grid information | ||||||
| ds_in['pole_kind'] = np.array([1, 2], np.int32) | ||||||
|
|
||||||
| bipole_regrid = xe.Regridder(ds_in, ds_out, 'bilinear', ignore_degenerate=True, periodic=True) | ||||||
| bipole_result = bipole_regrid(ds_in['sst']) | ||||||
|
|
||||||
| # Confirm results have changed | ||||||
| assert not bipole_result.equals(monopole_result) | ||||||
|
|
||||||
| # Confirm results are better with bipolar option | ||||||
| # without proper pole_kinds there are discontinuities close to the northfold | ||||||
| # easily visible on the northernmost rows | ||||||
| grad_monopole = np.gradient(monopole_result.isel(y=-2)) | ||||||
| grad_bipole = np.gradient(bipole_result.isel(y=-2)) | ||||||
|
|
||||||
| rms_grad_monopole = (grad_monopole * grad_monopole).sum() | ||||||
| rms_grad_bipole = (grad_bipole * grad_bipole).sum() | ||||||
|
|
||||||
| assert rms_grad_bipole < rms_grad_monopole | ||||||
|
|
||||||
| # Clean up files | ||||||
| file_to_rem = pathlib.Path('OM4_sample_sst.nc') | ||||||
| file_to_rem.unlink() | ||||||
| file_to_rem = pathlib.Path('xesmf_testing_OM4.tar.gz') | ||||||
| file_to_rem.unlink() | ||||||
| return None | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The possibility of passing
pole_kindin the dataset should maybe be added somewhere in the documentation ? The docstring ofRegriddermaybe ?