From 156fce32578c62ec965b2ae4c57790007d71bb4c Mon Sep 17 00:00:00 2001 From: "Documenter.jl" Date: Wed, 22 May 2024 10:45:31 +0000 Subject: [PATCH] build based on 86b7b7e --- dev/highlevel/index.html | 4 ++-- dev/index.html | 2 +- dev/intermediate/index.html | 2 +- dev/quickstart/index.html | 2 +- dev/search/index.html | 2 +- dev/strings/index.html | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dev/highlevel/index.html b/dev/highlevel/index.html index cfec323..119424b 100644 --- a/dev/highlevel/index.html +++ b/dev/highlevel/index.html @@ -1,3 +1,3 @@ -High-level interface · NetCDF.jl

High-level interface

Getting information

NetCDF.ncinfoFunction
ncinfo(filename)

prints information on the variables, dimension and attributes conatained in the file

source

Reading data

NetCDF.ncreadFunction
ncread(filename, varname)

reads the values of the variable varname from file filename and returns the values in an array.

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The value -1 is treated as a special case to read all values along the dimension

Example

To read the second slice of a 3D NetCDF variable, you can write:

ncread("filename","varname", start=[1,1,2], count = [-1,-1,1])
source
NetCDF.ncread!Function
ncread!(filename, varname, d)

reads the values of the variable varname from file filename and writes the results to the pre-allocated array d

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The value -1 is treated as a special case to read all values from this dimension

Example

To read the second slice of a 3D NetCDF variable one can write:

d = zeros(10,10,1)
-ncread!("filename","varname", d, start=[1,1,2], count = [-1,-1,1])
source

Writing data

NetCDF.ncwriteFunction
ncwrite(x::Array,fil::AbstractString,vname::AbstractString)

Writes the array x to the file fil and variable vname.

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for writing for each dimension
  • count Vector of length ndim(v) setting the count of values to be written along each dimension. The value -1 is treated as a special case to write all values along the dimension. This is usually inferred by the given array size.
source

Reading attributes

NetCDF.ncgetattFunction
ncgetatt(filename, varname, attname)

This reads a NetCDF attribute attname from the specified file and variable. To read global attributes, set varname to Global.

source

Writing attributes

NetCDF.ncputattFunction
ncputatt(nc::String,varname::String,atts::Dict)

Writes the attributes defined in atts to the variable varname for the given NetCDF file name nc. Existing attributes are overwritten. If varname is not a valid variable name, a global attribute will be written.

source

Creating files

NetCDF.nccreateFunction
nccreate (filename, varname, dimensions ...)

Creates a variable in an existing NetCDF file or generates a new file. filename and varname are strings. After that follows a list of dimensions. Each dimension entry starts with a dimension name (a String), and may be followed by a dimension length, an array with dimension values or a Dict containing dimension attributes. Then the next dimension is entered and so on. Have a look at examples/high.jl for an example use.

Keyword arguments

  • atts Dict of attribute names and values to be assigned to the variable created
  • gatts Dict of attribute names and values to be written as global attributes
  • compress Integer [0..9] setting the compression level of the file, only valid if mode=NC_NETCDF4
  • t variable type, currently supported types are: const NC_BYTE, NC_CHAR, NC_SHORT, NC_INT, NC_FLOAT, NC_LONG, NC_DOUBLE
  • mode file creation mode, only valid when new file is created, choose one of: NC_NETCDF4, NC_CLASSIC_MODEL, NC_64BIT_OFFSET
source
+High-level interface · NetCDF.jl

High-level interface

Getting information

NetCDF.ncinfoFunction
ncinfo(filename)

prints information on the variables, dimension and attributes conatained in the file

source

Reading data

NetCDF.ncreadFunction
ncread(filename, varname)

reads the values of the variable varname from file filename and returns the values in an array.

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The value -1 is treated as a special case to read all values along the dimension

Example

To read the second slice of a 3D NetCDF variable, you can write:

ncread("filename","varname", start=[1,1,2], count = [-1,-1,1])
source
NetCDF.ncread!Function
ncread!(filename, varname, d)

reads the values of the variable varname from file filename and writes the results to the pre-allocated array d

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The value -1 is treated as a special case to read all values from this dimension

Example

To read the second slice of a 3D NetCDF variable one can write:

d = zeros(10,10,1)
+ncread!("filename","varname", d, start=[1,1,2], count = [-1,-1,1])
source

Writing data

NetCDF.ncwriteFunction
ncwrite(x::Array,fil::AbstractString,vname::AbstractString)

Writes the array x to the file fil and variable vname.

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for writing for each dimension
  • count Vector of length ndim(v) setting the count of values to be written along each dimension. The value -1 is treated as a special case to write all values along the dimension. This is usually inferred by the given array size.
source

Reading attributes

NetCDF.ncgetattFunction
ncgetatt(filename, varname, attname)

This reads a NetCDF attribute attname from the specified file and variable. To read global attributes, set varname to Global.

source

Writing attributes

NetCDF.ncputattFunction
ncputatt(nc::String,varname::String,atts::Dict)

Writes the attributes defined in atts to the variable varname for the given NetCDF file name nc. Existing attributes are overwritten. If varname is not a valid variable name, a global attribute will be written.

source

Creating files

NetCDF.nccreateFunction
nccreate (filename, varname, dimensions ...)

Creates a variable in an existing NetCDF file or generates a new file. filename and varname are strings. After that follows a list of dimensions. Each dimension entry starts with a dimension name (a String), and may be followed by a dimension length, an array with dimension values or a Dict containing dimension attributes. Then the next dimension is entered and so on. Have a look at examples/high.jl for an example use.

Keyword arguments

  • atts Dict of attribute names and values to be assigned to the variable created
  • gatts Dict of attribute names and values to be written as global attributes
  • compress Integer [0..9] setting the compression level of the file, only valid if mode=NC_NETCDF4
  • t variable type, currently supported types are: const NC_BYTE, NC_CHAR, NC_SHORT, NC_INT, NC_FLOAT, NC_LONG, NC_DOUBLE
  • mode file creation mode, only valid when new file is created, choose one of: NC_NETCDF4, NC_CLASSIC_MODEL, NC_64BIT_OFFSET
source
diff --git a/dev/index.html b/dev/index.html index 4d35bdc..08e9e11 100644 --- a/dev/index.html +++ b/dev/index.html @@ -1,2 +1,2 @@ -Home · NetCDF.jl

NetCDF.jl

Reading and writing NetCDF files in Julia

Package features

High-level (MATLAB-like), low-level (C-like) and array-based access to NetCDF files.

Manual Outline

Acknowledgements

Thanks to everyone who contributed to the package with pull requests, especially to Martijn Visser for creating the new generated C wrapper.

Index

+Home · NetCDF.jl

NetCDF.jl

Reading and writing NetCDF files in Julia

Package features

High-level (MATLAB-like), low-level (C-like) and array-based access to NetCDF files.

Manual Outline

Acknowledgements

Thanks to everyone who contributed to the package with pull requests, especially to Martijn Visser for creating the new generated C wrapper.

Index

diff --git a/dev/intermediate/index.html b/dev/intermediate/index.html index e02aff6..a0890e4 100644 --- a/dev/intermediate/index.html +++ b/dev/intermediate/index.html @@ -1,2 +1,2 @@ -Medium-level interface · NetCDF.jl

Medium-level interface

Open a file

NetCDF.openFunction
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. Note that it is in the user's responsibility to close the file after usage using NetCDF.close.

Keyword arguments

  • mode mode in which the file is opened, defaults to NC_NOWRITE, choose NC_WRITE for write access
  • readdimvar determines if dimension variables will be read into the file structure, default is false
source
NetCDF.open(fil::AbstractString)

opens the NetCDF file fil and returns a NcFile handle. Note that it is in the user's responsibility to close the file after usage using NetCDF.close.

Keyword arguments

  • mode mode in which the file is opened, defaults to NC_NOWRITE, choose NC_WRITE for write access
  • readdimvar determines if dimension variables will be read into the file structure, default is false
source
NetCDF.open(f::Function, args...;kwargs...)

Opens a NetCDF file, applies the function f on the resulting file or variable handle and properly closes the file after usage. This is convenient to use together with the do block syntax, for example:

data = open("myfile.nc","myvar") do v NetCDF.readvar(v,start=[1,1,1], count=[-1,-1,1]) end

source

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 an NcVar object and retrieve information about it. You can run keys(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.readvarFunction
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 the whole variable is read

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The 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.

source
NetCDF.readvar{T,N}(v::NcVar{T,N},I::Union{Integer, UnitRange, Colon}...)

Reads data from a NetCDF file with array-style indexing. Integers and UnitRanges and Colons are valid indices for each dimension.

source

Writing data

NetCDF.putvarFunction
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 is an array with the same dimension as the variable in the NetCDF file.

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The value -1 is treated as a special case to read all values from this dimension
source
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.

source

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, atts 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, Int64, Float32, Float64) which will be translated to (NC_SHORT, NC_INT, NC_INT64, 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

Note that as of version 0.9 there is no need to close the NetCDF files anymore. This will be done through finalizers.

If you just want to synchronize your changes to the disk, run

NetCDF.sync(nc)

where nc is a NetCDF file handle.

As an alternative, NetCDF.open and NetCDF.create provide a method accepting a function as its first argument.

Interface for creating files

NetCDF.NcDimType
NcDim

Represents a NetCDF dimension of name name optionally holding the dimension values.

source
NetCDF.NcVarType
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.

source
NetCDF.createFunction
NetCDF.create(name::AbstringString,varlist::Array{NcVar};gatts::Dict=Dict{Any,Any}(),mode::UInt16=NC_NETCDF4,add_finalizer = true)

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 file
  • mode NetCDF file type (NC_NETCDF4, NC_CLASSIC_MODEL or NC_64BIT_OFFSET), defaults to NC_NETCDF4
source
NetCDF.create(f::Function, args...;kwargs...)

Creates a NetCDF file, applies the function f on the resulting file or variable handle and properly closes the file after usage. This is convenient to use together with the do block syntax, for example:

d = NcDim("time",1:10) v = NcVar("obs",d); NetCDF.create("newfile.nc",v) do nc nc["obs"][:] = rand(10) end

source
+Medium-level interface · NetCDF.jl

Medium-level interface

Open a file

NetCDF.openFunction
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. Note that it is in the user's responsibility to close the file after usage using NetCDF.close.

Keyword arguments

  • mode mode in which the file is opened, defaults to NC_NOWRITE, choose NC_WRITE for write access
  • readdimvar determines if dimension variables will be read into the file structure, default is false
source
NetCDF.open(fil::AbstractString)

opens the NetCDF file fil and returns a NcFile handle. Note that it is in the user's responsibility to close the file after usage using NetCDF.close.

Keyword arguments

  • mode mode in which the file is opened, defaults to NC_NOWRITE, choose NC_WRITE for write access
  • readdimvar determines if dimension variables will be read into the file structure, default is false
source
NetCDF.open(f::Function, args...;kwargs...)

Opens a NetCDF file, applies the function f on the resulting file or variable handle and properly closes the file after usage. This is convenient to use together with the do block syntax, for example:

data = open("myfile.nc","myvar") do v NetCDF.readvar(v,start=[1,1,1], count=[-1,-1,1]) end

source

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 an NcVar object and retrieve information about it. You can run keys(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.readvarFunction
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 the whole variable is read

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The 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.

source
NetCDF.readvar{T,N}(v::NcVar{T,N},I::Union{Integer, UnitRange, Colon}...)

Reads data from a NetCDF file with array-style indexing. Integers and UnitRanges and Colons are valid indices for each dimension.

source

Writing data

NetCDF.putvarFunction
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 is an array with the same dimension as the variable in the NetCDF file.

Keyword arguments

  • start Vector of length ndim(v) setting the starting index for each dimension
  • count Vector of length ndim(v) setting the count of values to be read along each dimension. The value -1 is treated as a special case to read all values from this dimension
source
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.

source

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, atts 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, Int64, Float32, Float64) which will be translated to (NC_SHORT, NC_INT, NC_INT64, 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

Note that as of version 0.9 there is no need to close the NetCDF files anymore. This will be done through finalizers.

If you just want to synchronize your changes to the disk, run

NetCDF.sync(nc)

where nc is a NetCDF file handle.

As an alternative, NetCDF.open and NetCDF.create provide a method accepting a function as its first argument.

Interface for creating files

NetCDF.NcDimType
NcDim

Represents a NetCDF dimension of name name optionally holding the dimension values.

source
NetCDF.NcVarType
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.

source
NetCDF.createFunction
NetCDF.create(name::AbstringString,varlist::Array{NcVar};gatts::Dict=Dict{Any,Any}(),mode::UInt16=NC_NETCDF4,add_finalizer = true)

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 file
  • mode NetCDF file type (NC_NETCDF4, NC_CLASSIC_MODEL or NC_64BIT_OFFSET), defaults to NC_NETCDF4
source
NetCDF.create(f::Function, args...;kwargs...)

Creates a NetCDF file, applies the function f on the resulting file or variable handle and properly closes the file after usage. This is convenient to use together with the do block syntax, for example:

d = NcDim("time",1:10) v = NcVar("obs",d); NetCDF.create("newfile.nc",v) do nc nc["obs"][:] = rand(10) end

source
diff --git a/dev/quickstart/index.html b/dev/quickstart/index.html index 75c48f2..a533ce1 100644 --- a/dev/quickstart/index.html +++ b/dev/quickstart/index.html @@ -230,4 +230,4 @@ 2001-01-01T23:00:00

Now we can generate the plot:

plot(tvec,ts)

Another example would be to generate a heatmap plot of the solar radiation at 12am UTC:

lons = ncread(fn,"lon")
 lats = ncread(fn,"lat")
 m    = ncread(fn,"rad",start=[1,1,12],count=[-1,-1,1])
-plot(heatmap(x=lons,y=lats,z=m))
+plot(heatmap(x=lons,y=lats,z=m)) diff --git a/dev/search/index.html b/dev/search/index.html index 6f39ba0..4c15cb7 100644 --- a/dev/search/index.html +++ b/dev/search/index.html @@ -1,2 +1,2 @@ -Search · NetCDF.jl

Loading search...

    +Search · NetCDF.jl

    Loading search...

      diff --git a/dev/strings/index.html b/dev/strings/index.html index 90d5834..a4d2cad 100644 --- a/dev/strings/index.html +++ b/dev/strings/index.html @@ -1,4 +1,4 @@ Short note on reading and writing NC_CHAR and NC_STRING variables · NetCDF.jl

      Short note on reading and writing NC_CHAR and NC_STRING variables

      There are two common types for storing String data in NetCDF variables. The first is NC_CHAR, where a 1D array of strings is stored in a 2D char** array. Here the user must define the maximum string length and add a respective NetCDF dimension. Since NetCDF4 there is the NC_STRING data type that allows the direct definition of String variables so that an N-dimensional String array directly maps to an N-dimensional array in the NetCDF file structure.

      NC_STRING variables

      In this package, the Julia type String maps to the NC_STRING datatype, which means that creating a variable with any of

      nccreate(filename, varname, dims..., t=String)

      or

      NcVar(varname,dims,t=String)

      will result in a NetCDF variable of type NC_STRING. You can directly write an Array{String} of matching shape to these variables. Similarly, calling ncread or NetCDF.readvar on any of these variables will return an Array{String}

      NC_CHAR variables

      Dealing with NC_CHAR variables is a bit more complicated because of two reasons. First, the dimensions of the NetCDF variables do not match the dimensions of the resulting string array because of the additional str_len (or similar) axis that is introduced in the NetCDF file. So an n-dimensional String-Array maps to an (n+1)-dimensional NC_CHAR array.

      Second, historically the NC_CHAR type has been used to store compressed data, too. So it is not always desirable to automatically convert these char arrays to strings. Anyhow, here is how you can deal with these variable types:

      Assume you have a NetCDF variable of type NC_CHAR of dimensions (str_len: 10, axis2: 20). Calling x=ncread(...) or x=readvar(...) on this variable will return an Array{ASCIIChar,2} with size (10,20) as it is represented on disk. The ASCIIChar type is a small wrapper around UInt8, needed for dispatch. You can simply convert them to either Char or UInt8 using the convert function. The returned array can either be used directly (if it is numeric, you might be able to use reinterpret(UInt8,x)) or convert them to a Vector{String} by calling

      y=nc_char2string(x)

      which will return a string vector of length 20.

      An example for creating NC_CHAR and writing variables would be the following:

      nccreate(filename,varname,"str_len",20,"DimValues",5,t=NC_CHAR)
       xs = ["a","bb","ccc","dddd","eeeee"]
      -ncwrite(filename,varname,nc_string2char(xs))

      The call of string2char will convert the Vector{String} to a Matrix{UInt8}. which can be written to the NetCDF file.

      +ncwrite(filename,varname,nc_string2char(xs))

      The call of string2char will convert the Vector{String} to a Matrix{UInt8}. which can be written to the NetCDF file.