[R-sig-Geo] reading select values from a binary file

Michael Sumner mdsumner at gmail.com
Wed Dec 9 20:59:48 CET 2009


If you can read the whole file at once it might be better to push the
results into a matrix for indexing in R. This could also be useful for
reading chunks of the file, and processing in parts.  seek is fine but
if you are moving around for three values each read it will be slow.
If the arrangement complex,  R's "[" indexing can save you from making
simple errors.

BTW, what is the format of the file? If GDAL can read it the rgdal
package provides a simpler route.

Cheers, Mike.

On Thu, Dec 10, 2009 at 5:33 AM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> On Wed, Dec 9, 2009 at 6:20 PM, rick reeves <reeves at nceas.ucsb.edu> wrote:
>> Hello All:
>> Faced with a similar challenge, and NOT wanting to resort to writing a C
>> language function
>> employing fseek(), I just used two readBin calls: One to read (and
>> implicitly discard) data up
>> to the spot I actually wanted to read, and a second call to read the desired
>> information.
>> here is a code sample:
>>
>>                     fileCon = file(sTsFileName,"rb")
>> #
>> # Offset has been calculated above: number of NumericByte-sized elements
>> preceeding
>> # the data of interest.
>> #
>>                     if (iOffset > 0)
>>                     {
>>
>>                        TestVec = readBin(fileCon,numeric(),size =
>> iNumericBytes,n=iOffset)
>>                     }
>>                       dTimeSeries = readBin(fileCon,numeric(),size =
>> iNumericBytes,n=iNumElements)                     close(fileCon)
>>
>> Crude, but effective. I WIS that we could add a seekBin() function that
>> positions the file pointer
>> to the desired spot.
>>
>
>  The R seek function does this. I hadn't tested it when I posted,
> hoping the original poster would work it from my message. So I just
> now tested it:
>
>  I created a file with ascii a to z in, and then:
>
>  > con = file("file.tst","rb")
>  # jump to 13th letter:
>  > seek(con,13)
>  [1] 0
>  # read it:
>  > readBin(con,what="raw",n=1)
>  [1] 6e     # thats ascii 'n'
>  # jump back:
>  > seek(con,1)
>  [1] 14 # returns where we were, we've now moved...
>  > readBin(con,what="raw",n=1)
>  [1] 62  # ascii 'b'. Things start at zero....
>
>  Is that what you are trying to do?
>
> Barry
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list