library(multiApply)
setwd("/esarchive/scratch/nperez/git/csindicators/")
source("R/TotalTimeExceedingThreshold.R")
source("R/Threshold.R")
source("R/AbsToProbs.R")

ref <- array(rnorm(1*4*10*2*3), c(member = 1, sweek = 10, lweek = 4, lat = 2, lon = 3))
p90 <- Threshold(ref, threshold = 0.9, sdate_dim = 'sweek')
dim(p90)
#lweek   lat   lon 
#    4     2     3 
fcst <- array(rnorm(5*4*2*3), c(member = 5, sweek = 1, lweek = 4, lat = 2, lon = 3))
index_weeksexceeding90p <- TotalTimeExceedingThreshold(fcst, p90, time_dim = 'sweek')
dim(index_weeksexceeding90p)
#member  lweek    lat    lon 
#     5      4      2      3 
 range(index_weeksexceeding90p)
#[1] 0 1

memb <- s2dv::MeanDims(index_weeksexceeding90p, 'member')
prob <- AbsToProbs(s2dv::MeanDims(index_weeksexceeding90p, 'member'))


#  check results are the same:
p90 <- Threshold(ref, threshold = 0.9, sdate_dim = c('sweek', 'lweek'))
dim(p90)

p90_2 <- Threshold(ref, threshold = 0.9, sdate_dim = 'sweek', memb_dim = 'lweek')
dim(p90_2)

all(p90 == p90_2[1,,])

