Application Object Methods Script
This script demonstrates all the methods in the Application object. If you wish to run the script, open Scripter and then open ApplicationObjectMethods.bas from the Surfer Samples folder.
'ApplicationObjectMethods.bas demonstrates the methods of the Surfer Application Object.
'See ApplicationObjectProperties.bas for the properties of the Surfer Application Object.
Sub Main
'Get existing Surfer instance, or create a new one If none exists.
On Error Resume Next 'Turn off error reporting.
Set SurferApp = GetObject(,"Surfer.Application")
If Err.Number<>0 Then
Set SurferApp = CreateObject("Surfer.Application")
SurferApp.Documents.Add(srfDocPlot)
End If
On Error GoTo 0 'Turn on error reporting.
'Version returns the full version number of Surfer
Debug.Print "-----------------------------------------------------"
Debug.Print "Surfer ";SurferApp.Version;" Application Object Properties"
Debug.Print "-----------------------------------------------------"
'Visible sets the visiblity of the Surfer applicaiton
SurferApp.Visible = True
path1 = SurferApp.Path +"\samples\"
Debug.Print SurferApp.Documents(1).Type = srfDocPlot
If SurferApp.Documents(1).Type = srfDocPlot Then
Set plotdoc1 = SurferApp.Documents(1)
Else
Set plotdoc1 = SurferApp.Documents.Add
End If
Set plotwin1 = plotdoc1.Windows(1)
plotwin1.Activate
plotwin1.Zoom(srfZoomPage)
Set shapes1 = plotdoc1.Shapes
AppActivate "Surfer "
plotdoc1.PageSetup.Orientation = srfLandscape
'-----------------------------------------------
'CrossValidate2 performs cross validation.
'-----------------------------------------------
Debug.Print "CrossValidate2"
SurferApp.CrossValidate2(DataFile:= path1+"sample1.dat", _
xCol:=1, yCol:=2, zCol:=3, _
Algorithm := srfKriging, _
ShowReport := False, _
ResultsFile:=path1+"CrossValidate.dat")
If shapes1.Count > 0 Then
For Each shp In shapes1
shp.Delete
Next
End If
shapes1.AddText(1,1,"CrossValidate2").Font.Size = 40
Wait 1
Set Wks = SurferApp.Documents.Open(path1+"CrossValidate.dat")
Wait 1
For Each shp In shapes1
shp.Delete
Next
SurferApp.Windows(1).Activate 'Activates the first open window
'-----------------------------------------------
' Use Grid Blank to blank a grid file using a BLN file.
'-----------------------------------------------
Debug.Print "GridBlank"
SurferApp.GridBlank (InGrid := path1+"demogrid.grd", _
BlankFile := path1+"demorect.bln", _
OutGrid := path1+"demoblanked.grd", _
OutFmt := srfGridFmtS7)
If shapes1.Count > 0 Then
For Each shp In shapes1
shp.Delete
Next
End If
shapes1.AddText(1,1,"GridBlank").Font.Size = 40
Set mapframe1 = shapes1.AddContourMap(path1+"demoblanked.grd")
Set contour1 = mapframe1.Overlays("Contours-demoblanked.grd")
'-----------------------------------------------
'Calculate a new grid with slopes in degrees using
'GridCalculus and helens2.grd.
'-----------------------------------------------
Debug.Print "GridCalculus"
SurferApp.GridCalculus(InGrid := path1+"helens2.grd", _
Operation := srfGCSlope, _
OutGrid := path1+"helens2slope.grd", _
OutFmt := srfGridFmtS7)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridCalculus Slope").Font.Size = 40
Set mapframe1 = shapes1.AddColorReliefMap(path1 +"helens2slope.grd")
Set colorReliefLayer1 = mapframe1.Overlays("Color Relief-helens2slope.grd")
'-----------------------------------------------
'Use GridConvert to convert a grid to an XYZ data file.
'-----------------------------------------------
Debug.Print "GridConvert"
SurferApp.GridConvert (InGrid := path1+"\demogrid.grd", _
OutGrid := path1+"\demogrid2.dat", _
OutFmt := srfGridFmtXYZ)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridConvert").Font.Size = 40
Set mapframe1 = shapes1.AddClassedPostMap(path1+"demogrid2.dat")
Set classedpost1 = mapframe1.Overlays("Classed Post-demogrid2.dat")
'-----------------------------------------------
'GridExtract extracts a subset of a grid.
'-----------------------------------------------
Debug.Print "GridExtract"
SurferApp.GridExtract (InGrid := path1+"demogrid.grd", _
r1:=1, r2:=26, c1:=1, c2:=25, _
OutGrid := path1+"DemoExtract.grd", _
OutFmt := srfGridFmtS7)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridExtract").Font.Size = 40
Set mapframe1 = shapes1.AddContourMap(path1+"demoextract.grd")
Set contour1 = mapframe1.Overlays("Contours-demoextract.grd")
'------------------------------------------------------
'GridFilter in Surfer 8 and above (GridMatrixSmooth in Surfer 7).
'------------------------------------------------------
Debug.Print "GridFilter"
If Left(SurferApp.Version, 1) = "8" Then
SurferApp.GridFilter(path1+"demogrid.grd", _
srfFilterEmbSouthwest, _
outgrid:=path1+"DemoFilter.grd")
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridFilter Emboss Southwest").Font.Size=40
shapes1.AddContourMap(path1+"demofilter.grd")
End If
'-----------------------------------------------
'Use GridFunction to create a new grid from a function.
'-----------------------------------------------
Debug.Print "GridFunction"
SurferApp.GridFunction( Function:="z=(pow(x,2)+pow(y,2))*(sin(8*atan2(x,y)))", _
xMin:=-25, xMax:=25, xInc:=1, _
yMin:=-25, yMax:=25, yInc:=1, _
OutGrid := path1 + "GridFunction.grd", _
OutFmt := srfGridFmtS7)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridFunction").Font.Size = 40
Set mapframe1 = shapes1.AddWireframe(path1+"GridFunction.grd")
Set wireframe1 = mapframe1.Overlays("3D Wireframe-GridFunction.grd")
'-----------------------------------------------
'Use GridData2 to grid two columns from a data file
'and GridMath to compare the two grid files.
'-----------------------------------------------
Debug.Print "GridMath"
SurferApp.GridData2(DataFile := path1+"sample1.dat", _
xCol:=1, yCol:=2, zCol:=3, _
Algorithm := srfKriging, _
ShowReport := False, _
OutGrid := path1+"Sample1a.grd", _
OutFmt := srfGridFmtS7)
SurferApp.GridData2(DataFile := path1+"sample1.dat", _
xCol:=1, yCol:=2, zCol:=4, _
Algorithm := srfKriging, _
ShowReport := False, _
OutGrid := path1+"Sample1b.grd", _
OutFmt := srfGridFmtS7)
SurferApp.GridMath(Function := "C = A - B", _
InGridA := path1+"Sample1a.grd", _
InGridB := path1+"Sample1b.grd", _
OutGridC := path1+"Sample1a-b.grd", _
OutFmt := srfGridFmtS7)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridMath").Font.Size = 40
Set mapframe1 = shapes1.AddContourMap(path1+"sample1a.grd")
With mapframe1
.xLength = .xLength/2
.yLength = .yLength/2
.Top = 8
.Left = 1
.Axes("Bottom Axis").Title = "Sample1a.grd"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
Set mapframe2 = shapes1.AddContourMap(path1+"sample1b.grd")
With mapframe2
.xLength = .xLength/2
.yLength = .yLength/2
.Top = 8
.Left = 5
.Axes("Bottom Axis").Title = "Sample1b.grd"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
Set mapframe3 = shapes1.AddContourMap(path1+"sample1a-b.grd")
With mapframe3
.xLength = .xLength/2
.yLength = .yLength/2
.Top = 4
.Left = 5
.Axes("Bottom Axis").Title = "Sample1a-b.grd"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
Wait 1
'----------------------------------------------------
'GridMosaic combines two or more adjacent grid files.
'----------------------------------------------------
Debug.Print "Grid Mosaic"
'Array of Grids
Dim gridarray(1 To 2) As String
gridarray(1)=path1+"conifer.grd"
gridarray(2)=path1+"golden.grd"
'Mosaic the grids in the gridarray
SurferApp.GridMosaic (InGrids:= gridarray, OutGrid:=path1+"GridMosaic.grd")
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridMosaic").Font.Size = 40
shapes1.AddContourMap(path1+"GridMosaic.grd")
Wait 1
'-----------------------------------------------
'GridResiduals2 calculates the difference between the grid
'and data file.
'-----------------------------------------------
Debug.Print "GridResiduals2"
SurferApp.GridData2(path1+"Demogrid.dat", _
algorithm:=srfRegression, _
RegrMaxXOrder:=2, RegrMaxYOrder:=2, _
RegrMaxTotalOrder:=4, _
ShowReport:=False, _
outgrid:=path1+"DemoRegression.grd")
SurferApp.GridResiduals2(InGrid := path1+"DemoRegression.grd", _
DataFile := path1+"demogrid.dat", _
xCol:=1, yCol:=2, zCol:=3, ResidCol:=4)
For Each shp In shapes1
shp.Delete
Next
Set wksdoc1 = SurferApp.Documents.Open(path1+"demogrid.dat")
wksdoc1.SaveAs(path1+"GridResiduals2.dat")
Wait 1
SurferApp.GridData2(path1+"demogrid.dat",zcol:=3, _
algorithm:=srfKriging, _
ShowReport:=False, _
outgrid:=path1+"DemoResiduals.grd")
Wait 1
SurferApp.Windows(1).Activate 'Activates the first open window
shapes1.AddText(1,1,"GridResiduals2").Font.Size=40
Set mapframe1 = shapes1.AddClassedPostMap(path1+"demogrid.dat", _
zcol:=3)
Set classedpost1 = mapframe1.Overlays("Classed Post-demogrid.dat")
Set mapframe2 = shapes1.AddContourMap(path1+"demoresiduals.grd")
mapframe1.Select
mapframe2.Select
Set mapframe3 = plotdoc1.Selection.OverlayMaps
mapframe3.Overlays("Classed Post-demogrid.dat").SetZOrder(srfZOToFront)
'-----------------------------------------------
'GridSlice calculates the XYZ values and accumulated
'distance along a cross-section profile.
'-----------------------------------------------
Debug.Print "GridSlice"
SurferApp.GridSlice (InGrid := path1+"demogrid.grd", _
BlankFile := path1+"DemoSlice.bln", _
OutDataFile := path1+"Demoslice.dat", _
OutsideVal:=-8888, BlankVal:=-9999)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridSlice").Font.Size = 40
Set mapframe1 = shapes1.AddContourMap(path1+"demogrid.grd")
Set mapframe2 = shapes1.AddBaseMap(path1+"demoslice.bln")
mapframe1.Select
mapframe2.Select
plotdoc1.Selection.OverlayMaps
With mapframe1
.xLength = 0.67 * .xLength
.yLength = 0.67 * .yLength
.Top = 8
.Left = 3
End With
Set mapframe2 = shapes1.AddPostMap(path1+"demoslice.dat", _
xcol:=4, ycol:=3)
With mapframe2
.xLength = 4
.yLength = 2
.Top = 4
.Left = 3
End With
'-----------------------------------------------
'GridSplineSmooth interpolates new grid nodes
'with a cubic spline.
'-----------------------------------------------
Debug.Print "GridSplineSmooth"
SurferApp.GridSplineSmooth (InGrid := path1+"demogrid.grd", _
nRow:=2, nCol:=4, Method:=srfSplineInsert, _
OutGrid := path1+"DemoSplineSmooth.grd", _
OutFmt := srfGridFmtS7)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridSplineSmooth").Font.Size = 40
Set mapframe1 = shapes1.AddContourMap(path1+"demogrid.grd")
With mapframe1
.xLength = 0.67 * .xLength
.yLength = 0.67 * .yLength
.Top = 8
.Left = 1
.Axes("Bottom Axis").Title = "Demogrid.grd"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
Set mapframe2 = shapes1.AddContourMap(path1+"DemoSplineSmooth.grd")
With mapframe2
.xLength = 0.67 * .xLength
.yLength = 0.67 * .yLength
.Top = 8
.Left = 6
.Axes("Bottom Axis").Title = "DemoSplineSmooth.grd"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
'-----------------------------------------------
'GridTransform performs XY transforms on a grid
'including Scale, Offset, Mirror, and Rotate.
'-----------------------------------------------
Debug.Print "GridTransform"
SurferApp.GridTransform (InGrid := path1+"demogrid.grd", _
Operation := srfGridTransOffset, _
xOffset:=3, yOffset:=-5, _
OutGrid:=path1+"demotransform1.grd", _
OutFmt:=srfGridFmtS7)
SurferApp.GridTransform(InGrid := path1+"demogrid.grd", _
Operation:=srfGridTransMirrorY, _
OutGrid:=path1+"demotransform2.grd", _
OutFmt:=srfGridFmtS7)
Wait 1
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1,"GridTransform").Font.Size = 40
Set mapframe1 = shapes1.AddContourMap(path1+"demogrid.grd")
With mapframe1
.xLength = .xLength/2
.yLength = .yLength/2
.Top = 8
.Left = 1
.Axes("Bottom Axis").Title = "Demogrid.grd"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
Set mapframe2 = shapes1.AddContourMap(path1+"DemoTransform1.grd")
With mapframe2
.xLength = .xLength/2
.yLength = .yLength/2
.Top = 8
.Left = 5
.Axes("Bottom Axis").Title = "DemoTransform1.grd Offset"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
Set mapframe3 = shapes1.AddContourMap(path1+"DemoTransform2.grd")
With mapframe3
.xLength = .xLength/2
.yLength = .yLength/2
.Top = 4
.Left = 5
.Axes("Bottom Axis").Title = "DemoTransform2.grd MirrorY"
.Axes("Bottom Axis").TitleFont.Size = 20
End With
'-----------------------------------------------
'GridVolume calculates volumes and areas between
'two surfaces.
'-----------------------------------------------
Debug.Print "GridVolume"
Dim results() As Double 'Array of volume results.
'Dim resultnames() As String
SurferApp.GridVolume (Upper := path1+"demogrid.grd", _
Lower:=50, pResults:=results, ShowReport:=False)
resultnames = Array( _
"srfGVTrapVol", "srfGVSimpVol", "srfGVSimp38Vol", "srfGVPosVol", "srfGVNegVol", _
"srfGVPosPlanarArea", "srfGVNegPlanarArea", "srfGVPosArea", "srfGVNegArea", "srfGVBlankedArea", _
"srfGVNumParams")
For i = 0 To srfGVNumParams-1 Step 1
Debug.Print resultnames(i);" ";results(i)
Next
Debug.Print "srfGVNumParams ";srfGVNumParams
Wait 4
For Each shp In shapes1
shp.Delete
Next
shapes1.AddText(1,1, _
"GridVolume - see Scripter Immediate Window").Font.Size = 35
'-----------------------------------------------
'NewGrid creates a new Grid object.
'All properties and methods of the Grid object apply here.
'-----------------------------------------------
Dim Grid As Object
Set Grid = SurferApp.NewGrid
'Uses properties and methods of the Grid object
Grid.LoadFile(FileName:=path1+"demogrid.grd", HeaderOnly:=True)
Debug.Print "New Grid: number of rows = ";Grid.NumRows
'----------------------------------------------------------
'GridData2 creates a GRD file from an XYZ DAT file.
'NewVarioComponent illustrates how to add and modify
'variogram components (models) for kriging. They
'are required when gridding with kriging using anisotropy.
'----------------------------------------------------------
Dim LinearComponent(1 To 1) As Object
Set LinearComponent(1) = _
SurferApp.NewVarioComponent( _
VarioType:=srfVarLinear, _
Param1:=1, _
Param2:=1, _
AnisotropyRatio:=5, _
AnisotropyAngle:=45) '<-- specify anisotropy.
'Pass an Array of one Or more VarioComponents With KrigVariogram:= .
SurferApp.GridData2(DataFile := path1+"demogrid.dat", _
Algorithm := srfKriging, _
ShowReport := False, _
OutGrid := path1+"DemoAnisot.grd", _
KrigVariogram := LinearComponent, _
SearchEnable := True)
Wait 1
For Each shp In shapes1
shp.Delete
Next shp
shapes1.AddText(1,1,"GridData2 with Anisotropy using NewVarioComponent").Font.Size = 30
shapes1.AddContourMap(path1+"DemoAnisot.grd")
End Sub