Follow

How can I convert my GRD files to DATs without NoData values in Surfer Scripter?

If you have multiple GRD files in a directory and you want to convert them to DAT files without the NoData values, you can use the script below to do this.

To run this script:

  1. Copy the script below, or download the attached BAS file: GRD2DAT_WithoutNoData.BAS.
  2. In a Windows Explorer window, navigate to C:\Program Files\GoldenSoftware\Surfer 15\Scripter.
  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. 
     

Sub Main
	Debug.Clear

'====================
'   User Variables
'====================
file_directory	= "C:\surfer\"
'====================

	Dim surf, Wks, WksRange, WksRange2, WksRange3 As Object
	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 + "\"

	'Loops throught the GRD files and converts them to DAT
	new_grids = Dir( file_directory  + "*.grd")
	While new_grids <> ""
		'Define output DAT file directory & name
		new_dats = file_directory + Left(new_grids, Len(new_grids)-(Len(new_grids)-InStrRev(new_grids,".")+1) ) + ".dat"

		'Converts the grid file to a DAT file
        surf.GridConvert2(InGrid:= file_directory + new_grids, OutGrid:=new_dats, OutFmt:=srfGridFmtXYZ)

		Debug.Print new_grids
		new_grids = Dir() 'get next file

	Wend

	'Loops throught the new DAT files, opening them in the worksheet and removing rows with NoData values
	converted_dats = Dir( file_directory  + "*.dat")
	While converted_dats <> ""
		'Define output grid file directory & name
		dats_no_nodata_vals	= file_directory + Left(converted_dats, Len(converted_dats)-(Len(converted_dats)-InStrRev(converted_dats,".")+1) ) + "_withoutnodata.dat"

		'Opens the DAT files in Surfer's worksheet
        Set Wks = surf.Documents.Open(FileName:=file_directory +converted_dats)

        'Sorts the data based on z
		Set WksRange = Wks.Columns(Col1:=1, Col2:=3) 'WksRange is all rows in columns 1-3 of the dat file
        WksRange.Sort(Col1:=3, Order1:=wksSortAscending, Header:=False)

        'Searches through the rows until it find the first one where z is the NoData value
		firstrow=0
        For i=1 To WksRange.RowCount
        Set WksRange2 = Wks.Cells(Row:=i, Col:=3) 'WksRange2 is all rows in column 3 of the dat file
			If WksRange2.Value = 1.70141e+038 Then
				firstrow=WksRange2.Row
				Exit For
			End If
        Next

        If firstrow>0 Then
        	'Deletes all rows from that first row of NoData values through the end of the file
			Set WksRange3 = Wks.Cells(Row:=firstrow, Col:=1, Lastrow:=WksRange.RowCount, Lastcol:=3) 'WksRange3 is the rows with NoData values in columns 1-3 of the dat file
			WksRange3.Delete

			'Saves the edited file
			Wks.SaveAs(FileName:=dats_no_nodata_vals)
		End If

		Debug.Print converted_dats
		converted_dats = Dir() 'get next file

	Wend

	surf.Quit
	Exit Sub

End Sub

Updated September 13, 2017

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

0 Comments

Please sign in to leave a comment.