alexandre pryet
2010-Jul-05 19:24 UTC
[R] export VTK from R : impossible to write data as float
Hello,
I've written a short code (below) to write 3D unstructured grid to binary
VTK files from R. Problem : I can only write integers with the command :
data<-as.numeric(c(3.3))
storage.mode(data)<-'integer'
writeBin(data,bfile_celldata,endian="swap")
the function storage.mode(data)<-'long' looks fine, but the VTK file
is not
readable.
thanks for any help or comments,
#----- R SCRIPT TO WRITE A CUBE IN VTK FORMAT
cat('# vtk DataFile Version 3.0\n',file="vtk_header")
cat('R Binary Export v3.0 of inversion
model\nBINARY\n',file="vtk_header",
append=TRUE)
cat('DATASET UNSTRUCTURED_GRID\n',file="vtk_header",
append=TRUE)
#placed here instead of top of vtk_points, since npoints is not known before
cat('POINTS', 8,'int\n',file="vtk_header",append=TRUE)
#write points
writeBin(as.integer(c(0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1,
0, 1, 1, 1, 1, 1)),bfile_points,endian="swap")
#cells header
cat('\nCELLS', 1,9,'\n',file="cells_header")
#write cells
writeBin(as.integer(c(8, 0, 1, 3, 2, 4, 5, 7,
6)),bfile_cells,endian="swap")
#cell types header
cat('\nCELL_TYPES', 1,'\n',file="celltypes_header")
#write cell types
writeBin(as.integer(c(12)),bfile_celltypes,endian="swap")
#cell data header
cat('\nCELL_DATA',1,'\n',file="celldata_header")
cat('SCALARS R float 1','\n',file="celldata_header",
append=TRUE)
cat('LOOKUP_TABLE default\n',file="celldata_header",
append=TRUE)
#write cell data
data<-as.numeric(c(3.3))
storage.mode(data)<-'integer'
writeBin(data,bfile_celldata,endian="swap")
#close binary connections
close(bfile_points)
close(bfile_cells)
close(bfile_celltypes)
close(bfile_celldata)
#concatenate files to produce VTK
system(paste('cat',"vtk_header","bfile_points","cells_header","bfile_cells","celltypes_header","bfile_celltypes","celldata_header","bfile_celldata",">","testb_unstructured.vtk",sep="
"))
[[alternative HTML version deleted]]
So, the problem was that R exports only double sized floats (double), and Paraview requires single sized floats. The solution was just to write : writeBin(data,bfile_celldata,endian="swap",size=4) Special Thanks to Sebastian Gibb : http://www.mail-archive.com/r-help at r-project.org/msg100994.html alex -- View this message in context: http://r.789695.n4.nabble.com/export-VTK-from-R-impossible-to-write-data-as-float-tp2278756p2281217.html Sent from the R help mailing list archive at Nabble.com.