|
Using IDL: Importing and Writing Data into Variables |
|
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.
One of the most common file access routines is FILEPATH. Use this to select a named file in a specified directory. For example, to select a file in the examples/data directory of the existing working directory, use the statement:
file = FILEPATH('mr_brain.dcm', SUBDIRECTORY=['examples', 'data'])
To access a file outside the existing working directory, use 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')
If your application requires a cross-platform path, one that is not specific to UNIX or Windows, consider using the DIALOG_PICKFILE routine with the GET_PATH keyword. This lets you choose a file and store the operating system native path to the file in a variable. In the following example, you choose an image file and the full directory path to the selected image is stored in path:
sFile = DIALOG_PICKFILE(/MUST_EXIST, $ TITLE = 'Select an Image File', $ FILTER = ['*.bmp', '*.jpg', '*.png', '*.ppm', '*.tif'], $ GET_PATH=path)
When you need to access a file in the directory stored in path, you can use the PATH_SEP function to return the correct path separation character for the operating system. Suppose you have a file called myTestFile.jpg that you want to delete before a program ends. FILE_DELETE requires a string File argument that is in the native syntax for the current operating system. To delete this file, you can use the directory information stored in path, plus the PATH_SEP function, plus the name of the file to delete as follows (the + operator concatenates strings):
FILE_DELETE, path+PATH_SEP()+'myTestFile.jpg', /ALLOW_NONEXISTENT
IDL also provides an extensive number of other file manipulation routines. See General File Access under the functional category Input/Output for a list.
FILEPATH is often used in conjunction with routines that access the data from a file, as shown in the following section.
IDL Online Help (March 06, 2007)