Skip to content

Commit 767ff62

Browse files
committed
apply runic formatting
1 parent 1a5bea7 commit 767ff62

17 files changed

+526
-606
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OceanTransportMatrixBuilder"
22
uuid = "c2b4a04e-6049-4fc4-aa6a-5508a29a1e1c"
3-
version = "0.8.0"
3+
version = "0.8.1"
44
authors = ["Benoit Pasquier <[email protected]> and contributors"]
55

66
[deps]

src/RediGM.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
3-
41
function localdensityslope(ρ, lon, lat, Z, I, gridtopology, dir)
52
distances = verticalfacetriadgroupdistances(lon, lat, Z, I, gridtopology, dir)
63
localρ = verticalfacetriadgroupvalues(ρ, I, gridtopology, dir)
@@ -38,9 +35,6 @@ function globalpotentialdensityslope(gsw_rho, so, ct, gridmetrics, indices, dir)
3835
end
3936

4037

41-
42-
43-
4438
"""
4539
bolus_GM_velocity(σ, gridmetrics; κGM = 600, maxslope = 0.01)
4640
@@ -65,7 +59,7 @@ function bolus_GM_velocity(ρ, gridmetrics, indices; κGM = 600, maxslope = 0.01
6559
Sc = 0.004
6660
Sd = 0.001
6761
# that should not work since Sᵢ and Sⱼ are not colocated
68-
taper = @. 0.5 * (1 + tanh((Sc - sqrt(Sᵢ ^ 2 + Sⱼ ^ 2)) / Sd))
62+
taper = @. 0.5 * (1 + tanh((Sc - sqrt(Sᵢ^2 + Sⱼ^2)) / Sd))
6963
Sᵢ = taper .* Sᵢ
7064
Sⱼ = taper .* Sⱼ
7165

src/classicderivatives.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
####################################
32
# Forward and backward derivatives #
43
####################################
@@ -78,10 +77,6 @@ function ∂ₖ₋(χ, gridmetrics)
7877
end
7978

8079

81-
82-
83-
84-
8580
# TODO: Check that I am properly dealing with unevenly spaced grids
8681
# The commented code below could help with that, as it provides the weights
8782
# for a finite difference stencil for any arrangement of grid points
@@ -100,5 +95,3 @@ end
10095
# A = @. (x' - x₀)^ℓ / factorial(ℓ)
10196
# return A \ (ℓ .== m) # vector of weights w
10297
# end
103-
104-

src/dyads.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
#########
42
# DYADS #
53
#########
@@ -78,4 +76,3 @@ function globalverticaldyadderivative(χ, gridmetrics, indices)
7876
end
7977
return ∂χ
8078
end
81-

src/extratools.jl

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ where the lumping should occur.
3535
Outside of this region, no lumping.
3636
Default is `f=Returns(true)`, i.e. lump everywhere.
3737
"""
38-
function lump_and_spray(wet3D, vol, T, mask=trues(size(wet3D)); di=2, dj=2, dk=1)
38+
function lump_and_spray(wet3D, vol, T, mask = trues(size(wet3D)); di = 2, dj = 2, dk = 1)
3939

4040
# extend the grid to avoid lumping cells outside of bounds
4141
nxyz = size(wet3D)
@@ -53,15 +53,15 @@ function lump_and_spray(wet3D, vol, T, mask=trues(size(wet3D)); di=2, dj=2, dk=1
5353

5454
# loop once over all grid cells and assign lumped indices
5555
c = 2 # index / counter (we start at 2 because 1 is reserved for dry cells)
56-
neighbours = CartesianIndices((0:di-1, 0:dj-1, 0:dk-1))
56+
neighbours = CartesianIndices((0:(di - 1), 0:(dj - 1), 0:(dk - 1)))
5757
for 𝑖 in eachindex(C)
5858
C𝑖 = C[𝑖]
5959
# skip if index already assigned and in mask
6060
#(if assigned but not in mask, must reassign)
6161
LUMPidx[C𝑖] > 0 && mask[C𝑖] && continue
6262
if mask[C𝑖] # if in region, assign all neighbours also in region
6363
# list of neighbours
64-
L𝑖 = L[C𝑖 .+ neighbours][:]
64+
L𝑖 = vec(L[C𝑖 .+ neighbours])
6565
# First, assign dry index to dry cells
6666
localwet = wet3Dext[L𝑖]
6767
dryidx = L𝑖[.!localwet]
@@ -105,23 +105,22 @@ function lump_and_spray(wet3D, vol, T, mask=trues(size(wet3D)); di=2, dj=2, dk=1
105105
nwet = size(LUMP, 2)
106106
nwet_c = size(LUMP, 1)
107107
@info """LUMP and SPRAY:
108-
Matrix size reduction: $(round(100(1 - nwet_c/nwet)))% ($nwet -> $nwet_c)
108+
Matrix size reduction: $(round(100(1 - nwet_c / nwet)))% ($nwet -> $nwet_c)
109109
"""
110110
111111
return LUMP, SPRAY, vol_c
112112
end
113113
114114
115-
116115
"""
117116
as2D(x, wet3D)
118117
119118
`x` must be a vector with `length(x) == sum(wet3D[:,:,1])`
120119
"""
121120
function as2D(x, wet3D)
122121
x2D = fill(NaN, size(wet3D)[1:2])
123-
@views x2D[wet3D[:,:,1]] .= x
124-
x2D
122+
@views x2D[wet3D[:, :, 1]] .= x
123+
return x2D
125124
end
126125
127126
"""
@@ -132,26 +131,5 @@ as3D(x, wet3D)
132131
function as3D(x, wet3D)
133132
x3D = fill(NaN, size(wet3D))
134133
@views x3D[wet3D] .= x
135-
x3D
134+
return x3D
136135
end
137-
138-
139-
140-
141-
142-
143-
144-
145-
146-
147-
148-
149-
150-
151-
152-
153-
154-
155-
156-
157-

src/gridcellgeometry.jl

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
abstract type ArakawaGridCell end
42

53
struct 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
142140
end
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)
183178
end
184179

185180

186-
187-
188181
# distances
189182
function horizontaldistance(lon, lat, I, J)
190183
I = horizontalindex(I)
@@ -207,42 +200,40 @@ verticalindex(I::CartesianIndex{1}) = I
207200
verticalindex(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
#
218209
function 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()
224215
end
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])
226217
function 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)
231222
end
232223
verticalfacewidth(edge_length_2D, i, j, dir) = edge_length_2D[dir][i, j]
233224

234225
function 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
238229
end
239230
function 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
243234
end
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])
246237
cellthickness(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)
256247
end
257248

258249
function 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)
271262
end
272263

273264

274-
275-
276265
function 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)
322311
end
323-

src/gridtopology.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ function getgridtopology(lon_vertices, lat_vertices, lev)
2525
ny = size(lon_vertices, 3)
2626
nz = length(lev)
2727
# North pole vertices
28-
NPlon = @view lon_vertices[3:4,:,end]
29-
NPlat = @view lat_vertices[3:4,:,end]
28+
NPlon = @view lon_vertices[3:4, :, end]
29+
NPlat = @view lat_vertices[3:4, :, end]
3030
# If all the latitudes of the "northmost" vertices are 90, then it's a "regular" grid with 2 poles
3131
if all(NPlat .== 90)
32-
return BipolarGridTopology(nx,ny,nz)
33-
# Otherwise check if the north pole is split in two
32+
return BipolarGridTopology(nx, ny, nz)
33+
# Otherwise check if the north pole is split in two
3434
elseif isapprox(NPlon, rot180(NPlon)) && isapprox(NPlat, rot180(NPlat))
35-
return TripolarGridTopology(nx,ny,nz)
35+
return TripolarGridTopology(nx, ny, nz)
3636
else
37-
return UnknownGridTopology(nx,ny,nz)
37+
return UnknownGridTopology(nx, ny, nz)
3838
end
3939
end
4040

@@ -55,20 +55,19 @@ k₋₁(C, ::AbstractGridTopology) = C.I[3] > 1 ? C + CartesianIndex(0, 0, -1) :
5555
getindexornan(χ, I) = isnothing(I) ? NaN : χ[I] # TODO check that this is going to work
5656

5757

58-
59-
function ishift(C, g::AbstractGridTopology, n=0)
58+
function ishift(C, g::AbstractGridTopology, n = 0)
6059
i, j, k = C.I
61-
CartesianIndex(mod1(i + n, g.nx), j, k)
60+
return CartesianIndex(mod1(i + n, g.nx), j, k)
6261
end
63-
function jshift(C, g::AbstractGridTopology, n=0)
62+
function jshift(C, g::AbstractGridTopology, n = 0)
6463
i, j, k = C.I
6564
j2 = j + n
66-
(1 j2 g.ny) ? CartesianIndex(i, j2, k) : nothing
65+
return (1 j2 g.ny) ? CartesianIndex(i, j2, k) : nothing
6766
end
68-
function kshift(C, g::AbstractGridTopology, n=0)
67+
function kshift(C, g::AbstractGridTopology, n = 0)
6968
i, j, k = C.I
7069
k2 = k + n
71-
(1 k2 g.nz) ? CartesianIndex(i, j, k2) : nothing
70+
return (1 k2 g.nz) ? CartesianIndex(i, j, k2) : nothing
7271
end
7372

7473
# Special behavior for tripolar grids where the seam connects the top and "folds" it around
@@ -81,7 +80,7 @@ end
8180
j₊₁(C, g::TripolarGridTopology) = C.I[2] < g.ny ? C + CartesianIndex(0, 1, 0) : CartesianIndex(g.nx - C.I[1] + 1, g.ny, C.I[3])
8281
j₊₁(C::CartesianIndex{2}, g::TripolarGridTopology) = C.I[2] < g.ny ? C + CartesianIndex(0, 1) : CartesianIndex(g.nx - C.I[1] + 1, g.ny)
8382

84-
function jshift(C, g::TripolarGridTopology, n=0)
83+
function jshift(C, g::TripolarGridTopology, n = 0)
8584
i, j, k = C.I
8685
j2 = j + n
8786
if !(1 j2)

0 commit comments

Comments
 (0)