|
| 1 | +# rtp3d.jl — reduce a magnetic field anomaly grid to the pole via 2-D Fourier transform, given the |
| 2 | +# inclination/declination of the field and of the magnetization (port of Mirone/M.A.Tivey's |
| 3 | +# utils/rtp3d.m, 1992/1994). Can also isolate the X (north), Y (east) or Z (up) component instead |
| 4 | +# of doing a full RTP. The 2-D FFT is built from GMT.jl's own `fft1d` (GMT_FFT_1D), applied |
| 5 | +# row-wise then column-wise — no FFTW.jl, no in-house FFT (same convention as xyanalysis.jl's 1-D |
| 6 | +# case: everything Fourier goes through the GMT library). |
| 7 | + |
| 8 | +""" |
| 9 | + fout, k = rtp3d(f3d, incl_fld, decl_fld, incl_mag, decl_mag; component=0) |
| 10 | +
|
| 11 | +Reduce a magnetic field anomaly map `f3d` to the pole, given the inclination/declination (degrees) |
| 12 | +of the ambient field (`incl_fld`, `decl_fld`) and of the magnetization (`incl_mag`, `decl_mag`). |
| 13 | +
|
| 14 | +`component` selects an alternative output instead of the plain RTP: `1` = X/North, `2` = Y/East, |
| 15 | +`3` = Z/Up component. Default `0` is the RTP. |
| 16 | +
|
| 17 | +Returns the transformed grid `fout` and the wavenumber array `k`. |
| 18 | +""" |
| 19 | +function rtp3d(f3d::Matrix{<:Real}, incl_fld::Real, decl_fld::Real, incl_mag::Real, decl_mag::Real; |
| 20 | + component::Integer=0) |
| 21 | + _rtp3d(Float64.(f3d), Float64(incl_fld), Float64(decl_fld), Float64(incl_mag), Float64(decl_mag), Int(component)) |
| 22 | +end |
| 23 | + |
| 24 | +# Annotate all inputs because Julia recompiles everything if a single input type changes. |
| 25 | +function _rtp3d(f3d::Matrix{Float64}, incl_fld::Float64, decl_fld::Float64, incl_mag::Float64, decl_mag::Float64, |
| 26 | + component::Int) |
| 27 | + D2R = pi / 180 |
| 28 | + incl_fld *= D2R; decl_fld *= D2R |
| 29 | + incl_mag *= D2R; decl_mag *= D2R |
| 30 | + |
| 31 | + ny, nx = size(f3d) |
| 32 | + x = collect((-0.5):(1/nx):(0.5 - 1/nx)) |
| 33 | + y = collect((-0.5):(1/ny):(0.5 - 1/ny)) |
| 34 | + |
| 35 | + X = Matrix{Float64}(undef, ny, nx) |
| 36 | + Y = Matrix{Float64}(undef, ny, nx) |
| 37 | + @inbounds for c = 1:nx, r = 1:ny |
| 38 | + X[r, c] = x[c] |
| 39 | + Y[r, c] = y[r] |
| 40 | + end |
| 41 | + k = 2pi .* sqrt.(X.^2 .+ Y.^2) # wavenumber array |
| 42 | + aux = atan.(Y, X) # auxiliary angle, computed once (as in the .m) |
| 43 | + |
| 44 | + # ------ geometric and amplitude factors |
| 45 | + Ob = sin(incl_fld) .+ im .* cos(incl_fld) .* sin.(aux .+ decl_fld) |
| 46 | + O = if (abs(incl_fld - incl_mag) > 0.03 || abs(decl_fld - decl_mag) > 0.03 || component != 0) # 0.03 rad is < 2 deg |
| 47 | + Om = sin(incl_mag) .+ im .* cos(incl_mag) .* sin.(aux .+ decl_mag) |
| 48 | + Ob .* Om |
| 49 | + else |
| 50 | + Ob .* Ob |
| 51 | + end |
| 52 | + |
| 53 | + if (component != 0) # X, Y or Z component instead of a plain RTP |
| 54 | + new_inc, new_dec = component == 1 ? (0.0, 0.0) : |
| 55 | + component == 2 ? (0.0, 90.0) : |
| 56 | + component == 3 ? (90.0, 0.0) : error("rtp3d: component must be 0, 1, 2 or 3") |
| 57 | + new_inc *= D2R; new_dec *= D2R |
| 58 | + O = O ./ ((sin(new_inc) .+ im .* (cos(new_inc) .* sin.(aux .+ new_dec)) .+ eps()) .* Om) |
| 59 | + end |
| 60 | + O = _fftshift2(O) |
| 61 | + |
| 62 | + mfin = sum(f3d) / length(f3d) # mean of the input field |
| 63 | + F = _fft2_gmt(complex.(f3d .- mfin)) |
| 64 | + fout = real.(_ifft2_gmt(F ./ O)) |
| 65 | + return fout, k |
| 66 | +end |
| 67 | + |
| 68 | +# ---- 2-D FFT/IFFT via GMT's own 1-D C FFT (GMT.fft1d -> GMT_FFT_1D), applied separably: each row |
| 69 | +# then each column. Mathematically identical to a native 2-D FFT since the DFT is separable. |
| 70 | +function _fft2_gmt(A::Matrix{<:Complex}) |
| 71 | + ny, nx = size(A) |
| 72 | + B = Matrix{ComplexF64}(undef, ny, nx) |
| 73 | + @inbounds for r = 1:ny |
| 74 | + B[r, :] = GMT.fft1d(view(A, r, :)) |
| 75 | + end |
| 76 | + C = Matrix{ComplexF64}(undef, ny, nx) |
| 77 | + @inbounds for c = 1:nx |
| 78 | + C[:, c] = GMT.fft1d(view(B, :, c)) |
| 79 | + end |
| 80 | + return C |
| 81 | +end |
| 82 | + |
| 83 | +function _ifft2_gmt(A::Matrix{<:Complex}) |
| 84 | + ny, nx = size(A) |
| 85 | + B = Matrix{ComplexF64}(undef, ny, nx) |
| 86 | + @inbounds for r = 1:ny |
| 87 | + B[r, :] = GMT.fft1d(view(A, r, :); inverse=true) |
| 88 | + end |
| 89 | + C = Matrix{ComplexF64}(undef, ny, nx) |
| 90 | + @inbounds for c = 1:nx |
| 91 | + C[:, c] = GMT.fft1d(view(B, :, c); inverse=true) |
| 92 | + end |
| 93 | + return C |
| 94 | +end |
| 95 | + |
| 96 | +# Same convention as AbstractFFTs' fftshift: circular shift by half (rounded down) each dimension. |
| 97 | +_fftshift2(A::Matrix{<:Complex}) = circshift(A, div.(size(A), 2)) |
0 commit comments