Accessing Files Programmatically

Regardless of the data type, there are several routines that are commonly used to access files and data. To read data into an IDL variable, you must identify the file containing the data, and then extract the data from the file. This section discusses file access. Following sections Accessing Image Data Programmatically and Accessing Non-Image Data Programmatically) discuss data access.

Locating Files in the IDL Installation

Use the FILEPATH function to select a file included in the IDL distribution. For example, to select a file in the examples/data subdirectory of the main IDL installation directory, use the statement:

file = FILEPATH('mr_brain.dcm', SUBDIRECTORY=['examples', 'data']) 

You can also use FILEPATH to access a file outside the IDL installation hierarchy by specifying the ROOT_DIR keyword. The following statement opens a file named testImg.tif in the C:\tempImages directory.

file = FILEPATH('testImg.tif', ROOT_DIR='C:', $ 
   SUBDIRECTORY='tempImages') 

Locating Files Anywhere in the Filesystem

To retrieve the path to a file when you know the file's location, use the FILE_SEARCH function. For example to retrieve the full path to a file named testFile.dat in the current directory, use the following statement:

path = FILE_SEARCH('testFile.dat', /FULLY_QUALIFY_PATH) 

This technique is useful when you know the relative location of the file and need to know the full path.

To select a file interactively using an operating system native file selection dialog, use the DIALOG_PICKFILE routine. For example, to select a JPEG file from anywhere in the filesystem, use the following statement:

path = DIALOG_PICKFILE(TITLE='Select a JPEG file', $ 
   FILTER = ['*.jpg', '*.jpeg']) 

This displays files with the extnesion .jpg or .jpeg and returns the full path to the selected file.