# Calculate the volume between my grid and Z=0, Z=10, Z=20, etc. via Surfer automation

There are several workflows that may benefit from calculating the volume between a surface and multiple Z values.  For example, when determining the elevation which would result in minimal backfil to level a field.  The sample script below calculates the volume and area of a grid file, loops through multiple Z values as the other constant surface value (with user-specified min, max and increment) and writes the results to the worksheet.

To run this script:

1. Copy the script below, or download the attached BAS file: GridVolume_MultipleZ.bas.
2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer.
3. Double click on Scripter.exe to launch Scripter.
4. Press Ctrl+A to select all of the existing lines then press Delete.
5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
6. Click Script | Run to run the script.

*********

```'Calculates the volume and area of a grid file, looping through multiple Z values
'as the other constant surface value (with user-specified min, max and increment)
'and writes the results to the worksheet
'Edit "User Input" section to define the upper surface grid file and the Z value iterations

Sub Main
Dim SurferApp As Object
Set SurferApp = CreateObject("Surfer.Application")
SurferApp.Visible = True

'-------------User Input ------------------------
Path = SurferApp.Path+"\samples\"
UpperSurfaceGrid = "demogrid.grd"
Min = 0
Max = 50
Increment = 10
'------------------------------------------------------
'Create a new worksheet window
For i =Min To Max Step Increment
UpperSurface =Path+UpperSurfaceGrid
LowerSurface = i
Dim Results() As Double
SurferApp.GridVolume(upper:=UpperSurface,lower:=LowerSurface, presults:=Results, showreport:=False)
Iteration= ((Format(i)-Min)/Increment)+1
Wks.Cells("A1").Value = "Z Value:"
Wks.Cells(Iteration, 1).Value =	i
Wks.Cells("B1").Value = "Positive Volume [Cut]:"
Wks.Cells(Iteration, 2).Value = Results(srfGVPosVol)
Wks.Cells("C1").Value = "Negative Volume [Fill]:"
Wks.Cells(Iteration, 3).Value = Results(srfGVNegVol)
Wks.Cells("D1").Value = "Net Volume [Cut minus Fill]:"
Wks.Cells(Iteration, 4).Value = Results(srfGVPosVol) - Results(srfGVNegVol)
Wks.Cells("E1").Value = "Positive Planar Area:"
Wks.Cells(Iteration, 5).Value = Results(srfGVPosPlanarArea)
Wks.Cells("F1").Value = "Positive Surface Area:"
Wks.Cells(Iteration, 6).Value = Results(srfGVPosArea)
Next i

'Saves the worksheet
Wks.SaveAs(Path+"VolumeReport.dat")
End Sub```

Updated November 2021