Running IDL program from bash with variables

13,442

Solution 1

There are two ways to go about this: using COMMAND_LINE_ARGS or constructing a valid IDL routine call. This routine uses both:

pro test, other_args
  compile_opt strictarr

  args = command_line_args(count=nargs)

  help, nargs
  if (nargs gt 0L) then print, args

  help, other_args
  if (n_elements(other_args) gt 0L) then print, other_args
end

Call it from the command line in either of the following two ways:

Desktop$ idl -e "test" -args $MODE
IDL Version 8.2, Mac OS X (darwin x86_64 m64). (c) 2012, Exelis Visual Information Solutions, Inc.
Installation number: 216855.
Licensed for use by: Tech-X Corporation

% Compiled module: TEST.
NARGS           LONG      =            1
test
OTHER_ARGS      UNDEFINED = <Undefined>
Desktop$ idl -e "test, '$MODE'"
IDL Version 8.2, Mac OS X (darwin x86_64 m64). (c) 2012, Exelis Visual Information Solutions, Inc.
Installation number: 216855.
Licensed for use by: Tech-X Corporation

% Compiled module: TEST.
NARGS           LONG      =            0
OTHER_ARGS      STRING    = 'test'
test

Solution 2

I don't know IDL, but here are fixes for your Bash script that are likely to help:

#!/bin/bash

indir=/path/to/indir/
outdir=/path/to/outdir/

# (commented out) files=`ls $indir` # no, just no
batchfile=/path/to/tempbatchfile.pro

echo ".r /path/to/scatterplot_1_2d_file.pro" > "$batchfile"  # overwrite the file on the first write, put everything inside the quotes

for file in "$indir/"*
    do
    name=${file%\.*}
    echo "scatterplot_1_2d_file $indir$name.txt $outdir$name.jpg $name Gauge Precipitation (mm) NMQ Precipitation (mm) * * * * 2" # quote the whole thing once, it's simpler and echo doesn't do anything differently
done >> "$batchfile" # do all the output from the loop
echo exit >> "$batchfile"

# *** where does idl learn the location of "$batchfile"? ***
idl <<EOF                                                                                                                                                                                                      
@/path/to/scatterplot_1_2d_file                                                                                                                                                                  
EOF                                                                                                                                                                                                            

rm "$batchfile"

To fix your command line version, use quoting:

idl -e "scatterplot_1_2d_file.pro" "$infile" "$outfile" "$title" "$xtitle" "$ytitle" "$xmin" "$xmax" "$ymin" "$ymax" "$timescale"

Always quote variables when they're expanded.

Share:
13,442
Frank Harris
Author by

Frank Harris

Updated on June 07, 2022

Comments

  • Frank Harris
    Frank Harris almost 2 years

    I've written a program in IDL to generate scatter plots based on command line arguments. I can successfully call the program directly in the terminal like this:

    idl -e "scatterplot_1_2d_file.pro" $infile $outfile $title $xtitle $ytitle $xmin $xmax $ymin $ymax $timescale

    Where $* refer to some string literals typed in. The problem is, I thought I'd be able to just type that very line, putting in variable names in place of the literals, into a bash script, and generate a million scatter plots while I'm on break. Unfortunately, if I do it that way, I get the error:

    idl: -e option cannot be specified with batch files

    So my next attempt was to try writing those commands to an IDL batch file that I'd then run.

    That attempt looks like this:

    #!/bin/bash
    
    indir=/path/to/indir/
    outdir=/path/to/outdir/
    
    files=`ls $indir`
    batchfile=/path/to/tempbatchfile.pro
    
    echo .r "/path/to/scatterplot_1_2d_file.pro" >> $batchfile
    
    for file in $files
      do
      name=${file%\.*}
      echo scatterplot_1_2d_file $indir$name.txt $outdir$name.jpg $name "Gauge Precipitation (mm)" "NMQ Precipitation (mm)" "*" "*" "*" "*" 2 >> $batchfile
    done #done file                                                                                                                                                                                                
    
    echo exit >> $batchfile
    
    idl <<EOF                                                                                                                                                                                                      
    @/path/to/scatterplot_1_2d_file                                                                                                                                                                  
    EOF                                                                                                                                                                                                            
    
    rm $batchfile
    

    I don't know if the bulk of the errors that script generates are relevant, so I'll just post the start and I'll post the rest later if you need it:

    [foo]$ bash script_thing.sh
    IDL Version 6.3 (linux x86 m32). (c) 2006, Research Systems, Inc.
    Installation number: 91418.
    Licensed for personal use by XXXXXXXXX only.
    All other use is strictly prohibited.
    
    
    PRO scatterplot_1_2d_file
                             ^
    % Programs can't be compiled from single statement mode.
      At: /path/to/scatterplot_1_2d_file.pro, Line 1
    % Attempt to subscript ARGS with <INT      (       1)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       2)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       3)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       4)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       5)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       6)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       7)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       8)> is out of range.
    % Execution halted at: $MAIN$          
    % Attempt to subscript ARGS with <INT      (       9)> is out of range.
    % Execution halted at: $MAIN$          
    

    I don't know if I'm just trying to do something that can't be done, but it doesn't SEEM like it. Any advice?