Home > atmlab > geographical > data2rectgrid.m

data2rectgrid

PURPOSE ^

DATA2RECTGRID Map irregular data to a rectangular grid

SYNOPSIS ^

function [Z,N] = data2rectgrid(xedges,yedges,x,y,z,funh,varargin)

DESCRIPTION ^

 DATA2RECTGRID   Map irregular data to a rectangular grid 

    The function finds all input data points inside each grid cell and
    performs the operation specified by *funh* to create data on a
    rectangular grid. The grid cells are defined by the position of the edges
    by *xedges* and *yedges*. Data outside these ranges are ignored.
    Empty entries in *Z* are returned as *backv*.

    The data values, *z*, must be given columns. If *z* has more than one 
    column this is interpreted as different "altitudes", and the operation 
    is performed for each altitude individually.

    The mapping function must return a scalar for a vector input. Possible
    choices for *fun* are: @sum, @mean and @max. 

 FORMAT   [Z,N] = data2rectgrid(xedges,yedges,x,y,z,funh[,lonrange])
        
 OUT   Z        Data on rectangular grid. Size 
                is [length(xedges)-1,length(yedges)-1,size(z,2)].
       N        Number of data points found inside each grid cell.
 IN    xedges   Edges of grid cells in x-direction.
       yedges   Edges of grid cells in y-direction.
       x        x-position of input data points.
       y        y-position of input data points.
       z        Input data values.
       funh     Handle to mapping function. 
 OPT   backv    Value for empty bins. Default is NaN

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

data2rectgrid.m

SOURCE CODE ^

0001 % DATA2RECTGRID   Map irregular data to a rectangular grid
0002 %
0003 %    The function finds all input data points inside each grid cell and
0004 %    performs the operation specified by *funh* to create data on a
0005 %    rectangular grid. The grid cells are defined by the position of the edges
0006 %    by *xedges* and *yedges*. Data outside these ranges are ignored.
0007 %    Empty entries in *Z* are returned as *backv*.
0008 %
0009 %    The data values, *z*, must be given columns. If *z* has more than one
0010 %    column this is interpreted as different "altitudes", and the operation
0011 %    is performed for each altitude individually.
0012 %
0013 %    The mapping function must return a scalar for a vector input. Possible
0014 %    choices for *fun* are: @sum, @mean and @max.
0015 %
0016 % FORMAT   [Z,N] = data2rectgrid(xedges,yedges,x,y,z,funh[,lonrange])
0017 %
0018 % OUT   Z        Data on rectangular grid. Size
0019 %                is [length(xedges)-1,length(yedges)-1,size(z,2)].
0020 %       N        Number of data points found inside each grid cell.
0021 % IN    xedges   Edges of grid cells in x-direction.
0022 %       yedges   Edges of grid cells in y-direction.
0023 %       x        x-position of input data points.
0024 %       y        y-position of input data points.
0025 %       z        Input data values.
0026 %       funh     Handle to mapping function.
0027 % OPT   backv    Value for empty bins. Default is NaN
0028 
0029 % 2005-03-27   Created by Patrick Eriksson.
0030 
0031 
0032 function [Z,N] = data2rectgrid(xedges,yedges,x,y,z,funh,varargin)
0033 %
0034 backv = optargs( varargin, { NaN } );
0035                                                                          %&%
0036                                                                          %&%
0037 %= Check input                                                           %&%
0038 %                                                                        %&%
0039 rqre_nargin( 6, nargin );                                                %&%
0040 %
0041 if length(y)~=length(x)  
0042   error('Vectors *x* and *y* must have same length.');
0043 end
0044 %
0045 if size(z,1)~=length(x)
0046   error('Mismatch in size between *x* and *z*.');
0047 end
0048 %
0049 if ~isa( funh, 'function_handle')
0050   error('Argument *funh* must be a function handle');
0051 end
0052 
0053 
0054 %= Size outpout
0055 %
0056 N = zeros( length(xedges)-1, length(yedges)-1 );
0057 Z = repmat( backv, [length(xedges)-1,length(yedges)-1,size(z,2)] );
0058 
0059 
0060 % Fix to simplify code below
0061 %
0062 yedges(end) = yedges(end)+eps;
0063 xedges(end) = xedges(end)+eps;
0064 
0065 for iy = 1 : length(yedges)-1
0066   for ix = 1 : length(xedges)-1
0067 
0068     ind = find( y>=yedges(iy) & y<yedges(iy+1) & ...
0069                 x>=xedges(ix) & x<xedges(ix+1) );
0070 
0071     if length(ind)
0072       N(ix,iy) = length( ind );
0073       for i = 1:size(z,2)
0074         Z(ix,iy,i) = funh( z(ind,i) );
0075       end
0076     end
0077   end
0078 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005