Terra不读取.NC文件(GDAL错误4)
我正在尝试使用Terra打开一个.NC文件,但是我会收到以下错误:
'' not recognised as a supported file format. (GDAL error 4)
可复制的示例(16MB NC文件):
if (!file.exists("HadISST_ice.nc")) {
download.file("https://www.metoffice.gov.uk/hadobs/hadisst/data/HadISST_ice.nc.gz","HadISST_ice.nc.gz")
R.utils:::gunzip("HadISST_ice.nc.gz")
}
library(terra)
hadISST <- rast('HadISST_ice.nc')
该文件在raster :: brick上打开,但不使用Terra:Rast:Rast,如果我从 @robert-hijmnan 答案是因为栅格使用NCDF4,而Terra则使用GDAL。
在GDAL中,.nc文件可以通过终端没有任何问题打开:
% gdalinfo HadISST_ice.nc
Driver: netCDF/Network Common Data Format
Files: HadISST_ice.nc
Size is 512, 512
但是我无法在不扔GDAL错误的情况下阅读Terra的文件4。会话信息下面:
print(sessionInfo())
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.4
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] terra_1.5-34
loaded via a namespace (and not attached):
[1] compiler_4.2.0 tools_4.2.0 Rcpp_1.0.8.3 codetools_0.2-18
and GDAL:
% gdal-config --version
GDAL 3.5.0, released 2022/05/10
I'm trying to open a .nc file with terra, but I'm getting the following error:
'' not recognised as a supported file format. (GDAL error 4)
Reproducible example here (16mb nc file):
if (!file.exists("HadISST_ice.nc")) {
download.file("https://www.metoffice.gov.uk/hadobs/hadisst/data/HadISST_ice.nc.gz","HadISST_ice.nc.gz")
R.utils:::gunzip("HadISST_ice.nc.gz")
}
library(terra)
hadISST <- rast('HadISST_ice.nc')
The file opens with raster::brick, but not with terra:rast, which if I understand correctly from @robert-hijmnan answer is because raster uses ncdf4 while terra uses GDAL.
In GDAL the .nc file opens with no issues via terminal:
% gdalinfo HadISST_ice.nc
Driver: netCDF/Network Common Data Format
Files: HadISST_ice.nc
Size is 512, 512
But I can't read the file with terra without throwing a GDAL error 4. Session info below:
print(sessionInfo())
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.4
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] terra_1.5-34
loaded via a namespace (and not attached):
[1] compiler_4.2.0 tools_4.2.0 Rcpp_1.0.8.3 codetools_0.2-18
and GDAL:
% gdal-config --version
GDAL 3.5.0, released 2022/05/10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:这个问题似乎与NetCDF驱动程序未安装的Terra是一个特定的MacOS问题:
r terra gdal版本不正确,无法读取.nc(gdal错误4)
安装Terra的开发版本解决了问题:
install.packages(' Terra',repos ='https://rspatial.r-universe.dev')
edit: the problem seems to be related to be a MacOS specific issue with terra not being installed with netCDF drivers:
R terra gdal version incorrect, cannot read .nc (GDAL error 4)
installing the development version of terra solves the problem:
install.packages('terra', repos='https://rspatial.r-universe.dev’)
我刚刚经历的
Terra
软件包的另一个可能原因4的原因:如果您通过Windows中的R从Web下载了多个.NC文件,请确保使用
mode =“ WB”
在函数参数中Another possible cause of the error 4 with
terra
package which I have just experienced:If you have downloaded multiple .nc files from the web through R in Windows make sure to use
mode = "wb"
in the function argument