Grid data and create contour maps for selected data files with Surfer automation

Batch processing of data is one of the most common uses of automation in Surfer.  This article contains a sample script for looping through the process of selecting a data file, gridding the data, creating a map, and saving the SRF file.

If you have many data files in the same directory, this script can be edited to automatically choose the next available file as shown in this article: Grid all data files in a directory at once in Surfer automation

To run this script:

  1. Copy the script below, or download the attached BAS file: Loop-SelectDataFile.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.
  6. Click Script | Run to run the script.


'Loop-SelectDataFile.bas loops the process of selecting a data file, gridding the data,
'creating a map, and saving the SRF file.
'By default, the X, Y and Z columns in the data file must be in columns A, B and C.
'You can change this in the GridData line.

Sub Main
	Dim SurferApp As Object
	Set SurferApp = CreateObject("Surfer.Application")
	SurferApp.Visible = True
	'Opens a new plot document
		Dim Plot As Object
		Set Plot = SurferApp.Documents.Add

	'Prompt the user for the name of the data file to process.
		InFile = GetFilePath(" ","DAT;TXT;CSV;XLS",CurDir(), "Select Data File", 0)
		If InFile = "" Then Exit Do

	'Get file name without the extension (for example, "week5.txt becomse "week5")
		BaseName = InFile
		ExtStart = InStrRev(InFile, ".")
		If ExtStart >1 Then BaseName = Left(InFile,ExtStart-1)

	'Create a grid from the specified data file
		GridFile = BaseName + ".grd"
		SurferApp.GridData (DataFile:= InFile, OutGrid:= GridFile)

	'Creates a contour map from the grid file
		Dim Map As Object
		Set Map = Plot.Shapes.AddContourMap(GridFileName:=GridFile)

	'Save to SRF
		Plot.SaveAs (FileName:=BaseName+".srf")

	'Closes the plot

End Sub


Updated November 2021

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



Please sign in to leave a comment.