I'm having trouble trying to read a plus 2Gb NetCDF file from https://cds.climate.copernicus.eu/cdsapp#!/dataset/satellite-land-cover?tab=form in R
If I try to read the whole file R returns me a message that I don't have enough memory,
'Error: cannot allocate vector of size 31.3 Gb'
as it was reported by the post https://gis.stackexchange.com/questions/360225/how-to-reduce-the-size-of-a-netcdf-in-r
Following this post advice, I decided to read only the part of the file based on the geographic coordinations of the given area. Here is my code:
#' load any packages
library(ncdf4)
# set path and filename
ncpath <- "C:\\Users\\Me\\Documents\\Science\\GIS\\Global Land Cover\\"
ncname <- "C3S-LC-L4-LCCS-Map-300m-P1Y-2018-v2.1.1"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "lccs_class"
# open a netCDF file
ncin <- nc_open(ncfname)
print(ncin)
# get longitude and latitude
lon <- ncvar_get(ncin,"lon")
nlon <- dim(lon)
head(lon)
lat <- ncvar_get(ncin,"lat")
nlat <- dim(lat)
head(lat)
print(c(nlon,nlat))
#' create a bounding box to work with a subset
LonIdx <- c(841, 842, 844,845,846,847,848)
LatIdx <- c(93,94,95,96)
LonIdx
Show in New Window
[1] 841 842 844 845 846 847 848
LatIdx
[1] 93 94 95 96
However, when I try to execute the code for the Subset:
Susbset <- ncvar_get(ncin, dname,
start = c(LatIdx[1], LonIdx[1]),
count = c(length(LatIdx),length(LonIdx)))
I get the error:
Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset, : Error: variable has 3 dims, but start has 2 entries. They must match!
Can anyone help me.