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

[Help wanted] Add dimension #453

Closed
ThomasAuriel opened this issue May 26, 2023 · 3 comments
Closed

[Help wanted] Add dimension #453

ThomasAuriel opened this issue May 26, 2023 · 3 comments

Comments

@ThomasAuriel
Copy link

I would like to add a dimension to a raster object.

Currently i do this:

# Get a subset of the initial ds raster object
subDs = ds[Ti=At(time)]
# Add the time dimension to keep the time dimension
subDs = rebuild(subDs; dims=(subDs.dims..., Ti(time)))

Today, this code give me this error:

Internal Error: BoundsError: attempt to access Tuple{} at index [1]

(dims has the X and Y dimensions)

  • Is it possible to keep the dimension when I retrieve a sub dataset ?
  • Is it possible to add a dimension ?
@rafaqz
Copy link
Owner

rafaqz commented May 27, 2023

You would need to add a dimension to the array at the same time or it wont match? rebuild is not fancy, it just uses what you give it.

But why no instead index with a range or vector:

subDs = ds[Ti=At([time])]

Then you wont lose the dimension

@ThomasAuriel
Copy link
Author

Oh ! That's clever. I hadn't thought of that. It works as expected. Thank you.

For the second question:
Let's say later in my code I need to add a dimension, how could I do ?

Does RasterStack could be the solution ?

@rafaqz
Copy link
Owner

rafaqz commented May 27, 2023

Yeah its a little harder...

With a base Array you can just add colons to indexing to get extra dimensions:

julia> zeros(2, 2)[:, :, :]
2×2×1 Array{Float64, 3}:
[:, :, 1] =
 0.0  0.0
 0.0  0.0

But here you also want to add a dimension. This should work fine if your array is a matrix:

subDs = rebuild(subDs; data=parent(subDs)[:, :, :], dims=(dims(subDs..., Ti(time)))

Its not a very common thing to do, so I'm not sure if it needs a function in DD.

(More common is using cat to add a dimension when joining arrays)

@rafaqz rafaqz closed this as completed Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants