44
55"""
66 StructuredGrid(X, Y, Z, ...)
7- StructuredGrid{Datum }(X, Y, Z, ...)
7+ StructuredGrid{M,C }(X, Y, Z, ...)
88
99A structured grid with vertices at sorted coordinates `X`, `Y`, `Z`, ...,
10- and a given `Datum ` (default to `NoDatum `).
10+ manifold `M` (default to `𝔼`) and CRS type `C ` (default to `Cartesian `).
1111
1212## Examples
1313
@@ -20,41 +20,78 @@ julia> Y = repeat([0.0, 0.1, 0.3, 0.7, 0.9, 1.0]', 6, 1)
2020julia> StructuredGrid(X, Y)
2121```
2222"""
23- struct StructuredGrid{Datum,Dim,ℒ<: Len ,A<: AbstractArray{ℒ} } <: Grid{𝔼{Dim},Cartesian{Datum,Dim,ℒ},Dim}
24- XYZ:: NTuple{Dim,A}
25- topology:: GridTopology{Dim}
26-
27- function StructuredGrid {Datum} (XYZ:: NTuple{Dim,<:AbstractArray{<:Len}} , topology:: GridTopology{Dim} ) where {Datum,Dim}
28- coords = float .(XYZ)
29- A = eltype (coords)
30- new {Datum,Dim,eltype(A),A} (coords, topology)
23+ struct StructuredGrid{M<: Manifold ,C<: CRS ,N,X<: NTuple{N,AbstractArray} } <: Grid{M,C,N}
24+ XYZ:: X
25+ topology:: GridTopology{N}
26+ StructuredGrid {M,C,N,X} (XYZ, topology) where {M<: Manifold ,C<: CRS ,N,X<: NTuple{N,AbstractArray} } = new (XYZ, topology)
27+ end
28+
29+ function StructuredGrid {M,C} (XYZ:: NTuple{N,AbstractArray} , topology:: GridTopology{N} ) where {M<: Manifold ,C<: CRS ,N}
30+ if M < : 🌐 && ! (C <: LatLon )
31+ throw (ArgumentError (" rectilinear grid on `🌐` requires `LatLon` coordinates" ))
32+ end
33+
34+ T = CoordRefSystems. mactype (C)
35+ nc = CoordRefSystems. ncoords (C)
36+ us = CoordRefSystems. units (C)
37+
38+ if N ≠ nc
39+ throw (ArgumentError ("""
40+ A $N -dimensional structured grid requires a CRS with $N coordinates.
41+ The provided CRS has $nc coordinates.
42+ """ ))
3143 end
44+
45+ XYZ′ = ntuple (i -> numconvert .(T, withunit .(XYZ[i], us[i])), nc)
46+ StructuredGrid {M,C,N,typeof(XYZ′)} (XYZ′, topology)
3247end
3348
34- StructuredGrid {Datum} (XYZ:: NTuple{Dim,<:AbstractArray} , topology:: GridTopology{Dim} ) where {Datum,Dim} =
35- StructuredGrid {Datum} (addunit .(XYZ, u " m" ), topology)
49+ function StructuredGrid {M,C} (XYZ:: NTuple{N,AbstractArray} ) where {M<: Manifold ,C<: CRS ,N}
50+ if ! allequal (size (X) for X in XYZ)
51+ throw (ArgumentError (" all coordinate arrays must be the same size" ))
52+ end
53+
54+ nd = ndims (first (XYZ))
3655
37- function StructuredGrid {Datum} (XYZ:: Tuple ) where {Datum}
38- coords = promote (XYZ... )
39- topology = GridTopology (size (first (coords)) .- 1 )
40- StructuredGrid {Datum} (coords, topology)
56+ if nd ≠ N
57+ throw (ArgumentError ("""
58+ A $N -dimensional structured grid requires coordinate arrays with $N dimensions.
59+ The provided coordinate arrays have $nd dimensions.
60+ """ ))
61+ end
62+
63+ topology = GridTopology (size (first (XYZ)) .- 1 )
64+ StructuredGrid {M,C} (XYZ, topology)
4165end
4266
43- StructuredGrid {Datum } (XYZ... ) where {Datum } = StructuredGrid {Datum } (XYZ)
67+ StructuredGrid {M,C } (XYZ:: AbstractArray ... ) where {M <: Manifold ,C <: CRS } = StructuredGrid {M,C } (XYZ)
4468
45- StructuredGrid (args... ) = StructuredGrid {NoDatum} (args... )
69+ function StructuredGrid (XYZ:: NTuple{N,AbstractArray} ) where {N}
70+ L = promote_type (ntuple (i -> aslentype (eltype (XYZ[i])), N)... )
71+ M = 𝔼{N}
72+ C = Cartesian{NoDatum,N,L}
73+ StructuredGrid {M,C} (XYZ)
74+ end
4675
47- vertex (g:: StructuredGrid{Datum} , ijk:: Dims ) where {Datum} =
48- Point (Cartesian {Datum} (ntuple (d -> g. XYZ[d][ijk... ], embeddim (g))))
76+ StructuredGrid (XYZ:: AbstractArray... ) = StructuredGrid (XYZ)
77+
78+ function vertex (g:: StructuredGrid , ijk:: Dims )
79+ ctor = CoordRefSystems. constructor (crs (g))
80+ Point (ctor (ntuple (d -> g. XYZ[d][ijk... ], paramdim (g))... ))
81+ end
4982
5083XYZ (g:: StructuredGrid ) = g. XYZ
5184
52- function Base. getindex (g:: StructuredGrid{Datum} , I:: CartesianIndices ) where {Datum}
53- @boundscheck _checkbounds (g, I)
54- dims = size (I)
55- cinds = first (I): CartesianIndex (Tuple (last (I)) .+ 1 )
56- XYZ = ntuple (i -> g. XYZ[i][cinds], embeddim (g))
57- StructuredGrid {Datum} (XYZ, GridTopology (dims))
85+ @generated function Base. getindex (g:: StructuredGrid{M,C,N} , I:: CartesianIndices ) where {M,C,N}
86+ exprs = ntuple (i -> :(g. XYZ[$ i][cinds]), N)
87+
88+ quote
89+ @boundscheck _checkbounds (g, I)
90+ dims = size (I)
91+ cinds = first (I): CartesianIndex (Tuple (last (I)) .+ 1 )
92+ XYZ = ($ (exprs... ),)
93+ StructuredGrid {M,C} (XYZ, GridTopology (dims))
94+ end
5895end
5996
6097function Base. summary (io:: IO , g:: StructuredGrid )
0 commit comments