Author Topic: Creating GRD files  (Read 485 times)

JonF

  • Professor
  • ****
  • Posts: 149
Creating GRD files
« on: March 01, 2023, 07:04:23 AM »
Hi all,

   Has anyone ever had success creating .grd files in R for use in CalcImage?

I can read GRD files in to R easy enough, but even reading in a CalcImage .grd file and then exporting the same file as .grd results in a "version number unknown" error in CalcImage.

Looking at the Surfer help topic on the .grd file format, it requires a header section with the version number, and I'm working on the basis that R (specifically the raster package) isn't creating an appropriate entry in the header (and why would it!). I can't edit the header of the GRD file after though, so I'm a bit stuck there. R's raster::writeRaster() is also creating a .gri file that isn't being used by CalcImage.

If there isn't a good way of solving this, I guess I'll just have to make a .prbimg first and then use CalcImage to convert that to a .grd file. 

Probeman

  • Emeritus
  • *****
  • Posts: 2836
  • Never sleeps...
    • John Donovan
Re: Creating GRD files
« Reply #1 on: March 01, 2023, 07:51:22 AM »
Hi all,

   Has anyone ever had success creating .grd files in R for use in CalcImage?

I can read GRD files in to R easy enough, but even reading in a CalcImage .grd file and then exporting the same file as .grd results in a "version number unknown" error in CalcImage.

Looking at the Surfer help topic on the .grd file format, it requires a header section with the version number, and I'm working on the basis that R (specifically the raster package) isn't creating an appropriate entry in the header (and why would it!). I can't edit the header of the GRD file after though, so I'm a bit stuck there. R's raster::writeRaster() is also creating a .gri file that isn't being used by CalcImage.

If there isn't a good way of solving this, I guess I'll just have to make a .prbimg first and then use CalcImage to convert that to a .grd file.

The VB code for reading/writing GRD files is attached to the post linked here:

https://probesoftware.com/smf/index.php?topic=324.msg8470#msg8470
The only stupid question is the one not asked!

JonF

  • Professor
  • ****
  • Posts: 149
Re: Creating GRD files
« Reply #2 on: March 01, 2023, 09:34:52 AM »
Ah brill, cheers!

JonF

  • Professor
  • ****
  • Posts: 149
Re: Creating GRD files
« Reply #3 on: March 02, 2023, 09:25:50 AM »
I've translated the VB code in to R, keeping as many of the variable names as possible, in case anyone else wants to do this in the future.

It's not responsive - you'll need to manually edit the script below with stage coordinates, step size etc and play around transposing the matrix to make the maps the correct orientation (JEOL vs Cameca), but the script below will take an array and create a file that is successfully read by CalcImage.

Bit annoying that the Surfer file format keeps bouncing between integers and doubles, but necessary I guess.

Code: [Select]
#GRD Creater

require(raster)

#Set this to wherever you have saved the file you want to import, or wherever you want to export the new grd file if you give the full path to the import file below
setwd("C:/Work/Rwork/")


#import existing .grd as raster for testing
import_grd <- raster::raster("[INSERT_FILENAME_TO_IMPORT].grd")

#Convert to matrix
import_matrix <- as.matrix(import_grd)

#Below commands to flip the matrix to match the orientation of the other maps. Might not be needed for Cameca? Best to have a play to figure out the rotation of your maps.
import_matrix <- import_matrix[nrow(import_matrix):1,ncol(import_matrix):1]

#Assign the objects for the GRD export file
theader.tag = 1112691524
theader.Size = 4
theader.Version = 1
tGrid.tag = 1145655879
tGrid.Size = 72
tGrid.nCol = dim(import_matrix)[[2]]
tGrid.nRow = dim(import_matrix)[[1]]
#Currently set to (0,0) for lower left X,Y coordinates. Change these to desired values for your map
tGrid.xLL = 0
tGrid.yLL = 0
#Insert correct step size below
tGrid.xSize = 1
tGrid.ySize = 1
tGrid.zmin = min(import_matrix)
tGrid.zmax = max(import_matrix)
tGrid.rotation = 0
#Default Surfer blank value
tGrid.BlankValue =  1.71041e38
tData.tag = 1096040772
tData.Size = tGrid.nRow * tGrid.nCol * 8
tData.hData = import_matrix


#Create the parts for the GRD export file
#32 bit sgned INTEGER
part1 <- as.integer(c(
theader.tag,
theader.Size,
theader.Version,
tGrid.tag,
tGrid.Size,
tGrid.nCol,
tGrid.nRow))

#64 bit DOUBLE
part2 <- as.double(c(
tGrid.xLL,
tGrid.yLL,
tGrid.xSize,
tGrid.ySize,
tGrid.zmin,
tGrid.zmax,
tGrid.rotation,
tGrid.BlankValue))

#32 bit INTEGER
part3 <- as.integer(c(
tData.tag,
tData.Size))

#64 bit DOUBLE
part4 <-as.double(
tData.hData)

#open connection to output file, change filename to desired output here
write.filename = file("export_01.grd", "wb")

#populate the file with contents in appropriate format (32 bit signed integers and 64 bit doubles)
writeBin(part1, write.filename, size=4, endian='little')
writeBin(part2, write.filename, size=8, endian='little')
writeBin(part3, write.filename, size=4, endian='little')
writeBin(part4, write.filename, size=8, endian='little')

#close connection to file
close(write.filename)