r - Extracting values from nc files in a for loop -
r - Extracting values from nc files in a for loop -
i have 4 netcdf files in folder , want extract values these files. str of file is:
[1] "file c:1.dbl.nc has 2 dimensions:" [1] "lat size: 1" [1] "lon size: 1" [1] "------------------------" [1] "file c:\\users\\data.nc has 3 variables:" [1] "short so[lon,lat] [1] "short il[lon,lat] [1] short fg[lon,lat]
my loop is:
a<-list.files("c:\\users\\data", "*.nc", full.names = true) for(i in 1:length(a)){ f <- open.ncdf(a[i]) = get.var.ncdf(nc=f,varid="so",verbose=true) b <- get.var.ncdf(nc=f,varid="il") c <- get.var.ncdf(nc=f,varid="fg") write.table(t(rbind(a,b,c)),file="output-all.txt")}
there no errors there 1 line of results in output text file:
"a" "b" "c" "1" 500 200 300
which should many number of files in folder (4), instance:
"a" "b" "c" "1" 500 200 300 "2" 500 200 300 "3" 500 200 300 "4" 500 200 300
it seems loop not taking place here.
move write.table
command out of loop. like:
a<-list.files("c:\\users\\data", "*.nc", full.names = true) d<-matrix(na,length(a),3) for(i in 1:length(a)){ f <- open.ncdf(a[i]) = get.var.ncdf(nc=f,varid="so",verbose=true) b <- get.var.ncdf(nc=f,varid="il") c <- get.var.ncdf(nc=f,varid="fg") d[i,]<-t(rbind(a,b,c))} write.table(d,file="output-all.txt")
r for-loop netcdf
Comments
Post a Comment