R中的netCDF文件 [英] netCDF files in R

查看:412
本文介绍了R中的netCDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从此处获得的netCDF文件,名称 precip.mon.total.v6.nc .我在R中使用ncdf包打开和分析文件.

I have netCDF file obtained from here with name precip.mon.total.v6.nc. I am using ncdf package in R to open and analyse the file.

 new <- open.ncdf("precip.mon.total.v6.nc")
    > new
    [1] "file precip.mon.total.v6.nc has 4 dimensions:"
    [1] "lat   Size: 360"
    [1] "lon   Size: 720"
    [1] "nbnds   Size: 2"
    [1] "time   Size: 1320"
    [1] "------------------------"
    [1] "file precip.mon.total.v6.nc has 1 variables:"
    [1] "float precip[lon,lat,time]  Longname:GPCC Monthly total of     precipitation Missval:-9.96920996838687e+36"

但是当我提取变量时,出现了错误

But when I extract the variable, I got the error

      > get.var.ncdf(new, "precip")
Error: cannot allocate vector of size 2.5 Gb
In addition: Warning messages:
1: In double(totvarsize) :
  Reached total allocation of 2047Mb: see help(memory.size)
2: In double(totvarsize) :
  Reached total allocation of 2047Mb: see help(memory.size)
3: In double(totvarsize) :
  Reached total allocation of 2047Mb: see help(memory.size)
4: In double(totvarsize) :
  Reached total allocation of 2047Mb: see help(memory.size)

我的查询是: (a)如何处理内存问题? (b)如何将该netCDF文件的分辨率从0.5 * 0.5更改为0.25 * 0.25数据?我已经在MATLAB中尝试过类似的问题.对于netCDF文件,它可以比R更好地解决内存问题.但是更改分辨率仍然是一个问题,因为我不擅长MATLAB.在这方面的任何帮助,我将非常感谢.

My queries are: (a) How to handle memory issue? (b) How can I change the resolution of this netCDF file from 0.5*0.5 to 0.25*0.25 data? I have tried the similar problem in MATLAB. It can tackle memory issue better than R for netCDF files. But changing resolution is still a problem as I am not good at MATLAB. I will be very thankful for any help in this direction.

推荐答案

提取变量时,需要指定所需的尺寸.目前,您正在要求R获取所有内容,因此我怀疑它正在创建一个3D阵列,该阵列可能很大.

When you extract your variable, you need to specify which dimensions you want. Currently you're asking R to get everything and so I suspect it's creating a 3D array which will likely be enormous.

ncdf4软件包通常会取代ncdf,您应该尝试使用它.您需要确定是要按时间读取数据还是按时间步读取数据.这在普通的2D网格上更容易设想:

The ncdf4 package generally supersedes ncdf, you should try using that instead. You need to decide if you want to read data by location for time or by time step for location. This is easier to envisage on a plain 2D grid:

  • 在任何时候都使用单个单元格
  • 所有地点都只需一个时间步

您的时间跨度是3D网格(尽管第3维只有两个波段),但是您的变量似乎没有使用band维度.这是一个基于ncdf4的2D工作流程,忽略了您的乐队:

Yours is a 3D grid through time (albeit with the 3rd dimension only two bands), however it looks like your variable isn't using the bands dimension. Here's a 2D workflow based on ncdf4, ignoring your bands:

包装:

install.packages("ncdf4")
library(ncdf4)

打开连接:

nc = nc_open("~/dir/dir/file.nc")

一次生成一个网格

阅读尺寸:

precip = list()
precip$x = ncvar_get(nc, "lon")
precip$y = ncvar_get(nc, "lat")

读取数据(注意start是开始的维数索引,count是从该点开始的观察数,因此这里我们在第一步就读取了整个网格)

Read data (note start is the index in dimensions to begin and count is how many observations from that point, so here we read the whole grid at the first time step):

precip$z = ncvar_get(nc, "precip", start=c(1, 1, 1), count=c(-1, -1, 1))
# Convert to a raster if required
precip.r = raster(precip)

要随时读取单个单元格

您需要找到您的单元格索引,precip$xprecip$y将有所帮助.有了它(例如,单元格x = 5和y = 10):

You need to find your cell index, precip$x and precip$y will help. Once you have it (e.g. cell x=5 and y=10):

precip.cell = ncvar_get(nc, "precip", start=c(5, 10, 1), count=c(1, 1, -1))

这篇关于R中的netCDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆