Fix north-south flip in FFT filters for descending-coordinate grids#683
Open
gaoflow wants to merge 1 commit into
Open
Fix north-south flip in FFT filters for descending-coordinate grids#683gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #586
Problem
The FFT-based filters (
derivative_*,upward_continuation,reduction_to_pole, etc., all routed throughapply_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: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
xrftalso 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 (nosortby/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.
derivative_upwardupward_continuationreduction_to_polenorthingandeasting) intest/test_transformations.pythat fails onmainand passes with the fix.test/test_transformations.py+test/test_filters.pysuite: 62 passed (was 60).ruff check/ruff format --checkclean.