From c55e8d7565073cabe57cab58308fdca55ce3ef27 Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Thu, 12 Dec 2024 16:35:55 +0100 Subject: [PATCH 1/4] no more Ti --- src/DatasetAPI/Datasets.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DatasetAPI/Datasets.jl b/src/DatasetAPI/Datasets.jl index 7a4bcf68..e3de1f1f 100644 --- a/src/DatasetAPI/Datasets.jl +++ b/src/DatasetAPI/Datasets.jl @@ -285,7 +285,7 @@ function toaxis(dimname, g, offs, len) catch ar[:] end - DD.Ti(tsteps[offs+1:end]) + DD.rebuild(DD.name2dim(axname), tsteps[offs+1:end]) elseif haskey(aratts, "_ARRAYVALUES") vals = identity.(aratts["_ARRAYVALUES"]) DD.rebuild(DD.name2dim(axname),(vals)) From f845879248726e702e3cef9bca9756e9bad8b05b Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Thu, 12 Dec 2024 16:53:19 +0100 Subject: [PATCH 2/4] go back to :time --- docs/src/UserGuide/group.md | 28 ++++++++++++++-------------- src/Cubes/Cubes.jl | 2 +- test/Datasets/datasets.jl | 6 +++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/src/UserGuide/group.md b/docs/src/UserGuide/group.md index 389efbe0..11c50731 100644 --- a/docs/src/UserGuide/group.md +++ b/docs/src/UserGuide/group.md @@ -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 ```` @@ -80,7 +80,7 @@ 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 @@ -88,7 +88,7 @@ mean_g = mean.(g_ds, dims=:Ti) 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 @@ -96,7 +96,7 @@ mean_g = dropdims.(mean_g, dims=:Ti) 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. @@ -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)) ```` @@ -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 @@ -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 diff --git a/src/Cubes/Cubes.jl b/src/Cubes/Cubes.jl index 6487b718..271bfa6a 100644 --- a/src/Cubes/Cubes.jl +++ b/src/Cubes/Cubes.jl @@ -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 diff --git a/test/Datasets/datasets.jl b/test/Datasets/datasets.jl index 67e31d09..21493d21 100644 --- a/test/Datasets/datasets.jl +++ b/test/Datasets/datasets.jl @@ -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) @@ -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 From 1ac2dff147384605a4ead43c79a8fd7d4fd6f15f Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Fri, 13 Dec 2024 09:27:17 +0100 Subject: [PATCH 3/4] no Ti check --- src/Cubes/Cubes.jl | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Cubes/Cubes.jl b/src/Cubes/Cubes.jl index 271bfa6a..ac1c2e6a 100644 --- a/src/Cubes/Cubes.jl +++ b/src/Cubes/Cubes.jl @@ -480,14 +480,7 @@ function Base.getindex(a::YAXArray, args::DD.Dimension...; kwargs...) for (k,v) in kwargsdict d = getAxis(k,a) if d !== nothing - if d isa DD.Ti - if v isa UnitRange{Int} - v = Date(first(v))..Date(last(v),12,31) - end - d2[:time] = v - else - d2[DD.name(d)] = v - end + d2[DD.name(d)] = v else d2[k] = v end From 273b8c9790d4400fe2c2347865a5557ce53dad49 Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Fri, 13 Dec 2024 12:26:50 +0100 Subject: [PATCH 4/4] use Dim{:time} --- docs/src/UserGuide/group.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/src/UserGuide/group.md b/docs/src/UserGuide/group.md index 11c50731..4101e21e 100644 --- a/docs/src/UserGuide/group.md +++ b/docs/src/UserGuide/group.md @@ -52,11 +52,11 @@ function weighted_seasons(ds) # calculate weights tempo = dims(ds, :time) month_length = YAXArray((tempo,), daysinmonth.(tempo)) - g_tempo = groupby(month_length, Ti => seasons(; start=December)) + g_tempo = groupby(month_length, Dim{:time} => seasons(; start=December)) sum_days = sum.(g_tempo, dims=:time) weights = map(./, g_tempo, sum_days) # unweighted seasons - g_ds = groupby(ds, Ti => seasons(; start=December)) + g_ds = groupby(ds, Dim{:time} => seasons(; start=December)) mean_g = mean.(g_ds, dims=:time) mean_g = dropdims.(mean_g, dims=:time) # weighted seasons @@ -74,7 +74,7 @@ end Now, we continue with the `groupby` operations as usual ````@ansi compareXarray -g_ds = groupby(ds, Ti => seasons(; start=December)) +g_ds = groupby(ds, Dim{:time} => seasons(; start=December)) ```` And the mean per season is calculated as follows @@ -113,7 +113,7 @@ month_length = YAXArray((tempo,), daysinmonth.(tempo)) Now group it by season ````@ansi compareXarray -g_tempo = groupby(month_length, Ti => seasons(; start=December)) +g_tempo = groupby(month_length, Dim{:time} => seasons(; start=December)) ```` Get the number of days per season @@ -181,9 +181,9 @@ with_theme(theme_ggplot2()) do fig = Figure(; size = (850,500)) axs = [Axis(fig[i,j], aspect=DataAspect()) for i in 1:3, j in 1:4] for (j, s) in enumerate(seasons_g) - hm_o = heatmap!(axs[1,j], mean_g[Ti=At(s)]; colorrange, lowclip, highclip, colormap) - hm_w = heatmap!(axs[2,j], weighted_g[Ti=At(s)]; colorrange, lowclip, highclip, colormap) - hm_d = heatmap!(axs[3,j], diff_g[Ti=At(s)]; colorrange=(-0.1,0.1), lowclip, highclip, + hm_o = heatmap!(axs[1,j], mean_g[time=At(s)]; colorrange, lowclip, highclip, colormap) + hm_w = heatmap!(axs[2,j], weighted_g[time=At(s)]; colorrange, lowclip, highclip, colormap) + hm_d = heatmap!(axs[3,j], diff_g[time=At(s)]; colorrange=(-0.1,0.1), lowclip, highclip, colormap=:diverging_bwr_20_95_c54_n256) end Colorbar(fig[1:2,5], hm_o, label=cb_label)