Grid all data files in a directory at once with Surfer automation

If you're looking to batch grid multiple data files in a single directory, this script is exactly what you need! The script selects files based on a user-specified extension (e.g., .dat), grids each file using Surfer's default settings, and saves the resulting grid files in a specified output directory. For more control over the gridding process (e.g., changing the gridding algorithm or specifying different data columns), explore the full range of parameters available in the GridData6 method!

To run this script:

  1. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer.
  2. Double click on Scripter.exe to launch Scripter.
  3. Copy and paste the script below into Scripter's code window (be sure to delete any existing lines first), or simply download the attached BAS file and open it in Scripter.
  4. Update the values in the USER-DEFINED VARIABLES section near the top of the script as needed. If you skip this step, the script will still run using sample data.
  5. Click Script | Run to run the script.

' This script grids all data files of the specified type in the specified directory.
' You must define the input file extension and directory under "USER-DEFINED VARIABLES"
' Grid files (*.grd) are saved in the specified output directory.
'
' Common errors and troubleshooting:
' - If the script appears to do nothing, ensure the input_directory is valid.
' - "xMin must be < xMax" usually means there is no griddable data. Check that data exists, is in columns ABC, and is formatted as numbers (not text or general).
' - "Insufficient data in worksheet" means there are fewer than three griddable Z values.
' - "Inadequate data (all data lie in a horizontal plane)" indicates that all Z values are the same.
' - "Unknown worksheet import format" suggests the file format is incompatible with Surfer, commonly an issue with Excel 2007 files in Surfer versions 8 or lower.

Sub Main
    
    'Clear any output in the Imediate window.
    Debug.Clear

    '==========================================================================
    'USER-DEFINED VARIABLES
    '==========================================================================
    file_extension  = "dat"
    input_directory = "C:\Program Files\Golden Software\Surfer Beta\Samples\"
    output_directory = "C:\Temp"
    '==========================================================================

    'Initialize Surfer
    Set SurferApp = CreateObject("Surfer.Application")

    'Make sure the file extension has no extra . and the data/output directories has a trailing \
    file_extension  = LCase(Right(file_extension,(Len(file_extension) - InStrRev(file_extension,"."))))
    If  Len(input_directory) - InStrRev(input_directory, "\") <> 0 Then input_directory = input_directory + "\"
    If  Len(output_directory) - InStrRev(output_directory, "\") <> 0 Then output_directory = output_directory + "\"

    'Get the first data file in the specified directory that matches the file extension
    data_file = Dir( input_directory  + "*." + file_extension)

    'Direct the script to the FileError section if an error occurs during file processing
    On Error GoTo FileError

    'Execute the following while loop until there are no more data files in input_directory
    While data_file <> ""
        
        'Print the name of the current data file being processed in the Immediate window
        Debug.Print data_file

        'Define the path and file name of the output grid       
        grid_file = output_directory + Left(data_file, Len(data_file) - (Len(data_file) - InStrRev(data_file, ".") + 1) ) + ".grd"

        'Grid the data file with the current Surfer defaults (but do not fill the screen with grid reports)
        SurferApp.GridData6(DataFile:= input_directory + data_file, ShowReport:=False, OutGrid:=grid_file)

        'Get the next file in the directory that matches the specified file extension
        data_file = Dir()
    Wend

    'Close the Surfer application
    SurferApp.Quit

    'Display any error messages in a message box
    If errorDescriptions <> "" Then
        MsgBox errorDescriptions, vbInformation, "One or more data files could not be gridded"
    End If

    'Exit the current Sub procedure
    Exit Sub

    'Print a meaningful error message in the Immediate window for each file that did not grid correctly
    FileError :
    Debug.Print  "Error: " & Err.Description
    errorDescriptions = errorDescriptions & data_file & vbCrLf & Err.Description & vbCrLf & vbCrLf
    Resume Next

End Sub

Related articles:

Updated October, 2024

Was this article helpful?
3 out of 8 found this helpful

Comments

0 comments

Please sign in to leave a comment.