When writing even small applications, it is often necessary to distribute resource files along with your code. For example, images and icons are frequently needed by GUI applications. Custom color table files or fonts might be needed by applications that create visualizations. Defaults might be stored in other data files. But how do you find these files, when the user could have installed your application anywhere on their system?

The answer is to place these files in a directory that you know the location of relative to your source code. Then use MG_SRC_ROOT [1] (or one of the other alternatives to it) to determine the location of your source code. Finally, use FILEPATH to specify the location of the resource. For example, in MG_LOADCT, I do the following to find the Brewer color tables, which are in the same directory as MG_LOADCT:

ctfilename = filepath('brewer.tbl', root=mg_src_root())

If I had placed the color tables in a resources subdirectory which was parallel to the code directory my source code lives in, I could just use the SUBDIR keyword:

ctfilename = filepath('brewer.tbl', subdir=['..', 'resources'], $
                      root=mg_src_root())

MG_SRC_ROOT is one of my most used routines[2]. Get all the source code for mglib on GitHub.


  1. Before the SCOPE_TRACEBACK routine was introduced in IDL 6.2, MG_SRC_ROOT had to do ugly things like parse the output from HELP. ??

  2. I count 89 uses of MG_SRC_ROOT in my library mglib and it is also used in IDLdoc and mgunit. ??