1

Trying to convert NOAA Snow data (NetCDF) into Raster format in R. This data has been pre-processed by me in CDO (interpolated from weekly-daily).

library(raster)
library(ncdf4)
nc<-nc_open('NOAA_Snow_JanJune2016.nc')

# extract variable name, size and dimension
v <- nc$var[[1]]
size <- v$varsize
dims <- v$ndims
nt <- size[dims]              # length of time dimension
lat <- nc$dim$latitude$vals   # latitude position
lon <- nc$dim$longitude$vals  # longitude position

# read sea ice variable
r<-list()
for (i in 1:nt) {
  start <- rep(1,dims)     # begin with start=(1,1,...,1)
  start[dims] <- i             # change to start=(1,1,...,i) to read    timestep i
  count <- size                # begin with count=(nx,ny,...,nt), reads entire var
  count[dims] <- 1             # change to count=(nx,ny,...,1) to read 1 tstep

  dt<-ncvar_get(nc, varid = 'snow_cover_extent', start = start, count = count)

  # convert to raster
  r[i]<-raster(dt)
}

Returns the following 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!

Has anyone else had and solved this problem? I wonder if prepping the file in CDO is causing the problem. The (.nc) data can be accessed here:

https://drive.google.com/file/d/0Bz0W7Ut_SNfjeE9ObXpySzJ5UWs/view?usp=sharing

Many thanks!

Ndharwood
  • 123
  • 3
  • 11
  • try to look at this post https://stackoverflow.com/questions/42982599/netcdf-to-raster-brick-unable-to-find-inherited-method-for-function-brick-for/43244622#comment74389320_43244622 – Eko Susilo May 29 '17 at 06:58

1 Answers1

1

Use ncdf4 to explore your ncdf attributes

library(ncdf4)
nc<-nc_open('D:/NOAA_Snow_JanJune2016.nc')
print(nc)
varname<-names(nc$var)

Use raster to convert your ncdf to raster

library (raster)
r<-brick('D:/NOAA_Snow_JanJune2016.nc',varname='snow_cover_extent')

Here the plot

spplot(r[1])

enter image description here

Eko Susilo
  • 250
  • 2
  • 15
  • Thank you - got that to work, but my plot (as above) is the incorrect orientation. (it may actually have inverted/reversed it, too) The code you provided before doesn't work either: `rt<-t(r)` `extent(rt)<-extent(c(range(lon), range(lat)))` Returns `Warning messages: 1: In min(x, na.rm = na.rm) : no non-missing arguments to min; returning Inf 2: In max(x, na.rm = na.rm) : no non-missing arguments to max; returning -Inf 3: In min(x, na.rm = na.rm) : no non-missing arguments to min; returning Inf 4: In max(x, na.rm = na.rm) : no non-missing arguments to max; returning -Inf` – Ndharwood Jun 07 '17 at 16:48