【问题标题】:Writing data to a netCDF file with R使用 R 将数据写入 netCDF 文件
【发布时间】:2015-05-11 01:54:20
【问题描述】:

我正在尝试使用我自己的 .csv 文件中的数据来使用 R 包“ncdf4”创建一个 netCDF 文件。 我的数据集由 3 列组成:经度、纬度和温度,它有 2592 行。 我一直按照包中的建议将维度和变量添加到 netCDF 文件中。一切都很好,直到我想在我的文件上写温度数据。 我收到了这个错误:

Error in ncvar_put(nc = ncnew, varid = var_temp, data, start = c(1, 1,  : 
  ncvar_put: error: you asked to write 65160 values, but the passed data 
  array only has 2592 entries! 

怎么了?

library(ncdf)
library(ncdf4)

TimeTable<-read.csv("time.csv",header=T,sep=",")
filename="time.nc"

xvals<-1:360
yvals<--90:90
nx<-length(xvals)
ny<-length(yvals)
lon1<-ncdim_def("longitude","degrees_east",xvals)
lat2<-ncdim_def("latitude", "degrees_north",yvals )

time<-ncdim_def("Time","months", 1:12, unlim=T )
mv <- -999 # missing value to use
var_temp<- ncvar_def("temperature", "celsius", list(lon1, lat2, time),longname="CRU_Global_1961-1990_Mean_Monthly_Surface_Temperature_Climatology",mv) 


ncnew<-nc_create(filename,list(var_temp))

print(paste("The file has",ncnew$nvars,"variables"))# 
print(paste("The file has",ncnew$ndim,"dimensions"))# 

data<-array(TimeTable$tem_1)
ncvar_put( nc=ncnew, varid=var_temp,data,start=c(1,1,1),count=c(nx,ny,1))

你能给我一些建议吗? 非常感谢

【问题讨论】:

  • xvals &lt;- 1:360 的长度为 360,yvals &lt;- -90:90 的长度为 181。因此,360*181 = 65160 个网格点。如果“温度”不是一个规则的网格,你需要写一个站NetCDF。如果“温度”是常规网格,则需要提供准确的 lon/x 和 lat/y 坐标,即正确的范围和正确的分辨率。
  • 谢谢@Pascal,但你的意思是站NetCDF吗?我检查了我的数据集,我的纬度和经度有这些范围:xvals
  • “温度”里面有什么?来自模型/再分析/插值的天气站或网格点?
  • lon lat tem_1 -177.5 87.5 -30.1 -172.5 87.5 -30.1 -167.5 87.5 -30.5 -162.5 87.5 -30.5 -157.5 87.5 -30.3 -152.5 87.5 -30.2 这就是我的数据集的样子跨度>
  • 所以你必须使用这个:xvals &lt;- seq(-177.5, 177.5, 5); yvals &lt;- seq(-87.5, 87.5, 5),即72经度和36纬度(72*36=2592)。

标签: r netcdf writing


【解决方案1】:
library(ncdf4)

filename="time.nc"

xvals <- seq(-177.5, 177.5, 5)
yvals <- seq(-87.5, 87.5, 5) 
nx <- length(xvals)
ny <- length(yvals)
lon1 <- ncdim_def("longitude", "degrees_east", xvals)
lat2 <- ncdim_def("latitude", "degrees_north", yvals)

time <- ncdim_def("Time","months", 1:12, unlim=TRUE)
mv <- -999 #missing value to use
var_temp <- ncvar_def("temperature", "celsius", list(lon1, lat2, time), longname="CRU_Global_1961-1990_Mean_Monthly_Surface_Temperature_Climatology", mv) 

ncnew <- nc_create(filename, list(var_temp))

print(paste("The file has", ncnew$nvars,"variables"))
#[1] "The file has 1 variables"
print(paste("The file has", ncnew$ndim,"dimensions"))
#[1] "The file has 3 dimensions"

# Some fake dataset based on latitude, to check whether the data are
# written in the correct order
data <- rep(yvals, each=nx)

# Add random -999 value to check whether missing values are correctly
# written
data[sample(1:(nx*ny), 100, replace = FALSE)] <- -999
ncvar_put(ncnew, var_temp, data, start=c(1,1,1), count=c(nx,ny,1))

# Don't forget to close the file
nc_close(ncnew)

# Verification
library(rasterVis)
out <- raster("time.nc")
levelplot(out, margin=FALSE)

【讨论】:

  • 嗨,我还有一个问题。我的 csv 有纬度、经度、温度。 xvals
  • ncvar_put(ncnew, var_temperature, data, start = c(1, 1, 1, 1), : ncvar_put: error: 你要求写入 8825524 个值,但传递的数据数组只有6147539 个条目!
  • 这很奇怪,因为它看起来无法真正获取我想要用来填充我的温度值的数据。
  • 你能给我一些建议吗?我的可变温度仍然是空的。它不包含我的 .csv 文件数据的第三列中的任何值
  • 你检查xvalsyvals的长度了吗?它们分别是 4322 和 2042,即 8825524 个网格点。 GlobT 的尺寸是多少?但最重要的是:您的 csv 文件中是否有所有网格点,还是只有地面以上的温度?对于后者,你的做法是完全错误的。
猜你喜欢
  • 2014-04-12
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 2018-10-06
  • 2012-12-11
  • 2014-12-29
  • 1970-01-01
  • 2021-02-24
相关资源
最近更新 更多