R/sm_inversion_lut.R

Defines functions sm_inversion_lut

Documented in sm_inversion_lut

#' Inversion of SM from look up table generated by WCM
#'
#' @param img raster object
#' @param lookuptable Look up table simulated from 'wcm_sim' function
#'
#' @return a raster object (pixel value represents SM)
#' @export
#'
#' @examples
#' radar1 <- raster::raster(ncol=10, nrow=10)
#' val <- seq(-12,-7, length.out=100)
#' radar1[] <- val
#' A= -9.596695
#' B= -0.005331
#' C= -11.758309
#' D=  0.011344
#' lut1 <- lut_wcm(LAI=seq(1,6,0.1), SM=seq(0,.6,.01),coeff=c(A,B,C,D))
#' example(out_sm <- sm_inversion_lut(img = radar1,lookuptable = lut1))
sm_inversion_lut <- function(img,lookuptable){
  lut <- deparse(substitute(lookuptable))
  search_sm <- function(x){
    search_col_data <- lookuptable$sig
    diff <- c()
    for (i in 1:NROW(search_col_data)){
      diff[i]<- abs(search_col_data[i]-x)
    }
    idx <- which.min(diff)
    min_diff <- diff[idx]
    matchedRowValues <- lookuptable[idx,]
    y <- matchedRowValues$SM
    return(y)
  }
  raster::beginCluster()
  output <- raster::clusterR(img, raster::calc, args=list(fun=search_sm), export =lut)
  raster::endCluster()
  return(output)
}

Try the WCM package in your browser

Any scripts or data that you put into this service are public.

WCM documentation built on April 1, 2020, 5:10 p.m.