Reputation: 182
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
Upvotes: 2
Views: 2239
Reputation: 140
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")
Upvotes: 2
Reputation: 182
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’)
Upvotes: 5