Code covered by the BSD License  

Highlights from
Gridding function, using Surfer

from Gridding function, using Surfer by Gabriel Ruiz
This script creates a grid from irregularly spaced XYZ data, using Surfer 8.

griddingsurfer
function griddingsurfer
%__________________________________________
% 1) Description:
% This script creates a grid from irregularly 
% spaced XYZ data, using Surfer 8.
%
% It is only necessarly to run the function.
%
% 2) Notes:
% It is very important to take account the DATA 
% MUST BE into a TXT or DAT file.
% This file must only have three columns and the 
% arrangement of these columns in file,
% it would be:
% - First column : X data; 
% - Second column:  Y values and finally,
% - Third column : Z data.
%
% Please, check the file does not have any 
% row with strings or characters.
%
% In command window, you can select the 
% Gridding method for your data.   
%
% This script was doing, using Matlab R2007a, 
% and it was tested, using Surfer v.8.02.
%
% It is not possible to assure this script woks 
% with anothers versions of Surfer.
%
% References:
% - Golden Surfer, Inc. (2002). Surfer User's Guide.
%             Colorado. 664 pp.
% - Hanselman, D. and Littlefield, B. (2005). Mastering
%             Matlab 7. Pearson Prentice Hall, Upper Sanddle
%             River. 852 pp.
%
% Thanks to Alberto Avila Armella to review this script.
%
% This script was done by Gabriel Ruiz Martinez.
% December, 2008
% v1
%___________________________________________

mes1(1) =  { 'This script creates a grid from irregularly' };
mes1(2) =  { '      spaced XYZ data, using Surfer 8.' };
mes1(3) =  { '' };
mes1(4) =  { 'It is very important  the DATA ' };
mes1(5) =  { 'MUST BE into a TXT or DAT file.' };
mes1(6) =  { '' };
mes1(7) =  { 'This file must only have three columns and' };
mes1(8) =  { 'the arrangement of these columns in file,'};
mes1(9) =  { 'it would be:'};
mes1(10) =  { ' - First column : X data; '};
mes1(11) =  { ' - Second column:  Y values and finally, '};
mes1(12) =  { ' - Third column : Z data.'};
mes1(13) =  { 'Please, check that the file does not have any'};
mes1(14) =  { '           row with strings or characters.'};
mes1(15) =  { '' };
mes1(16) =  { 'In command window, you can select the ' }; 
mes1(17) =  { '           Gridding method for your data.' };   
mes1(18) =  { ' ' };
mes1(19) = { 'This script was doing, using Matlab R2007a, and' };
mes1(20) = {             'it was tested, using Surfer v.8.02.' };
mes1(21) =  { ' ' };
mes1(22) = { 'It is not possible to assure this script woks with' };
mes1(23) = { '               anothers versions of Surfer.' };
mes1(24) =  { ' ' };
mes1(25) =  { 'This script was done by Gabriel RM. ' };
mes1(26) =  { ' ' };
window1 =questdlg(mes1,  'Important' , 'Next' , 'Cancel');
dirscript = cd;
clc; clear mes1

if strcmp( window1,'Next')
         clear window1
         [archivo,pathn] = uigetfile({ '*.txt', 'ASCII Data (*.txt)'; '*.dat', 'GS Data (*.dat)' }, 'Select the file with X, Y, Z data');
         if isequal(archivo,0)
                clear archivo pathn
                disp('User selected Cancel')
         else
                cdfiledatos = fullfile(pathn, archivo);
                [pathstr, name, exte, versn] = fileparts(archivo);
                [met,ext,extension] = choosemet;
                lonno = length(archivo);
                if (met >= 1 && met <= 9 ) && ( ext >= 1 && ext <= 3)
                        nombre = horzcat(archivo(1:1:lonno - 4), 'Grid', extension);
                        try
                                    if strcmp(exte, '.DAT')
                                           DataFile = csvread(cdfiledatos);
                                    else
                                           DataFile = textread(cdfiledatos);
                                    end
                                    xCols = [ ];
                                    yCols = [ ];
                                    zCols = [ ];
                                    ExclusionFilter = [ ];
                                    DupMethod = [ ];
                                    xDupTol = [ ];
                                    yDupTol = [ ];
                                    
                                    fprintf('Remember, Grid density limits are based on the amount of\n');
                                    fprintf('available memory in your computer; limited memory, very large\n');
                                    fprintf('data files, very dense grids, or any combination of these factors can\n');
                                    fprintf('increase gridding time. Grid density = (#Columns)(#Rows)\n');
                                    NumCols = input('Number of Columns  [ 50 < Colums < 5000 ]: ');
                                    NumRows= input('Number of Rows  [ 50 < Rows < 5000 ]: ');
                                    
                                    xMin = min(DataFile(:,1));
                                    xMax = max(DataFile(:,1));
                                    yMin = min(DataFile(:,2));
                                    yMax = max(DataFile(:,2));
                                    Algorithm = met;
                                    ShowReport = 0;
                                    SearchEnable =  [ ];
                                    SearchNumSectors = [ ];
                                    SearchRad1 = [ ];
                                    SearchRad2 = [ ];
                                    SearchAngle = [ ]; 
                                    SearchMinData = [ ];
                                    SearchDataPerSect = [ ];
                                    SearchMaxEmpty = [ ];
                                    FaultFileName = [ ];
                                    BreakFileName = [ ];
                                    AnisotropyRatio = [ ];
                                    AnisotropyAngle = [ ];
                                    IDPower = [ ];
                                    IDSmoothing = [ ];
                                    KrigType = [ ];
                                    KrigDriftType = [ ];
                                    KrigStdDevGrid = [ ];
                                    KrigVariogram = [ ];
                                    MCMaxResidual = [ ];
                                    MCMaxIterations = [ ];
                                    MCInternalTension = [ ];
                                    MCBoundaryTension = [ ];
                                    MCRelaxationFactor = [ ];
                                    ShepSmoothFactor = [ ];
                                    ShepQuadraticNeighbors = [ ];
                                    ShepWeightingNeighbors = [ ];
                                    ShepRange1 = [ ];
                                    ShepRange2 = [ ];
                                    RegrMaxXOrder = [ ];
                                    RegrMaxYOrder = [ ];
                                    RegrMaxTotalOrder = [ ];
                                    RBBasisType = [ ];
                                    RBRSquared = [ ];
                                    OutGrid = nombre;
                                    if strcmp(extension,'.dat')
                                           OutFmt = 3;
                                    else    
                                           OutFmt = 2;
                                    end
                                    SearchMaxData = [ ];
                                    KrigStdDevFormat = [ ];
                                    DataMetric = [ ];
                                    LocalPolyOrder = [ ];
                                    LocalPolyPower = [ ];
                                    TriangleFileName = [ ];
                        
                                    SurferApp = actxserver('Surfer.Application');
                        
                                    SurferApp.DefaultFilePath = dirscript;
                                    invoke(SurferApp, 'GridData' , cdfiledatos, xCols, yCols, zCols, ExclusionFilter, ...
                                                                                DupMethod, xDupTol,  yDupTol, NumCols, NumRows, ...
                                                                                xMin, xMax, yMin, yMax, Algorithm, ...
                                                                                ShowReport, SearchEnable, SearchNumSectors, ...
                                                                                SearchRad1,SearchRad2, SearchAngle, SearchMinData, ...
                                                                                SearchDataPerSect, SearchMaxEmpty, FaultFileName, ...
                                                                                BreakFileName, AnisotropyRatio, AnisotropyAngle, ...
                                                                                IDPower, IDSmoothing, KrigType, KrigDriftType, KrigStdDevGrid, ...
                                                                                KrigVariogram, MCMaxResidual, MCMaxIterations,  ...
                                                                                MCInternalTension, MCBoundaryTension, MCRelaxationFactor, ...
                                                                                ShepSmoothFactor, ShepQuadraticNeighbors, ...
                                                                                ShepWeightingNeighbors, ShepRange1, ShepRange2, ...
                                                                                RegrMaxXOrder, RegrMaxYOrder, RegrMaxTotalOrder, ...
                                                                                RBBasisType, RBRSquared, OutGrid, OutFmt, SearchMaxData, ...
                                                                                KrigStdDevFormat, DataMetric, LocalPolyOrder, LocalPolyPower, ...
                                                                                TriangleFileName );
                                    invoke(SurferApp, 'Quit');
                                    SurferApp.delete;
                                    disp('The grid file has been saved in the current path');
                        catch
                                    mist = lasterror;
                                    errormsg = mist.message;
                                    invoke(SurferApp, 'Quit');
                                    SurferApp.delete;
                                    herror =msgbox(errormsg, 'Fatal error',  'error');
                        end
                else
                        disp('User does not select any gridding method');
                end      
         end
else
        clear window1
        disp('The script was canceled');
end

function [met,ext,extension] = choosemet
    fprintf('Please, choose a Gridding method: \n');
    fprintf('  1) Kringing \n');
    fprintf('  2) Inverse distance to a power \n');
    fprintf('  3) Minimum Curvature \n');
    fprintf('  4) Modified Shepard_s mode \n');
    fprintf('  5) Natural Neighbor \n');
    fprintf('  6) Nearest Neighbor \n');
    fprintf('  7) Polynomial Regression \n');
    fprintf('  8) Radial Basis Function \n');
    fprintf('  9) Triangulation with linear interpolation \n');

    gridmet = input('What is your Gridding method?\n ');
    switch gridmet
                 case 1
                           met = 2;
                 case 2
                           met = 1;
                 case 3
                           met = 3;
                 case 4
                           met = 4;
                 case 5
                            met = 5;
                 case 6
                            met = 6;
                 case 7
                            met = 7;          
                 case 8
                            met = 8;
                 case 9
                            met = 9;
                 otherwise 
                           met = 0;
    end
    
    fprintf('\n');
    fprintf('Please, choose the type of file in which you will save  your grid: \n');
    fprintf('  1) [.txt] ASCII text file \n');
    fprintf('  2) [.dat] ASCII Data \n');
    fprintf('  3) [.grd] GS ASCII Grid File Format \n');
    ext = input('What is your type of file?\n ');
    switch ext
                 case 1
                              extension = '.txt';
                 case 2
                             extension = '.dat';
                 case 3
                             extension = '.grd';
                 otherwise 
                             extension = 0;
    end
    fprintf('\n');

Contact us at files@mathworks.com