Extract a subset of data within certain x,y limits with Surfer automation

Data found online or received from outside sources often covers a much larger area than a user is interested in mapping.  There are many methods available for dealing with this situation and one of them is to crop the original file to the area of interest.  The script below prompts the user for data extents and then extracts data from a data file within the specified XY limits to a new data file.

To run this script:

  1. Copy the script below, or download the attached BAS file: DataExtract.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.


'DataExtract.bas extracts data within the limits specified by
' xmin, xmax, ymin, ymax.  Modify the Input statement if not
' 3 columns of comma-delimited data.

Sub Main
	Debug.Print "----- ";Time;" -----"
  file1 = GetFilePath("Demogrid.dat","dat", _
  	"C:\Program Files\Golden Software\Surfer15\Samples\","Open Data File")
  file2 = Left(file1,Len(file1)-4) & "_Extract.dat"
	Begin Dialog UserDialog 400,161,"Limits" ' %GRID:10,7,1,1
		GroupBox 20,28,360,77,"Limits",.GroupBox1
		TextBox 80,42,110,21,.xMin
		TextBox 80,70,110,21,.yMin
		TextBox 260,42,110,21,.xMax
		TextBox 260,70,110,21,.yMax
		Text 30,42,30,14,"xMin",.Text1
		Text 210,42,40,14,"xMax",.Text2
		OKButton 60,126,100,21
		CancelButton 220,126,90,21
		Text 30,77,40,14,"yMin",.Text3
		Text 210,77,40,14,"yMax",.Text4
	End Dialog
	Dim dlg As UserDialog

	'Initializes the dialog controls
	dlg.xMin = "1.2"
	dlg.xMax = "3.4"
	dlg.yMin = "2.5"
	dlg.yMax = "5.2"
	On Error GoTo errmsg
	Dialog dlg

  xmin = 2.2
  xmax = 5.7
  ymin = 4.1
  ymax = 6.5
  Open file1 For Input As #1
  Open file2 For Output As #2
  While Not EOF(1)
    'Input #1, a, b  'ASCII comma delimited 2-column data only.
    Input #1, a, b, c  'ASCII comma delimited 3-column data only.
    If a >= xmin And a <= xmax And b >= ymin And b <= ymax Then
      Print #2,a;",";b;",";c
      numpts = numpts+1
    End If
  Debug.Print numpts&" points written to " & file2
  MsgBox(numpts & " points written to " & vbCrLf & file2)
End Sub


Updated November 2021

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



Please sign in to leave a comment.