API
Create netcdf file
NetcdfIO.create_nc!
— Functioncreate_nc!(file::String)
Create an empty netcdf file, given
file
Path to save the netcdf dataset
create_nc!(file::String, dim_names::Vector{String}, dim_sizes::Vector)
Create an empty netcdf file with dimensions, given
file
Path to save the netcdf datasetdim_names
Dimension names in the netcdf filedim_sizes
Sizes of the dimensions (must be Integer or Inf), the dimension is growable if size is Integer 0
Examples
create_nc!("test.nc");
create_nc!("test1.nc", String["lon", "lat", "ind"], [36, 18, 0]);
create_nc!("test2.nc", String["lon", "lat", "ind"], [36, 18, Inf]);
NetcdfIO.add_nc_dim!
— Functionadd_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
ds
ANCDatasets.Dataset
type datasetdim_name
Dimension namedim_size
Integer dimension size (0 for Inf, growable)
Examples
ds = Dataset("test.nc", "a");
add_nc_dim!(ds, "lat", 180);
add_nc_dim!(ds, "ind", 0);
add_nc_dim!(ds, "idx", Inf);
close(ds);
add_nc_dim!("test.nc", "lon", 360);
add_nc_dim!("test.nc", "idy", Inf);
Append new variables
NetcdfIO.append_nc!
— Functionappend_nc!(ds::Dataset, var_name::String, var_data::Array{T,N}, var_attributes::Dict{String,String}, dim_names::Vector{String}; compress::Int = 4) where {T<:Union{AbstractFloat,Integer,String},N}
append_nc!(file::String, var_name::String, var_data::Array{T,N}, var_attributes::Dict{String,String}, dim_names::Vector{String}; compress::Int = 4) where {T<:Union{AbstractFloat,Integer,String},N}
Append data to existing netcdf dataset, given
ds
ANCDatasets.Dataset
type datasetvar_name
New variable name to write tovar_data
New variable data to write, can be integer, float, and string with N dimensvar_attributes
New variable attributesdim_names
Dimension names in the netcdf filecompress
Compression level fro NetCDF, default is 4file
Path of the netcdf dataset
Examples
create_nc!("test.nc", String["lon", "lat", "ind"], [36, 18, 5]);
dset = Dataset("test.nc", "a");
append_nc!(dset, "str", ["A" for i in 1:18], Dict("longname" => "test strings"), ["lat"]);
append_nc!(dset, "d3d", rand(36,18,5), Dict("longname" => "a 3d dataset"), ["lon", "lat", "ind"]);
close(dset);
append_nc!("test.nc", "lat", collect(1:18), Dict("longname" => "latitude"), ["lat"]);
append_nc!("test.nc", "d2d", rand(36,18), Dict("longname" => "a 2d dataset"), ["lon", "lat"]);
Grow existing variables
NetcdfIO.grow_nc!
— Functiongrow_nc!(ds::Dataset, var_name::String, in_data::Union{AbstractFloat,Array,Integer,String}, pending::Bool)
grow_nc!(file::String, var_name::String, in_data::Union{AbstractFloat,Array,Integer,String}, pending::Bool)
Grow the netcdf dataset, given
ds
ANCDatasets.Dataset
type datasetvar_name
New variable name to write toin_data
New data to grow, can be integer, float, and string with N dimenspending
If 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)file
Path 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.
Examples
create_nc!("test.nc", String["lon", "lat", "ind"], [36, 18, 0]);
dset = Dataset("test.nc", "a");
append_nc!(dset, "lat", collect(1:18), Dict("longname" => "latitude"), ["lat"]);
append_nc!(dset, "lon", collect(1:36), Dict("longname" => "longitude"), ["lon"]; compress=4);
append_nc!(dset, "ind", collect(1:5), Dict("longname" => "index"), ["ind"]);
append_nc!(dset, "d2d", rand(36,18), Dict("longname" => "a 2d dataset"), ["lon", "lat"]);
append_nc!(dset, "d3d", rand(36,18,5), Dict("longname" => "a 3d dataset"), ["lon", "lat", "ind"]);
grow_nc!(dset, "ind", 6, true);
grow_nc!(dset, "d3d", rand(36,18), false);
grow_nc!(dset, "d3d", rand(36,18), true);
grow_nc!(dset, "ind", 7, false);
close(dset);
grow_nc!("test.nc", "ind", 8, true);
grow_nc!("test.nc", "d3d", rand(36,18), false);
grow_nc!("test.nc", "d3d", rand(36,18), true);
grow_nc!("test.nc", "ind", 9, false);
Information of the dataset
NetcdfIO.dimname_nc
— Functiondimname_nc(file::String)
Return all the names of the dimensions, given
file
Path of the netcdf dataset
Examples
dims = dimname_nc("test.nc");
NetcdfIO.varname_nc
— Functionvarname_nc(ds::Dataset)
varname_nc(file::String)
Return all the names of the variables (excluding the dimensions), given
ds
NCDatasets.Dataset type datasetfile
Path of the netcdf dataset
Examples
dset = Dataset("test.nc");
vars = varname_nc(dset);
close(dset);
vars = varname_nc("test.nc");
NetcdfIO.size_nc
— Functionsize_nc(file::String, var_name::String)
Return the dimensions and size of a NetCDF dataset, given
file
Path of the netcdf datasetvar_name
Variable name
Examples
ndims,sizes = size_nc("test.nc", "test");
Read existing variables
NetcdfIO.find_variable
— Functionfind_variable(ds::Dataset, var_name::String)
Return the path to dataset if it exists, given
ds
NCDatasets.Dataset type datasetvar_name
Variable to read
NetcdfIO.read_nc
— Functionread_nc(file::String, var_name::String; transform::Bool = true)
read_nc(T, file::String, var_name::String; transform::Bool = true)
Read entire data from NC file, given
file
Path of the netcdf datasetvar_name
Variable to readtransform
If true, transform the data using NCDatasets rules, otherwise read the raw dataT
Number type
read_nc(file::String, var_name::String, indz::Int; transform::Bool = true)
read_nc(T, file::String, var_name::String, indz::Int; transform::Bool = true)
Read a subset from nc file, given
file
Path of the netcdf datasetvar_name
Variable nameindz
The 3rd index of subset data to readtransform
If true, transform the data using NCDatasets rules, otherwise read the raw dataT
Number type
Note that the dataset must be a 1D or 3D array to use this method.
read_nc(file::String, var_name::String, indx::Int, indy::Int; transform::Bool = true)
read_nc(T, file::String, var_name::String, indx::Int, indy::Int; transform::Bool = true)
Read the subset data for a grid, given
file
Path of the netcdf datasetvar_name
Variable nameindx
The 1st index of subset data to read, typically longitudeindy
The 2nd index of subset data to read, typically latitudetransform
If true, transform the data using NCDatasets rules, otherwise read the raw dataT
Number type
read_nc(file::String, var_name::String, indx::Int, indy::Int, indz::Int; transform::Bool = true)
read_nc(T, file::String, var_name::String, indx::Int, indy::Int, indz::Int; transform::Bool = true)
Read the data at a grid, given
file
Path of the netcdf datasetvar_name
Variable nameindx
The 1st index of subset data to read, typically longitudeindy
The 2nd index of subset data to read, typically latitudeindz
The 3rd index of subset data to read, typically timetransform
If true, transform the data using NCDatasets rules, otherwise read the raw dataT
Number type
read_nc(file::String, selections::Vector{String} = varname_nc(file); transform::Bool = true)
Read the selected variables from a netcdf file as a DataFrame, given
file
Path of the netcdf datasetselections
Variables to read from the filetransform
If true, transform the data using NCDatasets rules, otherwise read the raw data
read_nc(file::String, var_name::String, dim_array::Vector; transform::Bool = true)
Read parts of the data specified in an array
file
Path of the netcdf datasetvar_name
Variable namedim_array
Vector containing the parts of the data to readtransform
If true, transform the data using NCDatasets rules, otherwise read the raw data
Examples
# read data labeled as test from test.nc
save_nc!("test.nc", "test", rand(36,18,12), Dict("description" => "Random randoms"));
data = read_nc("test.nc", "test");
data = read_nc(Float32, "test.nc", "test");
# read 1st layer data labeled as test from test.nc
data = read_nc("test.nc", "test", 1);
data = read_nc(Float32, "test.nc", "test", 1);
# read the data (time series) at a grid
save_nc!("test1.nc", "test", rand(36,18), Dict("description" => "Random randoms"));
save_nc!("test2.nc", "test", rand(36,18,12), Dict("description" => "Random randoms"));
data1 = read_nc("test1.nc", "test", 1, 1);
data2 = read_nc("test2.nc", "test", 1, 1);
data1 = read_nc(Float32, "test1.nc", "test", 1, 1);
data2 = read_nc(Float32, "test2.nc", "test", 1, 1);
# read the data at a grid
data = read_nc("test.nc", "test", 1, 1, 1);
data = read_nc(Float32, "test.nc", "test", 1, 1, 1);
# read the data as a DataFrame
df_raw = DataFrame();
df_raw[!,"A"] = rand(5);
df_raw[!,"B"] = rand(5);
df_raw[!,"C"] = rand(5);
save_nc!("test.nc", df_raw);
df_new = read_nc("test.nc");
df_new = read_nc("test.nc", ["A", "B"]);
Quick save
NetcdfIO.save_nc!
— Functionsave_nc!(file::String,
var_name::String,
var_data::Array{T,N},
var_attribute::Dict{String,String};
var_dims::Vector{String} = N == 2 ? ["lon", "lat"] : ["lon", "lat", "ind"],
compress::Int = 4,
growable::Bool = false) where {T<:Union{AbstractFloat,Integer,String},N}
Save the 1D, 2D, or 3D data as netcdf file, given
file
Path to save the datasetvar_name
Variable name for the data in the NC filevar_data
Data to savevar_attribute
Variable attributes for the data, such as unit and long namevar_dims
Dimension name of each dimension of the variable datacompress
Compression level fro NetCDF, default is 4growable
If 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{String}, var_attributes::Vector{Dict{String,String}}; compress::Int = 4, growable::Bool = false)
save_nc!(file::String, df::DataFrame; compress::Int = 4, growable::Bool = false)
Save DataFrame to NetCDF, given
file
Path to save the datadf
DataFrame to savevar_names
The label of data in DataFrame to savevar_attributes
Variable attributes for the data to savecompress
Compression level fro NetCDF, default is 4growable
If true, make index growable, default is false
Examples
# save 1D, 2D, and 3D data
data1 = rand(12) .+ 273.15;
data2 = rand(36,18) .+ 273.15;
data3 = rand(36,18,12) .+ 273.15;
save_nc!("data1.nc", "data1", data1, Dict("description" => "Random temperature", "unit" => "K"));
save_nc!("data2.nc", "data2", data2, Dict("description" => "Random temperature", "unit" => "K"));
save_nc!("data3.nc", "data3", data3, Dict("description" => "Random temperature", "unit" => "K"));
# save DataFrame
df = DataFrame();
df[!,"A"] = rand(5);
df[!,"B"] = rand(5);
df[!,"C"] = rand(5);
save_nc!("dataf.nc", df, ["A","B"], [Dict("A" => "Attribute A"), Dict("B" => "Attribute B")]);
save_nc!("test.nc", df);
Switch libnetcdf library
NetcdfIO.switch_netcdf_lib!
— Functionswitch_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_default
Whether to use the default libnetcdf library shipped with NCDatasets.jluser_defined
The path to the user-defined libnetcdf library (used only whenuse_default
is false)