This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Hello Robert, We do not maintain either the ncdf4 or RNetCDF packages for R, so I'm afraid I cannot say for certain what is happening with your script. You also don't mention specifically what the error is, but I assume that it is a failure to set new values in the existing file? The script you provided *looks* ok, as I am familiar with R, but since I don't know anything about the ncdf4 package you might contact the maintainer and see if they can provide some insight. I'm sorry I can't provide a more immediately helpful answer, -Ward > Dear UNIDATA, > I am a graduate student at Vrije Universiteit Brussels and am working with > NetCDF landsat images in R. I have a problem with writing data into a new > NetCDF file that i created. > The aim is to write data at rrc_1609 band and loop this through 62 images. > below is the script i used. > kindly assist me. Highly looking forward to your feedback. > > setwd("~/Documents/Landsat") > #Intall the NetCDF libraries: sudo apt-get install libnetcdf-dev > #Install the ncdf package in R: install.packages("ncdf4") > > > library("ncdf4") > library("RNetCDF") > > fname<-file.choose() > fid<-open.nc(fname) > > print.nc(fid) > newnc<-var.get.nc(fid,"rrc_1609") > nc_open(newnc) > > # Define netcdf dimensions: name, units and value > londim1 <- ncdim_def("lon", "degrees_east", 1:3342) > latdim2 <- ncdim_def("lat", "degrees_north", 1:2213) > > > # Define objets of class var.def.ncdf for the netcdf file with its name, > units, dimension(s) and missing value > var1 <- ncvar_def("lon", "degrees", londim1, 3342) > var2 <- ncvar_def("lat", "degrees", latdim2, 2213) > var3 <- ncvar_def("MYrrc_1609", "-", list(londim1, latdim2), -999, longname > = "An example of 2-dim variable") > > > # Create a netcdf file with its variables var1, var2 and var3 > filename <- ("ncnew.nc") > ncout<-nc_create("ncnew.nc",var3) > > > ncvar_get(ncout) > > nc_close(ncout) > nc_open(ncout) > nc_open("ncnew.nc",TRUE) > ## Write data variables to a netcdf file > data1<-c(1:3342) > ncvar_put(ncout,"lon",data1) > data2<-c(1:2213) > ncvar_put(ncout,var2,data2) > data3<-c(1:7395846) > ncvar_put(ncout,var3,data3) > > ? > ncnew.nc > <http://t.sidekickopen50.com/e1t/c/5/f18dQhb0S7lC8dDMPbW2n0x6l2B9nMJW7t5XYg4XXLDdN1qMxhdS9_qqW64JdXC56dxXpf4tsG-d02?t=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F0B2vMX4uM3XVKQzMtLV9ZY2JZQVk%2Fview%3Fusp%3Ddrive_web&si=6132647858798592&pi=f6fdcbe1-763a-4d3e-ba93-7ed75ed5cec2> > ? > > nc_close(ncout) > > -- > Robert Runya > MSc O & L, > Faculty of science & Bioengineering > Vrije Universiteit Brussels, > Pleinlaan 2, 1050 Brussels > Belgium > > Tel; *+32 489689627* > Email; address@hidden; > > Ticket Details =================== Ticket ID: VMC-329281 Department: Support netCDF Priority: Normal Status: Closed