Skip to content

Commit

Permalink
go back to :time
Browse files Browse the repository at this point in the history
  • Loading branch information
lazarusA committed Dec 12, 2024
1 parent c55e8d7 commit f845879
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
28 changes: 14 additions & 14 deletions docs/src/UserGuide/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ nothing # hide
````julia
function weighted_seasons(ds)
# calculate weights
tempo = dims(ds, :Ti)
tempo = dims(ds, :time)
month_length = YAXArray((tempo,), daysinmonth.(tempo))
g_tempo = groupby(month_length, Ti => seasons(; start=December))
sum_days = sum.(g_tempo, dims=:Ti)
sum_days = sum.(g_tempo, dims=:time)
weights = map(./, g_tempo, sum_days)
# unweighted seasons
g_ds = groupby(ds, Ti => seasons(; start=December))
mean_g = mean.(g_ds, dims=:Ti)
mean_g = dropdims.(mean_g, dims=:Ti)
mean_g = mean.(g_ds, dims=:time)
mean_g = dropdims.(mean_g, dims=:time)
# weighted seasons
g_dsW = broadcast_dims.(*, weights, g_ds)
weighted_g = sum.(g_dsW, dims = :Ti);
weighted_g = dropdims.(weighted_g, dims=:Ti)
weighted_g = sum.(g_dsW, dims = :time);
weighted_g = dropdims.(weighted_g, dims=:time)
# differences
diff_g = map(.-, weighted_g, mean_g)
seasons_g = lookup(mean_g, :Ti)
seasons_g = lookup(mean_g, :time)
return mean_g, weighted_g, diff_g, seasons_g
end
````
Expand All @@ -80,23 +80,23 @@ g_ds = groupby(ds, Ti => seasons(; start=December))
And the mean per season is calculated as follows

````@ansi compareXarray
mean_g = mean.(g_ds, dims=:Ti)
mean_g = mean.(g_ds, dims=:time)
````

### dropdims

Note that now the time dimension has length one, we can use `dropdims` to remove it

````@ansi compareXarray
mean_g = dropdims.(mean_g, dims=:Ti)
mean_g = dropdims.(mean_g, dims=:time)
````

### seasons

Due to the `groupby` function we will obtain new grouping names, in this case in the time dimension:

````@example compareXarray
seasons_g = lookup(mean_g, :Ti)
seasons_g = lookup(mean_g, :time)
````

Next, we will weight this grouping by days/month in each group.
Expand All @@ -106,7 +106,7 @@ Next, we will weight this grouping by days/month in each group.
Create a `YAXArray` for the month length

````@example compareXarray
tempo = dims(ds, :Ti)
tempo = dims(ds, :time)
month_length = YAXArray((tempo,), daysinmonth.(tempo))
````

Expand All @@ -119,7 +119,7 @@ g_tempo = groupby(month_length, Ti => seasons(; start=December))
Get the number of days per season

````@ansi compareXarray
sum_days = sum.(g_tempo, dims=:Ti)
sum_days = sum.(g_tempo, dims=:time)
````

### weights
Expand All @@ -146,8 +146,8 @@ g_dsW = broadcast_dims.(*, weights, g_ds)
apply a `sum` over the time dimension and drop it

````@ansi compareXarray
weighted_g = sum.(g_dsW, dims = :Ti);
weighted_g = dropdims.(weighted_g, dims=:Ti)
weighted_g = sum.(g_dsW, dims = :time);
weighted_g = dropdims.(weighted_g, dims=:time)
````

Calculate the differences
Expand Down
2 changes: 1 addition & 1 deletion src/Cubes/Cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ function Base.getindex(a::YAXArray, args::DD.Dimension...; kwargs...)
if v isa UnitRange{Int}
v = Date(first(v))..Date(last(v),12,31)
end
d2[:Ti] = v
d2[:time] = v
else
d2[DD.name(d)] = v
end
Expand Down
6 changes: 3 additions & 3 deletions test/Datasets/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using Dates
YAXArray(axlist2, data[3], props[3]),
)
ds = Dataset(avar=c1, something=c2, smaller=c3)
# previous version will throw this error: `KeyError: key :Ti not found`
# previous version will throw this error: `KeyError: key :time not found`
f = "./temp.zarr"
@test_nowarn savedataset(ds; path=f)
rm(f, recursive=true, force=true)
Expand Down Expand Up @@ -215,11 +215,11 @@ end
ds = open_dataset("test.mock")
@test size(ds.Var1) == (10, 5, 2)
@test size(ds.Var2) == (10, 5)
@test all(in(keys(ds.axes)), (:Ti, :d2, :d3))
@test all(in(keys(ds.axes)), (:time, :d2, :d3))
ar = Cube(ds)
@test ar isa YAXArray
@test size(ar) == (10, 5, 2, 2)
@test DD.name.(ar.axes) == (:Ti, :d2, :d3, :Variable)
@test DD.name.(ar.axes) == (:time, :d2, :d3, :Variable)
@test DD.lookup(ar.axes[4]) == ["Var1", "Var3"]
end
@testset "Dataset creation" begin
Expand Down

0 comments on commit f845879

Please sign in to comment.