Skip to content

regrid! does not accept AbstractMatrix despite docstring claiming n-dimensional support #67

@ctessum-claude

Description

@ctessum-claude

Description

In src/regridder/regrid.jl:21-47, all four regrid! method signatures restrict arguments to AbstractVector or DenseVector:

function regrid!(dst_field::DenseVector, regridder::Regridder, src_field::DenseVector)
function regrid!(dst_field::AbstractVector, regridder::Regridder, src_field::AbstractVector)
function regrid!(dst_field::DenseVector, regridder::Regridder, src_field::AbstractVector)
function regrid!(dst_field::AbstractVector, regridder::Regridder, src_field::DenseVector)

However, the docstring (lines 4-5) states:

src_field and dst_field can be any n-dimensional array in which case regridding of the 1st dimension is broadcast to additional dimensions.

Passing a matrix (e.g., for regridding multiple vertical levels at once) throws a MethodError.

MWE

using ConservativeRegridding
using GeoInterface
using GeometryOpsCore
using Extents

function make_grid(nx, ny)
    polys = Matrix{GeoInterface.Polygon}(undef, nx, ny)
    for j in 1:ny, i in 1:nx
        x0, x1 = (i-1)/nx, i/nx
        y0, y1 = (j-1)/ny, j/ny
        ring = GeoInterface.LinearRing([(x0,y0),(x1,y0),(x1,y1),(x0,y1),(x0,y0)])
        polys[i,j] = GeoInterface.Polygon([ring])
    end
    polys
end

src_grid = make_grid(2, 2)
dst_grid = make_grid(3, 3)

# Fix bug #65 to create a working regridder:
@eval function ConservativeRegridding.Trees.cell_range_extent(
    q::ConservativeRegridding.Trees.ExplicitPolygonGrid{<:GeometryOpsCore.Planar},
    irange::UnitRange{Int}, jrange::UnitRange{Int},
)
    return mapreduce(
        GeoInterface.extent, Extents.union,
        (ConservativeRegridding.Trees.getcell(q, i, j) for i in irange, j in jrange),
    )
end

r = ConservativeRegridding.Regridder(GeometryOpsCore.Planar(), dst_grid, src_grid; threaded=false)

# Vector inputs work fine:
src_vec = ones(Float64, 4)
dst_vec = zeros(Float64, 9)
ConservativeRegridding.regrid!(dst_vec, r, src_vec)  # works

# Matrix inputs fail:
src_mat = ones(Float64, 4, 3)    # 4 source cells × 3 levels
dst_mat = zeros(Float64, 9, 3)   # 9 destination cells × 3 levels
ConservativeRegridding.regrid!(dst_mat, r, src_mat)  # MethodError

Error

MethodError: no method matching regrid!(::Matrix{Float64}, ::Regridder{...}, ::Matrix{Float64})

Version

ConservativeRegridding v0.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions