On 03/05/2012 1:57 PM, kapo coulibaly wrote:
  I believe here is the structure of the file I'm trying to read:
record marker (4 bytes), 2 integers (4 bytes each), 2 doubles (8 bytes
each), one string (16 bytes or 16 characters), 3 integers (4 bytes each), 1
record marker (4 bytes) and a big array of doubles (8 bytes each).
Everything in the file is read correctly except for the doubles.
If any indication, I've read similar file before with readBin the only
difference is this one was created with a code compiled with gfortran in
linux 64 bit. I was able to read the same output binary file when the
fortran source code was compiled in windows xp 32 bit. The values I'm
expecting should be between 0 and about 32.

The first two doubles read properly as 1, and the next one as 33.674, when I follow your description above on the dump you sent me privately. I'm on a system with endian="little"; you might want to specify that explicitly in your readBin calls.

And please, in future, spend some time making your questions easier to answer: put the dump in the public message. Don't post irrelevant code, extract just the bit that's doing the reading.

Duncan Murdoch





The code I used is:



# Loading Required libraries
library(tcltk)

# Tk inputbox function
  inputBox<-function() {
   tt<-tktoplevel()
   Zmin<-tclVar("0")
   Zmax<-tclVar("0")
   dZ<-tclVar("0")
   entry.Zmin<-tkentry(tt,width="20",textvariable=Zmin)
   entry.Zmax<-tkentry(tt,width="20",textvariable=Zmax)
   entry.dZ<-tkentry(tt,width="20",textvariable=dZ)
   lbl.Zmin<-tklabel(tt,text="Number of layers")
   lbl.Zmax<-tklabel(tt,text="Number of Stress Periods")
   lbl.dZ<-tklabel(tt,text="dZ")
   tkgrid(lbl.Zmin,entry.Zmin)
   tkgrid(entry.Zmin)
   tkgrid(lbl.Zmax,entry.Zmax)
   tkgrid(entry.Zmax)
   #tkgrid(lbl.dZ,entry.dZ)
   #tkgrid(entry.dZ)

   OnOK<- function()
   {
       # NameVal<- c(tclvalue(Zmin),tclvalue(Zmax),tclvalue(dZ))
         tkdestroy(tt)
     }
   OK.but<-tkbutton(tt,text="   OK   ",command=OnOK)
   # tkbind(entry.Name, "<Return>",OnOK)
   tkgrid(OK.but,columnspan=3)
   tkfocus(tt)
   tkwait.window(tt)
   res<-as.numeric(c(tclvalue(Zmin),tclvalue(Zmax)))#,tclvalue(dZ)))
   return(res)
}

################################################################################
# Main program
################################################################################

# Model Parameters input (number of layers and stress periods)
param<-inputBox()

# Select and open Modflow Binary file for reading
fich<-tclvalue(tkgetOpenFile(title="Modflow Binary File",filetypes="{{hds
binary Files} {.hds}} {{All files} *}"))
zz<- file(fich, "rb")

# Cycling thru time steps and layers
for (k in 1:param[2]) {
   for (i in 1:param[1]) {
     readBin(zz,what="numeric",n=1,size=4) # record marker typical of
fortran access="sequential" in gfortran
     readBin(zz,what="integer",n=2,size=4)->N1
     readBin(zz,what="double",n=2,size=8)->N2
     readChar(zz,16)->txt1
     print(txt1)
     readBin(zz,what="integer",n=3,size=4)->N3
     tnber<-N3[1]*N3[2]
     readBin(zz,what="integer",n=1,size=4) # record marker typical of
fortran access="sequential" in gfortran
     readBin(zz,what=real(),n=tnber,size=4)->N4
     readBin(zz,what="integer",n=2,size=4) # record marker typical of
fortran access="sequential" in gfortran
     print(N4[1:10])


     }

}

close(zz)

On Thu, May 3, 2012 at 1:26 PM, Duncan Murdoch<murdoch.dun...@gmail.com>wrote:

>  On 03/05/2012 12:41 PM, kapo coulibaly wrote:
>
>>  I'm trying to read a binary file created by a fortran code using readBin
>>  and readChar. Everything reads fine (integers and strings) except for
>>  double precision numbers, they are read as huge or very small number
>>  (1E-250,...). I tried various endianness, swap, But nothing has worked so
>>  far.
>>  I also tried on R 64 bit for linux and windows (R 2.14) and R 2.11 on
>>  windows XP 32 bit.
>>  Any help would be appreciated.
>>
>
>  As I wrote to someone else with a similar problem a couple of weeks ago:
>
>  You need to see what's in the file.  The hexView package can dump it in
>  various formats; see example(viewFormat) for a couple.
>
>  Duncan Murdoch
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to