xrspatial.focal.mean(agg, passes=1, excludes=[nan], name='mean')[source]

Returns Mean filtered array using a 3x3 window. Default behaviour to ‘mean’ is to pad the borders with nans

  • agg (xarray.DataArray) – 2D array of input values to be filtered.

  • passes (int, default=1) – Number of times to run mean.

  • name (str, default='mean') – Output xr.DataArray.name property.


mean_agg – 2D aggregate array of filtered values.

Return type

xarray.DataArray of same type as agg


import datashader as ds
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

from xrspatial import generate_terrain
from xrspatial.focal import mean

# Generate Example Terrain
W = 500
H = 300

template_terrain = xr.DataArray(np.zeros((H, W)))
x_range=(-20e6, 20e6)
y_range=(-20e6, 20e6)

terrain_agg = generate_terrain(
    template_terrain, x_range=x_range, y_range=y_range

# Edit Attributes
terrain_agg = terrain_agg.assign_attrs(
        'Description': 'Example Terrain',
        'units': 'km',
        'Max Elevation': '4000',

terrain_agg = terrain_agg.rename({'x': 'lon', 'y': 'lat'})
terrain_agg = terrain_agg.rename('Elevation')

# Create Mean Aggregate Array
mean_agg = mean(agg = terrain_agg, name = 'Elevation')

# Edit Attributes
mean_agg = mean_agg.assign_attrs(
        'Description': 'Example Mean Filtered Terrain',