使用bash从变量中运行IDL程序

7
我已经用IDL编写了一个程序,根据命令行参数生成散点图。我可以像这样直接在终端调用程序:

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

其中$*代表一些输入的字符串字面量。问题是,我原本以为只需要将变量名替换成字面量,然后在bash脚本中输入这个命令,就可以在休息时间生成无数个散点图。但是,如果我这样做,就会出现以下错误:

idl: -e option cannot be specified with batch files

因此,我的下一个尝试是将这些命令写入一个IDL批处理文件,然后运行该文件。
#!/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

我不知道该脚本产生的大部分错误是否相关,因此我只会发布开头部分,如果您需要,稍后我将发布其余部分:

[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$          

我不知道我是否只是试图做一些无法完成的事情,但似乎并不是这样。有什么建议吗?


1
在我们能够给出具体建议之前,您需要修复所有的引用错误并纠正文件名处理。如果在此之后仍然卡住了,请发布新的错误信息。 - ormaaj
2个回答

7

有两种方法可以实现这个:使用COMMAND_LINE_ARGS或构建一个有效的IDL例程调用。此例程同时使用这两种方法:

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

可以通过以下两种方式之一从命令行调用:

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

4

我不懂IDL,但是这里有一些可能有帮助的Bash脚本修复方法:

#!/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"

为了修复您的命令行版本,请使用引号:
idl -e "scatterplot_1_2d_file.pro" "$infile" "$outfile" "$title" "$xtitle" "$ytitle" "$xmin" "$xmax" "$ymin" "$ymax" "$timescale"

在扩展变量时始终使用引号。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接