Skip to content

Commit

Permalink
clarify how to load a scalar variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Oct 13, 2023
1 parent a8f2bcd commit ef966c1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docs/src/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ Array(v_cf)

Since NCDatasets 0.13, the syntax `v_cf[:]` flattens the array, and is not equivalent with the above (unless `v_cf` is a vector).


You can only load sub-parts of it in memory via indexing each dimension:
```julia
v_cf[1:5, 10:20]
```
(here you must know the number of dimensions of the variable, as you must access all of them).
(here you must know the number of dimensions of the variable, as you must access all of them). A scalar variable can be loaded using `[]`, for example:

``` julia
using NCDatasets
NCDataset("test_sclar.nc","c") do ds
defVar(ds,"scalar",42,())
end

ds = NCDataset("test_sclar.nc")
value = ds["scalar"][] # 42
```



!!! note
`NCDatasets.Variable` and `NCDatasets.CFVariable` implement the interface of `AbstractArray`. It is thus possible to call any function that accepts an `AbstractArray`. But functions like `mean`, `sum` (and many more) would load every element individually which is very inefficient for large fields read from disk. You should instead convert such a variable to a standard Julia `Array` and then do computations with it. See also the [performance tips](@ref performance_tips) for more information.
Expand Down

0 comments on commit ef966c1

Please sign in to comment.