# CRPS version for climatology 
CRPS_clim <- function(obs, memb_dim ='ensemble', return_mean = TRUE, clim.cross.val= TRUE){  
  time_dim <- names(dim(obs))
  obs_time_len <- dim(obs)[time_dim]
  
  if (isFALSE(clim.cross.val)) { ## Without cross-validation
    ref <- array(data = rep(obs, each = obs_time_len), dim = c(obs_time_len, obs_time_len))
  } else if (isTRUE(clim.cross.val)) { ## With cross-validation (excluding the value of that year to create ref for that year)
    ref <- array(data = NA, dim = c(obs_time_len, obs_time_len - 1))
    for (i in 1:obs_time_len) {
      ref[i, ] <- obs[-i]
    }
  }
  
  names(dim(ref)) <- c(time_dim, memb_dim)
  # ref: [sdate, memb]
  # obs: [sdate]
  crps_ref <- s2dv:::.CRPS(exp = ref, obs = obs, time_dim = time_dim, memb_dim = memb_dim,
                           dat_dim = NULL, Fair = FALSE)
  
  # crps_ref should be [sdate]
  if (return_mean == TRUE) {
    return(mean(crps_ref)) 
  } else {
    return(crps_ref)
  }
}
