2012-05-13 77 views
8

我正嘗試使用R程序包ncdf創建多維NetCDF文件。我正在進行一組1500點的氣候日常觀測,每個觀測點的觀測數量爲〜18250。 的問題是,所述netCDF文件(create.ncdf)的結構佔有4Gb和每個點超過3 GB(put.var.ncdf在R中創建多維NetCDF

這是使文件增加的大小我使用的代碼:

# 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是與長和緯度每個點的矩陣拍攝向量。數據集是一個列表格式,每個點都有一個數值列表。

dataset[[1]]=c(0,0,0,9.7,0,7.5,3.6,2.9,0,0.5,....) 

我錯過了什麼,或者我應該嘗試其他包?

+0

Longvector和Latvector的長度是多少?你可以提供它們,也許是通過調用seq()或者只是轉儲代碼來用dput()重新創建它們。 – mdsumner

+0

請編輯該問題以包括缺少的信息 – mdsumner

+0

將建議移動接受的答案ncdf4解決方案,因爲ncdf現在已經過時了 - 現在大部分使用netcdf4約定的軟件已經過時。 –

回答

8

你的不可複製的代碼有一些錯誤,通過我的推算,這個文件是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,並使用第三維(不是-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 
3

這裏的mdsumner的回答的更新版本,這與NetCDF4包R(ncdf4)的作品。

# 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)