Learn R Programming

spatialEco (version 1.3-0)

parse.bits: Parse bits

Description

Returns specified bit value based on integer input

Usage

parse.bits(x, bit, depth = 8, order = c("reverse", "none"))

Arguments

x

Integer value

bit

A single or vector of bits to return

depth

The depth (length) of the bit range, default is 8

order

c("reverse", "none") sort order for the bits

Examples

Run this code
# NOT RUN {
 # Return value for bit 5 for integer value 100
 parse.bits(100, 5)
 
 # Return value(s) for bits 0 and 1 for integer value 100
 parse.bits(100, c(0,1))

 # Return value(s) for bits 0 and 1 for integer values 0-255
 for(i in 0:255) { print(parse.bits(i, c(0,1))) }
 
# }
# NOT RUN {
#### Applied Example using Harmonized Landsat Sentinel-2 QC 

# Create dummy data and qc band
 library(raster)
 r <- raster(nrow=100, ncol=100)
   r[] <- round(runif(ncell(r), 0,1)) 
 qc <- raster(nrow=100, ncol=100)
   qc[] <- round(runif(ncell(qc), 64,234)) 
 
 # Calculate bit values from QC table
 ( qc_bits <- data.frame(int=0:255, 
 	cloud = unlist(lapply(0:255, FUN=parse.bits, bit=1)),
 	shadow = unlist(lapply(0:255, FUN=parse.bits, bit=3)),
 	acloud = unlist(lapply(0:255, FUN=parse.bits, bit=2)),
 	cirrus = unlist(lapply(0:255, FUN=parse.bits, bit=0)),
 	aerosol = unlist(lapply(0:255, FUN=parse.bits, bit=c(7,6)))) )
 		
 # Query the results to create a vector of integer values indicating what to mask 
 m <- sort(unique(qc_bits[c(which(qc_bits$cloud == 1),
                            which(qc_bits$shadow == 1)
 						   ),]$int))
 
 # Apply queried integer values to mask image with QA band
 qc[qc %in% m] <- NA
 r <- mask(r, qc)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab