Scripter Example: Read All Data Files in Directory

Scripter is a built-in application to several of our programs that allows a user to automate tasks by running a series of commands using a Visual Basic-like interface. Scripter is available in Surfer, Grapher, MapViewer, and Voxler. Using Scripter, you are able to write scripts to perform almost every command available in the program! This can be very useful if you are trying to process many different datasets to produce the same maps or graphics.

To give you an idea of how you can use Scripter, below I have shown an example script for Surfer to read all the data files in a directory and create grid files from that data. For a general overview of Scripter, and a description of the Scripter interface, I would recommend reviewing the article linked below:

Golden Software Newsletter: Automation Debugging Tips and Tricks

The basic Scripter syntax to read all of the DAT files from a directory is shown in the script excerpt below. Please note that the lines of the script in green are comments and not read as commands in the script. 

'Set the directory and file name.
Directory = "C:\Users\ENTER PATH"
Files = Dir(Directory + "*.dat")

‘While reading Files Do
While Files <> ""
    file = (Directory + Files)
    'Enter in the commands that you would like to perform on each file here
    'Get next DAT file in folder
    Files = Dir( )

You can use the syntax above to automate the processing of most of your data in Surfer. Below I have provided an example that uses this method to automate the gridding of data files in Surfer.

The example script, GridAll.bas, uses this method to create a grid file from all of the XYZ data files that are included in the specified directory. Below is the script that uses the While Files <> command to perform operations on each data file in a specified directory.


' GridAll.bas grids all of the specified type of data file in the specified 
' directory.

' You must specify the file extension and file directory below in the script.
' Grid files are saved in the same directory.

' If you run the script and nothing appears to happen, make sure the 
' file_directory is valid.

' See bottom of script for common errors

Sub Main
''''''''''''' User Variables ''''''''''''''''''
file_extension  = "dat"
file_directory  = "C:\samples\dat\"
Set surf = CreateObject("surfer.application")
surf.Visible = True

' Make sure the file extension has no extra . and the data directory has a 
' trailing \
file_extension  = LCase(Right(file_extension,(Len(file_extension) - InStrRev(file_extension,"."))))
If Len(file_directory)-InStrRev(file_directory,"\") <> 0 Then file_directory = file_directory + "\"

data_file = Dir( file_directory  + "*." + file_extension)

OnErrorGoTo FileError
While data_file <> ""
    'Define output grid file directory & name
    grid_file   = file_directory + Left(data_file, Len(data_file)-(Len(data_file)-InStrRev(data_file,".")+1) ) + ".grd"

    'Grid the data file with the current Surfer defaults 
    'but do not fill the screen with grid reports)
    surf.GridData(DataFile:= file_directory + data_file, ShowReport:=False, OutGrid:=grid_file)

    'You can uncomment the line below to make explicit changes to the 
    'Gridding options. Make sure to comment out the line above or files will be gridded twice.

    'For more information about using these options and their
    'definitions please see the Surfer Help File, available by using the 
    'menu command HELP | ABOUT and then searching for "GridData".

    'surf.GridData(DataFile:= file_directory + data_file, xCol:=1, yCol:=2, zCol:=3, Algorithm:=srfKriging, ShowReport:=False, SearchEnable:=True, KrigStdDevGrid:=SurferApp.Path+"\Samples\StdDev.grd", OutGrid:=grid_file, OutFmt:=srfGridFmtS7)
    Debug.Print data_file
    data_file = Dir() 'get next file


' Print a meaningful error message for each file that did not grid correctly
    Debug.Print "Error:    " + data_file + "          " + Err.Description
' Helpful error hints:
' a) If the script appears to do nothing, make sure file_directory is valid.
' b) "xMin must be < xMax" usually means there is no griddable data.  Check 
' if there really is data, if it is in columns ABC, and that it is formatted as 
' number and not as text or general.
' c) "Insufficient data in worksheet" means there are less than three 
' griddable Z values in the worksheet.
' d) "Inadequate data (all data lie in a horizontal plane)" means that all Z 
' values are the same.
' e) "Unknown worksheet import format" the file format was incompatible with 
' Surfer.  Common if the file was an Excel 2007 spreadsheet and your Surfer 
' version is 8 or lower.


To illustrate what this script does, I have tested this script with the data files (*.dat) that are included in the Surfer 12 Samples folder, typically located here: 

C:\Program Files\Golden Software\Surfer 12\Samples\

This script grids each of the data files and saves them into the same directory. Once gridded, you can create grid based maps using the data. To illustrate this, I have included a few screenshots of the contour maps that are generated from these grid files. 

demogrid.dat | demogrid.grd

Diablo Example.dat | Diablo Example.grd

Sample1.dat | Sample1.grd

TutorWS.dat | TutorWS.grd

In addition to the GridAll script, you can download other Free Scripts from the Golden Software Support site and in the samples directory for each program.

I hope that this has been a quick and helpful example of how you could use Scripter to improve your workflow and efficiency in Surfer. If you have any questions about writing scripts, using the Scripter application, or other uses of Surfer, please do not hesitate to contact Golden Software Support.