Let’s say that for a given dimension d, we need to constrain the matrix to an specific value (or index). In other words, we need the subarray in which that dimension has an specific value (or index). How I can do something like:
B = A[where dim=d has index = i]
Maybe there is an specific method in Julia that I don’t know.
The motivation is that we read a variable A from a netcdf file, and the “position” of an specific coordinate in a A, in such way that depending on input netcdf that dimension (coordinate) can be in arbitrary axis in the array of the variable A. We need to extract a subarray based on the value of an specific coordinate whose position in the numerical array is variable depending on the input netcdf file.
I am using NCDatasets.jl, and I could use GeoData.jl and actually I can do what I want. But I was wondering how to do without GeoData.jl
using NCDatasets
using GeoData
inforule = Dict("zlev" => 3)
diag = "o3"
finput = "o3_dataset.nc"
function geomean_atlev(diag, finput, inforule)
ds_diag = Dataset(finput)[diag]
data = geoarray(finput)
subdata = Array( data[ z=inforule["zlev"] ] )
data = convert(Float64, mean(Array(subdata)))
return diag, finput, data, inforule
end
For example how to get the subdarray named “subdata” above in the code like this below, where
I know that the axis where I want to constrain the array is “posz”
using NCDatasets
inforule = Dict("zlev" => 3)
diag = "o3"
finput = "o3_dataset.nc"
function geomean_atlev(diag, finput, inforule)
ds_diag = Dataset(finput)[diag]
dimsizes= dimsize(ds_diag)
posz=0
let counter=0
for (index, value) in collect(pairs(dimsizes))
counter=counter+1
if index==:z
posz=counter
end
end
[...]
end # let
end #function
I don’t fully understand your problem, but this toy example using selectdim, views, and BitMatrix might help. The selectdim function creates a view. Modifying a view modifies the original matrix. The BitMatrix type can be used for logical indexing.