Follow

Can I write a Surfer script to grid and contour many data files with consecutive names?

Yes, you can write a script for looping through the process of gridding, contouring, and printing out separate data files that are saved with consecutive names (i.e. datafile1.dat, datafile2.dat, etc). A sample script demonstrating this is shown below.

To run this script:

  1. Copy the script below, or download the attached BAS file: Loop-ConsecutiveDataFileName.bas.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer 15\Scripter.
  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.
     

*********

Sub Main
	
	Dim SurferApp As Object
	Set SurferApp = CreateObject("Surfer.Application")
	SurferApp.Visible = True

'This starts a loop to grid, contour, and print out 5 separate data files
'that have consecutive names such as datafile1.dat, datafile2.dat, etc.

	For i = 1 To 3

		'Opens a new plot document
  			Dim Plot As Object
  			Set Plot = SurferApp.Documents.Add

		'Specifies the path to the data file
  			Path = "c:\temp\SAMPLE"+Format(i)

		'Create a grid from the data file
  			SurferApp.GridData(DataFile:=Path+".DAT", OutGrid:=Path+".grd")

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

  		'Prints the plot document
			Plot.PrintOut

	Next i

End Sub

 

Updated March 20, 2018

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

4 Comments

  • 0
    Avatar
    Kien Pham

    The code works great. Thank you.

    Is there a way to have the contours added to the same plot insteading of generating new plot for during each i in the loop?

    Is there a way to modify the placement of the contour in the plot window within the loop?

  • 1
    Avatar
    Leslie McWhirter

    Hi Kien,

    You could us something like this to add the contour layers to the existing map:

    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
    
        '==================================
        '               User-defined variables
        '==================================
            Path$ = "c:\temp\SAMPLE\datafile"
            Num = "1"
            FileFmt$ = ".dat"
        '==================================
    
        'Create ther first grid from the first data file
            SurferApp.GridData(DataFile:=Path$+Num+FileFmt$, OutGrid:=Path$+Num+".grd")
    
        'Creates a contour map from the grid file
            Dim Map As Object
            Set Map = Plot.Shapes.AddContourMap(GridFileName:=Path$+Num+".grd")
    
    'This starts a loop to grid teh 4 remaining data files'that have consecutive names
    ' (such as datafile1.dat, datafile2.dat,) then add them as contour layers to the existing map
        For i = 2 To 5
    
            'Specifies the path to the data file
                Path = "c:\temp\SAMPLE\datafile"+Format(i)
    
            'Create a grid from the data file
                SurferApp.GridData(DataFile:=Path+FileFmt$, OutGrid:=Path+".grd")
    
            'Creates a contour map from the grid file
                Plot.Shapes.AddContourLayer(Map:= Map, GridFileName:=Path+".grd")
    
        Next i
    
        'Prints the plot document
            Plot.PrintOut
    
    End Sub
    

     

     

    You could add a line in the loop to move the map (see below), but it doesn't make sense to do so for the above script, since you just have one map rather than multiple maps:

     

    Map.Left = Map.xLength+i
    Map.Top =Map.yLength+i

     

     

    Thanks,

    Leslie

     

    Edited by Leslie McWhirter
  • 0
    Avatar
    Kien Pham

    I am looking to add to this loop the automation of transformation, setting the x scale and z scale within each i data set. Can someone help?

  • 1
    Avatar
    Leslie McWhirter

    Hi Kien,

    You can use the zLength command or the zMapPerPU command for the MapFrame object to set the Z scale, and you can use the xLength command or the xMapPerPU command to set the X scale. Put these in the loop with a defined number to set the same size for all maps, or use the loop variable (i) in an equation to vary the size.

    Thanks,

    Leslie

Please sign in to leave a comment.