(sourcegrid, destgrid, method,
                      filename=None, extra_dims=None, ignore_degenerate=None)
return regrid
Create an ESMF.Regrid object, containing regridding weights.

    Parameters
    ----------
    sourcegrid, destgrid : ESMF.Grid object
        Source and destination grids.

        Should create them by ``esmf_grid()``
        (with optionally ``add_corner()``),
        instead of ESMPy's original API.

    method : str
        Regridding method. Options are

        - 'bilinear'
        - 'conservative', **need grid corner information**
        - 'patch'
        - 'nearest_s2d'
        - 'nearest_d2s'

    filename : str, optional
        Offline weight file. **Require ESMPy 7.1.0.dev38 or newer.**
        With the weights available, we can use Scipy's sparse matrix
        mulplication to apply weights, which is faster and more Pythonic
        than ESMPy's online regridding.

    extra_dims : a list of integers, optional
        Extra dimensions (e.g. time or levels) in the data field

        This does NOT affect offline weight file, only affects online regrid.

        Extra dimensions will be stacked to the fastest-changing dimensions,
        i.e. following Fortran-like instead of C-like conventions.
        For example, if extra_dims=[Nlev, Ntime], then the data field dimension
        will be [Nlon, Nlat, Nlev, Ntime]

    ignore_degenerate : bool, optional
        If False (default), raise error if grids contain degenerated cells
        (i.e. triangles or lines, instead of quadrilaterals)

    Returns
    -------
    grid : ESMF.Grid object
Create an ESMF.Regrid object, containing regridding weights.
Create an ESMF . Regrid object containing regridding weights .
def esmf_regrid_build(sourcegrid, destgrid, method,
                      filename=None, extra_dims=None, ignore_degenerate=None):
    '''
    Create an ESMF.Regrid object, containing regridding weights.

    Parameters
    ----------
    sourcegrid, destgrid : ESMF.Grid object
        Source and destination grids.

        Should create them by ``esmf_grid()``
        (with optionally ``add_corner()``),
        instead of ESMPy's original API.

    method : str
        Regridding method. Options are

        - 'bilinear'
        - 'conservative', **need grid corner information**
        - 'patch'
        - 'nearest_s2d'
        - 'nearest_d2s'

    filename : str, optional
        Offline weight file. **Require ESMPy 7.1.0.dev38 or newer.**
        With the weights available, we can use Scipy's sparse matrix
        mulplication to apply weights, which is faster and more Pythonic
        than ESMPy's online regridding.

    extra_dims : a list of integers, optional
        Extra dimensions (e.g. time or levels) in the data field

        This does NOT affect offline weight file, only affects online regrid.

        Extra dimensions will be stacked to the fastest-changing dimensions,
        i.e. following Fortran-like instead of C-like conventions.
        For example, if extra_dims=[Nlev, Ntime], then the data field dimension
        will be [Nlon, Nlat, Nlev, Ntime]

    ignore_degenerate : bool, optional
        If False (default), raise error if grids contain degenerated cells
        (i.e. triangles or lines, instead of quadrilaterals)

    Returns
    -------
    grid : ESMF.Grid object

    '''

    # use shorter, clearer names for options in ESMF.RegridMethod
    method_dict = {'bilinear': ESMF.RegridMethod.BILINEAR,
                   'conservative': ESMF.RegridMethod.CONSERVE,
                   'patch': ESMF.RegridMethod.PATCH,
                   'nearest_s2d': ESMF.RegridMethod.NEAREST_STOD,
                   'nearest_d2s': ESMF.RegridMethod.NEAREST_DTOS
                   }
    try:
        esmf_regrid_method = method_dict[method]
    except:
        raise ValueError('method should be chosen from '
                         '{}'.format(list(method_dict.keys())))

    # conservative regridding needs cell corner information
    if method == 'conservative':
        if not sourcegrid.has_corners:
            raise ValueError('source grid has no corner information. '
                             'cannot use conservative regridding.')
        if not destgrid.has_corners:
            raise ValueError('destination grid has no corner information. '
                             'cannot use conservative regridding.')

    # ESMF.Regrid requires Field (Grid+data) as input, not just Grid.
    # Extra dimensions are specified when constructing the Field objects,
    # not when constructing the Regrid object later on.
    sourcefield = ESMF.Field(sourcegrid, ndbounds=extra_dims)
    destfield = ESMF.Field(destgrid, ndbounds=extra_dims)

    # ESMPy will throw an incomprehensive error if the weight file
    # already exists. Better to catch it here!
    if filename is not None:
        assert not os.path.exists(filename), (
            'Weight file already exists! Please remove it or use a new name.')

    # 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)

    return regrid
def esmf_regrid_build ( sourcegrid , destgrid , method , filename = None , extra_dims = None , ignore_degenerate = None ) : # use shorter, clearer names for options in ESMF.RegridMethod method_dict = { 'bilinear' : ESMF . RegridMethod . BILINEAR , 'conservative' : ESMF . RegridMethod . CONSERVE , 'patch' : ESMF . RegridMethod . PATCH , 'nearest_s2d' : ESMF . RegridMethod . NEAREST_STOD , 'nearest_d2s' : ESMF . RegridMethod . NEAREST_DTOS } try : esmf_regrid_method = method_dict [ method ] except : raise ValueError ( 'method should be chosen from ' '{}' . format ( list ( method_dict . keys ( ) ) ) ) # conservative regridding needs cell corner information if method == 'conservative' : if not sourcegrid . has_corners : raise ValueError ( 'source grid has no corner information. ' 'cannot use conservative regridding.' ) if not destgrid . has_corners : raise ValueError ( 'destination grid has no corner information. ' 'cannot use conservative regridding.' ) # ESMF.Regrid requires Field (Grid+data) as input, not just Grid. # Extra dimensions are specified when constructing the Field objects, # not when constructing the Regrid object later on. sourcefield = ESMF . Field ( sourcegrid , ndbounds = extra_dims ) destfield = ESMF . Field ( destgrid , ndbounds = extra_dims ) # ESMPy will throw an incomprehensive error if the weight file # already exists. Better to catch it here! if filename is not None : assert not os . path . exists ( filename ) , ( 'Weight file already exists! Please remove it or use a new name.' ) # 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 ) return regrid