From 5185c5d99e4bd562996b670ae9945d33cf971003 Mon Sep 17 00:00:00 2001 From: Datseris Date: Tue, 11 Jun 2024 14:07:03 +0100 Subject: [PATCH 1/3] add dailyagg function --- CHANGELOG.md | 6 ++++++ Project.toml | 2 +- src/exports.jl | 2 +- src/physical_dimensions/temporal.jl | 27 +++++++++++++++++++++++---- test/temporal_tests.jl | 11 +++++++++++ 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c9ef70..336b834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.17.0 + +- New function `dailyagg` +- Many bug fixes in terms of compatibility with other packages +- Usage of Julia Package extensions for GeoMakie integration + # 0.16.3 - New functions `value_space, quantile_space`. - `globalattr` has been renamed to `ncglobalattr`. diff --git a/Project.toml b/Project.toml index 8153b1d..976d1ff 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.17.1" +version = "0.17.2" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/exports.jl b/src/exports.jl index cfd1a70..d37fbfa 100644 --- a/src/exports.jl +++ b/src/exports.jl @@ -7,7 +7,7 @@ # Temporal export monthday_indices, maxyearspan, daymonth, realtime_days, realtime_milliseconds, temporal_sampling, timemean, timeagg, monthlyagg, yearlyagg, temporalranges, seasonalyagg, -season, DAYS_IN_ORBIT, HOURS_IN_ORBIT, seasonality, sametimespan +season, DAYS_IN_ORBIT, HOURS_IN_ORBIT, seasonality, sametimespan, dailyagg # Spatial export transform_to_coord diff --git a/src/physical_dimensions/temporal.jl b/src/physical_dimensions/temporal.jl index b2a70b9..9f8cf89 100644 --- a/src/physical_dimensions/temporal.jl +++ b/src/physical_dimensions/temporal.jl @@ -20,9 +20,6 @@ no_time_datetime(::T) where {T<:TimeType} = T "daymonth(t) = day(t), month(t)" daymonth(t) = day(t), month(t) -maxyearspan(A::AbstractDimArray, tsamp = temporal_sampling(A)) = -maxyearspan(gnv(dims(A, Time)), tsamp) - """ temporal_sampling(x) โ†’ symbol Return the temporal sampling type of `x`, which is either an array of `Date`s or @@ -83,7 +80,8 @@ end """ maxyearspan(A::ClimArray) = maxyearspan(dims(A, Time)) - maxyearspan(t::Vector{<:DateTime}) โ†’ i + maxyearspan(t::AbstractVector{<:DateTime}) โ†’ i + Find the maximum index `i` of `t` so that `t[1:i]` covers exact(*) multiples of years. (*) For monthly spaced data `i` is a multiple of `12` while for daily data we find @@ -126,6 +124,9 @@ function maxyearspan(times, tsamp = temporal_sampling(times)) end end +maxyearspan(A::AbstractDimArray, tsamp = temporal_sampling(A)) = +maxyearspan(gnv(dims(A, Time)), tsamp) + """ monthday_indices(times, date = times[1]) @@ -399,6 +400,22 @@ function monthlyagg(A::ClimArray, f = mean; mday = 15) return timegroup(A, f, t, tranges) end +""" + dailyagg(A::ClimArray, f = mean) -> B +Create a new array where the temporal information has been aggregated into days +using the function `f`. +""" +function dailyagg(A::ClimArray, f = mean) + t0 = gnv(dims(A, Time)) + ts, tf = extrema(t0) + DT = no_time_datetime(ts) + startdate = DT(year(ts), month(ts), day(ts)) + finaldate = DT(year(tf), month(tf), day(tf)) + t = startdate:Day(1):finaldate + tranges = temporalranges(t0, Dates.day) + return timegroup(A, f, t, tranges) +end + """ yearlyagg(A::ClimArray, f = mean) -> B Create a new array where the temporal information has been aggregated into years @@ -414,6 +431,8 @@ function yearlyagg(A::ClimArray, f = mean) return timegroup(A, f, t, tranges) end +# TODO: this function does not respect the order of dimensions. +# it always puts time last function timegroup(A, f, t, tranges) other = otherdims(A, Time) B = ClimArray(zeros(eltype(A), length.(other)..., length(t)), diff --git a/test/temporal_tests.jl b/test/temporal_tests.jl index ab62b87..5c618ed 100644 --- a/test/temporal_tests.jl +++ b/test/temporal_tests.jl @@ -128,6 +128,17 @@ end Asea = seasonalyagg(A) tsea = dims(Asea, Ti).val @test Base.step(tsea) == Month(3) + + @testset "dailyagg" begin + t = DateTime(1,1,1,0):Hour(12):DateTime(1,1,3, 12) + x = [float(isodd(i)) for i in 1:length(t)] + x = hcat([copy(x) for j in 1:4]...) + X = ClimArray(x, (Tim(t), Lon(1:4))) + D = dailyagg(X, mean) + @test all(isequal(0.5), D) + @test step(gnv(dims(D, Tim))) == Day(1) + @test length(dims(D, Tim)) == length(t)รท2 + end end @testset "interannual variability" begin From 41c3b36f8d1967600e655625a3311de37b7eb046 Mon Sep 17 00:00:00 2001 From: Datseris Date: Tue, 11 Jun 2024 14:13:45 +0100 Subject: [PATCH 2/3] add function to docs --- docs/src/statistics.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/src/statistics.md b/docs/src/statistics.md index 22451cf..c3d0e2c 100644 --- a/docs/src/statistics.md +++ b/docs/src/statistics.md @@ -1,5 +1,8 @@ # Statistics +```@index +``` + ## Temporal Functions related with the `Time` dimension. ```@docs @@ -8,6 +11,7 @@ timeagg monthlyagg yearlyagg seasonalyagg +dailyagg temporalranges maxyearspan temporal_sampling From 8a501076d85302fa57d0ee26a6e642c15c92cb83 Mon Sep 17 00:00:00 2001 From: Datseris Date: Tue, 11 Jun 2024 14:14:26 +0100 Subject: [PATCH 3/3] correct Pages reference --- docs/src/statistics.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/statistics.md b/docs/src/statistics.md index c3d0e2c..73d604b 100644 --- a/docs/src/statistics.md +++ b/docs/src/statistics.md @@ -1,6 +1,7 @@ # Statistics ```@index +Pages = ["statistics.md"] ``` ## Temporal