Skip to content
Cristian Lussana edited this page Feb 4, 2021 · 1 revision

R-Examples

seNorge_2018 Latest (see also the manual of the R-package ncdf4)

library(ncdf4)
f<-"https://thredds.met.no/thredds/dodsC/senorge/seNorge_2018/Latest/seNorge2018_20210130.nc"
nc<-nc_open(f)
for (i in 1:nc$nvar) print( paste(i,nc$var[[i]]$name))
tg <- nc$var[[6]]
data <- ncvar_get( nc, tg )
print(data)
nc_close(nc)

# Want to read more than one file, then loop over a list of files
# e.g. files from 2021-01-01 to 2021-01-10
files<-paste0("https://thredds.met.no/thredds/dodsC/senorge/seNorge_2018/Latest/seNorge2018_",format(seq(as.POSIXct("2021-01-01",format="%Y-%m-%d"),as.POSIXct("2021-01-10",format="%Y-%m-%d"),by="1 day"),format="%Y%m%d"),".nc")
print(files)

seNorge_2018 Archive (see also the manual of the R-package ncdf4):

library(ncdf4)
f<-"https://thredds.met.no/thredds/dodsC/senorge/seNorge_2018/Archive/seNorge2018_2021.nc"
nc<-nc_open(f)
for (i in 1:nc$nvar) print( paste(i,nc$var[[i]]$name))
tg <- nc$var[[2]]
varsize <- tg$varsize
ndims <- tg$ndims
nt <- varsize[ndims]
for( i in 1:nt ) {
  start <- rep(1,ndims)
  start[ndims]
  count <- varsize
  count[ndims] <- 1
  data <- ncvar_get( nc, tg, start=start, count=count )
  timeval <- ncvar_get( nc, tg$dim[[ndims]]$name, start=i, count=1 )
  print(paste("Data for variable",tg$name,"at timestep",i,
  " (time value=",timeval,tg$dim[[ndims]]$units,"):"))
  print(data)
}
nc_close(nc)

Clone this wiki locally