Is There a Display?

QUESTION: Is there a way for an IDL program to detect whether or not IDL will crash due to an X Windows error, like these.

% PLOT: Unable to connect to X Windows display: :0.0
% PLOT: Unable to open X Windows display.
        Is your DISPLAY environment variable set correctly? 

The reason I ask is that I'm writing a program which can plot graphics on the display, but I'd like the program to run even if plotting is not possible because the DISPLAY variable hasn't been set. This often happens with remote logins or when running a CRON job, for example.

ANSWER: It is not possible to know ahead of time whether your IDL session can make a connection to the X Window enviroment. Basically, you have to try to make such a connection and if you fail, you know it is not possible. “How in the world,” I know you are asking yourself, “does this make my life any easier?”

Well, if you can catch the error with a CATCH error handler, you might be able to do something about it. For example, one way to make a window connection is to try to open an IDL graphics window. If you can't do it, you have no X Window connection and you have no DISPLAY environment variable set. Here is a simple function that can be used to determine if you have a window connection or not. It will return a 1 if you do, and a 0 if you don't.

    Function CanConnect 
      ; Assume a good connection.       haveConnection = 1 
      ; Try to open a window. If you fail, you have no window connection.
      Catch, theError       IF theError NE 0 THEN BEGIN           Catch, /CANCEL
          haveConnection = 0           GOTO, cleanUp       ENDIF 
      ; Try to open a window.       theWindow = !D.Window
      Window, /FREE, XSIZE=5, YSIZE=5, /PIXMAP       Catch, /CANCEL 
      ; Come here if things go south or you actually make it here.
      cleanUp:        IF !D.Window NE theWindow THEN BEGIN
          WDelete, !D.Window           IF theWindow GE 0 THEN WSet, theWindow
      ENDIF        RETURN, haveConnection    END 

Using this function, you can now test for a connection before you issue a graphics or other command that assumes a window connection.

   IDL> IF CanConnect() THEN Plot, ... 

Version of IDL used to prepare this article: IDL 7.1.2

Google
 
Web Coyote's Guide to IDL Programming