Create a time-animated model in Surfer from multiple DAT files via automation

To create a time-animated model in Surfer, you'll need to generate a separate model for each time point and export each one as a GIF. This process can be streamlined by using a script like the one below, which efficiently creates a grid, contour map, and GIF for each DAT file in a specified directory. These individual GIFs can then be combined using a third-party animated GIF creator.

If you think a built-in time-animation feature in Surfer would be helpful for your work, please let us know!


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.
'==========================
'GridContourExportLoop.bas
'==========================
' This script loops through data files in a directory, gridding them and creating a
' contour map from the grid, then exporting the map as a GIF to be animated by
' a third-party program
'==========================

Sub Main

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

    '================================================================
    'USER DEFINED VARIABLES
    '================================================================
    'Files
    file_extension = "dat" 'extension of the input data files
    input_file_directory = "c:\program files\golden software\surfer\samples\"
    output_file_directory = "C:\Temp"
    export_format = "gif" 'extension of the output image file

    'Grid
    numrows = 200 'number of rows in the grid file
    numcols = 200 'number of columns in the grid file
    x = 1 'x column from the data file
    y = 2 'y column from the data file
    z = 3 'z column from the data file

    'Colormap
    colormap_min = 40 'minimum data value for the colormap
    colormap_max = 100 'maximum data value for the colormap
    grad = "Rainbow" 'color gradient preset for the colormap
    '================================================================

    'Initializes Surfer
    Dim SurferApp As Object, Plot, MapFrame, ContourLayer As Object
    Set SurferApp = CreateObject("Surfer.Application")
    SurferApp.Visible = True

    'If you didn't include a \ at the end of the file directories, these lines add it
    If  Len(input_file_directory) - InStrRev(input_file_directory, "\") <> 0 Then
        input_file_directory = input_file_directory + "\"
    End If
    If  Len(output_file_directory) - InStrRev(output_file_directory, "\") <> 0 Then
        output_file_directory = output_file_directory + "\"
    End If

    'This sets the file name equal to any DAT files in the directory
    data_file = Dir( input_file_directory  + "*." + file_extension)

    'Initialize a string to hold any error messages that occur during gridding
    Dim errorLog As String
    errorLog = ""

    'Loops through all of the data files in the directory
    While data_file <> ""
        
        'Creates a plot window
        Set Plot = SurferApp.Documents.Add

        'Defines the name of the output grid file as the data file name with a GRD extension
        grid_file = output_file_directory + Left(data_file, _
            Len(data_file) - (Len(data_file) - InStrRev(data_file, ".") + 1) ) + ".grd"
        
        Debug.Print grid_file

        ' Set up an error handling procedure to handle data that cannot be gridded
        On Error Resume Next ' Redirect to error handler on error

        'Grids the data
        SurferApp.GridData6 (DataFile:=input_file_directory + data_file, Algorithm:= srfKriging, _
            NumRows:=numrows, NumCols:=numcols, ShowReport:=False, OutGrid:=grid_file, _
            xCol:=x, yCol:=y, zCol:=z)

        ' Save any error messages
        If Err.Number <> 0 Then
            errorLog = errorLog & vbCrLf & vbCrLf & data_file & vbCrLf & Err.Description
            GoTo NextIteration
        End If

        'Resume normal execution
        On Error GoTo 0

        'Creates a contour map
        Set MapFrame = Plot.Shapes.AddContourMap(GridFileName:=grid_file)
        Set ContourLayer = MapFrame.Overlays(1)

        'Fill contours
        ContourLayer.FillContours = True

        'Assigns a preset to the colormap
        ContourLayer.FillForegroundColorMap.LoadPreset(grad)

        'Sets colormap data limits so all exports use the same color scale
        ContourLayer.FillForegroundColorMap.SetDataLimits (DataMin:=colormap_min, DataMax:=colormap_max)

        'Defines the name of the output GIF file as the data file name with a GIF extension
        file_name = Left(data_file, Len(data_file) - (Len(data_file) - InStrRev(data_file, ".") + 1) )
        export_file = output_file_directory + file_name + "." + export_format

        'Exports to a GIF
        Plot.Export2(FileName:=export_file, FilterID:=export_format)

        'Go to the next DAT file
        data_file = Dir()

    NextIteration: ' Label to jump to for the next iteration

        ' Reset error handler to None
        On Error GoTo 0

        'Go to the next DAT file
        data_file = Dir()

    Wend

    'Closes Surfer
    SurferApp.Quit

    'Report any errors that occurred during gridding
    If errorLog <> "" Then MsgBox "Some files could not be gridded:" & vbCrLf & errorLog

End Sub

Related Articles:

 

Updated August 2024

Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.