Skip to content

Reading Binary Files

2 messages · Steve_Friedman at nps.gov, Henrik Bengtsson

#
Hello

I'm encountering some difficulty correctly reading binary files. The binary
files store data as "short"  rather than "double" , "int", or any of the
other  modes of the vector being read.

The data represents a regular grid of size 419 rows by 264 columns, to make
it more interesting, the data are daily records, for a total of 37 years.
The file size is therefore 419(rows) * 264(columns) * 365(days) * 37(years)
long.

The product  of these dimensions is 1493869080 records.

I'm using the following code to read these into R (windows 2.8.1 )

 con <- file(file.choose(), open="rb")
 Year66 <- readBin(con, integer, signed=TRUE, n = 40374840)
close(con)

length(Year66)

returns 2046396

I'm betting that I'm defining the "what" incorrectly, but after numerous
attempts with different choices I'm wondering if readBin can handle "short"
values?

Any help is greatly appreciated.

Steve


Steve Friedman Ph. D.
Spatial Statistical Analyst
Everglades and Dry Tortugas National Park
950 N Krome Ave (3rd Floor)
Homestead, Florida 33034

Steve_Friedman at nps.gov
Office (305) 224 - 4282
Fax     (305) 224 - 4147
#
Argument 'size' is what you are looking for, cf. help(readBin).
Whenever reading binary files this way, I strongly recommend that you
are explicit about all arguments of readBin(), e.g.

readBin(con, what=integer(), size=2, signed=TRUE, endian="little", n=n);

For instance, you probably do not want 'endian' to be dependent on the
platform (see help) you run on, but instead be specific to the file
format you are reading.

/Henrik
On Wed, Feb 11, 2009 at 8:04 AM, <Steve_Friedman at nps.gov> wrote: