


PURPOSE: compares two grids A & B of same extent and origin and crops
grid A by deleting all rows and cols that are completely filled
with NaNs or any value given by [nanval] in grid B.
-------------------------------------------------------------------
USAGE: [X,Y,grid] = crop_compare_nan(X,Y,grid,nangrid,nanval)
where:
[X],[Y] are the x and y coordinate vectors (for both grids)
[grid] is the source grid to be cropped
[nangrid] is the grid to be checked for NaN rows and cols
[nanval] is any optional value to be used as NaN, Default is NaN
-------------------------------------------------------------------------
OUTPUTS:
[X],[Y] are the cropped X and Y coordinate vectors
[grid] is the target cropped grid
-------------------------------------------------------------------
NOTE: Both grids need to have the same extent and origin:
size(X)*size(Y) = size(grid,1)*size(grid,2) = size(nangrid,1)*size(nangrid,2)
Coordinate vectors X and Y will be cropped according to index of [nangrid].
See also: crop_nan(), crop_compare_nan2();
Functions used: isnan(), all()
Felix Hebeler, Geography Dept., University Zurich, Juli 2006.

0001 function [X,Y,grid] = crop_compare_nan(X,Y,grid,nangrid,nanval) 0002 % PURPOSE: compares two grids A & B of same extent and origin and crops 0003 % grid A by deleting all rows and cols that are completely filled 0004 % with NaNs or any value given by [nanval] in grid B. 0005 % ------------------------------------------------------------------- 0006 % USAGE: [X,Y,grid] = crop_compare_nan(X,Y,grid,nangrid,nanval) 0007 % where: 0008 % [X],[Y] are the x and y coordinate vectors (for both grids) 0009 % [grid] is the source grid to be cropped 0010 % [nangrid] is the grid to be checked for NaN rows and cols 0011 % [nanval] is any optional value to be used as NaN, Default is NaN 0012 % ------------------------------------------------------------------------- 0013 % OUTPUTS: 0014 % [X],[Y] are the cropped X and Y coordinate vectors 0015 % [grid] is the target cropped grid 0016 % ------------------------------------------------------------------- 0017 % NOTE: Both grids need to have the same extent and origin: 0018 % size(X)*size(Y) = size(grid,1)*size(grid,2) = size(nangrid,1)*size(nangrid,2) 0019 % Coordinate vectors X and Y will be cropped according to index of [nangrid]. 0020 % 0021 % See also: crop_nan(), crop_compare_nan2(); 0022 % Functions used: isnan(), all() 0023 % 0024 % Felix Hebeler, Geography Dept., University Zurich, Juli 2006. 0025 0026 0027 if ~exist('nanval','var') 0028 nanval='nan'; 0029 end 0030 0031 % straightforward: standard case of NaN 0032 if (lower(nanval)=='nan') 0033 Y(reshape((all(isnan(nangrid),2)),1,length(all(isnan(nangrid),2))))=[]; %rowvector 0034 X(all(isnan(nangrid)))=[]; %col vector 0035 grid(:,all(isnan(nangrid)))=[]; %rows 0036 grid(all(isnan(nangrid),2),:)=[]; %cols 0037 else 0038 %use any number provided 0039 %disp(['Non NaN value used: ',num2str(nanval)]) 0040 Y(reshape((all(nangrid==nanval,2)),1,length(all(nangrid==nanval,2))))=[]; %rowvector 0041 X(all(nangrid==nanval))=[]; %col vector 0042 grid(all(nangrid==nanval,2),:)=[]; %rows 0043 grid(:,all(nangrid==nanval))=[]; %cols 0044 end