Skip to content

Commit

Permalink
add dailyagg function
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Jun 11, 2024
1 parent 05045b7 commit 5185c5d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
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.17.1"
version = "0.17.2"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
2 changes: 1 addition & 1 deletion src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 23 additions & 4 deletions src/physical_dimensions/temporal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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)),
Expand Down
11 changes: 11 additions & 0 deletions test/temporal_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5185c5d

Please sign in to comment.