Skip to content

Commit

Permalink
Merge 100f834 into 8c09c37
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelforget authored Mar 21, 2020
2 parents 8c09c37 + 100f834 commit 4031e97
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ function createfile(filename, field::Union{NCvar,Dict}, rdm="";
if ~isnothing(attribs)
fillval = pop!(attribs,"_FillValue",NaN)
missingval = pop!(attribs,"missing_value",NaN)
itile = pop!(attribs,"itile",1)
ntile = pop!(attribs,"ntile",1)
haskey(attribs,"itile") ? itile = pop!(attribs,"itile") : nothing
haskey(attribs,"ntile") ? ntile = pop!(attribs,"ntile") : nothing
description = [pop!(attribs,k,"") for k in ["description","A","B","C","D","E","F","G","H","I","J"]]
description = description[.~isempty.(description)]
if isempty(README)
Expand Down
51 changes: 9 additions & 42 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,11 @@ ncfilnc2d,tmp = mktemp()
ncfilnc3d,tmp = mktemp()
ncfiltile2d,tmp = mktemp()

ncfilarray2d_hl,tmp = mktemp()
ncfilarray3d_hl,tmp = mktemp()
ncfilbin2d_hl,tmp = mktemp()
ncfilbin3d_hl,tmp = mktemp()
ncfilnc2d_hl,tmp = mktemp()
ncfilnc3d_hl,tmp = mktemp()
ncfiltile2d_hl,tmp = mktemp()

tempfiles = vcat(testvars["fnames2d"], testvars["fnames3d"],
testvars["tile_ex"]["fnamestile2d"],
ncfilarray2d, ncfilarray3d,
ncfilbin2d, ncfilbin3d,
ncfilnc2d, ncfilnc3d,
ncfilarray2d_hl, ncfilarray3d_hl,
ncfilbin2d_hl, ncfilbin3d_hl,
ncfilnc2d_hl, ncfilnc3d_hl)
ncfilnc2d, ncfilnc3d)

@testset "NCtiles Tests" begin

Expand All @@ -39,17 +28,11 @@ tempfiles = vcat(testvars["fnames2d"], testvars["fnames3d"],
fld2d = NCvar("data2d",testvars["units"],[testvars["dims"][1:2]; testvars["dims"][4]],testvars["data2d"],Dict("long_name" =>testvars["longname2d"]),NCDatasets)
fld3d = NCvar("data3d",testvars["units"],testvars["dims"],testvars["data3d"],Dict("long_name" =>testvars["longname3d"]),NCDatasets)

writetestfile(ncfilarray2d,fld2d,NCDatasets)
writetestfile(ncfilarray3d,fld3d,NCDatasets)
write(fld2d,ncfilarray2d)
write(fld3d,ncfilarray3d)

@test testfile(ncfilarray2d,fld2d)
@test testfile(ncfilarray3d,fld3d)

write(fld2d,ncfilarray2d_hl)
write(fld3d,ncfilarray3d_hl)

@test testfile(ncfilarray2d_hl,fld2d)
@test testfile(ncfilarray3d_hl,fld3d)
end

# Test BinData write
Expand All @@ -60,17 +43,11 @@ tempfiles = vcat(testvars["fnames2d"], testvars["fnames3d"],
bfld2d = NCvar("data2d",testvars["units"],[testvars["dims"][1:2]; testvars["dims"][4]],bdatafld2d,Dict("long_name" =>testvars["longname2d"]),NCDatasets)
bfld3d = NCvar("data3d",testvars["units"],testvars["dims"],bdatafld3d,Dict("long_name" =>testvars["longname3d"]),NCDatasets)

writetestfile(ncfilbin2d,bfld2d,NCDatasets)
writetestfile(ncfilbin3d,bfld3d,NCDatasets)
write(bfld2d,ncfilbin2d)
write(bfld3d,ncfilbin3d)

@test testfile(ncfilbin2d,bfld2d)
@test testfile(ncfilbin3d,bfld3d)

write(bfld2d,ncfilbin2d_hl)
write(bfld3d,ncfilbin3d_hl)

@test testfile(ncfilbin2d_hl,bfld2d)
@test testfile(ncfilbin3d_hl,bfld3d)
end

#Test NCData write
Expand All @@ -81,17 +58,11 @@ tempfiles = vcat(testvars["fnames2d"], testvars["fnames3d"],
ncfld2d = NCvar("data2d",testvars["units"],[testvars["dims"][1:2]; testvars["dims"][4]],ncdatafld2d,Dict("long_name" =>testvars["longname2d"]),NCDatasets)
ncfld3d = NCvar("data3d",testvars["units"],testvars["dims"],ncdatafld3d,Dict("long_name" =>testvars["longname3d"]),NCDatasets)

writetestfile(ncfilnc2d,ncfld2d,NCDatasets)
writetestfile(ncfilnc3d,ncfld3d,NCDatasets)
write(ncfld2d,ncfilnc2d)
write(ncfld3d,ncfilnc3d)

@test testfile(ncfilnc2d,ncfld2d)
@test testfile(ncfilnc3d,ncfld3d)

write(ncfld2d,ncfilnc2d_hl)
write(ncfld3d,ncfilnc3d_hl)

@test testfile(ncfilnc2d_hl,ncfld2d)
@test testfile(ncfilnc3d_hl,ncfld3d)
end

@testset "Tile Data" begin
Expand Down Expand Up @@ -119,20 +90,16 @@ tempfiles = vcat(testvars["fnames2d"], testvars["fnames3d"],
"area" => NCvar("area","m^2",dims[1:2],tilarea,Dict(["long_name" => "grid cell area", "standard_name" => "cell_area"]),NCDatasets),
"land" => NCvar("land","1",dims[1:3],tilland,Dict(["long_name" => "land mask", "standard_name" => "land_binary_mask"]),NCDatasets),
"thic" => NCvar("thic","m",dims[3],thic,Dict("standard_name" => "cell_thickness"),NCDatasets)
])
writetestfile(ncfiltile2d,flds,NCDatasets)
write(flds, ncfiltile2d_hl)
])
write(flds, ncfiltile2d)
savenames = joinpath.(ncfiltile2d*".".*lpad.(string.(1:tilfld2d.numtiles),4,"0").*".nc")
savenames_hl = joinpath.(ncfiltile2d_hl*".".*lpad.(string.(1:tilfld2d.numtiles),4,"0").*".nc")
for k in keys(flds)
if k != "thic" && k != "land" && isa(flds[k].values,MeshArray)
applylandmask(flds[k],land)
end
end
@test all([testfile(fname,flds[fld]) for fname in savenames for fld in keys(flds)])
#@test all([testfile(fname,flds[fld]) for fname in savenames_hl for fld in keys(flds)])
rm.(savenames)
rm.(savenames_hl)
end

end
Expand Down
69 changes: 25 additions & 44 deletions test/testutils.jl
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
using NCTiles, NCDatasets, MeshArrays, Dates

function writetestfile(fname,field,package)

if isfile(fname)
rm(fname)
end

README = ["This file is written as a test for NCTiles.jl."]
if isa(field,Dict) # TileData Example
savenames = joinpath.(ncfiltile2d*".".*lpad.(string.(1:field["data2d"].values.numtiles),4,"0").*".nc")
dims = unique(vcat([field[v].dims for v in keys(field)]...))
datasets = [createfile(savenames[tidx],field,README,ntile = length(savenames), itile = tidx) for tidx in 1:length(savenames)]

ds = [x[1] for x in datasets]
fldvars = [x[2] for x in datasets]
#dims = [x[3] for x in datasets]

for k in keys(field)
if isa(field[k].values,TileData)
addData(fldvars,field[k])
else
tmpfldvars = [fv[findfirst(isequal(k),name.(fv))] for fv in fldvars]
addData.(tmpfldvars,Ref(field[k]))
end
end

for dim in dims
addDimData.(ds,Ref(dim))
end
close.(ds)

else
ds,fldvar,dimlist = createfile(fname,field,README)

addData(fldvar,field)
if package == NCDatasets
addDimData.(Ref(ds),field.dims)
end
# Close the file
close(ds)
end
end
"""
testfile(fname,checkfld)
Perform various checks on a netcdf file (fname) variable (checkfld).
"""
function testfile(fname,checkfld)
pass = true
ds = Dataset(fname)
dsvar = ds[checkfld.name]
varcheck = Dict()

#1. checks on dimensions
varcheck["dims"] = Dict()
for d in checkfld.dims
fildim = ds[d.name]
Expand Down Expand Up @@ -76,8 +42,7 @@ function testfile(fname,checkfld)
varcheck["dims"][d.name] = dimcheck
end

dsvar = ds[checkfld.name]

#2. checks on units
for k in keys(dsvar.attrib)
if k == "units"
varcheck["units"] = dsvar.attrib[k] == checkfld.units
Expand All @@ -88,7 +53,7 @@ function testfile(fname,checkfld)
end
end

# Check values of test data
#3. checks on values
varcheck["values"] = false
if isa(checkfld.values,Array)
varcheck["values"] = true
Expand Down Expand Up @@ -166,6 +131,7 @@ function testfile(fname,checkfld)
end
pass = pass && varcheck["values"]

#4. printout message if >0 test failed
if ~pass
missmatchstring = ""
for k in keys(varcheck)
Expand All @@ -188,6 +154,11 @@ function testfile(fname,checkfld)
return pass
end

"""
applylandmask(data,land)
Apply land mask either lazily (NCvar) or eagerly (other input types).
"""
function applylandmask(data,land)
if isa(data,NCvar)
if length(data.dims) == 2
Expand All @@ -209,6 +180,11 @@ function applylandmask(data,land)
return dataout
end

"""
maketestdata()
Create test data on a simple, coarse-grained Earth grid.
"""
function maketestdata()
lon=-180:20:180; lat=-90:20:90;
depth = 5:10:100
Expand Down Expand Up @@ -269,6 +245,11 @@ function maketestdata()

end

"""
getgrid()
Download (if not already done) real ocean model grid (GRID_CS32) for testing pkg.
"""
function getgrid()

testdir = abspath(joinpath(dirname(pathof(NCTiles)),"..","test"))
Expand Down

0 comments on commit 4031e97

Please sign in to comment.