Running IDL program from bash with variables

I wrote a program in IDL to generate scatterplots based on command line arguments. I can successfully call the program directly in the terminal as follows:

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

Where $ * refers to some string literals typed. The problem is that I thought I could just type the very string by typing the variable names instead of literals in the bash script, and generate a million scatter plots while I'm at the break. Unfortunately, if I do this, I get an error message:

idl: -e parameter cannot be specified with batch files

So, my next attempt was to try to write these commands to an IDL batch file that I would run.

This attempt is as follows:

#!/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 generated by the script are important, so I’ll just post the beginning and I will 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 do not know if I am trying to do something that cannot be done, but it does not look like it. Any tips?

+5
source share
2 answers

There are two ways to do this: using COMMAND_LINE_ARGSor creating a valid regular IDL call. This procedure 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 one 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
+5
source

I don't know IDL, but here are the fixes for your Bash script that may 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 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"

, :

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

.

+4

All Articles