Golden Software Blog

Helping you learn more about the latest product information, tips, tricks, techniques, and customer stories so you can visualize data and communicate results with ease.

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( )
Wend

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
Debug.Clear
''''''''''''' 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

Wend
surf.Quit
ExitSub

' Print a meaningful error message for each file that did not grid correctly
    Debug.Print "Error:    " + data_file + "          " + Err.Description
    ResumeNext
EndSub
' 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 website. Click on the link on each script’s name to download the script.  

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.

 

 

Comments 2

Guest - chenzhiquan on Saturday, 03 December 2016 00:01

I wanna know how to solve ' d) Inadequate data (all data lie in a horizontal plane) means that all Z ' values are the same in the Helpful error hints,I use c# and surfer12 to make a contourmap.My griddata is:
app.GridData(fileDir + fileName + @".dat", 1, 2, 3, Type.Missing, //5 Type.Missing, Type.Missing, Type.Missing, 502, 558, //10 xMin, xMax, yMin, yMax, 2, //15 false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //20 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //25 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //30 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //35 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //40 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //45 Type.Missing, Type.Missing, Type.Missing, Type.Missing, fileDir + fileName + @".grd", //50 SrfGridFormat.srfGridFmtAscii, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //55 Type.Missing, Type.Missing); Can you help me /

I wanna know how to solve ' d) Inadequate data (all data lie in a horizontal plane) means that all Z ' values are the same in the Helpful error hints,I use c# and surfer12 to make a contourmap.My griddata is: app.GridData(fileDir + fileName + @".dat", 1, 2, 3, Type.Missing, //5 Type.Missing, Type.Missing, Type.Missing, 502, 558, //10 xMin, xMax, yMin, yMax, 2, //15 false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //20 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //25 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //30 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //35 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //40 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //45 Type.Missing, Type.Missing, Type.Missing, Type.Missing, fileDir + fileName + @".grd", //50 SrfGridFormat.srfGridFmtAscii, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //55 Type.Missing, Type.Missing); Can you help me /
Guest - Leslie McWhirter on Monday, 05 December 2016 11:24

Hello chenzhiquan,

We don't support scripting from any language other than Scripter, so I cannot tell you specifically what's wrong with your C# script if anything is (though all of the Type.Missing lines seem strange), but I can tell you that the "Inadequate data (all data lie in a horizontal plane)" error indicates all of the z values are the same. Surfer does not support grids where all of the z values are the same. If you want to create a horizontal (planar) grid, you will need to change some of the z values just slightly in the data file before gridding. So for example if all z are 0, you may change a few to 0.0001 and a few to -0.0001.

Thanks,
Leslie

Hello chenzhiquan, We don't support scripting from any language other than Scripter, so I cannot tell you specifically what's wrong with your C# script if anything is (though all of the Type.Missing lines seem strange), but I can tell you that the "Inadequate data (all data lie in a horizontal plane)" error indicates all of the z values are the same. Surfer does not support grids where all of the z values are the same. If you want to create a horizontal (planar) grid, you will need to change some of the z values just slightly in the data file before gridding. So for example if all z are 0, you may change a few to 0.0001 and a few to -0.0001. Thanks, Leslie
Guest
Wednesday, 22 November 2017

Captcha Image

Subscribe To Our Blog

Most Popular

This week's new feature series is a tutorial on how to use the new coordinate system dialogs in MapV...
Over the years, one of the most common questions asked is “How can I get my contour map out of Surfe...
For more than 20 years, Surfer mapping software has been used by representatives of nearly all indus...

Exceeding expectations

Go to top