Skip to content

Commit

Permalink
fix IO, add test for IO
Browse files Browse the repository at this point in the history
all tests  pass :) :) :)
  • Loading branch information
Datseris committed Nov 13, 2020
1 parent 8993d26 commit 4a80b60
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.nc
Manifest.toml
################################################################################
# DrWatson Project Structure #
Expand Down
34 changes: 19 additions & 15 deletions src/core/nc_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,25 @@ function climarrays_to_nc(file::String, X::ClimArray; globalattr = Dict())
end
function climarrays_to_nc(file::String, Xs; globalattr = Dict())
ds = NCDataset(file, "c"; attrib = globalattr)
for (i, fieldname) in enumerate(Xs)
println("processing variable $fieldname...")
W = climarray_from_xarray(xa, fieldname)
println("converting to CERES format...")
X = to_CERES_latitude(W)
println("writing dimensions...")
add_dims_to_ncfile!(ds, dims(X))
println("writing the CF-variable...")
attrib = X.attrib
dnames = dim_to_commonname.(dims(X))
data = Array(X)
@show (fieldname, summary(data), dnames)
defVar(ds, fieldname, data, (dnames...,); attrib)
end
close(ds)
# NCDataset("file.nc", "c"; attrib = globalattr) do ds
for (i, X) in enumerate(Xs)
n = string(X.name)
if n == ""
n = "x$i"
@warn "$i-th ClimArray has no name, naming it $(n) instead."
end
println("processing variable $(n)...")
println("writing dimensions...")
add_dims_to_ncfile!(ds, dims(X))
println("writing the CF-variable...")
attrib = X.attrib
isnothing(attrib) && (attrib = Dict())
dnames = dim_to_commonname.(dims(X))
data = Array(X)
defVar(ds, n, data, (dnames...,); attrib)
end
close(ds)
# end
end

function add_dims_to_ncfile!(ds::NCDatasets.AbstractDataset, dimensions::Tuple)
Expand Down
25 changes: 22 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using ClimateBase, Test, Dates
using Statistics

Time = ClimateBase.Time
cd(@__DIR__)

# TODO: Further test spatial averaging by making one hemisphere 1 and other 0
# TODO: Test downloaded .nc file

# Create the artificial dimensional array A that will be used in tests
function monthly_insolation(t::TimeType, args...)
Expand Down Expand Up @@ -31,8 +32,8 @@ for i in 1:length(lats)
end
end

A = ClimArray(A, d; name = "lon-variation")
B = ClimArray(B, d; name = "lon-constant")
A = ClimArray(A, d; name = "insolation")
B = ClimArray(B, d; attrib = Dict("a" => 2))

# %%

Expand Down Expand Up @@ -183,3 +184,21 @@ end
@test y[j] < y[j-1]
end
end

@testset "NetCDF file IO" begin
globat = Dict("history" => "test")
climarrays_to_nc("test.nc", (A, B); globalattr = globat)
Aloaded = ClimArray("test.nc", "insolation")
Bloaded = ClimArray("test.nc", "x2")

@test A.data == Aloaded.data
@test dims(Aloaded, Lon).metadata["units"] == "degrees_east"
@test B.data == Bloaded.data
@test string(Bloaded.name) == "x2"
@test dims(Bloaded, Time).metadata["standard_name"] == "time"

ds = NCDataset("test.nc")
@test ds.attrib["history"] == "test"
close(ds)
rm("test.nc")
end

0 comments on commit 4a80b60

Please sign in to comment.