[R] Converting 16-bit to 8-bit encoding?
Matt.Shotwell at Vanderbilt.Edu
Thu Apr 21 19:24:59 CEST 2011
OK. I'm going to copy this back to R-help too.
With R, we can convert a file of 8-bit integers to 16-bit integers like so:
# Create a test file of 8-bit integers:
con <- file("test.8", "wb")
writeBin(sample(-1L:4L, 1024, TRUE), con, size=1)
# Convert test.8 to test.16
icon <- file("test.8", "rb")
ocon <- file("test.16", "wb")
while(length(dat <- readBin(icon, "integer", 1024, size=1)) > 0)
writeBin(dat, ocon, size=2)
This assumes (without considering a more formal description of the
format) that the file and your computing platform agree on how
multi-byte signed integers are represented.
Hope that will get you going.
On 04/21/2011 11:02 AM, Brian Buma wrote:
> Apologies. The 8-bit file (the one that needs to be converted) is just
> a series of integers, -1 to 4, which is no doubt why they are encoded in
> 8 bit. They don't need to be changed numerically, just put in a 16-bit
> encoding. No meta info, headerless. All the data is MODIS satellite
> I have been using the "raster" program to visualize things, and
> processing (when I get that far) will be done in that program mainly.
> I've used that program on a different project, and it seemed to work
> well. The actual program that can't handle two different inputs is
> Timesat, a phenology-program (not R). I was thinking that R could
> probably do this conversion quick and easy (fairly), but haven't figured
> out how to yet.
> As an example, I have an NDVI file (flat binary, 16bit encoding)- so a
> string of numbers, 4450, 4650, etc... The associated quality file is
> another string, 1,1,2,1,0, etc. It's encoded as an 8bit file.
> Conceptually, all it needs (I think) is to be read in and resaved in the
> less memory-efficient 16-bit format.
> Thanks! Sorry if the explanation isn't clear.
> On Thu, Apr 21, 2011 at 9:50 AM, Matt Shotwell
> <Matt.Shotwell at vanderbilt.edu <mailto:Matt.Shotwell at vanderbilt.edu>> wrote:
> On 04/21/2011 10:36 AM, Brian Buma wrote:
> Hello all-
> I have a question related to encoding. I'm using a seperate
> program which
> takes either 16 bit or 8 bit (flat binary files) as inputs (they
> are raster
> satellite imagery and the associated quality files), but can't
> handle both
> at the same time. Problem is the quality and the image come in
> formats (quality- 8bit, image- 16bit). I need to switch the
> encoding on the
> I think some more detail about these files is necessary. What do
> these 16/8 bit quantities represent? Are these files just a sequence
> of such quantities, or is there meta information (i.e. image dimension)?
> quality files to 16 bit, without altering anything else (they
> are img files
> right now). I imagine this is a fairly simply process, but I
> haven't been
> Does 'img files' indicate that these files are formatted according
> to a standard?. Finally, are you using some R code to manipulate
> these files? Have an example, including data?
> able to find a package or anything which can tell me how to do
> it- perhaps
> I'm searching the wrong terms, but I did look. Is there any
> methods to do
> this quickly? Ideally, the solution would involve reading in a
> list of
> files and replacing the original with the new, 16 bit version,
> as I have
> over 300 files to convert. I hope that's clear. Thanks in advance!
> Matthew S Shotwell Assistant Professor School of Medicine
> Department of Biostatistics Vanderbilt University
> Brian Buma
> PhD Candidate
> Ecology and Evolutionary Biology / CIRES
> University of Colorado, Boulder
> Brian.Buma at colorado.edu <mailto:Brian.Buma at colorado.edu>
Matthew S Shotwell Assistant Professor School of Medicine
Department of Biostatistics Vanderbilt University
More information about the R-help