Skip to content

Commit

Permalink
new function to fill with all constant
Browse files Browse the repository at this point in the history
  • Loading branch information
dhia-gharsallaoui committed Nov 8, 2021
1 parent de986bd commit b28dbba
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,7 @@ export(bellman_parallel_value)
export(check_Bellman_inc)
export(check_resh_vu_dec)
export(check_vu_dec)
export(constant_fill)
export(constraint_generator)
export(converged)
export(correct_outliers)
Expand Down
62 changes: 61 additions & 1 deletion R/post_process.R
Original file line number Diff line number Diff line change
@@ -1,3 1,58 @@
#' Fill constant water values
#'
#' @param results_dt Output from \code{watervalues} or \code{Grid_Matrix}
#' @param min_wv minimal water value to use
#' @param max_wv maximal water value to use
#' @import data.table
#' @return a \code{data.table}
#' @export

constant_fill <- function(results_dt,max_wv,min_wv)

{
results <- copy(results_dt)


for (i in 1:52){

temp <- results[weeks==i]


m <- 0
M <- 0

for (j in 1:nrow(temp)){

if (!is.finite(temp$vu[j])) next

if(m==0)m <- temp[j,]$statesid

M <- j

}
M <- temp[M,]$statesid

temp[statesid>m,vu:=max_wv]
temp[statesid<M,vu:=min_wv]
for (j in 2:nrow(temp)){
if(is.na(temp$vu[j])) temp$vu[j] <- temp$vu[j-1]
}
results[weeks==i,vu :=temp$vu]
}

return(results)
}











#' Fill the rest of reservoir states water values
#' @param results_dt Output from \code{watervalues} or \code{Grid_Matrix}.
#' @param max_cost maximal accepted water value (Replace by 'NA' crossed values)
Expand All @@ -17,7 72,7 @@
#' @export

post_process <- function(results_dt,max_cost=3000,min_cost=0,full_imputation=FALSE,
impute_method='pmm',fix=F,min_vu=0,max_vu=1000,blocker=F){
impute_method='pmm',fix=F,min_vu=0.5,max_vu=1000,blocker=F){

results <- copy(results_dt)
results[vu==Inf|is.na(vu),vu:=NA]
Expand All @@ -30,6 85,11 @@ post_process <- function(results_dt,max_cost=3000,min_cost=0,full_imputation=FAL
# results[vu<-down_cost,vu:=NA]

if(fix){
if(T){
results <- constant_fill(results,max_vu,min_vu)
return(results)
}

if(!is.numeric(min_vu))min_vu <- min(results$vu,na.rm = T)
if(!is.numeric(max_vu))max_vu <- min(results$vu,na.rm = T)
results[states>level_high,vu:=min_vu]
Expand Down
2 changes: 1 addition & 1 deletion man/post_process.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b28dbba

Please sign in to comment.