I am trying to store a set of 1 dimensional IDL arrays inside a 2-dimensional array in IDL. I cannot find any documentation on Harris Geospatial and I am not having luck just mucking around with syntax.
A 3-dimensional array seems the obvious solution, but the length of the 1D arrays I need to store varies from 1 - 800 integers in length, so I would utilize very little of a 3D array.
Any pointers much appreciated. Thanks.
Your statement,
Any pointers much appreciated
is somewhat ironic, given that one possible solution to your problem is to use pointers! To my knowledge, it's the only way you can store variable-length arrays in a single array. Basically, pointers are just variables, but instead of containing data, they contain a pointer to where data is stored in memory.
You can create a pointer by using the PTR_NEW
function:
IDL> p = ptr_new(findgen(5))
IDL> help, p
P POINTER = <PtrHeapVar22>
To "dereference" the pointer (i.e., access the data), you need to use an asterisk:
IDL> print, p
<PtrHeapVar22>
IDL> print, *p
0.00000 1.00000 2.00000 3.00000 4.00000
So, what you need is a PTRARR
(pointer array):
IDL> test_arr = ptrarr(2,3,/ALLOCATE_HEAP)
IDL> help, test_arr
TEST_ARR POINTER = Array[2, 3]
where each element of the array is one of your 1-D arrays. You can populate the array by storing pointers:
IDL> test_arr[0,0] = p
IDL> print, *test_arr[0,0]
0.00000 1.00000 2.00000 3.00000 4.00000
or by assigning your arrays to the dereferenced elements of the pointer array:
IDL> *test_arr[0,1] = randomu(seed, 4)
IDL> print, *test_arr[0,1]
0.838449 0.967399 0.0669304 0.101592
One downside of using pointers is that you lose a lot (if not all) of the nice vectorization benefits of ordinary IDL arrays. That is, you will generally need to loop over the elements of the array in order to access the values stored in the pointers, which means you will take a performance hit. One other thing to look out for is how operator precedence affects the use of the dereferencing operator (the asterisk). For example, if you store a structure in a pointer, you need to use parentheses in the following way to access the data in the structure:
IDL> str = {a:1,b:'x'}
IDL> str_ptr = ptr_new(str)
IDL> print, (*str_ptr).a
1
Otherwise you get an error:
IDL> print, *str_ptr.a
% Expression must be a structure in this context: STR_PTR.
% Execution halted at: $MAIN$
Here's the documentation for further reference.