Skip to content

Commit

Permalink
Added annualmin, annualmax and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Balinus committed Dec 20, 2016
1 parent e6ccf15 commit 9e638ac
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/ClimateTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ include("functions.jl")
include("indices.jl")

# Exported functions
export windnr, leftorright, inpoly, meshgrid, boxcar3, prcp1, frostdays, summerdays, icingdays, tropicalnights, customthresover, customthresunder
export windnr, leftorright, inpoly, meshgrid, boxcar3, prcp1, frostdays, summerdays, icingdays, tropicalnights, customthresover, customthresunder, annualmax, annualmin

end #module
120 changes: 103 additions & 17 deletions src/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function prcp1(data::Array{Float64, 1}, timeV::StepRange{Date, Base.Dates.Day})

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t >= 1, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t >= 1, +, view(FD, i:i), view(data, idx, :, :))
end
return FD
end
Expand All @@ -23,7 +23,7 @@ function prcp1(data::Array{Float64, 2}, timeV::StepRange{Date, Base.Dates.Day})

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t >= 1, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t >= 1, +, view(FD, i:i, :), view(data, idx, :, :))
end
return FD
end
Expand Down Expand Up @@ -56,7 +56,7 @@ function frostdays(data::Array{Float64, 1}, timeV::StepRange{Date, Base.Dates.Da

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t < 0, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t < 0, +, view(FD, i:i), view(data, idx, :, :))
end
return FD
end
Expand All @@ -68,7 +68,7 @@ function frostdays(data::Array{Float64, 2}, timeV::StepRange{Date, Base.Dates.Da

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t < 0, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t < 0, +, view(FD, i:i, :), view(data, idx, :, :))
end
return FD
end
Expand All @@ -86,7 +86,7 @@ function frostdays(data::Array{Float64, 3}, timeV::StepRange{Date, Base.Dates.Da
end

"
summerdays(TX::Array, time::StepRange{Date,Base.Dates.Day})
summerdays(data::Array, time::StepRange{Date,Base.Dates.Day})
SD, Number of summer days: Annual count of days when TX (daily maximum temperature) > 25 degree Celsius.
Expand All @@ -101,7 +101,7 @@ function summerdays(data::Array{Float64, 1}, timeV::StepRange{Date, Base.Dates.D

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t >= 25, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t >= 25, +, view(FD, i:i), view(data, idx, :, :))
end
return FD
end
Expand All @@ -113,7 +113,7 @@ function summerdays(data::Array{Float64, 2}, timeV::StepRange{Date, Base.Dates.D

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t >= 25, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t >= 25, +, view(FD, i:i, :), view(data, idx, :, :))
end
return FD
end
Expand All @@ -131,7 +131,7 @@ function summerdays(data::Array{Float64, 3}, timeV::StepRange{Date, Base.Dates.D
end

"
icingdays(TX::Array, time::StepRange{Date,Base.Dates.Day})
icingdays(data::Array, time::StepRange{Date,Base.Dates.Day})
ID, Number of summer days: Annual count of days when TX (daily maximum temperature) < 0 degree Celsius.
Expand All @@ -152,7 +152,7 @@ function icingdays(data::Array{Float64, 3}, timeV::StepRange{Date, Base.Dates.Da
end

"
tropicalnights(TN::Array, time::StepRange{Date,Base.Dates.Day})
tropicalnights(data::Array, time::StepRange{Date,Base.Dates.Day})
TropicalNights, Number of tropical nights: Annual count of days when TN (daily maximum temperature) > 20 degree Celsius.
Expand All @@ -167,7 +167,7 @@ function tropicalnights(data::Array{Float64, 1}, timeV::StepRange{Date, Base.Dat

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t >= 20, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t >= 20, +, view(FD, i:i), view(data, idx, :, :))
end
return FD
end
Expand All @@ -179,7 +179,7 @@ function tropicalnights(data::Array{Float64, 2}, timeV::StepRange{Date, Base.Dat

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t >= 20, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t >= 20, +, view(FD, i:i, :), view(data, idx, :, :))
end
return FD
end
Expand All @@ -197,7 +197,7 @@ function tropicalnights(data::Array{Float64, 3}, timeV::StepRange{Date, Base.Dat
end

"
customthresover(TN::Array, time::StepRange{Date,Base.Dates.Day}, thres)
customthresover(data::Array, time::StepRange{Date,Base.Dates.Day}, thres)
customthresover, annual number of days over a specified threshold.
Expand All @@ -212,7 +212,7 @@ function customthresover(data::Array{Float64, 1}, timeV::StepRange{Date, Base.Da

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t > thres, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t > thres, +, view(FD, i:i), view(data, idx, :, :))
end
return FD
end
Expand All @@ -224,7 +224,7 @@ function customthresover(data::Array{Float64, 2}, timeV::StepRange{Date, Base.Da

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t > thres, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t > thres, +, view(FD, i:i, :), view(data, idx, :, :))
end
return FD
end
Expand All @@ -242,7 +242,7 @@ function customthresover(data::Array{Float64, 3}, timeV::StepRange{Date, Base.Da
end

"
customthresunder(TN::Array, time::StepRange{Date,Base.Dates.Day}, thres)
customthresunder(data::Array, time::StepRange{Date,Base.Dates.Day}, thres)
customthresover, annual number of days under a specified threshold.
Expand All @@ -257,7 +257,7 @@ function customthresunder(data::Array{Float64, 1}, timeV::StepRange{Date, Base.D

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t < thres, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t < thres, +, view(FD, i:i), view(data, idx, :, :))
end
return FD
end
Expand All @@ -269,7 +269,7 @@ function customthresunder(data::Array{Float64, 2}, timeV::StepRange{Date, Base.D

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.mapreducedim!(t -> t < thres, +, view(FD, i:i, :, :), view(data, idx, :, :))
Base.mapreducedim!(t -> t < thres, +, view(FD, i:i, :), view(data, idx, :, :))
end
return FD
end
Expand All @@ -285,3 +285,89 @@ function customthresunder(data::Array{Float64, 3}, timeV::StepRange{Date, Base.D
end
return FD
end

"
annualmax(data::Array, time::StepRange{Date,Base.Dates.Day})
AM, Value of annual maximum of array data.
Let data(i,j) be daily time serie on day i in year j. Extract the highest value for year j."

function annualmax(data::Array{Float64, 1}, timeV::StepRange{Date, Base.Dates.Day})
years = Dates.year(timeV)
numYears = unique(years)
FD = zeros(Float64, (length(numYears), size(data, 2)))

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.maximum!(view(FD,i:i), view(data,idx))
end
return FD
end

function annualmax(data::Array{Float64, 2}, timeV::StepRange{Date, Base.Dates.Day})
years = Dates.year(timeV)
numYears = unique(years)
FD = zeros(Float64, (length(numYears), size(data, 2)))

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.maximum!(view(FD,i:i,:), view(data,idx,:))
end
return FD
end

function annualmax(data::Array{Float64, 3}, timeV::StepRange{Date, Base.Dates.Day})
years = Dates.year(timeV)
numYears = unique(years)
FD = zeros(Float64, (length(numYears), size(data, 2), size(data, 3)))

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.maximum!(view(FD,i:i,:,:), view(data,idx,:,:))
end
return FD
end

"
annualmin(data::Array, time::StepRange{Date,Base.Dates.Day})
AM, Value of annual minimum of array data.
Let data(i,j) be daily time serie on day i in year j. Extract the lowest value for year j."

function annualmin(data::Array{Float64, 1}, timeV::StepRange{Date, Base.Dates.Day})
years = Dates.year(timeV)
numYears = unique(years)
FD = zeros(Float64, (length(numYears), size(data, 2)))

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.minimum!(view(FD,i:i), view(data,idx))
end
return FD
end

function annualmin(data::Array{Float64, 2}, timeV::StepRange{Date, Base.Dates.Day})
years = Dates.year(timeV)
numYears = unique(years)
FD = zeros(Float64, (length(numYears), size(data, 2)))

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.minimum!(view(FD,i:i,:), view(data,idx,:))
end
return FD
end

function annualmin(data::Array{Float64, 3}, timeV::StepRange{Date, Base.Dates.Day})
years = Dates.year(timeV)
numYears = unique(years)
FD = zeros(Float64, (length(numYears), size(data, 2), size(data, 3)))

Threads.@threads for i in 1:length(numYears)
idx = searchsortedfirst(years, numYears[i]):searchsortedlast(years, numYears[i])
Base.minimum!(view(FD,i:i,:,:), view(data,idx,:,:))
end
return FD
end
38 changes: 37 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,43 @@ data[:, 1, 1] = collect(-800.:1025.); data[:, 1, 2] = collect(-800.:1025.);data[
Results = Array{Int64, 3}(5, 2, 2); Results[:, 1, 1] = [365, 366, 269, 0, 0]''; Results[:, 1, 2] = [365, 366, 269, 0, 0]''; Results[:, 2, 1] = [365, 366, 269, 0, 0]''; Results[:, 2, 2] = [365, 366, 269, 0, 0]'';
@test customthresunder(data, d, 200) == Results

## inpoly
# ANNUAL MAXIMUM
d = Date(2003,1,1):Date(2007,12,31)
data = collect(-800.:1025.)
@test annualmax(data, d) == [-436., -70., 295., 660., 1025.]''

data = hcat(collect(-800.:1025.), collect(-800.:1025.))
@test annualmax(data, d) == hcat([-436., -70., 295., 660., 1025.],[-436., -70., 295., 660., 1025.])

data = Array{Float64, 3}(1826, 2, 2)
data[:, 1, 1] = collect(-800.:1025.); data[:, 1, 2] = collect(-800.:1025.);data[:, 2, 1] = collect(-800.:1025.);data[:, 2, 2] = collect(-800.:1025.);
Results = Array{Int64, 3}(5, 2, 2); Results[:, 1, 1] = [-436., -70., 295., 660., 1025.]''; Results[:, 1, 2] = [-436., -70., 295., 660., 1025.]''; Results[:, 2, 1] = [-436., -70., 295., 660., 1025.]''; Results[:, 2, 2] = [-436., -70., 295., 660., 1025.]'';
@test annualmax(data, d) == Results

# ANNUAL MINIMUM
d = Date(2003,1,1):Date(2007,12,31)
data = collect(-800.:1025.)
@test annualmin(data, d) == [-800., -435., -69., 296., 661.]''

data = hcat(collect(-800.:1025.), collect(-800.:1025.))
@test annualmin(data, d) == hcat([-800., -435., -69., 296., 661.],[-800., -435., -69., 296., 661.])

data = Array{Float64, 3}(1826, 2, 2)
data[:, 1, 1] = collect(-800.:1025.); data[:, 1, 2] = collect(-800.:1025.);data[:, 2, 1] = collect(-800.:1025.);data[:, 2, 2] = collect(-800.:1025.);
Results = Array{Int64, 3}(5, 2, 2); Results[:, 1, 1] = [-800., -435., -69., 296., 661.]''; Results[:, 1, 2] = [-800., -435., -69., 296., 661.]''; Results[:, 2, 1] = [-800., -435., -69., 296., 661.]''; Results[:, 2, 2] = [-800., -435., -69., 296., 661.]'';
@test annualmin(data, d) == Results

# MESHGRID
YV = [1 2 3]'
XV = [1 2 3]'
@test meshgrid(XV, YV) == ([1 2 3; 1 2 3; 1 2 3], [1 1 1; 2 2 2; 3 3 3])

# BOXCAR3
A = [-0.0363553 -0.835458 -2.64605; 1.08281 -0.598676 0.312604; 3.34949 -1.73469 -0.100068; -0.49692 -0.0426256 0.4841; 0.183116 0.671138 -0.280205; -0.637663 -0.961894 -1.30388; 0.574248 -0.0970076 -1.45012; 0.880546 2.71189 2.34863; 1.82655 -0.227715 -1.7394; -0.654326 2.12978 0.973546]
Results =[-0.0969 -0.4535 -0.9419; 0.2045 -0.134 -0.9337; 0.2599 0.2507 -0.2799; 0.3216 0.2259 -0.1671; -0.2141 -0.265 -0.2389; -0.0447 -0.3669 -0.5703; 0.4117 0.2294 0.2079; 0.9448 0.5364 0.2577; 1.1111 0.9166 1.0328; 0.7686 0.3847 0.2841]
@test round(boxcar3(A), 4) == Results

## INPOLY
@test leftorright(0.5,0.5, 1,0,1,1) == -1
@test leftorright(1.5,.5, 1,0,1,1) == 1
@test leftorright(1,0.5, 1,0,1,1) == 0
Expand Down

0 comments on commit 9e638ac

Please sign in to comment.