从GAWK和Bash脚本调用Gnuplot,只绘制第一个图

时间:2012-09-24 22:14:28

标签: macos bash gnuplot gawk

好的,所以这是交易。我正在开始计算材料科学的本科论文,我正在尝试将一些脚本放在一起,以帮助准备数据分析。

我一直在准备一个GAWK脚本,它基本上会获取一些数据(排列成4列)并抓住其中两个并在GNUPLOT中绘制它们。为了达到这个目的,我读入了具有多个时间步长及其相关数据的数据文件,将文件拆分为每个时间步的单个.dat文件。

从那里我只为GNUPLOT生成一个基本输入脚本,并绘制数据文件中出现的每个时间步。

问题在于,由于某种原因,所有生成的图都是完全相同的图(在这种情况下始终是第一次),但它们被保存为正确的时间步。

我已经完成并跟踪整个脚本中的每个变量/文件名,最后确定问题是以某种方式从脚本调用GNUPLOT。我拿出了我的系统命令并编写了一个简短的bash脚本,它从for循环调用gnuplot:

#!/bin/bash
for file in ./*gnu
do
   gnuplot $file
done

这仍然会导致所有情节都相同的问题。然后,我从包含.gnu文件的目录中的命令行运行命令gnuplot * gnu,它运行正常。

我想我只是想知道是否有一些我需要冲洗的缓冲区或者我是否只是缺少一些东西?

GAWK脚本如下。我仍然是新手,所以如果你想以一些建设性的批评评论剧本,我也会非常感激。

#!/opt/local/bin/gawk -v inputf=$1 -f                                                   

# Write gnuplot files and plot RDF data                                                 
function plot_rdf(timestep, Load_RDF_dat)
{
# Set number of digits in filenames to 6 so data is organized                           
    if (timestep < 10){
        pad_timestep="00000"timestep;
    }
    else if (timestep < 100){
        pad_timestep="0000"timestep;
    }
    else if (timestep < 1000){
        pad_timestep="000"timestep;
    }
    else if (timestep < 10000){
        pad_timestep="00"timestep;
    }
    else if (timestep < 100000){
        pad_timestep="0"timestep;
    }
    else{
        pad_timestep=timestep;
    }

# Give output filenames                                                                 
       gnu_file="plot_RDF_"pad_timestep".gnu";
       png_file="RDF_"pad_timestep".png";

# Create input files for gnuplot                                                        
       print "set output \""png_file"\"" >> gnu_file;
       print "set terminal png" >> gnu_file;
       print "plot './"Load_RDF_dat"' u 1:2" >> gnu_file;
       close(gnu_file);
       system("gnuplot "gnu_file);
}


# Main part of script                                                                   
{
# Parse the RDF data and save it to GNUPLOT readable files                              
    while(getline < inputf){
       if ($1 == "#"){
           # skips the three commented header lines                                     
           next;
       }
       else if (NF == 2){
           timestep=$1;
           bin_num=$2;
           print "Reading timestep "timestep;
           RDF_dat="RDF_"timestep".dat";
           next;
       }
       else if (NF == 4){
           print $2" "$3 >> RDF_dat;
           if ($1 == bin_num){
               plot_rdf(timestep, RDF_dat);
               close(RDF_dat);
           }
           next;
       }
    }
    close(inputf);
    close(RDF_dat);
 }

我正在阅读的数据文件的片段是:

# Time-averaged data for fix rdf
# TimeStep Number-of-rows
# Row c_allrdf[1] c_allrdf[2] c_allrdf[3]
500 100
1 0.005 0 0
2 0.015 0 0
3 0.025 0 0
4 0.035 0 0
5 0.045 0 0
6 0.055 1.16597 0.00133333
7 0.065 2.08865 0.00466667
8 0.075 1.56958 0.008
9 0.085 0.733433 0.01
10 0.095 0.587288 0.012
600 100
1 0.005 0 0
2 0.015 0 0
3 0.025 2.79219 0.000666667
4 0.035 2.86766 0.002
5 0.045 0 0.002
6 0.055 0.582985 0.00266667
7 0.065 2.08865 0.006
8 0.075 0.62783 0.00733333
9 0.085 0.488955 0.00866667
10 0.095 1.17458 0.0126667

每个时间步长部分通常有100组数据,但我想我会缩短这里只是为了让你明白这一点。

2 个答案:

答案 0 :(得分:0)

我不确定我是否可以回答你的问题 - 但是,我会说当我稍微修改你的数据文件时,它(似乎)对我来说效果很好。

这是我修改过的数据文件版本:

# Time-averaged data for fix rdf
# TimeStep Number-of-rows
# Row c_allrdf[1] c_allrdf[2] c_allrdf[3]
500 100
1 0.005 0 0
2 0.015 0 0
3 0.025 0 0
4 0.035 0 0
5 0.045 0 0
6 0.055 1.16597 0.00133333
7 0.065 2.08865 0.00466667
8 0.075 1.56958 0.008
9 0.085 0.733433 0.01
10 0.095 0.587288 0.012
100 0.095 0.56 0.014     #<-added this line
600 100
1 0.005 0 0
2 0.015 0 0
3 0.025 2.79219 0.000666667
4 0.035 2.86766 0.002
5 0.045 0 0.002
6 0.055 0.582985 0.00266667
7 0.065 2.08865 0.006
8 0.075 0.62783 0.00733333
9 0.085 0.488955 0.00866667
10 0.095 1.17458 0.0126667
100 0.095 1.179 0.12      #<-added this line

由于以下几行,这些行是“触发”gnuplot绘图功能所必需的:

   if ($1 == bin_num){
       plot_rdf(timestep, RDF_dat);
       close(RDF_dat);
   }

由于bin_num取自“标题”中的第二个字段。 (例如600 100)。

我不确定您是否在完整数据文件中正确设置了。另外,我将脚本称为:

gawk -f test.awk -v inputf=test.dat test.dat

在开始时完全忽略了你的shebang,但我读过很多系统都无法正确地拆分它们。

最后,你有什么版本的gnuplot?如果你有4.6,你可以放弃很多这样的痛苦,几乎完全跳过gawk脚本并用一个更简单的脚本替换它。

答案 1 :(得分:0)

正如mgilson所说,由于没有$1 == bin_num,你可能无法调用plot_rdf。请注意,在命令行上使用数据文件名调用awk可以很容易地使用awk的内置文件读取循环。以下重写awk程序说明了这一点。另请注意:
•在两个地方使用>代替>> •在运行gnuplot之前关闭RDF_dat,而不是在执行后 •使用pad_timestep = sprintf("%06d", timestep);而不是笨拙的if语句系列

对于以下内容,我将程序放入文件so-gnuplot-awk,数据按原样放入文件data-so-gnuplot,并通过

调用程序
awk -f so-gnuplot-awk data-so-gnuplot

程序:

# Parse the RDF data and save it to GNUPLOT readable files
BEGIN { dopen=0 }

NF==2 {
    if (dopen) plot_rdf(timestep, RDF_dat);
    timestep = $1;
    print "Reading timestep "timestep;
    RDF_dat="RDF_"timestep".dat";
    printf "" > RDF_dat     # Init empty file
    dopen = 1;
}

NF == 4 {  if (dopen) print $2" "$3 >> RDF_dat; }

# Write gnuplot files and plot RDF data
function plot_rdf(timestep, Load_RDF_dat) {
# Set output filenames & create gnuplot command file
    pad_timestep = sprintf("%06d", timestep);
    gnu_file="plot_RDF_"pad_timestep".gnu";
    png_file="RDF_"pad_timestep".png";
    print "set output \""png_file"\"" > gnu_file; # Use > first
    print "set terminal png" >> gnu_file;
    print "plot './"Load_RDF_dat"' u 1:2" >> gnu_file;
    close(gnu_file);
    close(RDF_dat);
    print "Plotting with "RDF_dat" into "png_file
    system("gnuplot "gnu_file);
    dopen=0
}

END { if (dopen) plot_rdf(timestep, RDF_dat); }