Example 4 : Group Variables

Two variables (three-dimensional ones) are read from the netcdf files generated in Example3, combined into a single data structure, and then re-written together into a new netcdf file.

using NCTiles
import NCTiles: NCDatasets, Dates

inputs=NCTiles.NCTILES_TESTCASES
NCTiles.ensure_testcases_installed()
outputs = joinpath(tempdir(),"NCTILES_TESTCASES_OUTPUT/")
nt="0003"

file_in1=outputs*"ex3/THETA/THETA.$nt.nc"
file_in2=outputs*"ex3/SALT/SALT.$nt.nc"
~isfile(file_in1) ? error("Running Example3 first is needed to run Example4") : nothing

file_out=outputs*"ex4/TS.$nt.nc"
if ~ispath(outputs*"ex4/"); mkpath(outputs*"ex4/"); end

README = ["File created by","example 4 of NCTiles.jl","on "*string(Dates.now())]
3-element Vector{String}:
 "File created by"
 "example 4 of NCTiles.jl"
 "on 2024-05-04T18:20:18.867"

Get the first 3D variable

ncvars,ncdims,fileatts = readncfile(file_in1)
(Dict{String, NCvar}("lat" => NCvar("lat", "degrees_north", NCvar[NCvar("i_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "i_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 1"), NCDatasets), NCvar("j_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "j_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 2"), NCDatasets)], Float32[10.458642 11.438585 … NaN NaN; 10.458642 11.438585 … NaN NaN; … ; NaN NaN … NaN NaN; 10.458642 11.438585 … NaN NaN], Dict("units" => "degrees_north", "long_name" => "latitude"), NCDatasets), "thic" => NCvar("thic", "m", NCvar[NCvar("dep_c", "m", 50, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "dep_c", NCDatasets, Float32), Dict("units" => "m", "long_name" => "depth", "standard_name" => "depth", "positive" => "down"), NCDatasets)], Float32[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.01, 10.03, 10.11  …  249.5, 272.5, 295.5, 318.5, 341.5, 364.5, 387.5, 410.5, 433.5, 456.5], Dict("units" => "m", "standard_name" => "cell_thickness"), NCDatasets), "area" => NCvar("area", "m^2", NCvar[NCvar("i_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "i_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 1"), NCDatasets), NCvar("j_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "j_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 2"), NCDatasets)], Float32[1.1896091f10 1.1875574f10 … NaN NaN; 1.1896091f10 1.1875574f10 … NaN NaN; … ; NaN NaN … NaN NaN; 1.1896091f10 1.1875574f10 … NaN NaN], Dict("units" => "m^2", "long_name" => "grid cell area", "standard_name" => "cell_area"), NCDatasets), "THETA" => NCvar("THETA", "degC", NCvar[NCvar("i_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "i_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 1"), NCDatasets), NCvar("j_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "j_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 2"), NCDatasets), NCvar("dep_c", "m", 50, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "dep_c", NCDatasets, Float32), Dict("units" => "m", "long_name" => "depth", "standard_name" => "depth", "positive" => "down"), NCDatasets), NCvar("tim", "days since 1992-01-01 0:0:0", 3, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "tim", NCDatasets, Dates.DateTime), Dict("units" => "days since 1992-01-01 0:0:0", "long_name" => "time", "standard_name" => "time"), NCDatasets)], NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "THETA", NCDatasets, Float32), Dict("units" => "degC", "coordinates" => "lon lat dep_c tim"), NCDatasets), "land" => NCvar("land", "1", NCvar[NCvar("i_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "i_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 1"), NCDatasets), NCvar("j_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "j_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 2"), NCDatasets), NCvar("dep_c", "m", 50, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "dep_c", NCDatasets, Float32), Dict("units" => "m", "long_name" => "depth", "standard_name" => "depth", "positive" => "down"), NCDatasets)], Float32[1.0 1.0 … NaN NaN; 1.0 1.0 … NaN NaN; … ; NaN NaN … NaN NaN; 1.0 1.0 … NaN NaN;;; 1.0 1.0 … NaN NaN; 1.0 1.0 … NaN NaN; … ; NaN NaN … NaN NaN; 1.0 1.0 … NaN NaN;;; 1.0 1.0 … NaN NaN; 1.0 1.0 … NaN NaN; … ; NaN NaN … NaN NaN; 1.0 1.0 … NaN NaN;;; … ;;; NaN 1.0 … NaN NaN; NaN 1.0 … NaN NaN; … ; NaN NaN … NaN NaN; NaN NaN … NaN NaN;;; NaN NaN … NaN NaN; NaN NaN … NaN NaN; … ; NaN NaN … NaN NaN; NaN NaN … NaN NaN;;; NaN NaN … NaN NaN; NaN NaN … NaN NaN; … ; NaN NaN … NaN NaN; NaN NaN … NaN NaN], Dict("units" => "1", "long_name" => "land mask", "standard_name" => "land_binary_mask"), NCDatasets), "lon" => NCvar("lon", "degrees_east", NCvar[NCvar("i_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "i_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 1"), NCDatasets), NCvar("j_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "j_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 2"), NCDatasets)], Float32[-37.5 -37.5 … NaN NaN; -36.5 -36.5 … NaN NaN; … ; NaN NaN … NaN NaN; 51.5 51.5 … NaN NaN], Dict("units" => "degrees_east", "long_name" => "longitude"), NCDatasets)), Dict{String, NCvar}("i_c" => NCvar("i_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "i_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 1"), NCDatasets), "dep_c" => NCvar("dep_c", "m", 50, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "dep_c", NCDatasets, Float32), Dict("units" => "m", "long_name" => "depth", "standard_name" => "depth", "positive" => "down"), NCDatasets), "j_c" => NCvar("j_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "j_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 2"), NCDatasets), "tim" => NCvar("tim", "days since 1992-01-01 0:0:0", 3, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "tim", NCDatasets, Dates.DateTime), Dict("units" => "days since 1992-01-01 0:0:0", "long_name" => "time", "standard_name" => "time"), NCDatasets)), Dict{Any, Any}("missing_value" => NaN, "ntile" => 13.0, "itile" => 3.0, "_FillValue" => NaN))

Add a second 3D variable

tmp,_,_ = readncfile(file_in2)
T=ncvars["THETA"]
S=tmp["SALT"]
ncvars["SALT"]=NCvar(S.name,S.units,T.dims,S.values,S.atts,T.backend)
NCvar("SALT", "psu", NCvar[NCvar("i_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "i_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 1"), NCDatasets), NCvar("j_c", "1", 90, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "j_c", NCDatasets, Int64), Dict("units" => "1", "long_name" => "Cartesian coordinate 2"), NCDatasets), NCvar("dep_c", "m", 50, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "dep_c", NCDatasets, Float32), Dict("units" => "m", "long_name" => "depth", "standard_name" => "depth", "positive" => "down"), NCDatasets), NCvar("tim", "days since 1992-01-01 0:0:0", 3, NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/THETA/THETA.0003.nc", "tim", NCDatasets, Dates.DateTime), Dict("units" => "days since 1992-01-01 0:0:0", "long_name" => "time", "standard_name" => "time"), NCDatasets)], NCData("/tmp/NCTILES_TESTCASES_OUTPUT/ex3/SALT/SALT.0003.nc", "SALT", NCDatasets, Float32), Dict("units" => "psu", "coordinates" => "lon lat dep_c tim"), NCDatasets)

Rewrite to a new file

write(ncvars,file_out,README=README,globalattribs=fileatts)
closed Dataset

This page was generated using Literate.jl.