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

rick reeves reeves at nceas.ucsb.edu
Wed Dec 9 19:20:53 CET 2009


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.

Hope this helps,
Rick R



Barry Rowlingson wrote:
> On Wed, Dec 9, 2009 at 4:58 PM, Jonathan Thayn <jthayn at ilstu.edu> wrote:
>   
>> Hopefully there is a simple solution to this problem. I process really large satellite images in R and, in order to not offend the memory-limitation demi-gods, I open each row of the imagery one at a time, process it, write the results, and then open the next line. I use readBin() to open each line.  For examaple, say I have the following file:
>>
>> writeBin(1:20,con="desktop/temp.bip",size=2,endian="big")
>>
>> which I can open using readBin():
>>
>> readBin("desktop/temp.bip","integer",size=2,endian="big",n=20)
>>
>> Now I want to open select values from the binary file, not a complete line.  Say I want to read values 3, 7, and 10, process them, write the result, and then read values 4, 5, and 17.  How do I direct readBin to those specific values? Thanks.
>>     
>
>  You need to seek help. Or help(seek). Yes really.
>
> Barry
>
>
>   


-- 
Rick Reeves
Scientific Programmer/Analyst and Data Manager
National Center for Ecological Analysis and Synthesis
UC Santa Barbara
www.nceas.ucsb.edu
805 892 2533



More information about the R-sig-Geo mailing list