#  check kmeans synthetic data
setwd("/esarchive/scratch/nperez/git/Flor/s2dverification_tests/Edu_WR/")
library(abind)
x <-  1:50
y <- 10 * x^2 - x 
z <- exp(x)
a <-  c(1: (50 * 10), 2999:2500, y, y + 50, y + 10, y - 10, 
        y - 20, y * 2, y / 2, y * 7, y / 7, y + 1, z, z + 1,
        z + 5, z - 5, z * 2, z * 7, z / 7, z + 50, z - 10, z * 2)     

dim(a) <- c(d1 = 40, d2 = 50)
run1  <- kmeans(a, centers = 4)  
run2  <- kmeans(a, centers = 4)
run3  <- kmeans(a, centers = 4)
run <- abind(run1$centers, run2$centers, run3$centers, along = 3)

max(run1$centers)
max(run2$centers)
max(run3$centers)
min(run1$centers)
min(run2$centers)
min(run3$centers)


colors <- c(rep('maroon4', 4), rep('darkgreen', 4), rep('purple', 4))
boxplot(t(rbind(run1$centers, run2$centers, run3$centers)), col = colors, 
        main = "Centers 3 simulation", medcol = colors, whiskcol = colors, 
        staplecol = colors, boxcol = colors, outcol = colors, 
        outbg = colors) 
png("SyntheticKmeansBoxPlot.png")
boxplot(t(rbind(run1$centers, run2$centers, run3$centers)), col = colors,
        main = "Centers 3 simulation", medcol = colors, whiskcol = colors,
        staplecol = colors, boxcol = colors, outcol = colors,
        outbg = colors)
dev.off()

# Run in Power9:
png("/esarchive/scratch/nperez/git/Flor/s2dverification_tests/Edu_WR/SyntheticKmeansBoxPlot_POWER9.png")
boxplot(t(rbind(run1$centers, run2$centers, run3$centers)), col = colors,
        main = "Centers 3 simulation", medcol = colors, whiskcol = colors,
        staplecol = colors, boxcol = colors, outcol = colors,
        outbg = colors)
dev.off()

#--------------------------
# Example with original data
#---------------------------
file <- "/esarchive/scratch/emchamarro/appl/sit-cat/clusters/scripts/datasets/sic-temp-hadisstv22.1deg.aso.nc"

data <- Start(dat = file,
              var = 'sic',
              time = 'all',
              lat = 'all',
              lon = 'all',
              return_vars = list(lat = 'dat', lon = 'dat', time = 'dat'),
              retrieve = TRUE)
dim(data) <- c(time = 36, grid = 180 * 360)
NAgrid <- apply(data, 2, function(x) {sum(is.na(x))})
data <- data[,which(NAgrid == 0)]
run1  <- kmeans(data, centers = 4)
run2  <- kmeans(data, centers = 4)
run3  <- kmeans(data, centers = 4)
run <- abind(run1$centers, run2$centers, run3$centers, along = 3)

max(run1$centers)
max(run2$centers)
max(run3$centers)
min(run1$centers)
min(run2$centers)
min(run3$centers)

png("SICdata_SyntheticKmeansBoxPlot.png")
colors <- c(rep('maroon4', 4), rep('darkgreen', 4), rep('purple', 4))
boxplot(t(rbind(run1$centers, run2$centers, run3$centers)), col = colors,
        main = "Centers 3 simulation", medcol = colors, whiskcol = colors,
        staplecol = colors, boxcol = colors, outcol = colors,
        outbg = colors)
dev.off()

png("/esarchive/scratch/nperez/git/Flor/s2dverification_tests/Edu_WR/SICdata_SyntheticKmeansBoxPlot_POWER9.png")
boxplot(t(rbind(run1$centers, run2$centers, run3$centers)), col = colors,
        main = "Centers 3 simulation", medcol = colors, whiskcol = colors,
        staplecol = colors, boxcol = colors, outcol = colors,
        outbg = colors)
dev.off()




