


PURPOSE: resamples grid using maximum of all source cells with target cell
of defined size at location x-y.
-------------------------------------------------------------------
USAGE: value = resample_max(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 max 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_mean, resample_min, neighbourhood_statistics, block_statistics
Felix Hebeler, Geography Dept., University Zurich, September 2006.

0001 function value = resample_max(scs, tcs, tx, ty, xll, yll, grid) 0002 % PURPOSE: resamples grid using maximum of all source cells with target cell 0003 % of defined size at location x-y. 0004 % ------------------------------------------------------------------- 0005 % USAGE: value = resample_max(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 max 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_mean, resample_min, neighbourhood_statistics, block_statistics 0027 % 0028 % Felix Hebeler, Geography Dept., University Zurich, September 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 return 0052 else 0053 x_start=1; 0054 end 0055 elseif x_end>size(grid,2) 0056 if abs(xurc/scs)>size(grid,2)+1+(0.5*tcs) 0057 value = nan; 0058 return 0059 else 0060 x_end=size(grid,2); 0061 end 0062 end 0063 if y_start < 1 0064 if abs(yllc/scs)<0.5*tcs 0065 value=nan; 0066 return 0067 else 0068 y_start=1; 0069 end 0070 elseif y_end>size(grid,1) 0071 if abs(yurc/scs)>size(grid,1)+0.5*tcs 0072 value = nan; 0073 return 0074 else 0075 y_end=size(grid,1); 0076 end 0077 end 0078 0079 %% calc mean and return 0080 value=max(max(grid(y_start:y_end,x_start:x_end))); 0081