Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dailyagg function #106

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions docs/src/statistics.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Statistics

```@index
Pages = ["statistics.md"]
```

## Temporal
Functions related with the `Time` dimension.
```@docs
Expand All @@ -8,6 +12,7 @@ timeagg
monthlyagg
yearlyagg
seasonalyagg
dailyagg
temporalranges
maxyearspan
temporal_sampling
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
Loading