在 R 中创建多维 NetCDF
Posted
技术标签:
【中文标题】在 R 中创建多维 NetCDF【英文标题】:Creating multi-dimensional NetCDF in R 【发布时间】:2012-05-21 08:47:33 【问题描述】:我正在尝试使用 R 包 ncdf 创建多维 NetCDF 文件。我正在对一组 1500 个点进行气候日常观测,每个点的观测数约为 18250。 问题是NetCDF文件的结构(create.ncdf)占用4Gb,每个点使文件大小增加3Gb以上(put.var.ncdf em>)
这是我正在使用的代码:
# Make a few dimensions we can use
dimX <- dim.def.ncdf( "Long", "degrees", Longvector )
dimY <- dim.def.ncdf( "LAT", "degrees", Latvector )
dimT <- dim.def.ncdf( "Time", "days", 1:18250, unlim=FALSE )
# Make varables of various dimensionality, for illustration purposes
mv <- -9999 # missing value to use
var1d <- var.def.ncdf( "var1d", "units", dimX, mv,prec="double" )
var2d <- var.def.ncdf( "var2d", "units", list(dimX,dimY), mv,prec="double" )
var3d <- var.def.ncdf( "var3d", "units", list(dimX,dimY,dimT), mv,prec="double" )
# Create the test file
nc <- create.ncdf( "writevals.nc", list(var1d,var2d,var3d) )
# !!Creates a nc file with + 4 Gb
# Adding the complete time series for one point (the first point in the list of the dataset)
put.var.ncdf( nc, var3d,dataset[[1]], start=c(Longvector[1],Latvector[1],1), count=c(1,1,-1))
Longvector 和 Latvector 是从每个点的 Long 和 Lat 矩阵中获取的向量。数据集是一个列表格式,对于每个点我都有一个数值列表。
dataset[[1]]=c(0,0,0,9.7,0,7.5,3.6,2.9,0,0.5,....)
是我遗漏了什么还是应该尝试其他软件包??
【问题讨论】:
Longvector 和 Latvector 的长度是多少?你能提供它们吗,也许是调用 seq() 或者只是转储代码以使用 dput() 重新创建它们。 请编辑问题以包含缺失的信息 建议移动已接受的答案 ncdf4 解决方案,因为 ncdf 现在已经过时 - 现在大多数软件都使用 netcdf4 约定。 【参考方案1】:您的不可重现代码中存在一些错误,据我估计,该文件为 219Mb(1500 * 18250 * 8 字节)。
library(ncdf)
提供前两个维度的向量和数据集以匹配至少一个切片
Longvector = seq(-180, 180, length = 50)
Latvector = seq(-90, 90, length = 30)
dataset <- list(1:18250)
dimX <- dim.def.ncdf("Long", "degrees", Longvector)
dimY <- dim.def.ncdf("LAT", "degrees", Latvector)
dimT <- dim.def.ncdf("Time", "days", 1:18250, unlim = FALSE)
mv <- -9999
var1d <- var.def.ncdf( "var1d", "units", dimX, mv,prec="double")
var2d <- var.def.ncdf( "var2d", "units", list(dimX,dimY), mv,prec="double")
var3d <- var.def.ncdf( "var3d", "units", list(dimX,dimY,dimT), mv,prec="double")
nc <- create.ncdf( "writevals.nc", list(var1d,var2d,var3d))
count是维度的索引,不是轴位置值,所以我们把start
改成1,使用第3维度的count(长度)(不是-1)。
put.var.ncdf(nc, var3d, dataset[[1]], start = c(1, 1, 1), count = c(1, 1, length(dataset[[1]])))
close.ncdf(nc)
查询文件大小。
file.info("writevals.nc")$size/1e6
[1] 219.0866
【讨论】:
【参考方案2】:这是 mdsumner 答案的更新版本,它适用于 R (ncdf4) 的 NetCDF4 包。
# Open library
library(ncdf4)
# Get x and y vectors (dimensions)
Longvector = seq(-180, 180, length = 50)
Latvector = seq(-90, 90, length = 30)
# Define data
dataset = list(1:18250)
# Define the dimensions
dimX = ncdim_def("Long", "degrees", Longvector)
dimY = ncdim_def("Lat", "degrees", Latvector)
dimT = ncdim_def("Time", "days", 1:18250)
# Define missing value
mv = -9999
# Define the data
var1d = ncvar_def( "var1d", "units", dimX, mv, prec="double")
var2d = ncvar_def( "var2d", "units", list(dimX,dimY), mv, prec="double")
var3d = ncvar_def( "var3d", "units", list(dimX,dimY,dimT), mv, prec="double")
# Create the NetCDF file
# If you want a NetCDF4 file, explicitly add force_v4=T
nc = nc_create("writevals.nc", list(var1d, var2d, var3d))
# Write data to the NetCDF file
ncvar_put(nc, var3d, dataset[[1]], start=c(1, 1, 1),
count=c(1, 1, length(dataset[[1]])))
# Close your new file to finish writing
nc_close(nc)
【讨论】:
所以在:#Define data dataset = list(1:18250) 我们传递网格列表? 是的。有点。它是时间步长的索引。以上是关于在 R 中创建多维 NetCDF的主要内容,如果未能解决你的问题,请参考以下文章