xarray/core/variable.py
def _coarsen_reshape(self, windows, boundary, side):
        """
        Construct a reshaped-array for coarsen
        """
        if not utils.is_dict_like(boundary):
            boundary = {d: boundary for d in windows.keys()}

        if not utils.is_dict_like(side):
            side = {d: side for d in windows.keys()}

        # remove unrelated dimensions
        boundary = {k: v for k, v in boundary.items() if k in windows}
        side = {k: v for k, v in side.items() if k in windows}

        for d, window in windows.items():
            if window <= 0:
                raise ValueError(f"window must be > 0. Given {window}")

        variable = self
        for d, window in windows.items():
            # trim or pad the object
            size = variable.shape[self._get_axis_num(d)]
            n = int(size / window)
            if boundary[d] == "exact":
                if n * window != size:
                    raise ValueError(
                        "Could not coarsen a dimension of size {} with "
                        "window {}".format(size, window)
                    )
            elif boundary[d] == "trim":
                if side[d] == "left":
                    variable = variable.isel({d: slice(0, window * n)})
                else:
                    excess = size - window * n
                    variable = variable.isel({d: slice(excess, None)})
            elif boundary[d] == "pad":  # pad
                pad = window * n - size
                if pad < 0:
                    pad += window
                if side[d] == "left":
                    pad_width = {d: (0, pad)}
                else:
                    pad_width = {d: (pad, 0)}
                variable = variable.pad(pad_width, mode="constant")
            else:
                raise TypeError(
                    "{} is invalid for boundary. Valid option is 'exact', "
                    "'trim' and 'pad'".format(boundary[d])
                )

        shape = []
        axes = []
        axis_count = 0
        for i, d in enumerate(variable.dims):
            if d in windows:
                size = variable.shape[i]
                shape.append(int(size / windows[d]))
                shape.append(windows[d])
                axis_count += 1
                axes.append(i + axis_count)
            else:
                shape.append(variable.shape[i])

        return variable.data.reshape(shape), tuple(axes)

        Construct a reshaped-array for coarsen
        
Construct a reshaped-array for coarsen
Construct a reshaped - array for coarsen
def _coarsen_reshape(self, windows, boundary, side):
        if not utils.is_dict_like(boundary):
            boundary = {d: boundary for d in windows.keys()}
        if not utils.is_dict_like(side):
            side = {d: side for d in windows.keys()}
        boundary = {k: v for k, v in boundary.items() if k in windows}
        side = {k: v for k, v in side.items() if k in windows}
        for d, window in windows.items():
            if window <= 0:
                raise ValueError(f"window must be > 0. Given {window}")
        variable = self
        for d, window in windows.items():
            size = variable.shape[self._get_axis_num(d)]
            n = int(size / window)
            if boundary[d] == "exact":
                if n * window != size:
                    raise ValueError(
                        "Could not coarsen a dimension of size {} with "
                        "window {}".format(size, window)
                    )
            elif boundary[d] == "trim":
                if side[d] == "left":
                    variable = variable.isel({d: slice(0, window * n)})
                else:
                    excess = size - window * n
                    variable = variable.isel({d: slice(excess, None)})
            elif boundary[d] == "pad":  
                pad = window * n - size
                if pad < 0:
                    pad += window
                if side[d] == "left":
                    pad_width = {d: (0, pad)}
                else:
                    pad_width = {d: (pad, 0)}
                variable = variable.pad(pad_width, mode="constant")
            else:
                raise TypeError(
                    "{} is invalid for boundary. Valid option is 'exact', "
                    "'trim' and 'pad'".format(boundary[d])
                )
        shape = []
        axes = []
        axis_count = 0
        for i, d in enumerate(variable.dims):
            if d in windows:
                size = variable.shape[i]
                shape.append(int(size / windows[d]))
                shape.append(windows[d])
                axis_count += 1
                axes.append(i + axis_count)
            else:
                shape.append(variable.shape[i])
        return variable.data.reshape(shape), tuple(axes)
def _coarsen_reshape ( self , windows , boundary , side ) : if not utils . is_dict_like ( boundary ) : boundary = { d : boundary for d in windows . keys ( ) } if not utils . is_dict_like ( side ) : side = { d : side for d in windows . keys ( ) } boundary = { k : v for k , v in boundary . items ( ) if k in windows } side = { k : v for k , v in side . items ( ) if k in windows } for d , window in windows . items ( ) : if window <= 0 : raise ValueError ( f"window must be > 0. Given {window}" ) variable = self for d , window in windows . items ( ) : size = variable . shape [ self . _get_axis_num ( d ) ] n = int ( size / window ) if boundary [ d ] == "exact" : if n * window != size : raise ValueError ( "Could not coarsen a dimension of size {} with " "window {}" . format ( size , window ) ) elif boundary [ d ] == "trim" : if side [ d ] == "left" : variable = variable . isel ( { d : slice ( 0 , window * n ) } ) else : excess = size - window * n variable = variable . isel ( { d : slice ( excess , None ) } ) elif boundary [ d ] == "pad" : pad = window * n - size if pad < 0 : pad += window if side [ d ] == "left" : pad_width = { d : ( 0 , pad ) } else : pad_width = { d : ( pad , 0 ) } variable = variable . pad ( pad_width , mode = "constant" ) else : raise TypeError ( "{} is invalid for boundary. Valid option is 'exact', " "'trim' and 'pad'" . format ( boundary [ d ] ) ) shape = [ ] axes = [ ] axis_count = 0 for i , d in enumerate ( variable . dims ) : if d in windows : size = variable . shape [ i ] shape . append ( int ( size / windows [ d ] ) ) shape . append ( windows [ d ] ) axis_count += 1 axes . append ( i + axis_count ) else : shape . append ( variable . shape [ i ] ) return variable . data . reshape ( shape ) , tuple ( axes )
Construct a reshaped-array for coarsen
Construct a reshaped - array for coarsen
"""
        Construct a reshaped-array for coarsen
        """ # remove unrelated dimensions # trim or pad the object # pad
self
windows
boundary
side