Skip to content

Commit 707c65d

Browse files
committed
fix Manifest issue, add function for center of Climatology latitude (probably only useful for consolidated continents)
1 parent 31e2381 commit 707c65d

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

Project.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
name = "GEOCLIM"
22
uuid = "3dbb98da-0543-4a00-be75-dcc1e89fb417"
33
authors = ["Mark Baum <markmbaum@protonmail.com>", "Minmin Fu <mjfu@g.harvard.edu>"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
BasicInterpolators = "26cce99e-4866-4b6d-ab74-862489e035e0"
88
MultiAssign = "5e2c8b86-4afa-40de-bd09-d9d381e5dc2b"
9-
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
109
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
1110
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
1211
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
1312

1413
[compat]
1514
BasicInterpolators = "0.6"
1615
MultiAssign = "0.1"
17-
NLsolve = "4"
1816
NetCDF = "0.11"
1917
Roots = "1"
2018
UnPack = "1"
@@ -24,4 +22,4 @@ julia = "1.3"
2422
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2523

2624
[targets]
27-
test = ["Test"]
25+
test = ["Test"]

src/Climatology.jl

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
export Climatology
22

3+
checktranspose(A::Matrix)::Matrix = (size(A,1) > size(A,2)) ? collect(transpose(A)) : A
4+
35
function readgrid(fn, v)::Matrix
46
#read temperature file
57
X = ncread(fn, v)
68
#squash third dimension if necessary
7-
if ndims(X) > 2
8-
X = dropdims(X, dims=3)
9+
d = ndims(X)
10+
if d == 3
11+
return checktranspose(dropdims(X, dims=3))
12+
elseif d == 2
13+
return checktranspose(X)
914
end
10-
size(X,1) > size(X,2) ? collect(transpose(X)) : X
15+
error("unusual number of dimensions ($d) found in file $fn, variable $v")
1116
end
1217

1318
struct Climatology{𝒯}
@@ -73,14 +78,15 @@ function Climatology(fnr::String, #runoff file name
7378
#discard nonsense
7479
@. f[(f < 0) | (f > 1)] = 0
7580

76-
#demand everything is the same size
77-
@assert size(r) == size(T) == size(f)
78-
n, m = size(r)
79-
8081
#insist on the same types for grids
8182
𝒯 = promote_type(eltype.((r, T, f))...)
8283
r, T, f = convert.(Matrix{𝒯}, (r, T, f))
8384

85+
#demand everything is the same size
86+
@assert size(r) == size(T) == size(f) "Climatology arrays must have the same size/shape"
87+
n, m = size(r)
88+
@assert length(lat) == n "latitude vector length ($(length(lat))) must match number of Climatology rows ($n)"
89+
8490
#make a mask from the non-NaN runoff values
8591
mask = @. r |> isnan |> !
8692

@@ -118,49 +124,51 @@ end
118124

119125
#--------------------------------------
120126

121-
export landfraction, meanlandtemperature, meanlandrunoff, totallandrunoff
127+
export landfraction
128+
export meanlandtemperature
129+
export meanlandrunoff, totallandrunoff
130+
export meanlandlatitude
122131

123-
#already exported in main file
124-
#landfraction(𝒸::Climatology) = sum(𝒸.f .* 𝒸.A)/sum(𝒸.A)
132+
checkcut(cut) = @assert cut >= 0 "latitude cutoff must be positive"
133+
134+
checksize(n, m, X) = @assert size(X) == (n,m) "size mismatch between array and Climatology"
125135

126136
function landfraction(𝒸::Climatology{𝒯}; cut::Real=Inf) where {𝒯}
127137
@unpack A, f, lat, n, m = 𝒸
128-
@assert cut >= 0
129-
L = zero(𝒯)
130-
S = zero(𝒯)
138+
checkcut(cut)
139+
@multiassign num, den = zero(𝒯)
131140
@inbounds for i 1:n, j 1:m
132141
if -cut <= lat[i] <= cut
133-
L += A[i,j]*f[i,j]
134-
S += A[i,j]
142+
num += A[i,j]*f[i,j]
143+
den += A[i,j]
135144
end
136145
end
137-
return L/S
146+
return num/den
138147
end
139148

140149
function landmean(X::AbstractMatrix{𝒯}, 𝒸::Climatology{𝒯}, cut::Real=Inf) where {𝒯}
141150
@unpack mask, A, f, lat, n, m = 𝒸
142-
@assert size(X) == (n,m)
143-
@assert cut > 0
144-
s = zero(𝒯)
145-
a = zero(𝒯)
146-
@inbounds for i 1:n, j 1:m
151+
checksize(n, m, X)
152+
checkcut(cut)
153+
@multiassign num, den = zero(𝒯)
154+
for i 1:n, j 1:m
147155
if mask[i,j] & (-cut <= lat[i] <= cut)
148156
#land area of cell
149157
LA = A[i,j]*f[i,j]
150158
#contributions to averaging
151-
s += LA*X[i,j]
152-
a += LA
159+
num += LA*X[i,j]
160+
den += LA
153161
end
154162
end
155-
return s/a
163+
return num/den
156164
end
157165

158166
function landsum(X::AbstractMatrix{𝒯}, 𝒸::Climatology{𝒯}, cut::Real=Inf) where {𝒯}
159167
@unpack mask, A, f, lat, n, m = 𝒸
160-
@assert size(X) == (n,m)
161-
@assert cut > 0
168+
checksize(n, m, X)
169+
checkcut(cut)
162170
s = zero(𝒯)
163-
@inbounds for i 1:n, j 1:m
171+
for i 1:n, j 1:m
164172
if mask[i,j] & (-cut <= lat[i] <= cut)
165173
#land area of cell
166174
LA = A[i,j]*f[i,j]
@@ -176,3 +184,5 @@ meanlandtemperature(𝒸::Climatology; cut::Real=Inf) = landmean(𝒸.T, 𝒸, c
176184
meanlandrunoff(𝒸::Climatology; cut::Real=Inf) = landmean(𝒸.r, 𝒸, cut)
177185

178186
totallandrunoff(𝒸::Climatology; cut::Real=Inf) = landsum(𝒸.r, 𝒸, cut)
187+
188+
meanlandlatitude(𝒸::Climatology) = landmean(repeat(𝒸.lat, 1, 𝒸.m), 𝒸)

0 commit comments

Comments
 (0)