Jooil Kim
2012-Feb-15 03:58 UTC
[R] Using readBin to read binary "unformatted" output files from Fortran?
Hello, I'm wondering if I can get some help with reading Fortran binary "unformatted" output files into R. The Fortran output files were generated in Ubuntu 10.04 LTS using gfortran4.4, on a 32bit Intel Core 2 Duo 3.16 GHz machine, with little-endian and record marker lengths equal to 4. The machine I'm currently trying to read this Fortran output file is a Macbook Pro running Lion 10.7.3, on a 2.26 GHz Intel Core 2 Duo with 4Gb of ram. I'm running R 2.14.1. Based on whatever information I could gather from the web, I've been trying out the following code (the name of the Fortran output file is "header", located in the current working directory).> to.read <- file("header", "rb") > readBin(to.read, "integer", n=2, size = 4, endian = "little")Unfortunately, these commands return empty. Am I on the right track here? Is what I'm trying to accomplish here even technically possible? Any help would be greatly appreciated. Thanks in advance, Jooil -- Jooil Kim Postdoc Fellow Scripps Institution of Oceanography, UC San Diego Mailing address: 9500 Gilman Drive # 0244 La Jolla, CA 92093-0244, USA For FedEx/UPS: 8675 Discovery Way, Vaughan Hall Rm. 447 La Jolla, CA 92037, USA [[alternative HTML version deleted]]
Duncan Murdoch
2012-Feb-15 10:41 UTC
[R] Using readBin to read binary "unformatted" output files from Fortran?
On 12-02-14 10:58 PM, Jooil Kim wrote:> Hello, > > I'm wondering if I can get some help with reading Fortran binary "unformatted" output files into R. > > The Fortran output files were generated in Ubuntu 10.04 LTS using gfortran4.4, on a 32bit Intel Core 2 Duo 3.16 GHz machine, with little-endian and record marker lengths equal to 4. > > The machine I'm currently trying to read this Fortran output file is a Macbook Pro running Lion 10.7.3, on a 2.26 GHz Intel Core 2 Duo with 4Gb of ram. I'm running R 2.14.1. > > Based on whatever information I could gather from the web, I've been trying out the following code (the name of the Fortran output file is "header", located in the current working directory). > >> to.read<- file("header", "rb") >> readBin(to.read, "integer", n=2, size = 4, endian = "little") > > Unfortunately, these commands return empty. > > Am I on the right track here? Is what I'm trying to accomplish here even technically possible?Yes, that should have worked. To debug, I'd replace your second line with readBin(to.read, "raw", n=100) For example, when I create a file by writing out 1:10 using writeBin, that gives > readBin(to.read, "raw", n=100) [1] 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 [20] 00 06 00 00 00 07 00 00 00 08 00 00 00 09 00 00 00 0a 00 [39] 00 00 from which it is pretty obvious the file contains small integers in little endian form. Paul Murrell's hexView package gives more elaborate possibilities. Duncan Murdoch