ArrayToNetCDF(array(1:9, c(3, 3)), 'tmp.nc')

ArrayToNetCDF(array(1:27, c(3, 3, 3)), 'tmp.nc')

ArrayToNetCDF(list(tos = array(1:27, c(3, 3, 3))), 'tmp.nc')

a <- array(1:27, dim = c(3, 3, 3))
names(dim(a)) <- c('lon', 'lat', 'var')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:27, dim = c(3, 3, 3))
names(dim(a)) <- c('lon', 'lat', 'variable')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')

THESE DONT WORK!! TIME MUST BE THE LAST
a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('time', 'lat', 'lon', 'var')
ArrayToNetCDF(a, 'tmp.nc')
a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('lat', 'time', 'lon', 'var')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:1600, dim = c(10, 20, 4, 2))
names(dim(a)) <- c('lat', 'var', 'lon', 'time')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(list(a), 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(list(a, a), 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
b <- array(1:400, dim = c(5, 11, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(list(a, b), 'tmp.nc')

THIS HAS TO FAIL
a <- array(1:400, dim = c(5, 10, 4, 2))
b <- array(1:400, dim = c(5, 11, 4, 2))
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
names(dim(b)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(list(a, b), 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
              tos = list(),
              tas = list()
            )
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
              tos = list(name = 'name1'),
              tas = list(name = 'name2')
            )
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
              tos = list(units = 'K'),
              tas = list(units = 'K')
            )
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
              tos = list(addOffset = 100,
                         scaleFact = 10),
              tas = list(addOffset = 100,
                         scaleFact = 10)
            )
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
              tos = list(addOffset = 100,
                         scaleFact = 10,
                         dim = list(list(name = 'unlimited',
                                         unlim = TRUE))),
              tas = list(addOffset = 100,
                         scaleFact = 10,
                         dim = list(list(name = 'unlimited',
                                         unlim = TRUE)))
            )
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'unlimited', 'var')
ArrayToNetCDF(a, 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
              tos = list(addOffset = 100,
                         scaleFact = 10,
                         dim = list(list(name = 'time',
                                         unlim = FALSE))),
              tas = list(addOffset = 100,
                         scaleFact = 10,
                         dim = list(list(name = 'time',
                                         unlim = FALSE)))
            )
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')

tos <- array(1:400, dim = c(5, 10, 4))
metadata <- list(tos = list(units = 'K'))
attr(tos, 'variables') <- metadata
names(dim(tos)) <- c('lat', 'lon', 'time')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

a <- array(1:400, dim = c(5, 10, 4, 2))
metadata <- list(
              tos = list(addOffset = 100,
                         scaleFact = 10,
                         dim = list(list(name = 'time',
                                         unlim = FALSE)))
            )
attr(a, 'variables') <- metadata
names(dim(a)) <- c('lat', 'lon', 'time', 'var')
ArrayToNetCDF(a, 'tmp.nc')

THIS WORKS BUT THE RESULT FILE CANNOT BE INTERPOLATED WITH CDO 
BECAUSE TIME MUST BE IN THE LAST POSITION
tos <- array(1:400, dim = c(4, 5, 10))
metadata <- list(tos = list(units = 'K',
                            dim = list(list(name = 'time', 
                                            unlim = FALSE))
                           )
                )
attr(tos, 'variables') <- metadata
names(dim(tos)) <- c('time', 'lat', 'lon')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN INTERPOLATE
tos <- array(1:1200, dim = c(5, 3, 10, 4))
metadata <- list(tos = list(units = 'K',
                            dim = list(list(name = 'time', 
                                            unlim = FALSE))
                           )
                )
attr(tos, 'variables') <- metadata
names(dim(tos)) <- c('lat', 'extra', 'lon', 'time')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN NOT INTERPOLATE
tos <- array(1:3600, dim = c(3, 5, 3, 10, 4))
metadata <- list(tos = list(units = 'K',
                            dim = list(list(name = 'time', 
                                            unlim = FALSE))
                           )
                )
attr(tos, 'variables') <- metadata
names(dim(tos)) <- c('extra2', 'lat', 'extra', 'lon', 'time')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN NOT INTERPOLATE
tos <- array(1:3600, dim = c(5, 10, 3, 3, 4))
metadata <- list(tos = list(units = 'K',
                            dim = list(list(name = 'time', 
                                            unlim = FALSE))
                           )
                )
attr(tos, 'variables') <- metadata
names(dim(tos)) <- c('lat', 'lon', 'extra', 'extra2', 'time')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN NOT INTERPOLATE
tos <- array(1:450, dim = c(5, 10, 3, 3))
names(dim(tos)) <- c('lat', 'lon', 'extra', 'extra2')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN NOT INTERPOLATE
tos <- array(1:450, dim = c(3, 3, 5, 10))
names(dim(tos)) <- c('extra', 'extra2', 'lat', 'lon')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN INTERPOLATE
tos <- array(1:150, dim = c(5, 10, 3))
names(dim(tos)) <- c('lat', 'lon', 'extra')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN INTERPOLATE
tos <- array(1:50, dim = c(5, 10))
names(dim(tos)) <- c('lat', 'lon')
lon <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lon) <- length(lon)
metadata <- list(lon = list(units = 'degrees_east'))
attr(lon, 'variables') <- metadata
names(dim(lon)) <- 'lon'
lat <- seq(-90, 90, length.out = 5)
dim(lat) <- length(lat)
metadata <- list(lat = list(units = 'degrees_north'))
attr(lat, 'variables') <- metadata
names(dim(lat)) <- 'lat'
ArrayToNetCDF(list(tos, lon, lat), 'tmp.nc')

CAN INTERPOLATE
tos <- array(1:1200, dim = c(10, 5, 3, 4))
metadata <- list(tos = list(units = 'K',
                            dim = list(list(name = 'extra2', 
                                            unlim = TRUE))
                           )
                )
attr(tos, 'variables') <- metadata
names(dim(tos)) <- c('lun', 'lut', 'extra', 'extra2')
lun <- seq(0, 360 - 360 / 10, length.out = 10)
dim(lun) <- length(lun)
metadata <- list(lun = list(units = 'degrees_east'))
attr(lun, 'variables') <- metadata
names(dim(lun)) <- 'lun'
lut <- seq(-90, 90, length.out = 5)
dim(lut) <- length(lut)
metadata <- list(lut = list(units = 'degrees_north'))
attr(lut, 'variables') <- metadata
names(dim(lut)) <- 'lut'
ArrayToNetCDF(list(tos, lun, lut), 'tmp.nc')
