如何在R中更改分辨率(或重调)数据 [英] How to change the resolution (or regrid) data in R

查看:323
本文介绍了如何在R中更改分辨率(或重调)数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,该数据集包括lon,lat和1961年至1970年的月平均变量(例如,温度或降水)。该数据集的分辨率为0.5 x 0.5 lon / lat,涵盖了整个地球,已下载作为.NC文件,我使用以下方法提取了R中的数据:

I have a dataset consisting of lon, lat and a monthly mean variable (e.g. temperature or precipitation) covering 1961 to 1970. The dataset is at a resolution of 0.5 by 0.5 degree lon/lat and covers the whole globe and was downloaded as an .NC file which I have extracted the data in R by using:

library(ncdf)
f <- open.ncdf("D:/CRU/cru_ts3.21.1961.1970.tmp.dat.nc")
A <- get.var.ncdf(nc=f,varid="tmp")
B <- get.var.ncdf(nc=f,varid="lon")
C <- get.var.ncdf(nc=f,varid="lat")
D <- cbind(expand.grid(B, C))
E <- expand.grid(A)

扩展网格(E)是包含31,104,000行变量的数据表,扩展网格(D)是包含259,200行lon / lat的数据表。如果乘以259,200 * 10年* 12个月,则得出31,104,000。因此,可以通过以下方式将表E切成月度值:

The expanded grid (E) is a data table consisting of 31,104,000 rows of the variable and the expanded grid (D) is a data table consisting of 259,200 rows of lon/lat. If you multiply 259,200 * 10 years * 12 months you get the 31,104,000. Hence the table E can be chopped up into monthly values by using:

Month <- 1
Start <- (Month-1)*(259200)+1
Finish <- (Month*259200)
G <- E[Start:Finish,]
H <- expand.grid(G)
I <- cbind(D,H) 

因此我是现在是第一个月(即1961年1月)的数据表,其中包括lon,lat和变量。数据示例如下:

Therefore I is now a data table of the first month (i.e. January 1961) consisting of lon, lat and the variable. An example of the data is given below:

        lon    lat tmp
49184 -68.25 -55.75 7.5
49185 -67.75 -55.75 7.6
49186 -67.25 -55.75 7.6
49899 -70.75 -55.25 6.8
49900 -70.25 -55.25 7.0
49901 -69.75 -55.25 6.9
49902 -69.25 -55.25 7.1
49903 -68.75 -55.25 6.8
49904 -68.25 -55.25 7.6
49905 -67.75 -55.25 8.2

现在是我的问题。网格的当前分辨率为0.5 * 0.5度,我想重新定义数据,因此分辨率为0.25 * 0.25度。我不想对数据做任何特别聪明的事情,所以我只想让0.25网格采用它所在的0.5网格的值,即每个0.5 * 0.5网格包含4个0.25 * 0.25网格,而我只想4个0.25 * 0.25网格具有与0.5 * 0.5网格相同的值。

Now for my question. The current resolution of the grid is 0.5 * 0.5 degrees, and I would like to "regrid" the data so the resolution is 0.25 * 0.25 degrees. I don't want to do anything particularly clever with the data, so I just want the 0.25 grid to take the value of the 0.5 grid that it sits in i.e. each 0.5*0.5 grid contains 4 0.25*0.25 grids and I just want the 4 0.25*0.25 grids to have the same value as the 0.5*0.5 grid.

我看过栅格,但似乎无能为力。

I've looked at raster but don't seem to be able to do anything with it.

推荐答案

这不是R解决方案,只是要指出的是,您可以在Linux / MAC OS环境中使用CDO轻松从命令行重新注册netcdf文件。从您的描述中听起来,您似乎想使用最近邻插值,对于0.25度常规网格,这将是

This is not an R solution, but just to point out that you can use CDO to regrid netcdf files very easily from the command line in a linux/MAC OS environment. From your description it sounds as if you want to use nearest neighbour interpolation, which for a 0.25degree regular grid would be

cdo remapnn,r1440x720 in.nc out.nc

但是,您也可以使用一阶或二阶保守重映射。例如第一订单:

However, you can also use first or second order conservative remapping. For example for first order:

cdo remapcon,r1440x720 in.nc out.nc

然后,您可以以与当前相同的方式在regridded字段中读入R。

You can then read in the regridded field into R in the same way you are currently doing.

这篇关于如何在R中更改分辨率(或重调)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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