Skip to content

Commit

Permalink
Base.similar for <:AbstractGrids
Browse files Browse the repository at this point in the history
  • Loading branch information
milankl committed Aug 4, 2023
1 parent 5dbf707 commit 194a33d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/RingGrids/RingGrids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ include("octahealpix.jl")
include("quadrature_weights.jl")
include("interpolation.jl")
include("show.jl")
include("similar.jl")
end
24 changes: 24 additions & 0 deletions src/RingGrids/similar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# todo this should be replaced by general broadcasting for <: AbstractGrid
for Grid in (:FullGaussianGrid, :FullClenshawGrid, :FullHEALPixGrid, :FullOctaHEALPixGrid,
:OctahedralGaussianGrid, :OctahedralClenshawGrid, :HEALPixGrid,
:OctaHEALPixGrid)
@eval begin

function Base.similar(G::$Grid)
return $Grid{eltype(G)}(undef,G.nlat_half)
end

function Base.similar(G::$Grid,::Type{T}) where T
return $Grid{T}(undef,G.nlat_half)
end

function Base.similar(G::$Grid,nlat_half::Integer)
return $Grid{eltype(G)}(undef,nlat_half)
end

function Base.similar(G::$Grid,::Type{T},nlat_half::Integer) where T
return $Grid{T}(undef,nlat_half)
end

end
end
12 changes: 6 additions & 6 deletions src/SpeedyTransforms/spectral_transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,13 @@ end
"""
$(TYPEDSIGNATURES)
Spectral transform (grid to spectral) `map` to `grid(map)` to execute `spectral(map::AbstractGrid;kwargs...)`."""
function spectral( map::Grid, # gridded field
S::SpectralTransform{NF}, # spectral transform struct
) where {Grid<:AbstractGrid,NF} # number format NF
function spectral( map::AbstractGrid, # gridded field
S::SpectralTransform{NF}, # spectral transform struct
) where NF # number format NF

map_NF = Grid{NF}(map) # convert map to NF
map_NF = similar(map,NF) # convert map to NF
copyto!(map_NF,map)

# always use one more l for consistency with vector quantities
alms = LowerTriangularMatrix{Complex{NF}}(undef,S.lmax+1,S.mmax+1)
return spectral!(alms,map_NF,S) # in-place version
return spectral!(alms,map_NF,S) # in-place version
end

0 comments on commit 194a33d

Please sign in to comment.