66 Slice(x=(xmin, xmax), y=(ymin, ymax), z=(zmin, zmax))
77 Slice(lat=(latmin, latmax), lon=(lonmin, lonmax))
88
9- Retain the grid elements within `x` limits [`xmax`,`xmax`],
9+ Retain the domain elements within `x` limits [`xmax`,`xmax`],
1010`y` limits [`ymax`,`ymax`] and `z` limits [`zmin`,`zmax`]
1111in length units (default to meters), or within `lat` limits
1212[`latmin`,`latmax`] and `lon` limits [`lonmin`,`lonmax`]
@@ -15,11 +15,10 @@ in degree units.
1515## Examples
1616
1717```julia
18- Slice(x=(2, 4))
19- Slice(x=(1u"km", 3u"km"))
20- Slice(y=(1.2, 1.8), z=(2.4, 3.0))
21- Slice(lat=(30, 60))
22- Slice(lon=(45u"°", 90u"°"))
18+ Slice(x=(1000km, 3000km))
19+ Slice(x=(1000km, 2000km), y=(2000km, 5000km))
20+ Slice(lon=(0°, 90°))
21+ Slice(lon=(0°, 45°), lat=(0°, 45°))
2322```
2423"""
2524struct Slice{T} <: GeometricTransform
@@ -30,54 +29,51 @@ Slice(; kwargs...) = Slice(values(kwargs))
3029
3130parameters (t:: Slice ) = (; limits= t. limits)
3231
33- preprocess (t:: Slice , g :: Grid ) = cartesianrange (g, _fixlimits (boundingbox (g ), t. limits))
32+ preprocess (t:: Slice , d :: Domain ) = _sliceinds (d, _slicebox (boundingbox (d ), t. limits))
3433
35- apply (t:: Slice , g :: Grid ) = g[ preprocess (t, g)] , nothing
34+ apply (t:: Slice , d :: Domain ) = _slice (d, preprocess (t, d)) , nothing
3635
3736# -----------------
3837# HELPER FUNCTIONS
3938# -----------------
4039
41- function _fixlimits (box:: Box{<:𝔼} , limits)
42- lims = _xyzlimits (limits)
43- min = convert (Cartesian, coords (minimum (box)))
44- max = convert (Cartesian, coords (maximum (box)))
45- _minmax (min, max, lims)
46- end
47-
48- function _fixlimits (box:: Box{🌐} , limits)
49- lims = _latlonlimits (limits)
50- min = convert (LatLon, coords (minimum (box)))
51- max = convert (LatLon, coords (maximum (box)))
52- _minmax (min, max, lims)
53- end
40+ _slice (d:: Domain , inds) = view (d, inds)
41+ _slice (g:: Grid , inds:: CartesianIndices ) = getindex (g, inds)
5442
55- _xyzlimits (limits) = (
56- x= haskey (limits, :x ) ? _aslen .(limits. x) : nothing ,
57- y= haskey (limits, :y ) ? _aslen .(limits. y) : nothing ,
58- z= haskey (limits, :z ) ? _aslen .(limits. z) : nothing
59- )
43+ _sliceinds (d:: Domain , b) = indices (d, b)
44+ _sliceinds (g:: CartesianGrid , b) = cartesianrange (g, b)
45+ _sliceinds (g:: RectilinearGrid , b) = cartesianrange (g, b)
46+ _sliceinds (g:: Grid{🌐} , b:: Box{🌐} ) = cartesianrange (g, b)
6047
61- _latlonlimits (limits) =
62- (lat= haskey (limits, :lat ) ? _asdeg .(limits. lat) : nothing , lon= haskey (limits, :lon ) ? _asdeg .(limits. lon) : nothing )
63-
64- function _minmax (min:: Cartesian2D , max:: Cartesian2D , lims)
65- xmin, xmax = isnothing (lims. x) ? (min. x, max. x) : lims. x
66- ymin, ymax = isnothing (lims. y) ? (min. y, max. y) : lims. y
67- (xmin, xmax), (ymin, ymax)
48+ function _slicebox (box:: Box{𝔼{2}} , limits)
49+ min = convert (Cartesian, coords (minimum (box)))
50+ max = convert (Cartesian, coords (maximum (box)))
51+ xmin, xmax = get (limits, :x , (min. x, max. x))
52+ ymin, ymax = get (limits, :y , (min. y, max. y))
53+ bmin = _aslen .((xmin, ymin))
54+ bmax = _aslen .((xmax, ymax))
55+ Box (withcrs (box, bmin), withcrs (box, bmax))
6856end
6957
70- function _minmax (min:: Cartesian3D , max:: Cartesian3D , lims)
71- xmin, xmax = isnothing (lims. x) ? (min. x, max. x) : lims. x
72- ymin, ymax = isnothing (lims. y) ? (min. y, max. y) : lims. y
73- zmin, zmax = isnothing (lims. z) ? (min. z, max. z) : lims. z
74- (xmin, xmax), (ymin, ymax), (zmin, zmax)
58+ function _slicebox (box:: Box{𝔼{3}} , limits)
59+ min = convert (Cartesian, coords (minimum (box)))
60+ max = convert (Cartesian, coords (maximum (box)))
61+ xmin, xmax = get (limits, :x , (min. x, max. x))
62+ ymin, ymax = get (limits, :y , (min. y, max. y))
63+ zmin, zmax = get (limits, :z , (min. z, max. z))
64+ bmin = _aslen .((xmin, ymin, zmin))
65+ bmax = _aslen .((xmax, ymax, zmax))
66+ Box (withcrs (box, bmin), withcrs (box, bmax))
7567end
7668
77- function _minmax (min:: LatLon , max:: LatLon , lims)
78- lonmin, lonmax = isnothing (lims. lon) ? (min. lon, max. lon) : lims. lon
79- latmin, latmax = isnothing (lims. lat) ? (min. lat, max. lat) : lims. lat
80- (lonmin, lonmax), (latmin, latmax)
69+ function _slicebox (box:: Box{🌐} , limits)
70+ min = convert (LatLon, coords (minimum (box)))
71+ max = convert (LatLon, coords (maximum (box)))
72+ latmin, latmax = get (limits, :lat , (min. lat, max. lat))
73+ lonmin, lonmax = get (limits, :lon , (min. lon, max. lon))
74+ bmin = _asdeg .((latmin, lonmin))
75+ bmax = _asdeg .((latmax, lonmax))
76+ Box (withcrs (box, bmin, LatLon), withcrs (box, bmax, LatLon))
8177end
8278
8379_aslen (x:: Len ) = float (x)
0 commit comments