IDL

OS Detection in IDL

A lot of what I do in IDL involves using paths all over the place, the problem is, I want my code to work in Windows or Linux, but we of course have that old problem of / v \.

At the top of my IDL program I have usually defined a root directory of where we will be working, but during the program I might want to go to a subdirectory of (let’s say) y2011 and then d253, i.e. on Windows:

rootdir\y2011\d253\

and on Linux:

rootdir/y2011/d253/

But I don’t want to have to define what OS I happen to be working in, so here is a little function to make this process easy, detectos. The use of this function is very easy, lets assume we are running our IDL program on Windows:

path='rootdir/y2011/d253/'
path=detectos(path)
PRINT,path
rootdir\y2011\d253\

Of course you also could have just as easily used it (and tested it) in one line:

PRINT,detectos('rootdir/y2011/d253/')
rootdir\y2011\d253\

I usually call ‘detectos’ every time I create a path, to make sure that the path is in the correct format for the OS I am using.

2 thoughts on “OS Detection in IDL

  1. There’s a built-in for this, path_sep():

    IDL> print, !Version.OS_NAME
    Mac OS X
    IDL> print, path_sep()
    % Compiled module: PATH_SEP.
    /

    1. Hi Mike,

      Thanks for your comment, you are exactly right! I did not realise this function existed. However, it still does not quite do what I would like, since you would then need to define the output of PATH_SEP() as a variable and call it all over the place in the string, for example:

      IDL> slash=PATH_SEP()
      IDL> string=slash+’home’+slash+’dir’+slash

      Which is probably more hassle than it is worth. However since writing this post I’ve realised that the correct way of defining an OS independent path is using the command FILEPATH. From the help:
      “The FILEPATH function returns the fully-qualified path to a file contained in the IDL distribution. Operating system dependencies are taken into consideration.” If you set this all up properly this will do the job.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.