Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aulemahal committed Feb 25, 2021
1 parent 9177e30 commit 3c2fbc1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
10 changes: 5 additions & 5 deletions xesmf/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def _get_lon_lat_bounds(ds):
raise KeyError('lon_b')
lon_name = ds.cf['longitude'].name
lat_name = ds.cf['latitude'].name
ds2 = ds.cf.add_bounds([lon_name, lat_name])
lon_bnds = ds2.cf.get_bounds('longitude')
lat_bnds = ds2.cf.get_bounds('latitude')
ds = ds.cf.add_bounds([lon_name, lat_name])
lon_bnds = ds.cf.get_bounds('longitude')
lat_bnds = ds.cf.get_bounds('latitude')

# Convert from CF bounds to xESMF bounds.
# order=None is because we don't want to assume the dimension order for 2D bounds.
lon_b = cfxr.bounds_to_vertices(lon_bnds, 'bounds', order=None)
lat_b = cfxr.bounds_to_vertices(lat_bnds, 'bounds', order=None)
lon_b = cfxr.bounds_to_vertices(lon_bnds, ds.cf.get_bounds_dim_name('longitude'), order=None)
lat_b = cfxr.bounds_to_vertices(lat_bnds, ds.cf.get_bounds_dim_name('latitude'), order=None)
return lon_b, lat_b


Expand Down
19 changes: 18 additions & 1 deletion xesmf/tests/test_frontend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import warnings

import cf_xarray # noqa
import cf_xarray as cfxr
import dask
import numpy as np
import pytest
Expand All @@ -17,6 +17,8 @@

# same test data as test_backend.py, but here we can use xarray DataSet
ds_in = xe.util.grid_global(20, 12)
ds_in.lat.attrs['standard_name'] = 'latitude'
ds_in.lon.attrs['standard_name'] = 'longitude'
ds_out = xe.util.grid_global(15, 9)

horiz_shape_in = ds_in['lon'].shape
Expand Down Expand Up @@ -259,6 +261,21 @@ def test_regrid_with_1d_grid_infer_bounds():
assert_allclose(dr_out, dr_exp)


def test_regrid_cfbounds():
# Test regridding when bounds are given in cf format with a custom "bounds" name.
ds = ds_in.copy().drop_vars(['lat_b', 'lon_b'])
ds['lon_bounds'] = cfxr.vertices_to_bounds(ds_in.lon_b, ('bnds', 'y', 'x'))
ds['lat_bounds'] = cfxr.vertices_to_bounds(ds_in.lat_b, ('bnds', 'y', 'x'))
ds.lat.attrs['bounds'] = 'lat_bounds'
ds.lon.attrs['bounds'] = 'lon_bounds'

regridder = xe.Regridder(ds, ds_out, 'conservative', periodic=True)
dr_out = regridder(ds['data'])
# compare with provided-bounds solution
dr_exp = xe.Regridder(ds_in, ds_out, 'conservative', periodic=True)(ds_in['data'])
assert_allclose(dr_out, dr_exp)


# TODO: consolidate (regrid method, input data types) combination
# using pytest fixtures and parameterization

Expand Down

0 comments on commit 3c2fbc1

Please sign in to comment.