Use Python to automate Surfer

Surfer workflows can be automated using a Python script.  Several Python script snippets for common workflows are provided below.

Please note that we only offer support for automation of Surfer through Scripter, and we are unable to provide assistance in Python or any other programming language.

 

Start Surfer

import win32com.client def main(): 
app = win32com.client.Dispatch("Surfer.Application") 
plot = app.Documents.Add(1) 
app.Visible = True main()

Call the Surfer application

Surfer = win32com.client.Dispatch("Surfer.Application") 
Grid = Surfer.NewGrid() 
Grid.LoadFile("c:/test/test.grd", HeaderOnly=True) 
print "BlankValue" + str(Grid.BlankValue) 
print "NumCols" + str(Grid.NumCols)
print "NumRows" + str(Grid.NumRows)

Create and edit a vector map

import constants, Dispatch, CastTo 
import os.path def rgb(r, g, b): return b << 16 / g < 0: 
level.ShowLabel = True 
mapframe = plot.Shapes.AddVectorMap(GridFileName1=grid2, GridFileName2=grid3,  CoordSys=constants.srfVecPolar, AngleSys=constants.srfVecAngle) 
vectormap = CastTo(mapframe.Overlays(1), 'IVectorMap') 
orange = rgb(255, 102, 0) 
vectormap.ColorMap.SetNodes(Positions=[0.0, 1.0], Colors=[orange]*2)
vectormap.ColorScaleMethod = constants.srfVecMagnitude 
plot.Shapes.SelectAll() 
plot.Selection.OverlayMaps()
plot.SaveAs(output) 
plot.Export(exportfile)

Grid data

import glob 
import datetime 
#call this script like this: 
#\programs\python25\python.exe krig_data.py 
#get an instance of the Surfer application  
#Surfer = win32com.client.Dispatch('Surfer.Application') 
lFile = glob.glob('wl*.csv') 
for i in range(len(lFile)): 
#the min and max listed below are center coordinates while gdal_grid uses edge 
#coordinates 
sFilePrefix = lFile[i][:lFile[i].index('.')] 
Surfer.GridData(DataFile = lFile[i], xCol = 1, yCol = 2, zCol = 3, NumCols = 4133,  NumRows = 3017, xMin = 455355, xMax = 579315, yMin = 1954585, yMax = 2045065,  ShowReport = False, DupMethod = win32com.client.constants.srfDupAvg, OutFmt = win32com.client.constants.srfGridFmtBinary, OutGrid = '%s.grd' %sFilePrefix) print 'finished kriging %s at %s' % (lFile[i], datetim{source}

*This example was provided by a customer

Call Surfer, grid data, and create a contour map

def main():
    app = win32com.client.gencache.EnsureDispatch('Surfer.Application')
    Plot = app.Documents.Add(1)
    app.Visible = True
    DataFile = "C:\Program Files\Golden Software\Surfer 12\Samples\demogrid.dat"
    OutFile = "C:\Program Files\Golden Software\Surfer 12\Samples\outgrid.grd"
    app.GridData (DataFile=DataFile, Algorithm = win32com.client.constants.srfMinCurvature, NumRows=150, NumCols=150, ShowReport=False, OutGrid= OutFile)
        
    #Creates a contour map and assigns the map frame to the variable "MapFrame"
    MapFrame = Plot.Shapes.AddContourMap(GridFileName=OutFile)
    #Changes the limits and scale of the map
    MapFrame.SetLimits (xMin=0.5, xMax=4.5, yMin=0.5, yMax=3.5)
    MapFrame.xLength=6
    MapFrame.yLength=4
    #Declares ContourMap as an Object and assigns the contour map to variable "ContourMap"
    ContourMap = MapFrame.Overlays(1)
    
main()

Print all SRF files in a directory to PDF

  • The script below should be placed in a working directory with many SRF files and the script will open, print to PDF in a PDF directory, then close the file. No changes made to the file. You will overwrite PDFs if you run back to back.
    • This script was created by Corey Scheip – AECOM Technical Services of North Carolina
      import win32com.client   # If this fails, you must install Module pywin32
      import sys, os
      # Create Application Object
      surf = win32com.client.Dispatch("Surfer.Application")
      surf.Visible = False
      # Get list of files from working directory
      pwd = os.Path.dirname(sys.argv[0])
      fileList = os.listdir(pwd)
      # Check For PDF directory
      outDir = os.Path.join(pwd, 'pdf')
      If Not os.Path.isdir(outDir):
          os.mkdir(outDir)
          Print ('PDF Directory created')
      # Now go through files, Open SRF And Print To PDF
      For fil In fileList:
          Print 'Checking file... ' + fil
          If fil.endswith('.srf'):
              # Name To use For printing
              printName = os.Path.basename(fil)[:-4]
              # Open Surfer file
              Print ' ...opening file...'
              CurrentDoc = surf.Documents.Open(os.Path.join(pwd, fil))
              pdfFile = os.Path.join(outDir, printName + '.pdf')
              Print ' ...printing file...'
              CurrentDoc.Export2(pdfFile, False, True, "pdfv")
              # Close Surfer file
              CurrentDoc.Close()
      

 Updated October 2021

Was this article helpful?
28 out of 32 found this helpful

Comments

0 comments

Please sign in to leave a comment.