0

我知道周围有类似的问题和问题,但我的问题非常具体。

我正在使用 30 角秒分辨率的 WorldClim 和 Chelsa 气候数据集。在此过程中,我想将特定数据集放入 ncdf4 文件中。在此过程中,必须将数据转换为要转换为矩阵的向量。然后将该矩阵反转并最终转置。然后将输出数据转换为一个数组,稍后将其放入 ncdf 文件中。

我的示例代码如下所示。

library(raster)
library(ncdf4)

# Download of one of the raster files (here 110 Mb .tif-Raster from the Chelsa server)
temp.raster <- raster("https://envidatrepo.wsl.ch/uploads/chelsa/chelsa_V1/bioclim/integer/CHELSA_bio10_08.tif")

#determining the cellsize
cellsize <- (temp.raster@extent@xmax-temp.raster@extent@xmin)/temp.raster@ncols
# Set Longitudes
lon <- as.array(seq(temp.raster@extent@xmin+cellsize/2,
                    temp.raster@extent@xmax-cellsize/2,
                    (temp.raster@extent@xmax-temp.raster@extent@xmin)/temp.raster@ncols))
nlon <- length(lon)

# Set Latitudes
lat <- as.array(seq(temp.raster@extent@ymin+cellsize/2,
                    temp.raster@extent@ymax-cellsize/2,
                    (temp.raster@extent@xmax-temp.raster@extent@xmin)/temp.raster@ncols))
nlat <- length(lat)


temp.raster.data <- t(apply(matrix(data = as.numeric(values(temp.raster)),
                                             nrow = nlat,
                                             ncol = nlon,
                                             byrow = TRUE),
                            2,
                            rev))
temp.raster.data <- array(temp.raster.data, dim=c(nlon,nlat,1))

在从光栅文件中提取值的过程中,该错误发生在values()-function 处。

如其他问题中建议的那样设置更高的内存限制不起作用,并且添加更多 RAM 不是一种选择。

有人建议解决此问题的解决方法或更简单的方法吗?

sessionInfo()的是

R 版本 4.0.2 (2020-06-22)

平台:x86_64-w64-mingw32/x64(64位)

运行于:Windows 10 x64(内部版本 18363)

配备 12 GB 内存 ( memory.limit()--> 11984)

感谢您的时间!

4

1 回答 1

0

一种更简单的方法是使用writeRaster

f <- "https://envidatrepo.wsl.ch/uploads/chelsa/chelsa_V1/bioclim/integer/CHELSA_bio10_08.tif"
bf <- basename(f)
download.file(f, bf, mode="wb")

library(raster)
r <- raster(bf)
ncfile <- gsub(".tif", ".nc", bf)
x <- writeRaster(r, ncfile)

你也在重新发明轮子;考虑以下。

#determining the cellsize
cellsize <- res(r)

# Lon/lat
lon <- xFromCol(r, 1:ncol(r))
lat <- yFromRow(r, 1:nrow(r))
于 2020-07-16T19:50:12.373 回答