NetcdfIO.jl
Functions to read and write netcdf files. The core functions to read data from netcdf files is adapted from an old version of NCDatasets.jl. Some convenient features have been added, for example, one can read the dataset without diving into the nested structure of dataset/group/variable, with the use of find_variable function which detect the variable name recursively.
Installation
julia> using Pkg;
julia> Pkg.add("NetcdfIO");Read the general information
NetcdfIO.read_dimnames — Function
read_dimnames(file::String)Return all the names of the dimensions, given
filePath of the netcdf dataset
NetcdfIO.read_varnames — Function
read_varnames(ds::Dataset)
read_varnames(file::String)Return all the names of the variables (excluding the dimensions), given
dsNCDatasets.Dataset type datasetfilePath of the netcdf dataset
NetcdfIO.read_dims — Function
read_dims(file::String, var_name::UnionNameTypes)Return the dimensions and size of a NetCDF dataset, given
filePath of the netcdf datasetvar_nameVariable name
NetcdfIO.read_attributes — Function
read_attributes(ds::Dataset) -> Dict{String, Any}
read_attributes(file::String) -> Dict{String, Any}
read_attributes(ds::Dataset, var_name::UnionNameTypes) -> Dict{String, Any}
read_attributes(file::String, var_name::UnionNameTypes) -> Dict{String, Any}Return all the attributes of a NetCDF dataset or variable, given
dsNCDatasets.Dataset type datasetvar_nameVariable namefilePath of the netcdf dataset
Read the variable
NetcdfIO.find_variable — Function
find_variable(ds::Dataset, var_name::UnionNameTypes)Return the path to dataset if it exists, given
dsNCDatasets.Dataset type datasetvar_nameVariable to read
NetcdfIO.read_nc — Function
read_nc(file::String, var_name::UnionNameTypes; transform::Bool = true)
read_nc(T, file::String, var_name::UnionNameTypes; transform::Bool = true)Read entire data from NC file, given
filePath of the netcdf datasetvar_nameVariable to readtransformIf true, transform the data using NCDatasets rules, otherwise read the raw dataTNumber type
read_nc(file::String, var_name::UnionNameTypes, indz::Int; transform::Bool = true)
read_nc(T, file::String, var_name::UnionNameTypes, indz::Int; transform::Bool = true)Read a subset from nc file, given
filePath of the netcdf datasetvar_nameVariable nameindzThe 3rd index of subset data to readtransformIf true, transform the data using NCDatasets rules, otherwise read the raw dataTNumber type
Note that the dataset must be a 1D or 3D array to use this method.
read_nc(file::String, var_name::UnionNameTypes, indx::Int, indy::Int; transform::Bool = true)
read_nc(T, file::String, var_name::UnionNameTypes, indx::Int, indy::Int; transform::Bool = true)Read the subset data for a grid, given
filePath of the netcdf datasetvar_nameVariable nameindxThe 1st index of subset data to read, typically longitudeindyThe 2nd index of subset data to read, typically latitudetransformIf true, transform the data using NCDatasets rules, otherwise read the raw dataTNumber type
read_nc(file::String, var_name::UnionNameTypes, indx::Int, indy::Int, indz::Int; transform::Bool = true)
read_nc(T, file::String, var_name::UnionNameTypes, indx::Int, indy::Int, indz::Int; transform::Bool = true)Read the data at a grid, given
filePath of the netcdf datasetvar_nameVariable nameindxThe 1st index of subset data to read, typically longitudeindyThe 2nd index of subset data to read, typically latitudeindzThe 3rd index of subset data to read, typically timetransformIf true, transform the data using NCDatasets rules, otherwise read the raw dataTNumber type
read_nc(file::String, selections::Vector{<:UnionNameTypes} = read_varnames(file); transform::Bool = true)Read the selected variables from a netcdf file as a DataFrame, given
filePath of the netcdf datasetselectionsVariables to read from the filetransformIf true, transform the data using NCDatasets rules, otherwise read the raw data
read_nc(file::String, var_name::UnionNameTypes, dim_array::Vector; transform::Bool = true)Read parts of the data specified in an array
filePath of the netcdf datasetvar_nameVariable namedim_arrayVector containing the parts of the data to readtransformIf true, transform the data using NCDatasets rules, otherwise read the raw data
Write to a new file
NetcdfIO.create_nc! — Function
create_nc!(file::String)Create an empty netcdf file, given
filePath to save the netcdf dataset
create_nc!(file::String, dim_names::Vector{String}, dim_sizes::Vector)Create an empty netcdf file with dimensions, given
filePath to save the netcdf datasetdim_namesDimension names in the netcdf filedim_sizesSizes of the dimensions (must be Integer or Inf), the dimension is growable if size is Integer 0
NetcdfIO.add_nc_dim! — Function
add_nc_dim!(ds::Dataset, dim_name::String, dim_size::Int)
add_nc_dim!(ds::Dataset, dim_name::String, dim_size::AbstractFloat)
add_nc_dim!(file::String, dim_name::String, dim_size::Union{AbstractFloat,Int})Add dimension information to netcdf dataset, given
dsANCDatasets.Datasettype datasetdim_nameDimension namedim_sizeInteger dimension size (0 for Inf, growable)
NetcdfIO.append_nc! — Function
append_nc!(ds::Dataset,
var_name::UnionNameTypes,
var_data::Array{T,N},
var_attributes::UnionAttrTypes,
dim_names::Vector{String};
deflatelevel::Union{Int,Nothing} = 4) where {T<:Union{AbstractFloat,Integer,String},N}
append_nc!(file::String,
var_name::UnionNameTypes,
var_data::Array{T,N},
var_attributes::UnionAttrTypes,
dim_names::Vector{String};
args...) where {T<:Union{AbstractFloat,Integer,String},N}Append data to existing netcdf dataset, given
dsANCDatasets.Datasettype datasetvar_nameNew variable name to write tovar_dataNew variable data to write, can be integer, float, and string with N dimensvar_attributesNew variable attributesdim_namesDimension names in the netcdf filedeflatelevelCompression level fro NetCDF, default is 4filePath of the netcdf dataset
Add new data to unlimited dim
NetcdfIO.grow_nc! — Function
grow_nc!(ds::Dataset, var_name::UnionNameTypes, new_data::Union{AbstractFloat,Array,Integer,String}, pending::Bool)
grow_nc!(file::String, var_name::UnionNameTypes, new_data::Union{AbstractFloat,Array,Integer,String}, pending::Bool)Grow the netcdf dataset, given
dsANCDatasets.Datasettype datasetvar_nameNew variable name to write tonew_dataNew data to grow, can be integer, float, and string with N dimenspendingIf true, the new data is appened to the end (growth); if false, the data will replace the ones from the bottom (when dimension has already growed)filePath of the netcdf dataset
Note that if there are more variables to grow at the same time, set pending to true only for the first time you call this function, and set pending to false for the rest variables.
Automatic attribute detection (for global data)
NetcdfIO.save_nc! — Function
save_nc!(file::String,
var_name::UnionNameTypes,
var_data::Array{T,N},
var_attributes::Union{Dict{String,Any}, OrderedDict{String,Any}};
var_dims::Vector{String} = N == 2 ? ["lon", "lat"] : ["lon", "lat", "ind"],
deflatelevel::Union{Int,Nothing} = 4,
growable::Bool = false) where {T<:Union{AbstractFloat,Integer,String},N}Save the 1D, 2D, or 3D data as netcdf file, given
filePath to save the datasetvar_nameVariable name for the data in the NC filevar_dataData to savevar_attributesVariable attributes for the data, such as unit and long namevar_dimsDimension name of each dimension of the variable datadeflatelevelCompression level fro NetCDF, default is 4growableIf true, make index growable, default is false
Note that this is a wrapper function of createnc and appendnc:
- If var_data is 1D, the dim is set to ind
- If vardata is 2D, and no vardims are given, the dims are set to lon and lat
- If vardata is 3D, and no vardims are given, the dims are set to lon, lat, and ind
save_nc!(file::String,
df::DataFrame,
var_names::Vector{<:UnionNameTypes},
var_attributes_vec::UnionAttrVecTypes;
deflatelevel::Union{Int,Nothing} = 4,
growable::Bool = false)
save_nc!(file::String, df::DataFrame; deflatelevel::Union{Int,Nothing} = 4, growable::Bool = false)Save DataFrame to NetCDF, given
filePath to save the datadfDataFrame to savevar_namesThe label of data in DataFrame to savevar_attributesVariable attributes for the data to savedeflatelevelCompression level fro NetCDF, default is 4growableIf true, make index growable, default is false
NetcdfIO.detect_attribute — Function
detect_attribute(var_name::String, wavelength::Union{Int,Nothing} = nothing; showwarning::Bool = true)Return an ordered dictionary of variable attributes, given
var_nameName of the variablewavelengthWavelength in nm for wavelength-dependent variables like SIF, default isnothingshowwarningIf true, show a warning when the variable name is not recognized, default is true
Support to HDF4
NetcdfIO.switch_netcdf_lib! — Function
switch_netcdf_lib!(; use_default::Bool = true, user_defined::String = "/home/runner/.julia/conda/3/lib/libnetcdf.so")Switch between the default NetCDF library and a user-defined one, given
use_defaultWhether to use the default libnetcdf library shipped with NCDatasets.jluser_definedThe path to the user-defined libnetcdf library (used only whenuse_defaultis false)