Rabbidabbu
Rabbidabbu

Reputation: 43

How to append information to an array in a netCDF file in R

This is my first time posting on stackoverflow after a long time lurking, so please be gentle :)

I am working in R with a large dataset of climate data split across many files, performing an analysis on these files one at a time. Each file has exactly the same data, just at different times (approx. 6 months of data per file). After analysis of each file I output a multidimensional array of pressure values at coordinates (long, lat, height, time).

I then want to save these arrays to a netCDF file, but because the data from each file is just the same but separated in time, I eventually want the outputted arrays to be concatenated into one big array in the netCDF, i.e. for each file add new data to the array that already exists in the netCDF file.

While I can perform this analysis for the first file, create the netCDF file and write the array to it absolutely fine, when I try to append data from subsequent files to the netCDF file it returns

Error in R_nc_inq_varndims: NetCDF: Not a valid ID

Error in varndims.ncdf(nc, varid) : error returned from C call

When creating the netCDF file initially I used

t <- dim.def.ncdf("Time","Hours since January 1st 1901",seq(time[1],time[ntime],24),unlim=TRUE)
latitude <- dim.def.ncdf("Latitude","Degrees",seq(90,-90,-3))
longitude <- dim.def.ncdf("Longitude","Degrees",seq(0,359.25,3))
h <- dim.def.ncdf("Geopotential Height",'km',seq(1,30))

define the variable itself

pressvar <- var.def.ncdf("Pressure", "hPa", list(longitude,latitude,h,t), longname="Pressure at set geopotential heights", missval=-999,prec="double")

and then create the file and add the data

ncnew <- create.ncdf(new.name, pressvar)
starting <- c(1,1,1,1)
ending <- c(nlon, nlat, 30, ntime)
put.var.ncdf(ncnew, pressvar, pressure, start = starting,  count = ending)
close.ncdf(ncnew)

Which as I say all works fine. I then open the next file, repeat the analysis and produce my pressure array, open up the original netCDF file and

put.var.ncdf(ncnew, pressvar, pressure, start = c(1,1,1,time[1]), count = c(nlon,nlat,30,time[ntime]))

where the variables time and ntime have been read in from the second file. I think the problem is with my use of the start and count functions, or my attempted use of an unlimited time dimension, or possibly even in thinking that this is possible. Do I need to redefine the dimension t and the variable pressvar for each file? Or will this result in saving a second, identically named variable to the netCDF file? As you might be able to tell I've been slightly tied up in knots by this.

Any help appreciated. Thanks!

Upvotes: 4

Views: 2387

Answers (1)

koekenbakker
koekenbakker

Reputation: 3604

Seems like put.var.ncdf cannot find a variable in your NetCDF file. Try changing the last line to:

put.var.ncdf(nc = ncnew, varid = 'Pressure', vals = pressure, start = c(1,1,1,time[1]),  count = c(nlon,nlat,30,time[ntime]))

Now varid correctly refers to a variable name in the NetCDF file, instead of referring to the object created by var.def.ncdf.

Upvotes: 1

Related Questions