Skip to content

Commit

Permalink
allow further accessor keywords in sametimespan
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Feb 28, 2022
1 parent 305b662 commit 36b3834
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ClimateBase"
uuid = "35604d93-0fb8-4872-9436-495b01d137e2"
authors = ["Datseris <[email protected]>", "Philippe Roy <[email protected]>"]
version = "0.15.0"
version = "0.15.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
22 changes: 14 additions & 8 deletions src/physical_dimensions/temporal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,32 @@ realtime_milliseconds(A) = realtime_milliseconds(dims(A, Ti).val)


"""
sametimespan(Xs) → Ys
sametimespan(Xs; mintime, maxtime) → Ys
Given a container of `ClimArray`s, return the same `ClimArray`s but now accessed in the
`Time` dimension so that they all have span the same time interval.
`Time` dimension so that they all span the same time interval.
Also works for dictionaries with values `ClimArray`s.
Optionally you can provide `Date` or `DatTime` values for the keywords `mintime, maxtime`
that can further limit the minimum/maximum time span accessed.
`sametimespan` takes into consideration the temporal sampling of the arrays for
better accuracy.
"""
function sametimespan(Xs)
mint, maxt = findsametimespan(Xs)
function sametimespan(Xs; kwargs...)
mint, maxt = findsametimespan(Xs; kwargs...)
map(X -> X[Time(mint..maxt)], Xs)
end
function sametimespan(Xs::AbstractDict)
mint, maxt = findsametimespan(values(Xs))
function sametimespan(Xs::AbstractDict; kwargs...)
mint, maxt = findsametimespan(values(Xs); kwargs...)
return Dict(k => X[Time(mint..maxt)] for (k, X) in Xs)
end
end

function findsametimespan(Xs)
function findsametimespan(Xs; maxtime = nothing, mintime = nothing)
mint = maximum(minimum(dims(X, Time).val) for X in Xs)
maxt = minimum(maximum(dims(X, Time).val) for X in Xs)
mint = isnothing(mintime) ? mint : max(mint, mintime)
maxt = isnothing(maxtime) ? maxt : min(maxt, maxtime)

# Make an intelligent decision for monthly/yearly sampled data
tsamps = temporal_sampling.(Xs)
if all(isequal(:monthly), tsamps)
Expand Down
5 changes: 4 additions & 1 deletion test/temporal_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
tm2 = Date(2001, 1, 1):Month(1):Date(2011, 1, 1)
A1 = ClimArray(rand(length(tm1)), (Time(tm1),))
A2 = ClimArray(rand(length(tm2)), (Time(tm2),))
Bv = sametimespan(A1, A2)
Bv = sametimespan((A1, A2))
# dict version
d = Dict(:A1 => A1, :A2 => A2)
d2 = sametimespan(d)
Expand All @@ -66,6 +66,9 @@ end
@test dims(B1, Time)[end] == Date(2010, 3, 15)
@test dims(B2, Time)[end] == Date(2010, 3, 1)
end
B1, B2 = sametimespan(A1, A2; mintime = Date(2005,1,1), maxtime = Date(2008,12,30))
@test year(gnv(dims(B1, Time))[1]) == year(gnv(dims(B2, Time))[1]) == 2005
@test year(gnv(dims(B1, Time))[end]) == year(gnv(dims(B2, Time))[end]) == 2008
end

@testset "monthlyagg and co." begin
Expand Down

0 comments on commit 36b3834

Please sign in to comment.