我有WRF输出的netCDF文件,其尺寸为1499 749 ,尺寸是在非洲之角的“墨卡托”投影产生的。我想将netCDF文件转换为栅格堆栈以进行进一步的分析。我一直在尝试不同的选择,但对我而言不起作用。我在错误的位置获取价值。我在这方面需要帮助,非常感谢。
这是代码:
ro_rast <- nc_open("wrf_CAM0_daily_pre.nc")
pre <- ncvar_get(ro_rast, "pre") ro_rast$dim$lon$vals -> lon ro_rast$dim$lat$vals -> lat ro_rast$dim$ncl2$vals -> time rm(ro_rast)
r1_brick <- brick(pre, xmn=min(lat), xmx=max(lat), ymn=min(lon), ymx=max(lon), crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
names(r1_brick)<- seq(as.Date('2018-06-01'), as.Date('2018-08-31'), 'days')
# convert names of layer into date par(mar = c(2, 2, 2, 2))
cam1_mean <- t(calc(r1_brick, sum))
# seasonal sum precipitation
cam1 <- flip(cam1_mean, direction = 2)
library(akima)# intepolation
lonlat_reg <- expand.grid(lon = seq(min(lon), max(lon), length.out = 1499),
lat = seq(min(lat), max(lat), length.out = 749))
test <- interp(x = as.vector(lon), y = as.vector(lat), z = as.vector(pre),
xo = unique(lonlat_reg[,"lon"]), yo = unique(lonlat_reg[,"lat"]),
duplicate = "error", linear = FALSE, extrap = FALSE)
test <- interp(x = as.vector(lon), y = as.vector(lat), z = as.vector(pre),
nx = 1499, ny = 749, linear = FALSE, extrap = FALSE)
# turn into a raster
test_ras <- raster(test)
答案 0 :(得分:0)
标准方法是
library(raster)
b <- brick("wrf_CAM0_daily_pre.nc")
它不起作用,您能指出我们所用的文件吗? 我收到此错误消息(您应该已将其添加到您的问题中)。
Error in .rasterObjectFromCDF(x, type = objecttype, band = band, ...) :
cells are not equally spaced; you should extract values as points
我检查了文件,在这种情况下,栅格不是常规网格。像元的大小随纬度而变化。该文件未提供所使用的坐标参考系统的x和y值。因此,您最好的办法是像使用ncdf4
或其他包的接口一样,将这些值提取为点。这样便不能直接制作RasterBrick。但是您可以使用rasterize
或interpolate
。