Thank you, folks, for the ideas.
In the meanwhile, I wrote the program for my analysis and come to think that, as long as we assume the CF convention, it wouldn’t be so difficult to write a general variable exporter, although it would still be time-consuming to carefully read the CF convention and consider all possible cases.
I guess that nc_copy_var
, if wrapped in a high-level interface, would simplify part of the code.
I was pleasantly surprised at Raster
. It’s almost there. It would be perfect if NCDatasets had an “import Raster” feature:
orgvar = Raster("original.nc"; variable="orgvar") # I don't know if this works.
Dataset("new.nc", "c") do ds
importRaster(ds, orgvar; name=:newvar, importValues=false)
newvar = ds["newvar"]
newvar[:, :, :, :] = big_calculation(orgvar)
For your information, my program (which isn’t general enough and which assumes that there are no “bounds”) looks like this:
function get_orgvar() # get the original variable, its axes, and its attributes
ds = Dataset("original.nc", "r")
orgvar = ds["orgvar"]
axes = Vector{Any}(undef, 0)
for nam in dimnames(psi)
ax = ds[nam]
push!(axes, (ax, nam))
end
return orgvar, axes
end
orgvar, axes = get_orgvar()
Dataset("new.nc", "c") do ds # create the new variable
dims = Array{String}(undef,0)
for (ax,name) in axes
len = size(ax,1)
defDim(ds, name, len)
defVar(ds, name, Float64, (name,), attrib = ax.attrib)
ds[name][:] = ax[:]
push!(dims, name)
end
let
varname = "newvar"
defVar(ds, varname, Float64, dims, attrib = orgvar.attrib)
var = ds[varname]
var.attrib["units"] = "meters"
var[:,:,:,:] = big_calculation(orgvar)
end
Not too bad, but it’s tedious.