From e691c1c46eb7166ec501c85a8e2e41bc946c8211 Mon Sep 17 00:00:00 2001 From: Julia Sloan Date: Fri, 25 Oct 2024 13:04:23 -0700 Subject: [PATCH] fix regridder test warnings --- test/TestHelper.jl | 32 +++++++++++++++++++++++++++++++- test/regridder_tests.jl | 26 +++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/test/TestHelper.jl b/test/TestHelper.jl index 578fd1af2..537ec1785 100644 --- a/test/TestHelper.jl +++ b/test/TestHelper.jl @@ -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",)) @@ -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 diff --git a/test/regridder_tests.jl b/test/regridder_tests.jl index aa876f45c..bb570fc52 100644 --- a/test/regridder_tests.jl +++ b/test/regridder_tests.jl @@ -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 @@ -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 @@ -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 = @@ -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 =