Do you have an example of automating Surfer with Python?

Surfer can be called from a Python script. Although writing scripts in python are not officially supported, we do have some examples to share.


  1. Here is an example that shows how to start Surfer:
    import win32com.client def main(): 
    app = win32com.client.Dispatch("Surfer.Application") 
    plot = app.Documents.Add(1) 
    app.Visible = True main()

  2. Here is an example that shows how to call Surfer:
    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)

  3. Here is an example of how to 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 

  4. Here is a customer example of how to grid data:
    import glob 
    import datetime 
    #call this script like this: 
    #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 
    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}


  5. Here is an example on how to call Surfer and grid your data file to 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)
        #Declares ContourMap as an Object and assigns the contour map to variable "ContourMap"
        ContourMap = MapFrame.Overlays(1)


  6. 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):
          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

 Updated December 19, 2018

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


Please sign in to leave a comment.