SIZE
The SIZE function returns size and type information for its argument if no keywords are set. If a keyword is set, SIZE returns the specified quantity.
Examples
Print the size information for a 10 by 20 floating-point array by entering:
PRINT, SIZE(FINDGEN(10, 20))
IDL prints:
2 10 20 4 200
This IDL output indicates the array has 2 dimensions, equal to 10 and 20, a type code of 4, and 200 elements total. Similarly, to print only the number of dimensions of the same array:
PRINT, SIZE(FINDGEN(10, 20), /N_DIMENSIONS)
IDL prints:
2
For more information on using SIZE, see the Additional Examples.
Syntax
Result = SIZE( Expression [, /L64] [, /DIMENSIONS | , /FILE_LUN | , /FILE_OFFSET | , /N_DIMENSIONS | , /N_ELEMENTS | , /SNAME, | , /STRUCTURE | , /TNAME | , /TYPE] )
Return Value
If no keywords are set, SIZE returns a vector of integer type. The first element is equal to the number of dimensions of Expression. This value is zero if Expression is scalar or undefined. The next elements contain the size of each dimension, one element per dimension (none if Expression is scalar or undefined). After the dimension sizes, the last two elements contain the type code (zero if undefined) and the number of elements in Expression, respectively. The type codes are listed below.
If a keyword is set, SIZE returns the specified information.
Note: The SIZE function treats lists and hashes as if they are one-dimensional arrays.
IDL Type Codes and Names
The following table lists the IDL type codes and type names returned by the SIZE function:
Type Code |
Type Name |
Data Type |
0 |
UNDEFINED |
Undefined |
1 |
BYTE |
Byte |
2 |
INT |
Integer |
3 |
LONG |
Longword integer |
4 |
FLOAT |
Floating point |
5 |
DOUBLE |
Double-precision floating |
6 |
COMPLEX |
Complex floating |
7 |
STRING |
String |
8 |
STRUCT |
Structure |
9 |
DCOMPLEX |
Double-precision complex |
10 |
POINTER |
Pointer |
11 |
OBJREF |
Object reference |
12 |
UINT |
Unsigned Integer |
13 |
ULONG |
Unsigned Longword Integer |
14 |
LONG64 |
64-bit Integer |
15 |
ULONG64 |
Unsigned 64-bit Integer |
See IDL Data Types for additional information on attributes of the various IDL data types, including ways to create variables of each type.
Arguments
Expression
The expression for which size information is requested.
Note: Normally, providing a scalar object reference as an argument to SIZE returns standard information about an object reference. If the argument is an instance of an object class that overloads the IDL_Object::_overloadSize function method, the value returned by that function method will be output by SIZE. See IDL_Object for details.
Keywords
With the exception of L64, the following keywords determine the return value of the SIZE function and are mutually exclusive — specify at most one of the following.
DIMENSIONS
Set this keyword to return the dimensions of Expression. If Expression is scalar, the result is a scalar containing a 0. For arrays, lists, and hashes, the result is an array containing the array or list dimensions. The result is a 32-bit integer when possible, and 64-bit integer if the number of elements in Expression requires it. Set L64 to force 64-bit integers to be returned in all cases. If Expression is undefined, IDL reports eight dimensions.
FILE_LUN
Set this keyword to return the file unit to which Expression is associated, if it is an IDL file variable, as created with the ASSOC function. If Expression is not a file variable, 0 is returned (0 is not a valid file unit for ASSOC).
FILE_OFFSET
Set this keyword to return the ASSOC file offset for Expression. In the case of ASSOC file variables, this is the value of the Offset argument specified to ASSOC when the variable was created. In the case of regular non-file variables, 0 is returned. The result will be 32-bit integer when possible, and 64-bit integer if the size of the offset requires it. Set L64 to force a 64-bit integer to be returned in all cases.
L64
By default, the result of SIZE is a 32-bit integer when possible, and a 64-bit integer if the number of elements in Expression requires it. Set L64 to force 64-bit integers to be returned in all cases. In addition to affecting the default result, L64 also affects the output from the DIMENSIONS, FILE_OFFSET, N_ELEMENTS, and STRUCTURE keywords.
Note: Only 64-bit versions of IDL are capable of creating variables requiring 64-bit SIZE output. Check the value of !VERSION.MEMORY_BITS to see if your IDL is 64-bit or not.
N_DIMENSIONS
Set this keyword to return the number of dimensions in Expression, if it is an array or list. If Expression is scalar or undefined, 0 is returned.
N_ELEMENTS
Set this keyword to return the number of data elements in Expression. Setting this keyword is equivalent to using the N_ELEMENTS function. The result will be a 32-bit integer when possible, and a 64-bit integer if the number of elements in Expression requires it. Set L64 to force 64-bit integers to be returned in all cases. If Expression is undefined, 0 is returned.
SNAME
Set this keyword to return the name of the structure definition for Expression, if Expression is a named structure. If Expression is not a named structure, an empty string is returned. The string returned by this keyword is the same as that returned by TAG_NAMES(/STRUCTURE_NAME).
STRUCTURE
Set this keyword to return all available information about Expression in a structure.
Note: Since the structure is a named structure, the size of its fields is fixed. The result is an IDL_SIZE (32-bit) structure when possible, and an IDL_SIZE64 structure otherwise. Set L64 to force an IDL_SIZE64 structure to be returned in all cases.
The following are descriptions of the fields in the returned structure:
Field |
Description |
TYPE_NAME |
Name of IDL type of Expression |
STRUCTURE_NAME |
If Expression is a named (not anonymous) structure, the name of the structure definition; otherwise, an empty string |
TYPE |
Type code of Expression |
FILE_LUN |
If Expression is an IDL file variable, as created with the ASSOC function, the file unit to which it is associated; otherwise, 0 |
FILE_OFFSET |
If Expression is an IDL file variable, as created with the ASSOC function, the offset specified to ASSOC when the variable was created; otherwise, 0 |
N_ELEMENTS |
Number of data elements in Expression |
N_DIMENSIONS |
If Expression is an array or list, the number of dimensions; otherwise, 0 |
DIMENSIONS |
An 8-element array containing the dimensions of Expression |
TNAME
Set this keyword to return the IDL type of Expression as a string. See IDL Type Codes and Names for details.
TYPE
Set this keyword to return the IDL type code for Expression. See IDL Type Codes and Names for details. For an example illustrating how to determine the type code of an expression, see Example: Returning the IDL Type Code of an Expression.
Additional Examples
Example: Returning the IDL Type Code of an Expression
The following example illustrates two ways in which to determine the type code of the input expression (see IDL Type Codes and Names for a list). The type code is always the next to last element in the result returned by the SIZE function. Using this information, and the N_ELEMENTS procedure, you can quickly access the data’s type code. For example:
array = [[1,2,3], [4,5,6], [7,8,9]]
; A more flexible method:
sz = SIZE(array)
n = N_ELEMENTS(sz)
type = sz[n-2]
The second method involves using the TYPE keyword to SIZE. In this case, the value returned by the SIZE function contains only the type code of the input expression. You can use the TNAME keyword to return the type name. Select any image file and the pixel data type will be printed in the Output window.
; Select an image file.
result = DIALOG_READ_IMAGE(FILE=selectedFile, IMAGE=image)
type = SIZE(image, /TYPE)
typeName = SIZE(image, /TNAME)
PRINT, "Type code = ", type, " type code name = ", typename
Example: Returning Array Dimension Information
Assume A is an integer array with dimensions of (3,4,5). The statements:
arr = INDGEN(3,4,5)
S = SIZE(arr)
assign to the variable S
a six-element vector containing:
Element |
Value |
Description |
S0 |
3 |
Three dimensions |
S1 |
3 |
First dimension |
S2 |
4 |
Second dimension |
S3 |
5 |
Third dimension |
S4 |
2 |
Integer type |
S5 |
60 |
Number of elements = 3*4*5 |
The following code segment checks to see if the variable arr
is two-dimensional and extracts the dimensions:
;Create a variable.
arr = [[1,2,3],[4,5,6]]
;Get size vector.
S = SIZE(arr)
;Check if two dimensional.
IF S[0] NE 2 THEN $
;Print error message.
MESSAGE, 'Variable a is not two dimensional.'
;Get number of columns and rows.
NX = S[1] & NY = S[2]
PRINT, 'Array is ', NX, ' columns by ', NY, ' rows.'
IDL prints:
Array is 3 columns by 2 rows.
Version History
Original |
Introduced |
6.1 |
Added FILE_OFFSET and SNAME keywords Added FILE_OFFSET and STRUCTURE_NAME fields to STRUCTURE |