Skip to content

Fix north-south flip in FFT filters for descending-coordinate grids#683

Open
gaoflow wants to merge 1 commit into
fatiando:mainfrom
gaoflow:fix/586-filter-descending-coords
Open

Fix north-south flip in FFT filters for descending-coordinate grids#683
gaoflow wants to merge 1 commit into
fatiando:mainfrom
gaoflow:fix/586-filter-descending-coords

Conversation

@gaoflow
Copy link
Copy Markdown

@gaoflow gaoflow commented Jun 1, 2026

Fixes #586

Problem

The FFT-based filters (derivative_*, upward_continuation, reduction_to_pole, etc., all routed through apply_filter) silently produced an output flipped along a dimension when the input grid stored that coordinate in descending order. This happens for grids read from some file formats (e.g. ERMapper/ERS) where the northing decreases with row index. @santisoler reproduced this in the issue.

Root cause

In harmonica/filters/_utils.py::apply_filter, the inverse FFT (xrft.ifft) always returns the grid in ascending coordinate order. The code then did:

filtered_grid = filtered_grid.assign_coords(
    {dims[1]: grid[dims[1]], dims[0]: grid[dims[0]]}
)

assigning the original (descending) coordinates onto the ascending result — mislabelling every row/column so the data ends up flipped relative to its coordinates. (The frequency coordinates built by xrft also assume positive sample spacing.)

Fix

Sort the grid to ascending order before transforming, and restore the original coordinate order afterwards via reindex. Grids already in ascending order take the same path as before (no sortby/reindex), so their results are unchanged.

Reproduction / verification

A minimal synthetic check (no external data needed): apply a filter to a grid and to the same physical grid with one coordinate reversed; after aligning on that coordinate the two results must be identical.

filter rel. mismatch (desc vs asc), before → after
derivative_upward ~1.0 (flipped) → 0.0
upward_continuation ~1.0 → 0.0
reduction_to_pole ~1.0 → 0.0
  • Added a parametrised regression test (northing and easting) in test/test_transformations.py that fails on main and passes with the fix.
  • Full test/test_transformations.py + test/test_filters.py suite: 62 passed (was 60). ruff check / ruff format --check clean.

The FFT-based filters (derivatives, upward continuation, reduction to pole,
etc.) silently produced an output flipped along a dimension when the input
grid stored that coordinate in descending order, as happens for grids read
from some file formats (e.g. ERMapper/ERS) where northing decreases with row
index.

The inverse FFT always returns the grid in ascending coordinate order, but
apply_filter then assigned the original (descending) coordinates onto that
ascending result, mislabelling every row/column and flipping the data
relative to its coordinates. The frequency coordinates built by xrft also
assume positive sample spacing.

Sort the grid to ascending order before transforming and restore the original
coordinate order afterwards via reindex. Grids already in ascending order are
unaffected. Adds a parametrised regression test (northing and easting)
asserting a descending-coordinate grid is not flipped.

Fixes fatiando#586
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

harmonica.reduction_to_pole flipping the output in north-south

1 participant