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

To create a time animated model in Surfer each time point will need to be modeled separately and then exported to GIF for use with an online animated GIF creator.  This can be done fairly quickly using a script like the one below which creates a grid, contour map, and GIF for each DAT file in a specified directory. 


If you would find a built in time-animation feature in Surfer helpful with your work, please let us know.

To run this script:

  1. Copy the script below, or download the attached BAS file: GridContourExportLoop.bas.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press DELETE.
  5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
    Update the User-defined variables section with your own files and preferences.
  6. Click Script | Run to run the script. 

' This script loops through data files in a directory, 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

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

  file_extension = "dat"		'extension of the data files in the directory
  file_directory = "c:\program files\golden software\surfer 16\samples\"		'directory containing all of the data files
  export_format = "gif"		'extension of the output image file

  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_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
'If you didn't include a \ at the end of the file_directory, this line adds it
  If  Len(file_directory)-InStrRev(file_directory,"\") <> 0 Then file_directory = file_directory + "\"

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

'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	= file_directory + Left(data_file, Len(data_file)-(Len(data_file)-InStrRev(data_file,".")+1) ) + ".grd"
      Debug.Print grid_file

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

    '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

    '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	= file_directory + Left(data_file, Len(data_file)-(Len(data_file)-InStrRev(data_file,".")+1) ) + "." + export_format

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

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

'Closes Surfer
End Sub

Related Articles:


Updated November 2021

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



Please sign in to leave a comment.