Opens an existing netCDF file for reading (or, optionally, writing).
Name of the existing netCDF file to be opened.
If FALSE (default), then the file is opened read-only. If TRUE, then writing to the file is allowed.
When invoked, this function reads in the values of all dimensions from
the associated variables. This can be slow for a large file with a long unlimited
dimension. If set to FALSE, the values for the unlimited dimension are not
automatically read in (they can be read in later, manually, using
If TRUE, then messages are printed out during execution of this function.
This routine opens an existing netCDF file for reading (or, if write=TRUE, for writing).
To create a new netCDF file, use
In addition to simply opening the file, information about the file and its contents
is read in and stored in the returned object, which is of class
This class has the following user-accessible fields, all of which are read-only: 1) filename,
which is a character string holding the name of the file; 2) ndims, which is an
integer holding the number of dimensions in the file; 3) nvars, which is an integer
holding the number of the variables in the file that are NOT coordinate variables
(aka dimensional variables); 4) natts, which is an integer holding the number of
global attributes; 5) unlimdimid, which is an integer holding the dimension id of
the unlimited dimension, or -1 if there is none; 6) dim, which is a list of
objects of class
ncdim4; 7) var, which is a list of objects of class
ncvar4; 8) writable, which is TRUE or FALSE, depending on whether the file
was opened with write=TRUE or write=FALSE.
The concept behind the R interface to a netCDF file is that the
returned by this function, as well as the list of
ncdim objects contained
in the ncdf object's "dim" list and the
ncvar objects contained in the
ncdf object's "var" list, completely describe the netCDF file. I.e., they hold
the entire contents of the file's metadata. Therefore, there are no R interfaces
to the explicit netCDF query functions, such as "nc_inq_nvars" or "nc_inq_natts".
The upshot is, look in the ncdf object or its children to get information about
the netCDF file. (Note: the
ncdim object is described in the help
ncvar object is described
in the help file for
An object of class
ncdf4 that has the fields described above.
David W. Pierce firstname.lastname@example.org
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
## Not run: # Define an integer dimension dimState <- ncdim_def( "StateNo", "count", 1:50 ) # Make an integer variable. Note that an integer variable can have # a double precision dimension, or vice versa; there is no fixed # relationship between the precision of the dimension and that of the # associated variable. We just make an integer variable here for # illustration purposes. varPop <- ncvar_def("Pop", "count", dimState, -1, longname="Population", prec="integer") # Create a netCDF file with this variable ncnew <- nc_create( "states_population.nc", varPop ) # Write some values to this variable on disk. popAlabama <- 4447100 ncvar_put( ncnew, varPop, popAlabama, start=1, count=1 ) # Add source info metadata to file ncatt_put( ncnew, 0, "source", "Census 2000 from census bureau web site") nc_close(ncnew) # Now open the file and read its data ncold <- nc_open("states_population.nc") data <- ncvar_get(ncold) print("here is the data in the file:") print(data) nc_close( ncold ) ## End(Not run)