Previous ION Script User's Guide: Creating ION Script Applications Next

Passing IDL Variables to ION Script

There may be cases in which you need to access the value of an IDL variable in your ION Script application. This can be accomplished by writing IDL code that writes an ION_EVALUATE tag to a temporary file, and using ION_INCLUDE to include the IDL-generated ION_EVALUTE tag in your ION Script page.

For example, suppose you want to conditionally execute ION Script code depending on the value of an IDL variable. In the following example, the user enters a number and submits the form. The number entered is passed to IDL, which evaluates the square root of the number, and writes an ION_EVALUATE tag containing the square root to a temporary file. The ION Script page contains an ION_IF block that conditionally executes code depending on the value of the variable defined in the IDL-generated ION_EVALUTE tag.

First, we create the form in which the user enters a number:

page1.ion

<ION_SCRIPT>  
<ION_HEADER>   
  <EVENTS>   
    <EVENT_DECL NAME="go" ACTION="page2.ion"/>   
  </EVENTS>   
</ION_HEADER>   
  
<ION_BODY>  
  
<ION_FORM NAME="form1">  
	<INPUT NAME="num" TYPE="TEXT">  
	<ION_BUTTON TYPE="BUTTON" EVENT="go" LABEL="Find square root"/>  
</ION_FORM>  
  
</ION_BODY>  
</ION_SCRIPT>  

In the action page of this form, page2.ion, we initialize two variables: the $TEMPFILE variable is used to hold the name of the temporary file that IDL will create, and the $IDLVALUE variable holds the value of the IDL variable.

Then we use ION_EVALUATE to define the value of the $TEMPFILE variable using the $ION.uniqueID system variable to ensure the name of our temporary file does not collide with any other filenames.

Next, we use ION_DATA_OUT to call an IDL function defined in writefile.pro. This IDL function accepts the value of $TEMPFILE and the value entered by the user, calculates the square root, and defines the value of $IDLVALUE by writing the result as an ION_EVALUTE string to the file defined by $TEMPFILE.

The ION Script page then includes this temporary file, thereby re-evaluating the value of the $IDLVALUE variable. An ION_IF block then conditionally evaluates the $IDLVALUE variable written by IDL.

page2.ion

<ION_SCRIPT>  
<ION_HEADER>   
   <VARIABLES>   
      <VARIABLE_DECL NAME="TEMPFILE" VALUE="''" TYPE="STR"/>   
      <VARIABLE_DECL NAME="IDLVALUE" VALUE="0.0" TYPE="DOUBLE"/>   
   </VARIABLES>   
</ION_HEADER>   
<ION_BODY>  
  
<ION_EVALUATE EXPR="$TEMPFILE = $ION.temp + 'val' + $ION.uniqueID  
   + '.ion'"/>   
<ION_DATA_OUT DEBUG="FALSE">   
   <IDL>   
      writefile,'$TEMPFILE', $Form.num   
   </IDL>   
</ION_DATA_OUT>   
  
<ION_INCLUDE SRC="ion:///$TEMPFILE" PRE="FALSE"/>   
  
<ION_IF EXPR="$IDLVALUE GT 4">  
	IDL returned a value greater than 4  
<ION_ELSE/>  
	IDL returned a value less or equal to 4  
</ION_IF>  
  
</ION_BODY>  
</ION_SCRIPT>  

writefile.pro

PRO writefile,tempfile, val  
   A = SQRT(val)  
   OPENW,lun,tempfile,/GET_LUN, ERROR=err  
   if err NE 0 then begin  
      print,!ERROR_STATE.MSG  
      RETURN  
   endif  
   PRINTF,lun,'<ION_EVALUATE 
   EXPR="$IDLVALUE='+STRCOMPRESS(A,/REMOVE_ALL)+'"/>'  
   FREE_LUN,lun  
END  

  IDL Online Help (March 06, 2007)