Returning Type and Size Information

The SIZE function returns size and type information for a given expression. The returned vector is always of longword type.

See IDL Type Codes and Names under the SIZE function for a complete list of type codes. See the following examples for more information on the SIZE function:

In addition to the examples listed above, also see the following SIZE function examples:

Determining if a Variable is a Scalar or an Array

The SIZE function can be used to determine whether a variable holds a scalar value or an array. Setting the DIMENSIONS keyword causes the SIZE function to return a 0 if the variable is a scalar, or the dimensions if the variable is an array:

A = 1 
B = [1] 
C = [1,2,3] 
D = [[1,2],[3,4]] 
 
PRINT, SIZE(A, /DIMENSIONS) 
PRINT, SIZE(B, /DIMENSIONS) 
PRINT, SIZE(C, /DIMENSIONS) 
PRINT, SIZE(D, /DIMENSIONS) 

IDL Prints:

0 
1 
3 
2    2 

Using SIZE to Return Image Dimensions

The following example reads an image array and uses the SIZE function DIMENSIONS keyword to access the number of rows and columns in the image file. In this simple example, the information is used to create a display window of the correct size.

PRO ex_displayImage 
 
; Select and read the image file. 
earth = READ_PNG (FILEPATH ('avhrr.png', $ 
   SUBDIRECTORY = ['examples', 'data']), R, G, B) 
 
; Load the color table and designate white to occupy the 
; final position in the red, green and blue bands. 
TVLCT, R, G, B 
maxColor = !D.TABLE_SIZE - 1 
TVLCT, 255, 255, 255, maxColor 
 
; Prepare the display device. 
DEVICE, DECOMPOSED = 0, RETAIN = 2 
 
; Get the size of the original image array. 
earthSize = SIZE(earth, /DIMENSIONS) 
 
; Prepare a window and display the new image. 
WINDOW, 0, XSIZE = earthSize[0], YSIZE = earthSize[1] 
TV, earth 
 
END