[R-sig-Geo] Ncdf with missing variable columns
matteo lattuada
m.lattuada87 at gmail.com
Mon Jun 13 10:14:08 CEST 2016
Hi all,
I'm trying to get some variables iteratively from a directory with a large
number of .nc files and create a csv.file.
I wrote this script:
filenames <- list.files("directory", pattern="*.nc", full.names=TRUE)
res<-lapply(filenames,function(nc){
t<-nc_open(filename=nc)
cast<-ncvar_get(t,"wod_unique_cast")
temp<-ncvar_get(t,"Temperature")
sal<-ncvar_get(t,"Salinity")
x<-ncvar_get(t,"lon")
y<-ncvar_get(t,"lat")
date<-ncvar_get(t,"date")
depth<-ncvar_get(t,"z")
df<-data.frame(cast,x,y,date,depth,temp,sal)
})
df.new<-ldply(res,data.frame)
This script is working well. The problem is that some of the .nc file miss
one of the variables. For example some don't have measurements of
"salinity". When a variable is not found, the iteration stops. Do you have
any hint to get all the variables present in a .nc file and add a NA column
for the absent variables?
Thank you
Kindest Regards
Matteo
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list