-1

I have 365 .nc files located in a folder containing daily soil moisture information. I want to extract data at five-coordinate locations for the whole year and write them into five separate csv files. My code is attached below. However, I am getting this error after the line:

s <- stack(ff)

>Error in if (is.na(get("has_proj_def.dat", envir = .RGDAL_CACHE))) { : argument is of length zero In addition: Warning message: In .varName(nc, varname, warn = warn) : varname used is: sm If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor 

No idea how to proceed further.

library(raster)
library(ncdf4)
ptf <- "D://SMOS_ECV_SM//SMOS_ECV_SM//ECV_SM_Data_1978_2010//1978"
ff <- list.files(path=ptf, pattern="[.]nc$", full.names=TRUE)

s <- stack(ff)
points <- rbind(c(0,1), c(100,120), c(80,5), c(85,4), c(82,4))
v <- extract(s, points)

for (i in 1:ncol(v)) {
  write.csv(v[,i,drop=FALSE], paste0("file", i, ".csv"))
}
  • Please change your question to show where the actual error happens. I suppose it is in line `s <- stack(ff)` --- but then you need to unpack that. Try for a single file. If that fails just show that and make the file available. Also please report the version of R and rgdal (and first upgrade to the latest version). – Robert Hijmans Jun 24 '20 at 15:56

1 Answers1

0
library(raster)
#Loading required package: sp
f <- list.files("try", full=T)

First try for a single file

r <- raster(f[1])
#Loading required namespace: ncdf4
#Warning message:
#In .varName(nc, varname, warn = warn) : varname used is: sm
#If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor

To get rid of the warning:

r <- raster(f[1], varname="sm")

Now for all files

s <- stack(f, varname="sm")
s
#class      : RasterStack 
#dimensions : 720, 1440, 1036800, 2  (nrow, ncol, ncell, nlayers)
#resolution : 0.25, 0.25  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs 
#names      : Soil.Moisture.1, Soil.Moisture.2 

Extract values

points <- rbind(c(-96.7, 47), c(34.55, 54.85))
v <- extract(s, points)

v
#     Soil.Moisture.1 Soil.Moisture.2
#[1,]          0.3254          0.3018
#[2,]          0.3386          0.3386
 
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Some other errors were coming during the stack operation. However, they got resolved after reinstalling the raster package. – fantasy_world Jun 26 '20 at 03:35