用R将数据写入netCDF文件

编程入门 行业动态 更新时间:2024-10-18 20:19:45
本文介绍了用R将数据写入netCDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用R包"ncdf4"使用自己的.csv文件中的数据来创建netCDF文件. 我的数据集由3列组成:经度,纬度和温度,它有2592行. 我一直在遵循软件包中的建议,将维度和变量添加到netCDF文件中.一切顺利,直到我想在文件中写入温度数据为止. 我收到此错误:

I am trying to use the data on my own .csv file to create a netCDF file using the R package "ncdf4". My dataset is composed by 3 columns: longitude, latitude and temperature and it has 2592 rows. I have been following the suggestions in the package to add the dimension and the variables to the netCDF file. Everything is fin until I want to write the temperature data on my file. I got this error:

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

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

Could you please suggest me something? Many thanks

推荐答案

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)

更多推荐

用R将数据写入netCDF文件

本文发布于:2023-10-26 18:41:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   数据   netCDF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!