|
841 | 841 | var,
|
842 | 842 | )
|
843 | 843 | end
|
| 844 | + |
| 845 | +@testset "split_by_season" begin |
| 846 | + lon = collect(range(-179.5, 179.5, 360)) |
| 847 | + lat = collect(range(-89.5, 89.5, 180)) |
| 848 | + time = [0.0] |
| 849 | + push!(time, 5_184_000.0) # correspond to 2024-3-1 |
| 850 | + push!(time, 5_184_001.0) |
| 851 | + push!(time, 13_132_800.0) # correspond to 2024-6-1 |
| 852 | + push!(time, 13_132_802.0) |
| 853 | + push!(time, 13_132_803.0) |
| 854 | + data = ones(length(lat), length(time), length(lon)) |
| 855 | + dims = OrderedDict(["lat" => lat, "time" => time, "lon" => lon]) |
| 856 | + attribs = Dict("long_name" => "hi", "start_date" => "2024-1-1") |
| 857 | + dim_attribs = OrderedDict([ |
| 858 | + "lat" => Dict("units" => "deg"), |
| 859 | + "time" => Dict("units" => "s"), |
| 860 | + "lon" => Dict("units" => "deg"), |
| 861 | + ]) |
| 862 | + var = ClimaAnalysis.OutputVar(attribs, dims, dim_attribs, data) |
| 863 | + |
| 864 | + MAM, JJA, SON, DJF = ClimaAnalysis.split_by_season(var) |
| 865 | + |
| 866 | + # Check size of data |
| 867 | + @test size(MAM.data) == (length(lat), 2, length(lon)) |
| 868 | + @test size(JJA.data) == (length(lat), 3, length(lon)) |
| 869 | + @test size(SON.data) == (0,) |
| 870 | + @test size(DJF.data) == (length(lat), 1, length(lon)) |
| 871 | + |
| 872 | + # Check times are correct in OutputVars |
| 873 | + @test MAM.dims["time"] == [5_184_000.0, 5_184_001.0] |
| 874 | + @test JJA.dims["time"] == [13_132_800.0, 13_132_802.0, 13_132_803.0] |
| 875 | + @test DJF.dims["time"] == [0.0] |
| 876 | + |
| 877 | + # Check start date |
| 878 | + MAM.attributes["start_date"] == "2024-1-1" |
| 879 | + JJA.attributes["start_date"] == "2024-1-1" |
| 880 | + DJF.attributes["start_date"] == "2024-1-1" |
| 881 | + |
| 882 | + # Check empty OutputVar |
| 883 | + @test isempty(SON) |
| 884 | + |
| 885 | + # Check error handling |
| 886 | + attribs_no_start_date = Dict("long_name" => "hi") |
| 887 | + var = |
| 888 | + ClimaAnalysis.OutputVar(attribs_no_start_date, dims, dim_attribs, data) |
| 889 | + @test_throws ErrorException ClimaAnalysis.split_by_season(var) |
| 890 | + |
| 891 | + dim_attribs_no_sec = OrderedDict([ |
| 892 | + "lat" => Dict("units" => "deg"), |
| 893 | + "time" => Dict("units" => "min"), |
| 894 | + "lon" => Dict("units" => "deg"), |
| 895 | + ]) |
| 896 | + var = ClimaAnalysis.OutputVar(attribs, dims, dim_attribs_no_sec, data) |
| 897 | + @test_throws ErrorException ClimaAnalysis.split_by_season(var) |
| 898 | + |
| 899 | + lon = collect(range(-179.5, 179.5, 360)) |
| 900 | + data = ones(length(lon)) |
| 901 | + dims = OrderedDict(["lon" => lon]) |
| 902 | + attribs = Dict("long_name" => "hi", "start_date" => "2024-1-1") |
| 903 | + dim_attribs = OrderedDict(["lon" => Dict("units" => "deg")]) |
| 904 | + var = ClimaAnalysis.OutputVar(attribs, dims, dim_attribs, data) |
| 905 | + @test_throws ErrorException ClimaAnalysis.split_by_season(var) |
| 906 | +end |
0 commit comments