makeWeights.Rd 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. % Generated by roxygen2: do not edit by hand
  2. % Please edit documentation in R/makeWeights.R
  3. \name{extractBits}
  4. \alias{extractBits}
  5. \alias{makeWeights}
  6. \alias{maskWater}
  7. \title{Extract Bit-Encoded Information and Create Weights Raster}
  8. \usage{
  9. extractBits(x, bitShift = 2, bitMask = 15, filename = "", ...)
  10. makeWeights(x, bitShift = 2, bitMask = 15, threshold = NULL,
  11. filename = "", decodeOnly = FALSE, ...)
  12. maskWater(X, bitShift = NULL, bitMask = NULL, keep = NULL,
  13. datatype = "INT1U", ...)
  14. }
  15. \arguments{
  16. \item{x}{\code{matrix}, vector or \code{Raster*} object.}
  17. \item{bitShift}{\code{integer}. Bit starting point, see examples and
  18. \code{\link{detectBitInfo}}.}
  19. \item{bitMask}{\code{integer}. Bit mask size, see examples and
  20. \code{\link{detectBitInfo}}.}
  21. \item{filename}{\code{character} passed to \code{\link{writeRaster}}. If not
  22. specified, output is written to a temporary file.}
  23. \item{...}{Other arguments passed to \code{\link{writeRaster}}.}
  24. \item{threshold}{\code{integer}. Threshold for valid quality.}
  25. \item{decodeOnly}{\code{logical}. If \code{FALSE} (default), convert bits to
  26. weights from 0 (not used) to 1 (best quality). If \code{TRUE}, only extract
  27. selected bits and convert to decimal system.}
  28. \item{X}{\code{Raster*} object.}
  29. \item{keep}{If \code{NULL} (default), bits are only encoded, else an
  30. \code{integer} vector of values you want to keep (becomes \code{TRUE}), the
  31. rest becomes \code{NA}. See examples.}
  32. \item{datatype}{\code{character}. Output datatype, see
  33. \code{\link{writeRaster}}.}
  34. }
  35. \value{
  36. A \code{Raster*} object.
  37. }
  38. \description{
  39. This function applies \code{\link{bitAnd}} and \code{\link{bitShiftR}}
  40. from \strong{bitops} to convert bit-encoded information. It is also possible
  41. to convert this information to a scale from 0 to 1 in order to use it as
  42. weighting information in functions like \code{\link{whittaker.raster}} or
  43. \code{\link{smooth.spline.raster}}.
  44. }
  45. \section{Functions}{
  46. \itemize{
  47. \item \code{extractBits}: Extract bit-encoded information from \code{Raster*} file
  48. \item \code{maskWater}: Masks water (additional information required)
  49. }}
  50. \note{
  51. \code{\link{makeWeights}} and \code{\link{extractBits}} are identical with
  52. the only difference that \code{\link{makeWeights}} does additionally convert
  53. the data into weighting information.
  54. }
  55. \examples{
  56. \dontrun{
  57. # example MOD13Q1 see https://lpdaac.usgs.gov/products/modis_products_table/mod13q1
  58. # enter in Layers
  59. # See in TABLE 2: MOD13Q1 VI Quality
  60. # column 1 (bit) row 2 VI usefulness
  61. bitShift = 2
  62. bitMask = 15 # ('15' is the decimal of the binary '1111')
  63. # or try to use
  64. detectBitInfo("MOD13Q1") # not all products are available!
  65. viu <- detectBitInfo("MOD13Q1","VI usefulness") # not all products are available!
  66. viu
  67. # simulate bit info
  68. bit <- round(runif(10*10,1,65536))
  69. # extract from vector
  70. makeWeights(bit,bitShift,bitMask,decodeOnly=TRUE)
  71. # the same as
  72. extractBits(bit,bitShift,bitMask)
  73. # create a Raster object
  74. VIqual <- raster(ncol=10,nrow=10)
  75. VIqual[] <- bit
  76. # extract from Raster object
  77. a <- makeWeights(VIqual,bitShift,bitMask,decodeOnly=TRUE)
  78. # linear conversion of 0 (0000) to 15 (1111) to 1 fo 0
  79. b <- makeWeights(VIqual,bitShift,bitMask,decodeOnly=FALSE)
  80. threshold=6 # every thing < threshold becomes a weight = 0
  81. c <- makeWeights(VIqual,bitShift,bitMask,threshold,decodeOnly=FALSE)
  82. res <- round(cbind(a[],b[],c[]),2)
  83. colnames(res) <- c("ORIG","Weight","WeightThreshold")
  84. res
  85. #####
  86. # water mask
  87. tif = runGdal(product="MOD13A2",begin="2009001",end="2009001", extent=extent(c(-9,-3 ,54,58)),
  88. SDSstring="001",job="delme") # 6.4 MB
  89. x <- raster(unlist(tif))
  90. res1 <- maskWater(x)
  91. plot(res1)
  92. res2 <- maskWater(x,keep=1) # 1 = Land (nothing else)
  93. x11()
  94. plot(res2)
  95. # Land (Nothing else but land) + Ocean coastlines and lake shorelines + shallow inland Water,
  96. # the rest becomes NA
  97. x11()
  98. res3 <- maskWater(x,keep=c(1,2,3))
  99. plot(res3)
  100. ###############
  101. # as on Linux you can read HDF4 directly you can also do:
  102. if(.Platform$OS.type=="unix")
  103. {
  104. x <- getHdf(HdfName="MOD13A2.A2009001.h17v03.005.2009020044109.hdf", wait=0) # 6.4 MB
  105. detectBitInfo(x) # just info
  106. getSds(x) # just info
  107. x <- getSds(x)$SDS4gdal[3] # you need 'VI Quality'
  108. x <- raster(x)
  109. # plot(x)
  110. # ex <- drawExtent()
  111. ex <- extent(c(-580779,-200911,5974929,6529959))
  112. x <- crop(x,ex) # just for speed-up
  113. res1 <- maskWater(x)
  114. plot(res1)
  115. res2 <- maskWater(x,keep=1) # 1 = Land (Nothing else but land), the rest becomes NA
  116. x11()
  117. plot(res2)
  118. # Land (Nothing else but land) + Ocean coastlines and lake shorelines + shallow inland Water,
  119. # the rest becomes NA
  120. res3 <- maskWater(x,keep=c(1,2,3))
  121. x11()
  122. plot(res3)
  123. }
  124. }
  125. }
  126. \seealso{
  127. \code{\link{detectBitInfo}}.
  128. }
  129. \author{
  130. Matteo Mattiuzzi
  131. }