ECOdyagnostics/_remapping.py
def interpolate(da_fr, z_fr, z_to, coord_nme):
    """
    Compute the linear interpolation of *da* on the new vertical coordinates.

    Parameters
    ---------
    da_fr : xarray.DataArray
        The data to interpolate
    coords_fr / coords_to : empty dataset with all coordinates
        Contain the physical position of the old / new coordinates
    axis_nme_fr : str
        The coordinate base for the interpolation (axisension of *da*). A dimension
        coord_fr+"_pos" must be provided in *coords_fr*. This coordinate contains the physical position
        of the coordinate (e.g. coord_fr can contain integers, representing the number
        of the points, and coord_fr+"_pos" will contain there position along an axis in meters)
    axis_nme_to : str
        coordinate where to go
    """
    ####################
    #   Initialisation
    ####################

    # Add all necessary dimensions so they all match between the 3 arrays. Maybe not the most optimized, but easier to implemented for now
    (z_fr, z_to) = _add_dims(z_fr, z_to)
    (z_fr, da_fr) = _add_dims(z_fr, da_fr)
    (z_fr, z_to) = _add_dims(z_fr, z_to)

    # For the call of interp_new_vertical, we need to shape the arrays as:
    # z_old : (z, y, x, t)
    # z_new : (z, y, x, t)
    # v_old : (z, y, x, t)
    # At least, we need to have z as first dimension and all extra dimensions at the end

    # transpose the arrays in the order (z, y, x, t)
    dims_fr = list(z_fr.dims)
    dims_to = list(z_to.dims)
    dims_fr.sort()
    dims_to.sort()
    # dims_fr and dims_to should be the same
    if dims_fr != dims_to:
        raise (
            ValueError(
                f"The dimensions of the coordinates from and to should match. Got {dims_fr} and {dims_to}"
            )
        )
    # remove coord_nme from the dims
    dims_fr.remove(coord_nme)
    dims_fr.insert(0, coord_nme)

    z_fr = z_fr.transpose(*dims_fr, transpose_coords=False)
    z_to = z_to.transpose(*dims_fr, transpose_coords=False)

    # gets coords of da_fr that are not present in dims_fr
    coords_da_fr = [i for i in da_fr.coords if i not in dims_fr]
    da_fr = da_fr.transpose(*dims_fr, *coords_da_fr, transpose_coords=False)

    z_fr_data = z_fr.data
    z_to_data = z_to.data
    da_fr_data = da_fr.data

    # arrays z_fr and z_to
    if len(z_fr.dims) != 4 or len(z_to.dims) != 4 or len(da_fr.dims) != 4:
        need_to_reshape = True
    else:
        need_to_reshape = False

    if need_to_reshape:
        # we need to reshape the data
        z_fr_shape = z_fr_data.shape
        z_to_shape = z_to_data.shape
        da_fr_shape = da_fr_data.shape

        z_fr_data = z_fr_data.reshape(_shape_to_shape_of_len_4(z_fr_shape), order="C")
        z_to_data = z_to_data.reshape(_shape_to_shape_of_len_4(z_to_shape), order="C")
        da_fr_data = da_fr_data.reshape(
            _shape_to_shape_of_len_4(da_fr_shape), order="C"
        )

        # raise (ValueError(f"The vertical coordinates must be 3D (x, y, z) arrays, got {z_fr.dims} and {z_to.dims}\n"+"The data must be a 4D array, got {da_fr.dims}"))

    # We have the 3 necessary arrays:
    # z_fr, z_to, and da_fr
    # we interpolate
    try:
        v_to = interp_new_vertical(z_fr_data, z_to_data, da_fr_data)
    except TypeError:
        # Pythran needs arrays in C order and not in Fortran order
        try:
            v_to = interp_new_vertical(
                np.ascontiguousarray(z_fr_data),
                np.ascontiguousarray(z_to_data),
                np.ascontiguousarray(da_fr_data),
            )
        except TypeError as error:
            # falls back on the pure python version
            warnings.warn(
                f"Falling back to pure python implementation of the remapping function due to unsupported data type:\n{error}"
            )
            v_to = interp_new_vertical_pure_python(z_fr_data, z_to_data, da_fr_data)

    if need_to_reshape:
        # new shape:
        # jpk_new, jpj, and jpi from z_new
        # we decompose back jpt to the value it had
        (jpk_new, jpj, jpi) = z_fr_shape[:3]
        t_tuple = da_fr_shape[3:]

        v_to = v_to.reshape((jpk_new, jpj, jpi) + t_tuple)

    # we create a new dataset containing the interpolated values
    # dropping all coordinates of da_fr that are not necessary
    """
    for coord in da_fr.coords.keys():
        if coord not in da_fr.dims:
            da_fr = da_fr.drop(coord)
    """

    da_to = xr.DataArray(
        data=v_to,
        coords=[
            *[(i, z_to.coords[i]) for i in dims_fr],
            *[(i, da_fr.coords[i]) for i in coords_da_fr],
        ],
        attrs=da_fr.attrs,
        name=da_fr.name,
    )

    return da_to

    Compute the linear interpolation of *da* on the new vertical coordinates.

    Parameters
    ---------
    da_fr : xarray.DataArray
        The data to interpolate
    coords_fr / coords_to : empty dataset with all coordinates
        Contain the physical position of the old / new coordinates
    axis_nme_fr : str
        The coordinate base for the interpolation (axisension of *da*). A dimension
        coord_fr+"_pos" must be provided in *coords_fr*. This coordinate contains the physical position
        of the coordinate (e.g. coord_fr can contain integers, representing the number
        of the points, and coord_fr+"_pos" will contain there position along an axis in meters)
    axis_nme_to : str
        coordinate where to go
    
Compute the linear interpolation of *da* on the new vertical coordinates.
Parameters

da_fr : xarray.DataArray
The data to interpolate
coords_fr / coords_to : empty dataset with all coordinates
Contain the physical position of the old / new coordinates
axis_nme_fr : str
The coordinate base for the interpolation (axisension of *da*). A dimension
coord_fr+"_pos" must be provided in *coords_fr*. This coordinate contains the physical position
of the coordinate
axis_nme_to : str
coordinate where to go
Compute the linear interpolation of * da * on the new vertical coordinates . Parameters da_fr : xarray . DataArray The data to interpolate coords_fr / coords_to : empty dataset with all coordinates Contain the physical position of the old / new coordinates axis_nme_fr : str The coordinate base for the interpolation ( axisension of * da * ) . A dimension coord_fr + " _pos " must be provided in * coords_fr * . This coordinate contains the physical position of the coordinate axis_nme_to : str coordinate where to go
def interpolate(da_fr, z_fr, z_to, coord_nme):
    (z_fr, z_to) = _add_dims(z_fr, z_to)
    (z_fr, da_fr) = _add_dims(z_fr, da_fr)
    (z_fr, z_to) = _add_dims(z_fr, z_to)
    dims_fr = list(z_fr.dims)
    dims_to = list(z_to.dims)
    dims_fr.sort()
    dims_to.sort()
    if dims_fr != dims_to:
        raise (
            ValueError(
                f"The dimensions of the coordinates from and to should match. Got {dims_fr} and {dims_to}"
            )
        )
    dims_fr.remove(coord_nme)
    dims_fr.insert(0, coord_nme)
    z_fr = z_fr.transpose(*dims_fr, transpose_coords=False)
    z_to = z_to.transpose(*dims_fr, transpose_coords=False)
    coords_da_fr = [i for i in da_fr.coords if i not in dims_fr]
    da_fr = da_fr.transpose(*dims_fr, *coords_da_fr, transpose_coords=False)
    z_fr_data = z_fr.data
    z_to_data = z_to.data
    da_fr_data = da_fr.data
    if len(z_fr.dims) != 4 or len(z_to.dims) != 4 or len(da_fr.dims) != 4:
        need_to_reshape = True
    else:
        need_to_reshape = False
    if need_to_reshape:
        z_fr_shape = z_fr_data.shape
        z_to_shape = z_to_data.shape
        da_fr_shape = da_fr_data.shape
        z_fr_data = z_fr_data.reshape(_shape_to_shape_of_len_4(z_fr_shape), order="C")
        z_to_data = z_to_data.reshape(_shape_to_shape_of_len_4(z_to_shape), order="C")
        da_fr_data = da_fr_data.reshape(
            _shape_to_shape_of_len_4(da_fr_shape), order="C"
        )
    try:
        v_to = interp_new_vertical(z_fr_data, z_to_data, da_fr_data)
    except TypeError:
        try:
            v_to = interp_new_vertical(
                np.ascontiguousarray(z_fr_data),
                np.ascontiguousarray(z_to_data),
                np.ascontiguousarray(da_fr_data),
            )
        except TypeError as error:
            warnings.warn(
                f"Falling back to pure python implementation of the remapping function due to unsupported data type:\n{error}"
            )
            v_to = interp_new_vertical_pure_python(z_fr_data, z_to_data, da_fr_data)
    if need_to_reshape:
        (jpk_new, jpj, jpi) = z_fr_shape[:3]
        t_tuple = da_fr_shape[3:]
        v_to = v_to.reshape((jpk_new, jpj, jpi) + t_tuple)
    da_to = xr.DataArray(
        data=v_to,
        coords=[
            *[(i, z_to.coords[i]) for i in dims_fr],
            *[(i, da_fr.coords[i]) for i in coords_da_fr],
        ],
        attrs=da_fr.attrs,
        name=da_fr.name,
    )
    return da_to
def interpolate ( da_fr , z_fr , z_to , coord_nme ) : ( z_fr , z_to ) = _add_dims ( z_fr , z_to ) ( z_fr , da_fr ) = _add_dims ( z_fr , da_fr ) ( z_fr , z_to ) = _add_dims ( z_fr , z_to ) dims_fr = list ( z_fr . dims ) dims_to = list ( z_to . dims ) dims_fr . sort ( ) dims_to . sort ( ) if dims_fr != dims_to : raise ( ValueError ( f"The dimensions of the coordinates from and to should match. Got {dims_fr} and {dims_to}" ) ) dims_fr . remove ( coord_nme ) dims_fr . insert ( 0 , coord_nme ) z_fr = z_fr . transpose ( * dims_fr , transpose_coords = False ) z_to = z_to . transpose ( * dims_fr , transpose_coords = False ) coords_da_fr = [ i for i in da_fr . coords if i not in dims_fr ] da_fr = da_fr . transpose ( * dims_fr , * coords_da_fr , transpose_coords = False ) z_fr_data = z_fr . data z_to_data = z_to . data da_fr_data = da_fr . data if len ( z_fr . dims ) != 4 or len ( z_to . dims ) != 4 or len ( da_fr . dims ) != 4 : need_to_reshape = True else : need_to_reshape = False if need_to_reshape : z_fr_shape = z_fr_data . shape z_to_shape = z_to_data . shape da_fr_shape = da_fr_data . shape z_fr_data = z_fr_data . reshape ( _shape_to_shape_of_len_4 ( z_fr_shape ) , order = "C" ) z_to_data = z_to_data . reshape ( _shape_to_shape_of_len_4 ( z_to_shape ) , order = "C" ) da_fr_data = da_fr_data . reshape ( _shape_to_shape_of_len_4 ( da_fr_shape ) , order = "C" ) try : v_to = interp_new_vertical ( z_fr_data , z_to_data , da_fr_data ) except TypeError : try : v_to = interp_new_vertical ( np . ascontiguousarray ( z_fr_data ) , np . ascontiguousarray ( z_to_data ) , np . ascontiguousarray ( da_fr_data ) , ) except TypeError as error : warnings . warn ( f"Falling back to pure python implementation of the remapping function due to unsupported data type:\n{error}" ) v_to = interp_new_vertical_pure_python ( z_fr_data , z_to_data , da_fr_data ) if need_to_reshape : ( jpk_new , jpj , jpi ) = z_fr_shape [ : 3 ] t_tuple = da_fr_shape [ 3 : ] v_to = v_to . reshape ( ( jpk_new , jpj , jpi ) + t_tuple ) """
    for coord in da_fr.coords.keys():
        if coord not in da_fr.dims:
            da_fr = da_fr.drop(coord)
    """ da_to = xr . DataArray ( data = v_to , coords = [ * [ ( i , z_to . coords [ i ] ) for i in dims_fr ] , * [ ( i , da_fr . coords [ i ] ) for i in coords_da_fr ] , ] , attrs = da_fr . attrs , name = da_fr . name , ) return da_to
Compute the linear interpolation of *da* on the new vertical coordinates.
Compute the linear interpolation of * da * on the new vertical coordinates .
"""
    Compute the linear interpolation of *da* on the new vertical coordinates.

    Parameters
    ---------
    da_fr : xarray.DataArray
        The data to interpolate
    coords_fr / coords_to : empty dataset with all coordinates
        Contain the physical position of the old / new coordinates
    axis_nme_fr : str
        The coordinate base for the interpolation (axisension of *da*). A dimension
        coord_fr+"_pos" must be provided in *coords_fr*. This coordinate contains the physical position
        of the coordinate (e.g. coord_fr can contain integers, representing the number
        of the points, and coord_fr+"_pos" will contain there position along an axis in meters)
    axis_nme_to : str
        coordinate where to go
    """ #################### #   Initialisation #################### # Add all necessary dimensions so they all match between the 3 arrays. Maybe not the most optimized, but easier to implemented for now # For the call of interp_new_vertical, we need to shape the arrays as: # z_old : (z, y, x, t) # z_new : (z, y, x, t) # v_old : (z, y, x, t) # At least, we need to have z as first dimension and all extra dimensions at the end # transpose the arrays in the order (z, y, x, t) # dims_fr and dims_to should be the same # remove coord_nme from the dims # gets coords of da_fr that are not present in dims_fr # arrays z_fr and z_to # we need to reshape the data # raise (ValueError(f"The vertical coordinates must be 3D (x, y, z) arrays, got {z_fr.dims} and {z_to.dims}\n"+"The data must be a 4D array, got {da_fr.dims}")) # We have the 3 necessary arrays: # z_fr, z_to, and da_fr # we interpolate # Pythran needs arrays in C order and not in Fortran order # falls back on the pure python version # new shape: # jpk_new, jpj, and jpi from z_new # we decompose back jpt to the value it had # we create a new dataset containing the interpolated values # dropping all coordinates of da_fr that are not necessary """
    for coord in da_fr.coords.keys():
        if coord not in da_fr.dims:
            da_fr = da_fr.drop(coord)
    """
da_fr
z_fr
z_to
coord_nme