ECOdyagnostics/_remapping.py
def remap_vertical(
    da,
    grid_fr,
    grid_to,
    axis="Z",
    scale_factor_fr=None,
    scale_factor_to=None,
    z_fr=None,
    z_to=None,
):
    """
    Interpolate the dataset on the new grid grid dept_1d, only for T point vars for the moment


    Parameters
    ----------
    scale_factor_fr: None or dataarray
        If None, will use the scale factor from grid_fr => constant scale factors in time
        If a dataarray, will use it for the integration
    """
    # 1. integrate on W points
    # 2. interpolate on the W points of the new grid
    # 3. Differenciate on the T points of the new grid

    if axis != "Z":
        raise (
            NotImplementedError(
                "Only interpolation along the 'Z' axis is implemented yet"
            )
        )

    if not isinstance(da, xr.core.dataarray.DataArray):
        raise (
            TypeError(
                "da must be a xarray.DataArray, {} is not allowed".format(type(da))
            )
        )

    if z_fr is None:
        z_fr = _compute_depth_of_shifted_array(grid_fr, da, axis, e3=scale_factor_fr)
    if z_to is None:
        z_to = _compute_depth_of_shifted_array(grid_to, da, axis, e3=scale_factor_to)

    # add dimensions if necessary, so they match
    (z_fr, z_to) = _add_dims(z_fr, z_to)

    ax = grid_fr.axes[axis]
    (position_fr, coord_nme_fr) = ax._get_axis_coord(da)  # e.g. ('center', 'z_c')
    if position_fr not in ["center", "left"]:
        raise (
            NotImplementedError(
                "Only interpolation from T, U, V, and F points is possible for now"
            )
        )

    # pos_nme_fr = xgcm_tools.mtc_nme(coord_nme_fr)  # e.g. 'z_c_pos'
    position_intermediate = ax._default_shifts[position_fr]  # e.g. 'left' W point
    coord_nme_intermediate = ax.coords[position_intermediate]  # e:g. 'z_f'
    # pos_nme_intermediate = xgcm_tools.mtc_nme(coord_nme_intermediate)  # e.f. 'z_f_pos'

    ##############################
    #   Integration
    ##############################
    # TODO use xgcm integration cumsum
    # integration => will shift the position of the coordinate          # e.g. fr 'center' to 'left'
    # e.g. 'z_c_dif' e3t scale factor
    # Should use the xgcm internal way to get the metrics
    if scale_factor_fr is None:
        scale_factor_fr = grid_fr.get_metric(array=da, axes=axis)
    if scale_factor_to is None:
        scale_factor_to = grid_to.get_metric(array=da, axes=axis)
    da_integrate = integrate(
        da=da,
        grid=grid_fr,
        scale_factor=scale_factor_fr,
        axis=axis,
        position_fr=position_fr,
        position_to=position_intermediate,
    )
    # print('*********', da_integrate.isel({'x_c':0,'y_c':0}))

    ##############################
    #   Interpolation
    ##############################
    # interpolation : no position shifting                              # e.g. stay at 'left'

    da_interpolate = interpolate(
        da_fr=da_integrate, z_fr=z_fr, z_to=z_to, coord_nme=coord_nme_intermediate,
    )
    # print('*********', da_interpolate.isel({'x_c':0,'y_c':0}))

    ##############################
    # Derivation
    ##############################
    # TODO use xgcm derivative
    # differentiation => shift the position of the coordinate
    # e.g. from 'left' back to 'center'
    # e.g. 'z_c_diff' e3t scale factor
    """
    scale_factor_nme = xgcm_tools.mtc_nme(coord_nme_fr, diff=True)
    scale_factor = grid_to._ds[scale_factor_nme]
    """
    # scale_factor = grid_to._ds[e3t_0]
    # here positions will be inverted because we go back on the initial position
    # print('------', scale_factor.isel({'x_c':0,'y_c':0}))
    da_derivative = derivative(
        da=da_interpolate,
        grid=grid_to,
        scale_factor=scale_factor_to,
        axis=axis,
        position_fr=position_intermediate,
        position_to=position_fr,
    )
    da_derivative.name = da.name
    # print('!!!!!!!!',da_derivative.isel({'x_c':0,'y_c':0}))
    return da_derivative.transpose(*da.dims, transpose_coords=False)

    Interpolate the dataset on the new grid grid dept_1d, only for T point vars for the moment


    Parameters
    ----------
    scale_factor_fr: None or dataarray
        If None, will use the scale factor from grid_fr => constant scale factors in time
        If a dataarray, will use it for the integration
    
Interpolate the dataset on the new grid grid dept_1d, only for T point vars for the moment
Parameters

None or dataarray
If None, will use the scale factor from grid_fr => constant scale factors in time
If a dataarray, will use it for the integration
Interpolate the dataset on the new grid grid dept_1d only for T point vars for the moment Parameters None or dataarray If None will use the scale factor from grid_fr = > constant scale factors in time If a dataarray will use it for the integration
def remap_vertical(
    da,
    grid_fr,
    grid_to,
    axis="Z",
    scale_factor_fr=None,
    scale_factor_to=None,
    z_fr=None,
    z_to=None,
):
    if axis != "Z":
        raise (
            NotImplementedError(
                "Only interpolation along the 'Z' axis is implemented yet"
            )
        )
    if not isinstance(da, xr.core.dataarray.DataArray):
        raise (
            TypeError(
                "da must be a xarray.DataArray, {} is not allowed".format(type(da))
            )
        )
    if z_fr is None:
        z_fr = _compute_depth_of_shifted_array(grid_fr, da, axis, e3=scale_factor_fr)
    if z_to is None:
        z_to = _compute_depth_of_shifted_array(grid_to, da, axis, e3=scale_factor_to)
    (z_fr, z_to) = _add_dims(z_fr, z_to)
    ax = grid_fr.axes[axis]
    (position_fr, coord_nme_fr) = ax._get_axis_coord(da)  
    if position_fr not in ["center", "left"]:
        raise (
            NotImplementedError(
                "Only interpolation from T, U, V, and F points is possible for now"
            )
        )
    position_intermediate = ax._default_shifts[position_fr]  
    coord_nme_intermediate = ax.coords[position_intermediate]  
    if scale_factor_fr is None:
        scale_factor_fr = grid_fr.get_metric(array=da, axes=axis)
    if scale_factor_to is None:
        scale_factor_to = grid_to.get_metric(array=da, axes=axis)
    da_integrate = integrate(
        da=da,
        grid=grid_fr,
        scale_factor=scale_factor_fr,
        axis=axis,
        position_fr=position_fr,
        position_to=position_intermediate,
    )
    da_interpolate = interpolate(
        da_fr=da_integrate, z_fr=z_fr, z_to=z_to, coord_nme=coord_nme_intermediate,
    )
    da_derivative = derivative(
        da=da_interpolate,
        grid=grid_to,
        scale_factor=scale_factor_to,
        axis=axis,
        position_fr=position_intermediate,
        position_to=position_fr,
    )
    da_derivative.name = da.name
    return da_derivative.transpose(*da.dims, transpose_coords=False)
def remap_vertical ( da , grid_fr , grid_to , axis = "Z" , scale_factor_fr = None , scale_factor_to = None , z_fr = None , z_to = None , ) : if axis != "Z" : raise ( NotImplementedError ( "Only interpolation along the 'Z' axis is implemented yet" ) ) if not isinstance ( da , xr . core . dataarray . DataArray ) : raise ( TypeError ( "da must be a xarray.DataArray, {} is not allowed" . format ( type ( da ) ) ) ) if z_fr is None : z_fr = _compute_depth_of_shifted_array ( grid_fr , da , axis , e3 = scale_factor_fr ) if z_to is None : z_to = _compute_depth_of_shifted_array ( grid_to , da , axis , e3 = scale_factor_to ) ( z_fr , z_to ) = _add_dims ( z_fr , z_to ) ax = grid_fr . axes [ axis ] ( position_fr , coord_nme_fr ) = ax . _get_axis_coord ( da ) if position_fr not in [ "center" , "left" ] : raise ( NotImplementedError ( "Only interpolation from T, U, V, and F points is possible for now" ) ) position_intermediate = ax . _default_shifts [ position_fr ] coord_nme_intermediate = ax . coords [ position_intermediate ] if scale_factor_fr is None : scale_factor_fr = grid_fr . get_metric ( array = da , axes = axis ) if scale_factor_to is None : scale_factor_to = grid_to . get_metric ( array = da , axes = axis ) da_integrate = integrate ( da = da , grid = grid_fr , scale_factor = scale_factor_fr , axis = axis , position_fr = position_fr , position_to = position_intermediate , ) da_interpolate = interpolate ( da_fr = da_integrate , z_fr = z_fr , z_to = z_to , coord_nme = coord_nme_intermediate , ) """
    scale_factor_nme = xgcm_tools.mtc_nme(coord_nme_fr, diff=True)
    scale_factor = grid_to._ds[scale_factor_nme]
    """ da_derivative = derivative ( da = da_interpolate , grid = grid_to , scale_factor = scale_factor_to , axis = axis , position_fr = position_intermediate , position_to = position_fr , ) da_derivative . name = da . name return da_derivative . transpose ( * da . dims , transpose_coords = False )
Interpolate the dataset on the new grid grid dept_1d, only for T point vars for the moment
Parameters
Interpolate the dataset on the new grid grid dept_1d only for T point vars for the moment Parameters
"""
    Interpolate the dataset on the new grid grid dept_1d, only for T point vars for the moment


    Parameters
    ----------
    scale_factor_fr: None or dataarray
        If None, will use the scale factor from grid_fr => constant scale factors in time
        If a dataarray, will use it for the integration
    """ # 1. integrate on W points # 2. interpolate on the W points of the new grid # 3. Differenciate on the T points of the new grid # add dimensions if necessary, so they match # e.g. ('center', 'z_c') # pos_nme_fr = xgcm_tools.mtc_nme(coord_nme_fr)  # e.g. 'z_c_pos' # e.g. 'left' W point # e:g. 'z_f' # pos_nme_intermediate = xgcm_tools.mtc_nme(coord_nme_intermediate)  # e.f. 'z_f_pos' ############################## #   Integration ############################## # TODO use xgcm integration cumsum # integration => will shift the position of the coordinate          # e.g. fr 'center' to 'left' # e.g. 'z_c_dif' e3t scale factor # Should use the xgcm internal way to get the metrics # print('*********', da_integrate.isel({'x_c':0,'y_c':0})) ############################## #   Interpolation ############################## # interpolation : no position shifting                              # e.g. stay at 'left' # print('*********', da_interpolate.isel({'x_c':0,'y_c':0})) ############################## # Derivation ############################## # TODO use xgcm derivative # differentiation => shift the position of the coordinate # e.g. from 'left' back to 'center' # e.g. 'z_c_diff' e3t scale factor """
    scale_factor_nme = xgcm_tools.mtc_nme(coord_nme_fr, diff=True)
    scale_factor = grid_to._ds[scale_factor_nme]
    """ # scale_factor = grid_to._ds[e3t_0] # here positions will be inverted because we go back on the initial position # print('------', scale_factor.isel({'x_c':0,'y_c':0})) # print('!!!!!!!!',da_derivative.isel({'x_c':0,'y_c':0}))
da
grid_fr
grid_to
axis
scale_factor_fr
scale_factor_to
z_fr
z_to