A Smart Way To Help IDL Find Data Files In Your File System
This Help Article is designed for programmers, who, in calls needing file path inputs, are either:
- trying to debug errors like "The system cannot find the file specified" or "No such file or directory", or ...
- trying to optimize their custom IDL program and data files for sharing with other users or porting to other systems.
We believe that the approach presented below offers the best strategy for organizing your data files and for addressing them in IDL procedure/function calls.
One of the biggest problems in porting IDL programs from the developer's machine to other machines arises because developers cannot always be sure into what directory location their program files will be installed. For this reason, if you are supplying data or resource files needed by your IDL program, then it is dangerous to identify these files by an absolute path. Identifying them by a path relative to IDL's default current working directory is also dangerous, because that default current working directory could also vary from machine to machine. When a suboptimal choice of file-addressing strategy has been made, errors like the following can occur:
IDL> openr, lun, 'C:\MyIDLApps\data\mydata.dat', /GET_LUN
OPENR: Error opening file. Unit: 100, File: C:\MyIDLApps\data\mydata.dat
The system cannot find the file specified.
or:
IDL> read_jpeg, 'myimage.jpg', image
READ_JPEG: Error opening file. Unit: 100, File: /home/johndoe/myimage.jpg
No such file or directory
To eliminate the above errors we can recommend at least two strategies for positioning and finding the data and resource files needed by your IDL program.
The first strategy is popular with Exelis Visual Information Solutions' PSG consulting group. It is based on use of a routine named
SOURCEPATH and the source code for that routine can be downloaded into your IDL search path from Exelis' 'Code Contrib' site at URL:
The instructions for how to use SOURCEPATH are shown at that link.
'sourcepath.pro' uses an approach based on IDL's
SCOPE_TRACEBACK routine, introduced in IDL 6.2. An alternative strategy, which you could embed at the beginning of your main application routine, is based on the IDL function ROUTINE_INFO, and is discussed below:
Place your data and resource files in either the same directory or in a child or sibling directory to your main application routine. Thus, let's say you have an application, whose main program you are distributing as "myidlapp.pro" or "myidlapp.sav" and this program is dependent for some of its data on "mydata.dat" and for one of its widget button bitmaps on "mybutton.bmp". Let's say that you export the data in the same directory that holds the main program (the "application root directory") and you export the bitmap in a subdirectory to the application root called "resource". We would then recommend that your main program have a line like the following near the top:
info = routine_info('myidlapp', /SOURCE)
appRootDir = file_dirname(info.path)
Now, in subsequent calls for the data or the resource file the following lines will work on any system that anyone installs your application on:
dataFile = filepath('mydata.dat', ROOT_DIR=appRootDir)
and
bitmapPath = filepath('mybutton.bmp', ROOT_DIR=appRootDir, $
SUBDIRECTORY=['resource'])
Note one other strategy: If all your data files and resource files are in the same directory with your main program file, you can avoid the need for FILEPATH calls completely. In this scenario, after you have run the ROUTINE_INFO query block above, you can simply call:
and after that call you can open files by their name, no further path info required,
e.g. "openr, lun, 'mydata.dat', /GET_LUN".