Medium-level interface
Open a file
NetCDF.open
— Function.NetCDF.open(fil::AbstractString,v::AbstractString; , )
opens a NetCDF variable v
in the NetCDF file fil
and returns an NcVar
handle that implements the AbstractArray interface for reading and writing.
Keyword arguments
mode
mode in which the file is opened, defaults toNC_NOWRITE
, chooseNC_WRITE
for write accessreaddimvar
determines if dimension variables will be read into the file structure, default isfalse
NetCDF.open(fil::AbstractString)
opens the NetCDF file fil
and returns a NcFile
handle.
Keyword arguments
mode
mode in which the file is opened, defaults toNC_NOWRITE
, chooseNC_WRITE
for write accessreaddimvar
determines if dimension variables will be read into the file structure, default isfalse
Getting information
The NetCDF.open
function returns an object of type NcFile
which contains meta-Information about the file and associated variables. You can index the NcFile object nc[varname]
to retrieve and NcVar object and retrieve information about it. You can run names(nc)
to get a list of available variables.
Most of the following functions of the medium-level interface will use either an NcFile
or an NcVar
object as their first argument.
Reading data
NetCDF.readvar
— Function.NetCDF.readvar(v::NcVar;start::Vector=ones(UInt,ndims(d)),count::Vector=size(d))
Reads the values from the file associated to the NcVar
object v
and returns them. By default th whole variable is read
Keyword arguments
start
Vector of lengthndim(v)
setting the starting index for each dimensioncount
Vector of lengthndim(v)
setting the count of values to be read along each dimension. Th value -1 is treated as a special case to read all values from this dimension
Example
Assume v
is a NetCDF variable with dimensions (3,3,10).
x = NetCDF.readvar(v, start=[1,2,1], count=[3,1,-1])
This reads all values from the first and last dimension and only the second value from the second dimension.
NetCDF.readvar{T,N}(v::NcVar{T,N},I::Union{Integer, UnitRange, Colon}...)
Reads data from a NetCDF file with array-style indexing. Integer
s and UnitRange
s and Colon
s are valid indices for each dimension.
Writing data
NetCDF.putvar
— Function.NetCDF.putvar(v::NcVar,vals::Array;start::Vector=ones(Int,length(size(vals))),count::Vector=[size(vals)...])
Writes the values from the array vals
to a netcdf file. v
is the NcVar handle of the respective variable and vals
an array with the same dimension as the variable in the netcdf file.
Keyword arguments
start
Vector of lengthndim(v)
setting the starting index for each dimensioncount
Vector of lengthndim(v)
setting the count of values to be read along each dimension. Th value -1 is treated as a special case to read all values from this dimension
NetCDF.putvar(v::NcVar, val, i...)
Writes the value(s) val
to the variable v
while the indices are given in in an array-style indexing manner.
Creating files
To create a netCDF file you first have to define the dimensions and variables that it is supposed to hold. As representations for netCDF dimensions and variables there are the predefined NcVar and NcDim types. An NcDim object is created by:
NcDim(dimname, dimlength, atts=Dict{Any,Any}(), values=[], unlimited=false)
here dimname is the dimension name, dimlength is the dimension length. The optional argument values is a 1D array of values that are written to the dimension variable and the optional argument atts is a Dict holding pairs of attribute names and values. Setting unlimited=true
creates an unlimited dimension.
After defining the dimensions, you can create NcVar objects with
NcVar(varname , dimlist; atts=Dict{Any,Any}(), t=Float64, compress=-1)
Here varname is the name of the variable, dimlist an array of type NcDim holding the dimensions associated to the variable, varattributes is a Dict holding pairs of attribute names and values. t is the data type that should be used for storing the variable. You can either specify a Julia type(Int16, Int32, Float32, Float64) which will be translated to(NC_SHORT, NC_INT, NC_FLOAT, NC_DOUBLE) or directly specify one of the latter list. You can also set the compression level of the variable by setting compress to a number in the range 1..9 This has only an effect in netCDF4 files.
Having defined the variables, the netCDF file can be created:
netCDF.create(filename, varlist, gatts=Dict{Any,Any}(),mode=NC_netCDF4)
Here, filename is the name of the file to be created and varlist an array of NcVar holding the variables that should appear in the file. In the optional argument gatts you can specify a Dict containing global attributes and mode is the file type you want to create(NC_netCDF4, NC_CLASSIC_MODEL or NC_64BIT_OFFSET).
Miscellaneous
once you have finished reading, writing or editing your files you can close the file with
netCDF.close(nc)
If you just want to synchronize your changes to the disk, run
netCDF.sync(nc)
where nc is a netCDF file handler.
Interface for creating files
NetCDF.NcDim
— Type.NcDim
Represents a NetCDF dimension of name name
optionally holding the dimension values.
NetCDF.NcVar
— Type.NcVar
NcVar{T,N,M}
represents a NetCDF variable. It is a subtype of AbstractArray{T,N}, so normal indexing using []
will work for reading and writing data to and from a NetCDF file. NcVar
objects are returned by NetCDF.open
, by indexing an NcFile
object (e.g. myfile["temperature"]
) or, when creating a new file, by its constructor. The type parameter M
denotes the NetCDF data type of the variable, which may or may not correspond to the Julia Data Type.
NetCDF.create
— Function.NetCDF.create(name::String,varlist::Array{NcVar};gatts::Dict=Dict{Any,Any}(),mode::UInt16=NC_NETCDF4)
Creates a new NetCDF file. Here, name
is the name of the file to be created and varlist
an array of NcVar
holding the variables that should appear in the file.
Keyword arguments
gatts
a Dict containing global attributes of the NetCDF filemode
NetCDF file type (NC_NETCDF4, NC_CLASSIC_MODEL or NC_64BIT_OFFSET), defaults to NC_NETCDF4