Home > custom > grid_tools > crop_nan.m

crop_nan

PURPOSE ^

PURPOSE: crops a grid by deleting all rows and cols that are completely

SYNOPSIS ^

function [X,Y,grid] = crop_nan(X,Y,grid,nanval)

DESCRIPTION ^

 PURPOSE:  crops a grid by deleting all rows and cols that are completely
           filled with NaNs or any value given by [nanval]
 -------------------------------------------------------------------
 USAGE: [X,Y,grid] = crop_nan(X,Y,grid,nanval)
 where: 
        [X],[Y] are the x and y coordinate vectors
        [grid]  is the source grid to be cropped
        [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 
 -------------------------------------------------------------------
 See also: crop_compare_nan(), crop_compare_nan2()
 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] = crop_nan(X,Y,grid,nanval)
0002 % PURPOSE:  crops a grid by deleting all rows and cols that are completely
0003 %           filled with NaNs or any value given by [nanval]
0004 % -------------------------------------------------------------------
0005 % USAGE: [X,Y,grid] = crop_nan(X,Y,grid,nanval)
0006 % where:
0007 %        [X],[Y] are the x and y coordinate vectors
0008 %        [grid]  is the source grid to be cropped
0009 %        [nanval] is any optional value to be used as NaN. Default is NaN
0010 % -------------------------------------------------------------------------
0011 % OUTPUTS:
0012 %        [X],[Y] are the cropped X and Y coordinate vectors
0013 %        [grid]  is the target cropped grid
0014 % -------------------------------------------------------------------
0015 % See also: crop_compare_nan(), crop_compare_nan2()
0016 % Functions used: isnan(), all()
0017 %
0018 % Felix Hebeler, Geography Dept., University Zurich, Juli 2006.
0019 
0020 
0021 if ~exist('nanval','var')
0022     nanval='nan';
0023 end
0024 
0025 % straightforward: standard case of NaN
0026 if (lower(nanval)=='nan')
0027     Y(reshape((all(isnan(grid),2)),1,length(all(isnan(grid),2))))=[]; %rowvector
0028     X(all(isnan(grid)))=[]; %col vector
0029     grid(:,all(isnan(grid)))=[]; %rows
0030     grid(all(isnan(grid),2),:)=[]; %cols
0031 else 
0032     %use any number provided
0033    disp(['Non NaN value used: ',num2str(nanval)])
0034     Y(reshape((all(grid==nanval,2)),1,length(all(grid==nanval,2))))=[]; %rowvector
0035     X(all(grid==nanval))=[]; %col vector
0036     grid(all(grid==nanval,2),:)=[]; %rows
0037     grid(:,all(grid==nanval))=[]; %cols
0038 end

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