Let's say I have an array of dimensions declared like this:
integer, dimension(5) :: dims
dims = (/ 5, 6, 7, 8, 9 /)
How can I most simply use this array to allocate another array, using the elements of dims
to specify the size of the respective dimensions? Say the second array is declared like this:
real, dimension(:,:,:,:,:), allocatable :: dset
Why can't I do the following?
allocate (dset(dims(:)))
I get Error: Rank mismatch in array reference
when I attempt to compile.
I realize that I can do this:
allocate (dset(dims(1), dims(2), dims(3), dims(4), dims(5)))
But I'm looking for something more easily extensible.
You cannot write allocate(dset(dims(:)))
for exactly the reason the compiler gives: they have different ranks. If you were to print shape(dims)
, you would see 5
on-screen; if you were to print shape(dset)
, you would see either 0 0 0 0 0
or 5 6 7 8 9
(depends if you allocated it or not); dims
is a rank-1 array, dset
is a rank-5 array.
So pretty much the only way to allocate an allocatable is via the explicit method that you feel is inextensible.