function [cz_mean] = fct_downsample(cx, cy, fx, fy, fz)
%% Downsample fine-scale map to coarse scale by averaging
%
%  last edit: 11.05.2022 by Georg
%
%  Input
%  cx ... coarse x (lon) vector
%  cy ... coarse y (lat) vector
%  fx ... fine x (lon) vector
%  fy ... fine y (lat) vector
%  fz ... fine map
%
%  Output
%  cz_mean ... downsampled coarse map (mean)
%  cz_std ... downsampled coarse map (std)

c = interp1(cx, 1:numel(cx), fx, 'nearest', 'extrap');
r = interp1(cy, 1:numel(cy), fy, 'nearest', 'extrap');
[cg, rg] = meshgrid(c', r');
cz_mean = accumarray([rg(:) cg(:)], fz(:), [length(cy) length(cx)], @nanmean, NaN);
%cz_std = accumarray([rg(:) cg(:)], fz(:), [length(cy) length(cx)], @nanstd, NaN);

end