Skip to content

Commit

Permalink
fix regridder test warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Oct 28, 2024
1 parent 0e36efb commit e691c1c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
32 changes: 31 additions & 1 deletion test/TestHelper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function gen_ncdata(FT, path, varname, val)
# Define dataset information
NCDatasets.defDim(nc, "lon", 64)
NCDatasets.defDim(nc, "lat", 32)
nc.attrib["title"] = "this is an NCDataset containing all 1s on a lat/lon grid"
nc.attrib["title"] = "this is an NCDataset containing the same value at each point on a lat/lon grid"

# Define variables
lon = NCDatasets.defVar(nc, "lon", FT, ("lon",))
Expand All @@ -103,4 +103,34 @@ function gen_ncdata(FT, path, varname, val)
close(nc)
end

function gen_ncdata_time(FT, path, varname, val)
isfile(path) && rm(path)

# Create dataset of all ones
nc = NCDatasets.NCDataset(path, "c")

# Define dataset information
NCDatasets.defDim(nc, "lon", 64)
NCDatasets.defDim(nc, "lat", 32)
NCDatasets.defDim(nc, "time", 2)
nc.attrib["title"] = "this is an NCDataset containing the same value at each point on a lat/lon grid at two times"

# Define variables
lon = NCDatasets.defVar(nc, "lon", FT, ("lon",))
lat = NCDatasets.defVar(nc, "lat", FT, ("lat",))
time = NCDatasets.defVar(nc, "time", Float64, ("time",))
v = NCDatasets.defVar(nc, varname, FT, ("lon", "lat", "time"))

# Populate lon and lat
lon[:] = [i for i in 0.0:(360 / 64):(360 - (360 / 64))]
lat[:] = [i for i in (-90 + (180 / 64)):(180 / 32):(90 - (180 / 64))]
time[:] = [Float64(i) for i in 1:2]

# Generate some example data and write it to v
v[:, :, 1] = [val * 0 for i in 1:nc.dim["lon"], j in 1:nc.dim["lat"]]
v[:, :, 2] = [val for i in 1:nc.dim["lon"], j in 1:nc.dim["lat"]]

close(nc)
end

end
26 changes: 23 additions & 3 deletions test/regridder_tests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#=
Unit tests for ClimaCoupler Regridder module
=#
import Test: @testset, @test
import Test: @testset, @test, @test_logs
import Dates
import NCDatasets
import ClimaComms
Expand Down Expand Up @@ -139,6 +139,26 @@ for FT in (Float32, Float64)
@test all(parent(combined_field) .== FT(sum(fractions) * sum(fields) / length(fields)))
end

@testset "test get_time" begin
# Set up regrid directory for this test only
mktempdir(pwd()) do regrid_dir
# Test dataset containing times
data_path = joinpath(regrid_dir, "data_times.nc")
varname = "test_data"
TestHelper.gen_ncdata_time(FT, data_path, varname, FT(1))
NCDatasets.NCDataset(data_path, "r") do ds
@test Regridder.get_time(ds) == Dates.DateTime.(Array(ds["time"]))
end

# Test warning when no dates are available in input data file
data_path = joinpath(regrid_dir, "data_no_times.nc")
varname = "test_data"
TestHelper.gen_ncdata(FT, data_path, varname, FT(1))
NCDatasets.NCDataset(data_path, "r") do ds
@test_logs (:warn, "No dates available in input data file") Regridder.get_time(ds)
end
end
end

# Add tests which use TempestRemap here -
# TempestRemap is not built on Windows because of NetCDF support limitations
Expand Down Expand Up @@ -193,7 +213,7 @@ for FT in (Float32, Float64)
# Initialize dataset of all ones
data_path = joinpath(regrid_dir, "ls_mask_data.nc")
varname = "test_data"
TestHelper.gen_ncdata(FT, data_path, varname, FT(1))
TestHelper.gen_ncdata_time(FT, data_path, varname, FT(1))

# Test monotone masking
land_fraction_mono =
Expand All @@ -219,7 +239,7 @@ for FT in (Float32, Float64)
# Initialize dataset of all 0.5s
data_path = joinpath(regrid_dir, "ls_mask_data.nc")
varname = "test_data_halves"
TestHelper.gen_ncdata(FT, data_path, varname, FT(0.5))
TestHelper.gen_ncdata_time(FT, data_path, varname, FT(0.5))

# Test non-monotone masking
land_fraction_halves =
Expand Down

0 comments on commit e691c1c

Please sign in to comment.