Terra不读取.NC文件(GDAL错误4)

发布于 2025-02-10 12:30:31 字数 1374 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2025-02-17 12:30:31

编辑:这个问题似乎与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’)

﹏雨一样淡蓝的深情 2025-02-17 12:30:31

我刚刚经历的Terra软件包的另一个可能原因4的原因:

如果您通过Windows中的R从Web下载了多个.NC文件,请确保使用mode =“ WB”在函数参数中

download.file(url = "https://example/xx.nc", destfile = "/example.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

download.file(url = "https://example/xx.nc", destfile = "/example.nc", mode = "wb")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文