Skip to content

Commit 0dcbb11

Browse files
authored
Fix cartesianrange of Grid (#1044)
* Fix 'cartesianrange' of 'Grid' * Update code * Fix typo * Fix typo * Rename variables
1 parent d3ab52d commit 0dcbb11

File tree

1 file changed

+77
-32
lines changed

1 file changed

+77
-32
lines changed

src/utils/misc.jl

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,37 @@ function cartesianrange(grid::Grid{𝔼{2}}, limits)
170170
b = convert(Cartesian, coords(vertex(grid, (nx, 1))))
171171
c = convert(Cartesian, coords(vertex(grid, (1, ny))))
172172

173-
xmin = max(xₛ, a.x)
174-
ymin = max(yₛ, a.y)
175-
xmax = min(xₑ, b.x)
176-
ymax = min(yₑ, c.y)
173+
swapx = a.x > b.x
174+
swapy = a.y > c.y
177175

178-
iₛ = findlast(1:nx) do i
176+
xinds = swapx ? (nx:-1:1) : (1:1:nx)
177+
yinds = swapy ? (ny:-1:1) : (1:1:ny)
178+
179+
gridxₛ, gridxₑ = swapx ? (b.x, a.x) : (a.x, b.x)
180+
gridyₛ, gridyₑ = swapy ? (c.y, a.y) : (a.y, c.y)
181+
182+
xmin = max(xₛ, gridxₛ)
183+
ymin = max(yₛ, gridyₛ)
184+
xmax = min(xₑ, gridxₑ)
185+
ymax = min(yₑ, gridyₑ)
186+
187+
iₛ = findlast(xinds) do i
179188
p = vertex(grid, (i, 1))
180189
c = convert(Cartesian, coords(p))
181190
c.x xmin
182191
end
183-
iₑ = findfirst(1:nx) do i
192+
iₑ = findfirst(xinds) do i
184193
p = vertex(grid, (i, 1))
185194
c = convert(Cartesian, coords(p))
186195
c.x xmax
187196
end
188-
jₛ = findlast(1:ny) do i
197+
198+
jₛ = findlast(yinds) do i
189199
p = vertex(grid, (1, i))
190200
c = convert(Cartesian, coords(p))
191201
c.y ymin
192202
end
193-
jₑ = findfirst(1:ny) do i
203+
jₑ = findfirst(yinds) do i
194204
p = vertex(grid, (1, i))
195205
c = convert(Cartesian, coords(p))
196206
c.y ymax
@@ -200,7 +210,10 @@ function cartesianrange(grid::Grid{𝔼{2}}, limits)
200210
throw(ArgumentError("the passed limits are not valid for the grid"))
201211
end
202212

203-
CartesianIndex(iₛ, jₛ):CartesianIndex(iₑ - 1, jₑ - 1)
213+
iₛ, iₑ = swapx ? (iₑ, iₛ) : (iₛ, iₑ)
214+
jₛ, jₑ = swapy ? (jₑ, jₛ) : (jₛ, jₑ)
215+
216+
CartesianIndex(xinds[iₛ], yinds[jₛ]):CartesianIndex(xinds[iₑ] - 1, yinds[jₑ] - 1)
204217
end
205218

206219
function cartesianrange(grid::Grid{𝔼{3}}, limits)
@@ -213,39 +226,53 @@ function cartesianrange(grid::Grid{𝔼{3}}, limits)
213226
c = convert(Cartesian, coords(vertex(grid, (1, ny, 1))))
214227
d = convert(Cartesian, coords(vertex(grid, (1, 1, nz))))
215228

216-
xmin = max(xₛ, a.x)
217-
ymin = max(yₛ, a.y)
218-
zmin = max(zₛ, a.z)
219-
xmax = min(xₑ, b.x)
220-
ymax = min(yₑ, c.y)
221-
zmax = min(zₑ, d.z)
229+
swapx = a.x > b.x
230+
swapy = a.y > c.y
231+
swapz = a.z > d.z
222232

223-
iₛ = findlast(1:nx) do i
233+
xinds = swapx ? (nx:-1:1) : (1:1:nx)
234+
yinds = swapy ? (ny:-1:1) : (1:1:ny)
235+
zinds = swapz ? (nz:-1:1) : (1:1:nz)
236+
237+
gridxₛ, gridxₑ = swapx ? (b.x, a.x) : (a.x, b.x)
238+
gridyₛ, gridyₑ = swapy ? (c.y, a.y) : (a.y, c.y)
239+
gridzₛ, gridzₑ = swapz ? (d.z, a.z) : (a.z, d.z)
240+
241+
xmin = max(xₛ, gridxₛ)
242+
ymin = max(yₛ, gridyₛ)
243+
zmin = max(zₛ, gridzₛ)
244+
xmax = min(xₑ, gridxₑ)
245+
ymax = min(yₑ, gridyₑ)
246+
zmax = min(zₑ, gridzₑ)
247+
248+
iₛ = findlast(xinds) do i
224249
p = vertex(grid, (i, 1, 1))
225250
c = convert(Cartesian, coords(p))
226251
c.x xmin
227252
end
228-
iₑ = findfirst(1:nx) do i
253+
iₑ = findfirst(xinds) do i
229254
p = vertex(grid, (i, 1, 1))
230255
c = convert(Cartesian, coords(p))
231256
c.x xmax
232257
end
233-
jₛ = findlast(1:ny) do i
258+
259+
jₛ = findlast(yinds) do i
234260
p = vertex(grid, (1, i, 1))
235261
c = convert(Cartesian, coords(p))
236262
c.y ymin
237263
end
238-
jₑ = findfirst(1:ny) do i
264+
jₑ = findfirst(yinds) do i
239265
p = vertex(grid, (1, i, 1))
240266
c = convert(Cartesian, coords(p))
241267
c.y ymax
242268
end
243-
kₛ = findlast(1:nz) do i
269+
270+
kₛ = findlast(zinds) do i
244271
p = vertex(grid, (1, 1, i))
245272
c = convert(Cartesian, coords(p))
246273
c.z zmin
247274
end
248-
kₑ = findfirst(1:nz) do i
275+
kₑ = findfirst(zinds) do i
249276
p = vertex(grid, (1, 1, i))
250277
c = convert(Cartesian, coords(p))
251278
c.z zmax
@@ -255,38 +282,53 @@ function cartesianrange(grid::Grid{𝔼{3}}, limits)
255282
throw(ArgumentError("the passed limits are not valid for the grid"))
256283
end
257284

258-
CartesianIndex(iₛ, jₛ, kₛ):CartesianIndex(iₑ - 1, jₑ - 1, kₑ - 1)
285+
iₛ, iₑ = swapx ? (iₑ, iₛ) : (iₛ, iₑ)
286+
jₛ, jₑ = swapy ? (jₑ, jₛ) : (jₛ, jₑ)
287+
kₛ, kₑ = swapz ? (kₑ, kₛ) : (kₛ, kₑ)
288+
289+
CartesianIndex(xinds[iₛ], yinds[jₛ], zinds[kₛ]):CartesianIndex(xinds[iₑ] - 1, yinds[jₑ] - 1, zinds[kₑ] - 1)
259290
end
260291

261292
function cartesianrange(grid::Grid{🌐}, limits)
262293
nlon, nlat = vsize(grid)
263-
(llonmin, llonmax), (llatmin, llatmax) = limits
294+
295+
(lonₛ, lonₑ), (latₛ, latₑ) = limits
264296

265297
a = convert(LatLon, coords(vertex(grid, (1, 1))))
266298
b = convert(LatLon, coords(vertex(grid, (nlon, 1))))
267299
c = convert(LatLon, coords(vertex(grid, (1, nlat))))
268300

269-
lonmin = max(llonmin, a.lon)
270-
latmin = max(llatmin, a.lat)
271-
lonmax = min(llonmax, b.lon)
272-
latmax = min(llatmax, c.lat)
301+
swaplon = a.lon > b.lon
302+
swaplat = a.lat > c.lat
303+
304+
loninds = swaplon ? (nlon:-1:1) : (1:1:nlon)
305+
latinds = swaplat ? (nlat:-1:1) : (1:1:nlat)
273306

274-
iₛ = findlast(1:nlon) do i
307+
gridlonₛ, gridlonₑ = swaplon ? (b.lon, a.lon) : (a.lon, b.lon)
308+
gridlatₛ, gridlatₑ = swaplat ? (c.lat, a.lat) : (a.lat, c.lat)
309+
310+
lonmin = max(lonₛ, gridlonₛ)
311+
latmin = max(latₛ, gridlatₛ)
312+
lonmax = min(lonₑ, gridlonₑ)
313+
latmax = min(latₑ, gridlatₑ)
314+
315+
iₛ = findlast(loninds) do i
275316
p = vertex(grid, (i, 1))
276317
c = convert(LatLon, coords(p))
277318
c.lon lonmin
278319
end
279-
iₑ = findfirst(1:nlon) do i
320+
iₑ = findfirst(loninds) do i
280321
p = vertex(grid, (i, 1))
281322
c = convert(LatLon, coords(p))
282323
c.lon lonmax
283324
end
284-
jₛ = findlast(1:nlat) do i
325+
326+
jₛ = findlast(latinds) do i
285327
p = vertex(grid, (1, i))
286328
c = convert(LatLon, coords(p))
287329
c.lat latmin
288330
end
289-
jₑ = findfirst(1:nlat) do i
331+
jₑ = findfirst(latinds) do i
290332
p = vertex(grid, (1, i))
291333
c = convert(LatLon, coords(p))
292334
c.lat latmax
@@ -296,5 +338,8 @@ function cartesianrange(grid::Grid{🌐}, limits)
296338
throw(ArgumentError("the passed limits are not valid for the grid"))
297339
end
298340

299-
CartesianIndex(iₛ, jₛ):CartesianIndex(iₑ - 1, jₑ - 1)
341+
iₛ, iₑ = swaplon ? (iₑ, iₛ) : (iₛ, iₑ)
342+
jₛ, jₑ = swaplat ? (jₑ, jₛ) : (jₛ, jₑ)
343+
344+
CartesianIndex(loninds[iₛ], latinds[jₛ]):CartesianIndex(loninds[iₑ] - 1, latinds[jₑ] - 1)
300345
end

0 commit comments

Comments
 (0)