Home > custom > grid_tools > crop_compare_nan2.m

crop_compare_nan2

PURPOSE ^

PURPOSE: compares two grids A & B of same extent and origin and crops

SYNOPSIS ^

function [X,Y,grid,nangrid] = crop_compare_nan2(X,Y,grid,nangrid,nanval)

DESCRIPTION ^

 PURPOSE:  compares two grids A & B of same extent and origin and crops 
           BOTH grids 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,nangrid] = crop_compare_nan2(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 
        [nangrid]  is the target cropped nangrid 
 -------------------------------------------------------------------
 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_nan()
 Functions used:  isnan(), all()

 Felix Hebeler, Geography Dept., University Zurich, Juli 2006.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [X,Y,grid,nangrid] = crop_compare_nan2(X,Y,grid,nangrid,nanval)
0002 % PURPOSE:  compares two grids A & B of same extent and origin and crops
0003 %           BOTH grids 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,nangrid] = crop_compare_nan2(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 %        [nangrid]  is the target cropped nangrid
0017 % -------------------------------------------------------------------
0018 % NOTE:  Both grids need to have the same extent and origin:
0019 %        size(X)*size(Y) = size(grid,1)*size(grid,2) = size(nangrid,1)*size(nangrid,2)
0020 %        Coordinate vectors X and Y will be cropped according to index of [nangrid].
0021 %
0022 % See also: crop_nan(), crop_compare_nan()
0023 % Functions used:  isnan(), all()
0024 %
0025 % Felix Hebeler, Geography Dept., University Zurich, Juli 2006.
0026 
0027 
0028 if ~exist('nanval','var')
0029     nanval='nan';
0030 end
0031 
0032 % straightforward: standard case of NaN
0033 if (lower(nanval)=='nan')
0034     Y(reshape((all(isnan(nangrid),2)),1,length(all(isnan(nangrid),2))))=[]; %rowvector
0035     X(all(isnan(nangrid)))=[]; %col vector
0036     grid(:,all(isnan(nangrid)))=[]; %rows
0037     nangrid(:,all(isnan(nangrid)))=[]; %rows
0038     grid(all(isnan(nangrid),2),:)=[]; %cols
0039     nangrid(all(isnan(nangrid),2),:)=[]; %cols
0040 else 
0041     %use any number provided
0042    %disp(['Non NaN value used: ',num2str(nanval)])
0043     Y(reshape((all(nangrid==nanval,2)),1,length(all(nangrid==nanval,2))))=[]; %rowvector
0044     X(all(nangrid==nanval))=[]; %col vector
0045     grid(all(nangrid==nanval,2),:)=[]; %rows
0046     grid(:,all(nangrid==nanval))=[]; %cols
0047     nangrid(all(nangrid==nanval,2),:)=[]; %rows
0048     nangrid(:,all(nangrid==nanval))=[]; %cols
0049 end

Generated on Tue 24-Feb-2009 19:14:50 by m2html © 2003