好了,事情是这样的。我正在开始我的计算材料科学的本科生论文,我试图把一些脚本放在一起,以帮助准备数据分析。
我一直在准备一个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通常每个timestep部分有100组数据,但我想我在这里缩短一下,这样您就能明白了。
发布于 2012-09-25 08:54:56
正如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); }发布于 2012-09-25 07:57:48
我不确定我是否能回答您的问题--但是,我会说,当我稍微修改您的数据文件时,它(似乎)对我来说工作得很好。
以下是我修改后的数据文件版本:
# 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取自"header“中的第二个字段。(例如600 100)。
我不确定您是否在完整的数据文件中正确设置了它。另外,我将该脚本命名为:
gawk -f test.awk -v inputf=test.dat test.dat它在一开始完全忽略了你的shebang,但我读到过很多系统很难正确地将它们分开。
最后,您有哪个版本的gnuplot?如果你的版本是4.6,你可以放弃很多这样的痛苦,完全跳过gawk脚本,用一个更简单的脚本来代替它。
https://stackoverflow.com/questions/12573690
复制相似问题