Skip to content

Commit 5d7d2ac

Browse files
authored
add dailyagg function (#106)
* add dailyagg function * add function to docs * correct Pages reference
1 parent 05045b7 commit 5d7d2ac

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.17.0
2+
3+
- New function `dailyagg`
4+
- Many bug fixes in terms of compatibility with other packages
5+
- Usage of Julia Package extensions for GeoMakie integration
6+
17
# 0.16.3
28
- New functions `value_space, quantile_space`.
39
- `globalattr` has been renamed to `ncglobalattr`.

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ClimateBase"
22
uuid = "35604d93-0fb8-4872-9436-495b01d137e2"
33
authors = ["Datseris <[email protected]>", "Philippe Roy <[email protected]>"]
4-
version = "0.17.1"
4+
version = "0.17.2"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

docs/src/statistics.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Statistics
22

3+
```@index
4+
Pages = ["statistics.md"]
5+
```
6+
37
## Temporal
48
Functions related with the `Time` dimension.
59
```@docs
@@ -8,6 +12,7 @@ timeagg
812
monthlyagg
913
yearlyagg
1014
seasonalyagg
15+
dailyagg
1116
temporalranges
1217
maxyearspan
1318
temporal_sampling

src/exports.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Temporal
88
export monthday_indices, maxyearspan, daymonth, realtime_days, realtime_milliseconds,
99
temporal_sampling, timemean, timeagg, monthlyagg, yearlyagg, temporalranges, seasonalyagg,
10-
season, DAYS_IN_ORBIT, HOURS_IN_ORBIT, seasonality, sametimespan
10+
season, DAYS_IN_ORBIT, HOURS_IN_ORBIT, seasonality, sametimespan, dailyagg
1111

1212
# Spatial
1313
export transform_to_coord

src/physical_dimensions/temporal.jl

+23-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ no_time_datetime(::T) where {T<:TimeType} = T
2020
"daymonth(t) = day(t), month(t)"
2121
daymonth(t) = day(t), month(t)
2222

23-
maxyearspan(A::AbstractDimArray, tsamp = temporal_sampling(A)) =
24-
maxyearspan(gnv(dims(A, Time)), tsamp)
25-
2623
"""
2724
temporal_sampling(x) → symbol
2825
Return the temporal sampling type of `x`, which is either an array of `Date`s or
@@ -83,7 +80,8 @@ end
8380

8481
"""
8582
maxyearspan(A::ClimArray) = maxyearspan(dims(A, Time))
86-
maxyearspan(t::Vector{<:DateTime}) → i
83+
maxyearspan(t::AbstractVector{<:DateTime}) → i
84+
8785
Find the maximum index `i` of `t` so that `t[1:i]` covers exact(*) multiples of years.
8886
8987
(*) 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))
126124
end
127125
end
128126

127+
maxyearspan(A::AbstractDimArray, tsamp = temporal_sampling(A)) =
128+
maxyearspan(gnv(dims(A, Time)), tsamp)
129+
129130

130131
"""
131132
monthday_indices(times, date = times[1])
@@ -399,6 +400,22 @@ function monthlyagg(A::ClimArray, f = mean; mday = 15)
399400
return timegroup(A, f, t, tranges)
400401
end
401402

403+
"""
404+
dailyagg(A::ClimArray, f = mean) -> B
405+
Create a new array where the temporal information has been aggregated into days
406+
using the function `f`.
407+
"""
408+
function dailyagg(A::ClimArray, f = mean)
409+
t0 = gnv(dims(A, Time))
410+
ts, tf = extrema(t0)
411+
DT = no_time_datetime(ts)
412+
startdate = DT(year(ts), month(ts), day(ts))
413+
finaldate = DT(year(tf), month(tf), day(tf))
414+
t = startdate:Day(1):finaldate
415+
tranges = temporalranges(t0, Dates.day)
416+
return timegroup(A, f, t, tranges)
417+
end
418+
402419
"""
403420
yearlyagg(A::ClimArray, f = mean) -> B
404421
Create a new array where the temporal information has been aggregated into years
@@ -414,6 +431,8 @@ function yearlyagg(A::ClimArray, f = mean)
414431
return timegroup(A, f, t, tranges)
415432
end
416433

434+
# TODO: this function does not respect the order of dimensions.
435+
# it always puts time last
417436
function timegroup(A, f, t, tranges)
418437
other = otherdims(A, Time)
419438
B = ClimArray(zeros(eltype(A), length.(other)..., length(t)),

test/temporal_tests.jl

+11
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ end
128128
Asea = seasonalyagg(A)
129129
tsea = dims(Asea, Ti).val
130130
@test Base.step(tsea) == Month(3)
131+
132+
@testset "dailyagg" begin
133+
t = DateTime(1,1,1,0):Hour(12):DateTime(1,1,3, 12)
134+
x = [float(isodd(i)) for i in 1:length(t)]
135+
x = hcat([copy(x) for j in 1:4]...)
136+
X = ClimArray(x, (Tim(t), Lon(1:4)))
137+
D = dailyagg(X, mean)
138+
@test all(isequal(0.5), D)
139+
@test step(gnv(dims(D, Tim))) == Day(1)
140+
@test length(dims(D, Tim)) == length(t)÷2
141+
end
131142
end
132143

133144
@testset "interannual variability" begin

0 commit comments

Comments
 (0)