从不规则间隔的x和y的ncdf创建光栅图块

时间:2019-01-09 20:03:32

标签: r raster ncdf4

我对于将R用于GIS还是很陌生。我有一个netcdf文件,其中包含多个具有多个维度(x,y,z,值和时间)的变量。我正在尝试将其变成光栅砖。数据很大,因此我需要从指定的时间窗口和z(depth)中提取数据。这不是问题,并使用以下代码提取具有适当维数的数组。

library(ncdf4)
library(raster)
t <- ncvar_get(nc, "model_time")
t1<-ncvar_get(nc,"model_time_step")
tIdx<-t[t> 20120512 & t < 20120728]
tIdx2<-which(t> 20120512 & t < 20120728)
# Depths profiles < 6 meters
dIdx<-which(nc$dim$depthu$vals <6)
# ncdf dimension lengths
T3      <- nc$var[[7]]
varsize <- T3$varsize
# Define the data (depths,time,etc.) you wish to extract from the ncdf
start <- c(x = 1, y= 1,depthu=1, time_counter = min(tIdx2))
count <- c(x = max(varsize[1]), y = max(varsize[2]),depthu=1, time_counter = 
max(tIdx2)-min(tIdx2)+1)
# order of the dimensions
dim.order <- sapply(nc$var$votemper$dim, function(x) x$name)
temp<-ncvar_get(nc,"votemper",start=start[dim.order],count=count[dim.order])
nc$var$votemper

我的数据示例(删除深度/ z和时间维度)

   temp<-structure(c(0,0,0,0,0,0,0,15.7088003158569,15.3642873764038,14.9720048904419,,15.9209365844727,14.9940872192383,15.0184164047241,15.0260219573975, 0,15.7754755020142, 15.424690246582, 15.6697931289673,15.6437339782715, 0,15.6151847839355, 15.5979156494141, 15.6487197875977,15.432520866394), .Dim = c(x = 5L, y = 5L))

从ncdf提取的纬度和经度是不规则间隔的,并且每个维度都是二维的(即,每个像元的不规则间隔的纬度和经度)

lon<-structure(c(-71.2870483398438,-71.2038040161133,-71.1205596923828,-71.0373153686523, -70.9540710449219, -71.2887954711914, -71.2055587768555,-71.122314453125, -71.0390701293945,-70.9558258056641,-71.2905654907227,-71.2073211669922,-71.1240844726562,-71.0408401489258,-70.9576034545898,-71.292350769043,-71.209114074707, -71.1258773803711, -71.0426330566406,-70.9593963623047, -71.2941513061523, -71.2109222412109, -71.127685546875,-71.0444488525391, -70.9612045288086), .Dim = c(5L, 5L))

lat<-structure(c(38.5276718139648, 38.529125213623, 38.5305824279785,38.532039642334, 38.5334968566895, 38.5886116027832, 38.5900802612305,38.591552734375, 38.5930252075195, 38.5944976806641, 38.6494789123535,38.6509628295898, 38.6524467468262, 38.6539344787598, 38.6554222106934,38.7102699279785, 38.7117652893066, 38.713264465332, 38.7147674560547,38.7162704467773, 38.7709808349609, 38.7724952697754, 38.7740097045898,38.7755241394043, 38.777042388916), .Dim = c(5L, 5L))

通常,我将使用

从此数据生成栅格图块
Temp_brick <- brick(temp, xmn=min(lat), xmx=max(lat), ymn=min(lon), ymx=max(lon),transpose=T)
Temp_brick<-t(flip(Temp_brick,1))

但是,这不能解决不规则间距的问题,并且栅格像元值位于错误的位置(lon,lat)。我搜索了堆栈溢出和其他gis帮助资源,但找不到解决方案的类似问题,或者我问的不是正确的问题。我不太确定该怎么做。不知道从netcdf提取数据时是否应该处理此问题,或者是否应在没有定义范围的情况下创建栅格块之后处理该问题。我试图找到一种方法来为栅格定义lon lat,而没有任何运气。尝试将lon,lat和value转换为3列数据框,然后使用raster :: rasterFromXYZ函数。这对于我正在处理的数据大小来说不够快,实际上是197(x)* 234(y)* 2(z)* 900(时间)* 5(变量)* 12(年(单独的netcdf文件)。

非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

带有akima的选项,首先将数据插入常规网格,然后将其转换为栅格:

<svg width="100px" height="100px" viewBox="0 0 100 100">
  <circle stroke="navy" stroke-width="5" fill="pink" cx="50" cy="50" r="40"/>
</svg>

<svg width="100px" height="100px" viewBox="0 0 100 100">
  <circle stroke="green" stroke-width="5" fill="gold" cx="50" cy="50" r="40"/>
</svg>

检查function的参数以选择要执行的内插等,如果使用外推,请小心!

我也看到了method

欢呼