1-
2-
31abstract type ArakawaGridCell end
42
53struct AGridCell <: ArakawaGridCell
@@ -70,8 +68,8 @@ function getarakawagrid(u_lon, u_lat, v_lon, v_lat, gridmetrics)
7068
7169 cell = (; C, SW, SE, NE, NW, S, N, W, E)
7270
73- u_distances = (; (k => haversine(P, u_point) for (k,P) in pairs(cell)). .. )
74- v_distances = (; (k => haversine(P, v_point) for (k,P) in pairs(cell)). .. )
71+ u_distances = (; (k => haversine(P, u_point) for (k, P) in pairs(cell)). .. )
72+ v_distances = (; (k => haversine(P, v_point) for (k, P) in pairs(cell)). .. )
7573
7674 u_distance, u_pos = findmin(u_distances)
7775 v_distance, v_pos = findmin(v_distances)
@@ -126,8 +124,8 @@ function interpolateontodefaultCgrid(u, u_lon, u_lat, v, v_lon, v_lat, gridmetri
126124 # so that's what we do here
127125 u2 = replace(u, _FillValue => 0.0 )
128126 v2 = replace(v, _FillValue => 0.0 )
129- u2 = 0.5 (u2 + [fill(0.0 , nx, 1 , nz);; u2[:, 1 : end - 1 , :]])
130- v2 = 0.5 (v2 + [fill(0.0 , 1 , ny, nz); v2[1 : end - 1 , :, :]])
127+ u2 = 0.5 (u2 + [fill(0.0 , nx, 1 , nz);; u2[:, 1 : ( end - 1 ) , :]])
128+ v2 = 0.5 (v2 + [fill(0.0 , 1 , ny, nz); v2[1 : ( end - 1 ) , :, :]])
131129 SE_points = [(lon, lat) for (lon, lat) in zip(lon_vertices[2 , :, :], lat_vertices[2 , :, :])]
132130 NE_points = [(lon, lat) for (lon, lat) in zip(lon_vertices[3 , :, :], lat_vertices[3 , :, :])]
133131 NW_points = [(lon, lat) for (lon, lat) in zip(lon_vertices[4 , :, :], lat_vertices[4 , :, :])]
@@ -142,9 +140,6 @@ function interpolateontodefaultCgrid(u, u_lon, u_lat, v, v_lon, v_lat, gridmetri
142140end
143141
144142
145-
146-
147-
148143"""
149144 vertexpermutation(lon_vertices, lat_vertices)
150145
@@ -167,8 +162,8 @@ function vertexpermutation(lon_vertices, lat_vertices)
167162 i = j = 1
168163 # Turn the vertices into points
169164 points = collect(zip(lon_vertices[:, i, j], lat_vertices[:, i, j]))
170- points_east = collect(zip(lon_vertices[:, i+ 1 , j], lat_vertices[:, i+ 1 , j]))
171- points_north = collect(zip(lon_vertices[:, i, j+ 1 ], lat_vertices[:, i, j+ 1 ]))
165+ points_east = collect(zip(lon_vertices[:, i + 1 , j], lat_vertices[:, i + 1 , j]))
166+ points_north = collect(zip(lon_vertices[:, i, j + 1 ], lat_vertices[:, i, j + 1 ]))
172167 # Find the common points
173168 common_east = Set(points) ∩ Set(points_east)
174169 common_noth = Set(points) ∩ Set(points_north)
@@ -183,8 +178,6 @@ function vertexpermutation(lon_vertices, lat_vertices)
183178end
184179
185180
186-
187-
188181# distances
189182function horizontaldistance(lon, lat, I, J)
190183 I = horizontalindex(I)
@@ -207,42 +200,40 @@ verticalindex(I::CartesianIndex{1}) = I
207200verticalindex(I:: CartesianIndex{3} ) = CartesianIndex(I. I[3 ])
208201
209202
210-
211-
212203# The default orientation is the following:
213204#
214205# 4 ────┐ 3
215206# │
216207# 1 ────┘ 2
217208#
218209function vertexindices(dir)
219- dir == :south ? (1 , 2 ) :
220- dir == :east ? (2 , 3 ) :
221- dir == :north ? (3 , 4 ) :
222- dir == :west ? (1 , 4 ) :
223- error()
210+ return dir == :south ? (1 , 2 ) :
211+ dir == :east ? (2 , 3 ) :
212+ dir == :north ? (3 , 4 ) :
213+ dir == :west ? (1 , 4 ) :
214+ error()
224215end
225- vertexpoint(vlon, vlat, i, j, vertexidx) = (vlon[vertexidx,i, j], vlat[vertexidx,i, j])
216+ vertexpoint(vlon, vlat, i, j, vertexidx) = (vlon[vertexidx, i, j], vlat[vertexidx, i, j])
226217function verticalfacewidth(vlon, vlat, i, j, dir)
227218 a, b = vertexindices(dir)
228219 A = vertexpoint(vlon, vlat, i, j, a)
229220 B = vertexpoint(vlon, vlat, i, j, b)
230- haversine(A, B)
221+ return haversine(A, B)
231222end
232223verticalfacewidth(edge_length_2D, i, j, dir) = edge_length_2D[dir][i, j]
233224
234225function verticalfacearea(vlon, vlat, lev_bnds_or_thkcello, i, j, k, dir)
235226 height = cellthickness(lev_bnds_or_thkcello, i, j, k)
236227 width = verticalfacewidth(vlon, vlat, i, j, dir)
237- height * width
228+ return height * width
238229end
239230function verticalfacearea(edge_length_2D, lev_bnds_or_thkcello, i, j, k, dir)
240231 height = cellthickness(lev_bnds_or_thkcello, i, j, k)
241232 width = verticalfacewidth(edge_length_2D, i, j, dir)
242- height * width
233+ return height * width
243234end
244235
245- cellthickness(lev_bnds:: Matrix , i, j, k) = abs(lev_bnds[2 ,k] - lev_bnds[1 ,k])
236+ cellthickness(lev_bnds:: Matrix , i, j, k) = abs(lev_bnds[2 , k] - lev_bnds[1 , k])
246237cellthickness(thkcello, i, j, k) = thkcello[i, j, k]
247238
248239
@@ -252,11 +243,11 @@ function centroid2edgedistance(lon, lat, vlon, vlat, i, j, dir)
252243 A = vertexpoint(vlon, vlat, i, j, a)
253244 B = vertexpoint(vlon, vlat, i, j, b)
254245 M = midpointonsphere(A, B)
255- haversine(C, M)
246+ return haversine(C, M)
256247end
257248
258249function midpointonsphere(A, B)
259- if abs(A[1 ] - B[1 ]) < 180
250+ return if abs(A[1 ] - B[1 ]) < 180
260251 (A .+ B) ./ 2
261252 else # if the edge crosses the longitudinal edge of the map
262253 (A .+ B) ./ 2 .+ (180 , 0 )
@@ -271,8 +262,6 @@ function horizontalcentroiddistance(lon, lat, iA, jA, iB, jB)
271262end
272263
273264
274-
275-
276265function makegridmetrics(; areacello, volcello, lon, lat, lev, lon_vertices, lat_vertices)
277266
278267 # Differences in output "missing" data is commonplace,
@@ -282,19 +271,19 @@ function makegridmetrics(; areacello, volcello, lon, lat, lev, lon_vertices, lat
282271 haskey(volcello. properties, " _FillValue" ) && push!(toreplace, volcello. properties[" _FillValue" ])
283272 replacelist = (x => NaN for x in toreplace)
284273
285- # volume (3D)
274+ # volume (3D)
286275 v3D = volcello |> Array{Union{Missing, Float64}}
287- v3D = replace(v3D, replacelist... )
276+ v3D = replace(v3D, replacelist... )
288277
289278 # area (2D)
290279 area2D = areacello |> Array{Union{Missing, Float64}}
291280 area2D = replace(area2D, replacelist... )
292281
293- # depth and cell height (3D)
294- thkcello = v3D ./ area2D
282+ # depth and cell height (3D)
283+ thkcello = v3D ./ area2D
295284 ZBOT3D = cumsum(thkcello, dims = 3 )
296285 Z3D = ZBOT3D - 0.5 * thkcello
297- zt = lev |> Array
286+ zt = lev |> Array
298287
299288 lat = lat |> Array
300289 lon = lon |> Array
@@ -305,19 +294,18 @@ function makegridmetrics(; areacello, volcello, lon, lat, lev, lon_vertices, lat
305294
306295 # sort the vertices to match the default orientation
307296 vertexidx = vertexpermutation(lon_vertices, lat_vertices)
308- lon_vertices = lon_vertices[vertexidx,:, :]
309- lat_vertices = lat_vertices[vertexidx,:, :]
297+ lon_vertices = lon_vertices[vertexidx, :, :]
298+ lat_vertices = lat_vertices[vertexidx, :, :]
310299
311300 C = CartesianIndices(size(lon))
312301
313302 gridtopology = getgridtopology(lon_vertices, lat_vertices, zt)
314303
315304 dirs = (:south, :east, :north, :west)
316305 𝑗s = (j₋₁, i₊₁, j₊₁, i₋₁)
317- edge_length_2D = Dict(d=> [verticalfacewidth(lon_vertices, lat_vertices, 𝑖. I[1 ], 𝑖. I[2 ], d) for 𝑖 in C] for d in dirs)
318- distance_to_edge_2D = Dict(d=> [centroid2edgedistance(lon, lat, lon_vertices, lat_vertices, 𝑖. I[1 ], 𝑖. I[2 ], d) for 𝑖 in C] for d in dirs)
319- distance_to_neighbour_2D = Dict(d=> [horizontaldistance(lon, lat, 𝑖, 𝑗(𝑖, gridtopology)) for 𝑖 in C] for (d,𝑗) in zip(dirs,𝑗s))
306+ edge_length_2D = Dict(d => [verticalfacewidth(lon_vertices, lat_vertices, 𝑖. I[1 ], 𝑖. I[2 ], d) for 𝑖 in C] for d in dirs)
307+ distance_to_edge_2D = Dict(d => [centroid2edgedistance(lon, lat, lon_vertices, lat_vertices, 𝑖. I[1 ], 𝑖. I[2 ], d) for 𝑖 in C] for d in dirs)
308+ distance_to_neighbour_2D = Dict(d => [horizontaldistance(lon, lat, 𝑖, 𝑗(𝑖, gridtopology)) for 𝑖 in C] for (d, 𝑗) in zip(dirs, 𝑗s))
320309
321- return (; area2D, v3D, thkcello, lon_vertices, lat_vertices, lon, lat, Z3D, zt, edge_length_2D, distance_to_edge_2D, distance_to_neighbour_2D, gridtopology)
310+ return (; area2D, v3D, thkcello, lon_vertices, lat_vertices, lon, lat, Z3D, zt, edge_length_2D, distance_to_edge_2D, distance_to_neighbour_2D, gridtopology)
322311end
323-
0 commit comments