Create a time-animated model in Surfer from x, y, z1, z2, ... data 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 Z column in a data file. 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. 
'==========================
'GridZCols_ExportGIF_Loop.bas
'==========================
' This script loops through z columns in a single data file, gridding each one
' and creating a contour map from the grid, then exporting the grid 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
    data_dir = "C:\Program Files\Golden Software\Surfer\Samples\"       'directory where the data file is located
    data_file = "Sample1.dat"   'data file name
    grid_dir = "C:\Temp\"       'directory where grids will be saved
    gif_dir = "C:\Temp\"        'directory where gifs will be saved

    '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 = 259      'minimum data value for the colormap
    colormap_max = 5032     'maximum data value for the colormap
    grad = "Rainbow"        'color gradient preset for the colormap
    '================================================================

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

    'Opens the data file in Surfer's worksheet and sets the worksheet range to row 1
    Dim Wks As Object, WksRange As Object
    Set Wks = SurferApp.Documents.Open(data_dir+data_file)
    Set WksRange = Wks.Rows(1, 1)

    'Initializes variables used in the for loop below
    Dim Plot As Object, MapFrame As Object, ContourLayer As Object

    'Loops through all of the z columns in the data file
    'This assumes columns A and B are x and y, and columns C through the end of the file are z's
    For i=3 To WksRange.ColumnCount

        'Creates a plot window
        Set Plot = SurferApp.Documents.Add

        'Defines output grid file directory & name
        grid_file   = grid_dir + Left(data_file, Len(data_file)-(Len(data_file)-InStrRev(data_file,".")+1) ) + "_zcol"+Str(i)+".grd"

        'Grids the data file
        SurferApp.GridData6(DataFile:= data_dir + data_file, xCol:=1, yCol:=2, zCol:=i, Algorithm:=srfKriging, ShowReport:=False, OutGrid:=grid_file, OutFmt:=srfGridFmtS7)
        Debug.Print "zCol = "+Str(i)

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

        'Fills 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
        export_file = gif_dir + Left(data_file, Len(data_file)-(Len(data_file)-InStrRev(data_file,".")+1) ) + "_zcol"+Str(i)+ ".gif"

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

        'Closes the plot without saving 
        Plot.Close(SaveChanges:=srfSaveChangesNo)
    Next i

'Closes Surfer
    SurferApp.Quit
End Sub

Related Articles:

Updated August 2024

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

Comments

0 comments

Please sign in to leave a comment.