Skip to content

Commit

Permalink
Add normalization option for conservative regridding
Browse files Browse the repository at this point in the history
See JiaweiZhuang#17.
This together with masking can address the normalization issue
in JiaweiZhuang#23
  • Loading branch information
JiaweiZhuang authored and Raphael Dussin committed Jul 24, 2020
1 parent 8db5d96 commit 667cc68
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions xesmf/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def esmf_regrid_build(sourcegrid, destgrid, method,
- 'bilinear'
- 'conservative', **need grid corner information**
- 'conservative_normed', **need grid corner information**
- 'patch'
- 'nearest_s2d'
- 'nearest_d2s'
Expand Down Expand Up @@ -226,6 +227,7 @@ def esmf_regrid_build(sourcegrid, destgrid, method,
# use shorter, clearer names for options in ESMF.RegridMethod
method_dict = {'bilinear': ESMF.RegridMethod.BILINEAR,
'conservative': ESMF.RegridMethod.CONSERVE,
'conservative_normed': ESMF.RegridMethod.CONSERVE,
'patch': ESMF.RegridMethod.PATCH,
'nearest_s2d': ESMF.RegridMethod.NEAREST_STOD,
'nearest_d2s': ESMF.RegridMethod.NEAREST_DTOS
Expand Down Expand Up @@ -257,13 +259,21 @@ def esmf_regrid_build(sourcegrid, destgrid, method,
assert not os.path.exists(filename), (
'Weight file already exists! Please remove it or use a new name.')

# re-normalize conservative regridding results
# https://github.com/JiaweiZhuang/xESMF/issues/17
if method == 'conservative_normed':
norm_type = ESMF.NormType.FRACAREA
else:
norm_type = ESMF.NormType.DSTAREA

# Calculate regridding weights.
# Must set unmapped_action to IGNORE, otherwise the function will fail,
# if the destination grid is larger than the source grid.
regrid = ESMF.Regrid(sourcefield, destfield, filename=filename,
regrid_method=esmf_regrid_method,
unmapped_action=ESMF.UnmappedAction.IGNORE,
ignore_degenerate=ignore_degenerate,
norm_type=norm_type,
src_mask_values=[0], dst_mask_values=[0])

return regrid
Expand Down
1 change: 1 addition & 0 deletions xesmf/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(self, ds_in, ds_out, method, periodic=False,
- 'bilinear'
- 'conservative', **need grid corner information**
- 'conservative_normed', **need grid corner information**
- 'patch'
- 'nearest_s2d'
- 'nearest_d2s'
Expand Down

0 comments on commit 667cc68

Please sign in to comment.