Home > atmlab > gridcreation > grid_refine.m

grid_refine

PURPOSE ^

GRID_REFINE Refine a grid by adding additional points

SYNOPSIS ^

function ng = grid_refine(og, delta)

DESCRIPTION ^

 GRID_REFINE Refine a grid by adding additional points

 This function refines a grid by adding additional points in such a way
 that each interval in the new grid is smaller than the desired value
 delta. All points of the original grid are retained. A typical
 application of this is to create a finer vertical grid for atmospheric
 profiles.

 The original grid og must be strictly monotonic (either increasing or
 decreasing, no duplicate values).

 See also PROFILE_REFINE, which uses this function.

 FORMAT ng = grid_refine(og, delta)

 OUT   ng      New (denser) grid.
 IN    og      Original grid.
       delta   Desired maximum grid spacing.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

grid_refine.m

SOURCE CODE ^

0001 % GRID_REFINE Refine a grid by adding additional points
0002 %
0003 % This function refines a grid by adding additional points in such a way
0004 % that each interval in the new grid is smaller than the desired value
0005 % delta. All points of the original grid are retained. A typical
0006 % application of this is to create a finer vertical grid for atmospheric
0007 % profiles.
0008 %
0009 % The original grid og must be strictly monotonic (either increasing or
0010 % decreasing, no duplicate values).
0011 %
0012 % See also PROFILE_REFINE, which uses this function.
0013 %
0014 % FORMAT ng = grid_refine(og, delta)
0015 %
0016 % OUT   ng      New (denser) grid.
0017 % IN    og      Original grid.
0018 %       delta   Desired maximum grid spacing.
0019 
0020 % 2010-07-02 Created by Stefan Buehler
0021 
0022 function ng = grid_refine(og, delta)
0023 
0024 % Original grid must have at least two elements.
0025 if length(og) < 2
0026     error( 'Original grid og must have at least two elements.' );
0027 end
0028 
0029 % Find out direction of grid and, if necessary, revert it so that it is
0030 % always increasing. (Just to simplify things)
0031 inverted = false;
0032 dir = og(2)-og(1);
0033 if dir < 0
0034     og = og(end:-1:1);
0035     inverted = true;
0036 end
0037 
0038 % Check that original grid is monotoneously increasing (even duplicate
0039 % values are not allowed):
0040 d = diff(og);
0041 if min(d) <= 0
0042    error('Original grid og must be strictly monotonic.'); 
0043 end
0044 
0045 % Find out if the original grid is a row or column vector. We will turn
0046 % round the result to match the original grid.
0047 transpose = false;
0048 [rows,cols] = size(og);
0049 if (rows > cols)
0050     transpose = true;
0051 end
0052 
0053 % Now the actual work starts...
0054 
0055 % Find out how many subdivisions are needed for each interval of the
0056 % original grid.
0057 nsub = ceil(d/delta);
0058 
0059 % Calculate subdivision intervals:
0060 sub = d./nsub;
0061 
0062 % Create a cell array with the new grid points for each interval in the
0063 % original grid.
0064 ng = arrayfun(@linspace, og(1:end-1), og(2:end)-sub, nsub,...
0065             'UniformOutput', false);
0066         
0067 % Get rid of the cell array by concatenating all elements, and add the
0068 % missing last element of the original grid.
0069 ng = [ng{:}, og(end)];
0070 
0071 
0072 % Turn round the result if the original grid was decreasing:
0073 if inverted==true
0074     ng = ng(end:-1:1);
0075 end
0076 
0077 % Transpose the result, if necessary, to match the orientation (row or
0078 % column vector) of the original grid.
0079 if transpose==true
0080    ng = ng'; 
0081 end
0082 
0083

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