我有WRF输出的netCDF文件与1499_749_91尺寸生产的“墨卡托”投影在非洲之角。我想把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)
发布于 2020-07-07 02:53:21
标准的方法是
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
来实现。
https://stackoverflow.com/questions/62760290
复制相似问题