This routine opens an existing netCDF file for reading (or, if write=TRUE, for writing).
To create a new netCDF file, use nc_create
instead.
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 ncdf4
.
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 ncdf4
object
returned by this function, as well as the list of ncdim4
objects contained
in the ncdf object's "dim" list and the ncvar4
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 ncdf4 object or its children to get information about
the netCDF file. (Note: the ncdim4
object is described in the help
file for ncdim_def
; the ncvar4
object is described
in the help file for ncvar_def
).
Missing values: R uses "NA" as a missing value. Netcdf files have various
standards for indicating a missing value. The most common is that a variable
will have an attribute named "_FillValue" indicating the value that should
be interpreted as a missing value. (For example, the _FillValue attribute might
have the value of 1.e30, indicating that any data in the netcdf file with
a value of 1.e30 should be interpreted as a missing value.)
If the "_FillValue" attribute is found, then the ncdf4
package transparently maps all the netcdf file's missing values to NA's; this
is the most common case.
The attribute "missing_value" is also recognized if there is no "_FillValue"
attribute.
Some netcdf files specify both a "_FillValue" and a "missing_value" attribute
for a variable. If these two
attributes have the same value, then everything is fine. If they have different
values, I consider this a malformed netcdf file and I suggest you contact
the person who made your netcdf file to fix it. In this event you can
set the "raw_datavals" flag in the ncvar_get() call and handle the
conflicting missing values however you want.
If the netcdf file does not have a missing value, then the ncdf4
package assigns a default missing value of 1.e30 to the netcdcf file so that R NA's,
which are always possible in the R environment, can be sensibly handled
in the netcdf file. On rare occasions this can cause problems with non-compliant
or incorrect netcdf files that implicitly use some particular value,
for example 9.96921e+36, to indicate a missing value but without
setting a proper _FillValue attribute. The best way to fix such netcdf files is to explicitly
put in the correct _FillValue attribute using an ncatt_put
call.