简体   繁体   English

使用 R 从 NetCDF4 文件访问大型数据集

[英]accessing large dataset from NetCDF4 file using R

I am trying to use data from this file (MOD13.A2010.unaccum.nc4) in a project.我正在尝试在项目中使用此文件 (MOD13.A2010.unaccum.nc4) 中的数据。 I have installed and loaded the ncdf4, raster, ggplot2, and viridis librarys.我已经安装并加载了 ncdf4、raster、ggplot2 和 viridis 库。 I open the file successfully using:我使用以下方法成功打开文件:

mcd_file <- nc_open("C:\\Program Files\\RStudio\\R\\MCD13.A2010.unaccum.nc4")

and can access attributes of mcd_file by highlighting mcd_file and clicking the RUN button and by using:并且可以通过突出显示 mcd_file 并单击 RUN 按钮并使用以下命令来访问 mcd_file 的属性:

vars <- names(my_file$var)
print(vars)

This indicates that mcd_file has 5 variables, with time_bnds being the second variable, and the dataset NDVI as the third variable.这表明 mcd_file 有 5 个变量,time_bnds 是第二个变量,数据集 NDVI 作为第三个变量。

I can access correct information regarding the time_bns variable using this:我可以使用以下方法访问有关 time_bns 变量的正确信息:

time.layers < ncvar_get(mcd_file, "time_bnds")

but when I try this:但是当我尝试这个时:

NDVI <- ncvar_get(mcd_file, "NDVI")

I get an error message that reads:我收到一条错误消息,内容如下:

Error: cannot allocate vector of size 49.5 Gb错误:无法分配大小为 49.5 Gb 的向量

I looked up the meaning of the error message, and it means that I don't have enough RAM to hold all the information in NDVI, but then I don't know anyone with over 49.5 Gb of RAM.我查找了错误消息的含义,这意味着我没有足够的 RAM 来保存 NDVI 中的所有信息,但是我不知道任何人的 RAM 超过 49.5 Gb。 Yet people do analyze this file using R so the information in NDVI should be accessible using R somehow.然而,人们确实使用 R 分析了这个文件,因此应该可以使用 R 以某种方式访问​​ NDVI 中的信息。

I know that raster objects from the raster library can be used to access data in files that are too big to fit in RAM.我知道光栅库中的光栅对象可用于访问太大而无法放入 RAM 的文件中的数据。 But I can't figure out how to extract the information in NDVI from the original file so I can write it to file without the other information in the original file so I can than use raster objects to access it.但是我不知道如何从原始文件中提取 NDVI 中的信息,这样我就可以将它写入文件,而原始文件中没有其他信息,这样我就可以使用光栅对象来访问它。 This:这个:

write.csv(my_file$NDVI, file = "NDVI.csv")

creates a file called NDVI, but it is empty.创建一个名为 NDVI 的文件,但它是空的。

This:这个:

write.csv(as.raster(ncvar_get(my_file, "NDVI")),file = "NDVI2010.csv")

generates this error message:生成此错误消息:

Error: cannot allocate vector of size 49.5 Gb错误:无法分配大小为 49.5 Gb 的向量

Can anyone help me out?谁能帮我吗?

Solved.解决了。

Don't need to go through base R at all.根本不需要通过基础 R。 Just let the raster package do it's magic.只需让光栅包发挥作用即可。

>f <- "C:\\Program Files\\RStudio\\R\\MCD13.A2010.unaccum.nc4"
>b <- brick(f)  

The raster program opens the indicated file and accesses the data set therein, but leaves the data on disk, bringing out the desired data in chunks when needed.光栅程序打开指定的文件并访问其中的数据集,但将数据留在磁盘上,在需要时分块取出所需的数据。

Plotting a raster layer uses similar syntax:绘制栅格图层使用类似的语法:

>x <- desiredBandToPlot
>r <- raster(f, band = x)
>plot(r)

Because each layer has 28,000,000 pixels of data to plot, it took my computer (with AMD A8 CPU) about 5 minutes for the plot to be (compressed and then) displayed, but it works.因为每层有 28,000,000 像素的数据要绘制,所以我的计算机(使用 AMD A8 CPU)花了大约 5 分钟时间(压缩然后)显示该图,但它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM