


PURPOSE: resamples grid using mean of all source cells with target cell
of defined size at location x-y.
-------------------------------------------------------------------
USAGE: value = resample_mean(scs, tcs, tx, ty, xll, yll, grid)
where:
[scs] is the source cell size of the input grid in map units
[tcs] is the target cell size in map units, that is the size of
the block to calculate mean for
[tx],[ty] are the the target cell x and y coordinates (center of
the resampled cell)
[xll,yll] are the coordinates of the lower left corner (center of
cell) of the source grid
[grid] is the source with the cells to be resampled
-------------------------------------------------------------------------
OUTPUTS:
[value] is a scalar holding the mean of all cells that fall
into the range of the target cellsize (counting center of cells)
-------------------------------------------------------------------
Notes: if the distance of any corner of the resampled cell is more than 0.5 the
cell size over the source grid edges, NaN is returned.
This means the original size of the larger grid is kept, filled with NaNs
which can be cut later using crop_nan()
SEE ALSO: resample_max, resample_min, neighbourhood_statistics, block_statistics
Felix Hebeler, Geography Dept., University Zurich, March 2006.

0001 function value = resample_mean(scs, tcs, tx, ty, xll, yll, grid) 0002 % PURPOSE: resamples grid using mean of all source cells with target cell 0003 % of defined size at location x-y. 0004 % ------------------------------------------------------------------- 0005 % USAGE: value = resample_mean(scs, tcs, tx, ty, xll, yll, grid) 0006 % where: 0007 % [scs] is the source cell size of the input grid in map units 0008 % [tcs] is the target cell size in map units, that is the size of 0009 % the block to calculate mean for 0010 % [tx],[ty] are the the target cell x and y coordinates (center of 0011 % the resampled cell) 0012 % [xll,yll] are the coordinates of the lower left corner (center of 0013 % cell) of the source grid 0014 % [grid] is the source with the cells to be resampled 0015 % ------------------------------------------------------------------------- 0016 % OUTPUTS: 0017 % [value] is a scalar holding the mean of all cells that fall 0018 % into the range of the target cellsize (counting center of cells) 0019 % 0020 % ------------------------------------------------------------------- 0021 % Notes: if the distance of any corner of the resampled cell is more than 0.5 the 0022 % cell size over the source grid edges, NaN is returned. 0023 % This means the original size of the larger grid is kept, filled with NaNs 0024 % which can be cut later using crop_nan() 0025 % 0026 % SEE ALSO: resample_max, resample_min, neighbourhood_statistics, block_statistics 0027 % 0028 % Felix Hebeler, Geography Dept., University Zurich, March 2006. 0029 0030 %% get distance of sample cell llc from llc of source grid 0031 xllc = tx-xll; 0032 yllc = ty-yll; 0033 xurc = tx+-xll; 0034 yurc = ty-yll; 0035 0036 %% convert differences to grid indices 0037 x_start = round(xllc/scs)+1; 0038 x_end = round(xurc/scs)+1; 0039 y_start = size(grid,1)-round(yllc/scs); 0040 y_end = size(grid,1)-round(yurc/scs); 0041 0042 %% do something about grid edges 0043 % if the distance of any corner of the resampled cell is more than 0.5 the 0044 % cell size over the source grid edges, NaN is returned. 0045 % This means the original size of the larger grid is kept, filled with NaNs 0046 % which can be cut later using crop_nan() 0047 0048 if x_start < 1 0049 if abs(xllc/scs)<0.5*tcs 0050 value=nan; 0051 disp(' x_start < 1') 0052 return 0053 else 0054 x_start=1; 0055 end 0056 elseif x_end>size(grid,2) 0057 if abs(xurc/scs)>size(grid,2)+1+(0.5*tcs) 0058 value = nan; 0059 disp(' x_end>size(grid,2)') 0060 return 0061 else 0062 x_end=size(grid,2); 0063 end 0064 end 0065 if y_start < 1 0066 if abs(yllc/scs)<0.5*tcs 0067 value=nan; 0068 disp(' y_start < 1') 0069 return 0070 else 0071 y_start=1; 0072 end 0073 elseif y_end>size(grid,1) 0074 if abs(yurc/scs)>size(grid,1)+0.5*tcs 0075 value = nan; 0076 disp(' y_end>size(grid,1)') 0077 return 0078 else 0079 y_end=size(grid,1); 0080 end 0081 end 0082 0083 %% calc mean and return 0084 value=mean(nanmean(grid(y_start:y_end,x_start:x_end))); 0085