是否可以将字符串变量矩阵写入 R 中的 netCDF 文件?当我运行以下 R 会话中止。
library(ncdf4)
# define a small set of netCDF dimensions
lon3 <- seq(0, 2.0, 0.5)
nlon3 <- length(lon3)
lat3 <- seq(0, 0.5, 0.5)
nlat3 <- length(lat3)
# Make the matrix of strings
id_matrix <- matrix(c("top_extreme_left", "top_left", "top_centre", "top_right", "top_extreme_right", "bottom_extreme_left", "bottom_left", "bottom_centre", "bottom_right", "bottom_extreme_right"), nrow=2, byrow = TRUE)
# create and write NetCDF file
# path and file name, set dname
ncpath <- "C:/"
ncname <- "tiny_test_netcdf"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
# create and write the netCDF file -- ncdf4 version
# define dimensions
londim <- ncdim_def("lon","degrees_east",as.double(lon3))
latdim <- ncdim_def("lat","degrees_north",as.double(lat3))
# dimension for each char in the strings (max string length of 25 characters)
dimchar <- ncdim_def("nchar", "", 1:25, create_dimvar = FALSE)
# define variables
dlname <- "test string matrix data"
id_def <- ncvar_def("op_id", "", list(londim, latdim, dimchar), longname = dlname, prec="char")
# create netCDF file and put arrays
ncout <- nc_create(ncfname, list(id_def), force_v4=TRUE)
# Put the matrix of id strings into the netCDF file.
ncvar_put(ncout, id_def, id_matrix, start=c(1,1,1), count = c(5, 2, 25), verbose = TRUE)
# Close the file to write the data
nc_close(ncout)
我正在尝试扩展帮助中给出的示例,该示例ncvar_put
显示了字符串向量的过程。知道如何/是否可以对矩阵做同样的事情吗?