@@ -3,6 +3,7 @@ using ClimateTools
33using YAXArrays
44using DimensionalData
55using Dates
6+ using Serialization
67
78@testset " Regridder rectilinear workflow" begin
89 src_lon = collect (0.0 : 1.0 : 4.0 )
313314 out_multi = regrid_cube (ds, :pr , dest_multi; method= " bilinear" )
314315 @test Base. maximum (abs .(Array (out_multi) .- expected)) < 1e-6
315316end
317+
318+ @testset " Regrid helper coverage" begin
319+ @test ClimateTools. _normalize_regrid_method (" linear" ) == :bilinear
320+ @test ClimateTools. _normalize_regrid_method (" nearest" ) == :nearest_s2d
321+ @test ClimateTools. _normalize_regrid_method (" idw" ) == :idw
322+ @test_throws ErrorException ClimateTools. _normalize_regrid_method (" cubic" )
323+
324+ ascending, asc_flip = ClimateTools. _prepare_source_coordinate ([1.0 , 2.0 , 3.0 ], :longitude )
325+ descending, desc_flip = ClimateTools. _prepare_source_coordinate ([3.0 , 2.0 , 1.0 ], :latitude )
326+ @test ascending == [1.0 , 2.0 , 3.0 ]
327+ @test asc_flip == false
328+ @test descending == [1.0 , 2.0 , 3.0 ]
329+ @test desc_flip == true
330+ @test_throws ErrorException ClimateTools. _prepare_source_coordinate ([1.0 , 1.0 , 2.0 ], :longitude )
331+ @test_throws ErrorException ClimateTools. _prepare_source_coordinate ([1.0 , 3.0 , 2.0 ], :longitude )
332+
333+ @test ClimateTools. _weighted_value ((1.0 , 2.0 , 3.0 , 4.0 ), (0.25 , 0.25 , 0.25 , 0.25 )) ≈ 2.5
334+ @test isnan (ClimateTools. _weighted_value ((1.0 , missing , 3.0 , 4.0 ), (0.25 , 0.25 , 0.25 , 0.25 )))
335+ @test ClimateTools. _weighted_value ((1.0 , missing , 3.0 , 5.0 ), (0.25 , 0.25 , 0.25 , 0.25 ); skipna= true , na_thres= 0.3 ) ≈ 3.0
336+ @test isnan (ClimateTools. _weighted_value ((1.0 , missing , 3.0 , 5.0 ), (0.25 , 0.25 , 0.25 , 0.25 ); skipna= true , na_thres= 0.2 ))
337+
338+ lon2d, lat2d = ClimateTools. _destination_grid ([0.0 , 1.0 ], [10.0 , 11.0 ])
339+ @test size (lon2d) == (2 , 2 )
340+ @test size (lat2d) == (2 , 2 )
341+ @test_throws ErrorException ClimateTools. _destination_grid (reshape ([0.0 , 1.0 ], 2 , 1 ), [10.0 , 11.0 ])
342+ @test_throws ErrorException ClimateTools. _destination_grid (ones (2 , 2 ), ones (3 , 1 ))
343+
344+ pts = [0.0 10.0 ; 1.0 11.0 ]
345+ lon_grid = [0.0 1.0 ; 0.0 1.0 ]
346+ lat_grid = [10.0 10.0 ; 11.0 11.0 ]
347+ @test all (isnan, ClimateTools. idw_griddata (pts, [NaN , NaN ], lon_grid, lat_grid))
348+
349+ src_lon = [0.0 , 1.0 ]
350+ src_lat = [10.0 , 11.0 ]
351+ src = YAXArray ((Dim {:longitude} (src_lon), Dim {:latitude} (src_lat)), [1.0 2.0 ; 3.0 4.0 ])
352+ dest = YAXArray ((Dim {:longitude} ([0.5 ]), Dim {:latitude} ([10.5 ])), zeros (1 , 1 ))
353+ regridder = Regridder (src, dest; method= " bilinear" )
354+ @test_throws ErrorException regrid (src, regridder; na_thres= 1.5 )
355+
356+ payload = ClimateTools. _regridder_payload (regridder)
357+ restored = ClimateTools. _regridder_from_payload (payload)
358+ @test restored. method == regridder. method
359+ @test restored. dest_lon == regridder. dest_lon
360+
361+ mktempdir () do tmpdir
362+ missing_version_path = joinpath (tmpdir, " missing-version.bin" )
363+ open (missing_version_path, " w" ) do io
364+ Serialization. serialize (io, (method= :bilinear ,))
365+ end
366+ @test_throws ErrorException load_regridder (missing_version_path)
367+
368+ bad_version_path = joinpath (tmpdir, " bad-version.bin" )
369+ open (bad_version_path, " w" ) do io
370+ Serialization. serialize (io, (version= 999 , method= :bilinear ))
371+ end
372+ @test_throws ErrorException load_regridder (bad_version_path)
373+ end
374+ end
375+
376+ @testset " Regrid rotated dataset helper fallback" begin
377+ north_pole_lon = 0.0
378+ north_pole_lat = 42.5
379+
380+ rlon_vals = collect (0.0 : 1.0 : 3.0 )
381+ rlat_vals = collect (- 1.0 : 1.0 : 2.0 )
382+ rlon2d, rlat2d = ClimateTools. ndgrid (rlon_vals, rlat_vals)
383+ data = Float64 .(rlon2d .+ 2 .* rlat2d)
384+
385+ pr_cube = YAXArray (
386+ (Dim {:rlon} (rlon_vals), Dim {:rlat} (rlat_vals)),
387+ data,
388+ Dict {String, Any} (" grid_mapping" => " rotated_pole" ),
389+ )
390+ rp_cube = YAXArray (
391+ (Dim {:maxStrlen64} (1 : 1 ),),
392+ [' ' ],
393+ Dict {String, Any} (
394+ " grid_north_pole_longitude" => north_pole_lon,
395+ " grid_north_pole_latitude" => north_pole_lat,
396+ ),
397+ )
398+ ds = Dataset (pr= pr_cube, rotated_pole= rp_cube)
399+
400+ lon_geo, lat_geo = ClimateTools. _extract_geographic_coords (ds, pr_cube, " rotated_pole" )
401+ @test size (lon_geo) == size (data)
402+ @test size (lat_geo) == size (data)
403+ @test all (isfinite, lon_geo)
404+ @test all (isfinite, lat_geo)
405+
406+ dest = YAXArray ((Dim {:longitude} (collect (range (minimum (lon_geo), maximum (lon_geo), length= 3 ))), Dim {:latitude} (collect (range (minimum (lat_geo), maximum (lat_geo), length= 3 )))), zeros (3 , 3 ))
407+ regridder = Regridder (ds, :pr , dest; method= " idw" )
408+ out = regrid (ds[:pr ], regridder)
409+ @test size (out) == (3 , 3 )
410+ @test all (isfinite, Array (out))
411+ end
0 commit comments