Hi, I've read up on readBin() and chapter 6 in the R Data Import/Export manual, but I still can't read a binary file. Here is how the creator of the file described the code that would be needed in Fortran: "Every record has a return in fortran. The length of each record is nx*ny*4. To read you would use the following: nlayx = nx*ny*4 do iz=1,nz,4 read(binary file) var(1:nlayx) enddo nrest=mod(nx*ny*nz,nlayx) read(binary file) var(1:nrest)" The first value in the file should be 0.05, and all of the data values are real. Here is what I get (with similar answers using double):> v<-readBin("plotb.251", numeric(), size=4, n=1) > v[1] 1.614296e-39> v<-readBin("plotb.251", numeric(), size=4, n=1, endian="swap") > v[1] 1.359775e-38 Platform is Intel Linux. How can I read the file described above? Thanks, Scott Waichler, PhD Hydrology Group, Energy & Environment Directorate Pacific Northwest National Laboratory scott.waichler at pnnl.gov
Hi Scott: On Apr 13, 2012, at 1:45 PM, Waichler, Scott R wrote:> Hi, I've read up on readBin() and chapter 6 in the R Data Import/Export manual, but I still can't read a binary file. Here is how the creator of the file described the code that would be needed in Fortran: > > "Every record has a return in fortran. The length of each record is nx*ny*4. To read you would use the following: > > nlayx = nx*ny*4 > do iz=1,nz,4 > read(binary file) var(1:nlayx) > enddo > nrest=mod(nx*ny*nz,nlayx) > read(binary file) var(1:nrest)" > > The first value in the file should be 0.05, and all of the data values are real. Here is what I get (with similar answers using double): > >> v<-readBin("plotb.251", numeric(), size=4, n=1) >> v > [1] 1.614296e-39 > >> v<-readBin("plotb.251", numeric(), size=4, n=1, endian="swap") >> v > [1] 1.359775e-38 > > Platform is Intel Linux. How can I read the file described above?The creator of the file left out the "open" statement and the defaults for binary files in the compiler s/he used. This is just a guess, but some Fortrans when writing "binary" files also include the record length in each record. That would throw off a strict binary read. Again, just a guess, but I have seen this before. Don't know enough abut readBin to see if you can get the result of reading the first record in Hex or binary, but that might tell you what is going on. -Roy M. ********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: Roy.Mendelssohn at noaa.gov (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected" "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
On 12-04-13 4:45 PM, Waichler, Scott R wrote:> Hi, I've read up on readBin() and chapter 6 in the R Data Import/Export manual, but I still can't read a binary file. Here is how the creator of the file described the code that would be needed in Fortran:You need to see what's in the file. The hexView package can dump it in various formats; see example(viewFormat) for a couple. If you can't figure it out from the dump, post it here, and someone should be able to help. Duncan Murdoch> "Every record has a return in fortran. The length of each record is nx*ny*4. To read you would use the following: > > nlayx = nx*ny*4 > do iz=1,nz,4 > read(binary file) var(1:nlayx) > enddo > nrest=mod(nx*ny*nz,nlayx) > read(binary file) var(1:nrest)" > > The first value in the file should be 0.05, and all of the data values are real. Here is what I get (with similar answers using double): > >> v<-readBin("plotb.251", numeric(), size=4, n=1) >> v > [1] 1.614296e-39 > >> v<-readBin("plotb.251", numeric(), size=4, n=1, endian="swap") >> v > [1] 1.359775e-38 > > Platform is Intel Linux. How can I read the file described above? > > Thanks, > Scott Waichler, PhD > Hydrology Group, Energy& Environment Directorate > Pacific Northwest National Laboratory > scott.waichler at pnnl.gov > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.