-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,6 @@ include("lmoments.jl") | |
|
||
include("DIST/DIST.jl") | ||
include("main_spei.jl") | ||
include("ZSI.jl") | ||
|
||
end # module SPEI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Ipaper | ||
using Dates | ||
using NaNStatistics | ||
|
||
function get_dn(date; delta=8) | ||
days = Dates.dayofyear(date) | ||
cld(days, delta) # int | ||
end | ||
|
||
|
||
""" | ||
drought_ZSI(A::AbstractArray{T,3}, dates; | ||
ref=(2000, 2020), | ||
fun_date=get_dn, delta::Int=8, | ||
progress=true, parallel=true) where {T<:Real} | ||
Calculate the ZSI (Z-Score Index) of the input data `A` along the time dimension. | ||
> Extremely fast! | ||
""" | ||
function drought_ZSI(A::AbstractArray{T,3}, dates; | ||
ref=(2000, 2020), | ||
fun_date=get_dn, delta::Int=8, | ||
progress=true, parallel=true) where {T<:Real} | ||
|
||
by = fun_date.(dates; delta) | ||
grps = unique_sort(by) | ||
|
||
nlon, nlat, ntime = size(A) | ||
R = zeros(T, nlon, nlat, ntime) | ||
|
||
p = Progress(length(grps)) | ||
@inbounds @par parallel for grp in grps | ||
progress && next!(p) | ||
|
||
I = findall(by .== grp) | ||
I_ref = I[findall(ref[1] .<= year.(dates[I]) .<= ref[2])] | ||
|
||
for j = 1:nlat, i = 1:nlon | ||
_x_ref = @view A[i, j, I_ref] # 两次嵌套会导致速度变慢 | ||
μ = nanmean(_x_ref) | ||
sd = nanstd(_x_ref) | ||
|
||
for _i in I | ||
R[i, j, _i] = (A[i, j, _i] - μ) / sd | ||
end | ||
end | ||
end | ||
R | ||
end | ||
|
||
|
||
export get_dn, drought_ZSI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Statistics, Test | ||
|
||
@testset "drought_ZSI" begin | ||
dates = Date(2000):Day(8):Date(2010, 12, 31) |> collect | ||
A = rand(Float32, 100, 100, length(dates)) | ||
ref = (2001, 2005) | ||
@time R = drought_ZSI(A, dates; ref) | ||
|
||
by = get_dn.(dates; delta=8) | ||
grps = unique_sort(by) | ||
|
||
grp = grps[1] | ||
I = findall(by .== grp) | ||
I_ref = I[findall(ref[1] .<= year.(dates[I]) .<= ref[2])] | ||
|
||
x = A[1, 1, I] | ||
x_ref = A[1, 1, I_ref] | ||
μ = mean(x_ref) | ||
sd = std(x_ref) | ||
@test R[1, 1, I] ≈ (x .- μ) ./ sd | ||
end |