From 36b3834cf18ec16f7c514c936278bfd0421c7a45 Mon Sep 17 00:00:00 2001 From: Datseris Date: Mon, 28 Feb 2022 13:06:58 +0100 Subject: [PATCH] allow further accessor keywords in `sametimespan` --- Project.toml | 2 +- src/physical_dimensions/temporal.jl | 22 ++++++++++++++-------- test/temporal_tests.jl | 5 ++++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index d1a4bb09..2b47257b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ClimateBase" uuid = "35604d93-0fb8-4872-9436-495b01d137e2" authors = ["Datseris ", "Philippe Roy "] -version = "0.15.0" +version = "0.15.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/physical_dimensions/temporal.jl b/src/physical_dimensions/temporal.jl index 2b5811d9..ab631373 100644 --- a/src/physical_dimensions/temporal.jl +++ b/src/physical_dimensions/temporal.jl @@ -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) diff --git a/test/temporal_tests.jl b/test/temporal_tests.jl index 1d6b022b..f8b20243 100644 --- a/test/temporal_tests.jl +++ b/test/temporal_tests.jl @@ -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) @@ -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