Gnuplot:透過讀取列標題從不同目錄的 *txt 檔案進行繪圖

Gnuplot:透過讀取列標題從不同目錄的 *txt 檔案進行繪圖

我的資料夾結構看起來有點像這樣

$ tree
.
├── Original_folder
│   └── cat.txt
├── folderCD
│   └── cat.txt
├── folderGK
│   └── cat.txt
├── folderFE
    └── cat.txt

每個 cat.txt 檔案在開始列標題之前都有 5 行。樣本cat.txt檔案是這樣的

Version LRv1.10.0
Build date 2017-12-06
MOL-calc
PRESSURE
!                       
      Time[s]     InletT[K]   InletP[Pa]   O2_GasOut     C_GasOut
       100         0.000885   1000000       0.0007       0.2111
and so on....

我想繪製第一列以及列標題帶有關鍵字“_GasOut”的列。 (帶有此關鍵字的標題數量未知,對於每一列,我希望有一個單獨的圖表)。此外,對於來自folderCD、folderGK、folderFE……等的所有繪圖,Original_folder 的圖形結果應繪製在同一圖表中。

相應的圖表應保存在相應的資料夾中,標題與列標題相同。每個圖中應該有兩個圖例,一個是“original_folder”,另一個是“folderCD/folderGK/…”

我將 Original_folder 的所有輸出繪圖命令放在一個 txt 檔案中,並將所有其他資料夾的繪圖命令放入另一個 txt 檔案中。之後我找不到繼續前進的方法..我怎樣才能為所有其他情況做到這一點?以及如何將列標題設為標題?

最新更新

for dir in folder* ; do
 echo "Preparing Post_processing files for ${dir}"
 mkdir "$dir"/Post_processing                        
 gawk -F  $'\t' '                                    
    /_GasOut/{                                      
       for(f=1;f<=NF;f++){                          
          hdr=$f                                    
         colhdr[f]=hdr                              
         if(index(hdr,"_GasOut"))wanted[f]=1
       }
    }
    ENDFILE{                                         

          print "reset\nset terminal pngcairo size 1024,768\nset encoding utf8\nset termoption dash\nset termopt enhanced"  
          print "set key top right"                                                                                         
          print "set xlabel '"'Time[s]'"';"    
       for(f in wanted){                           
          if(length(cmds)) cmds = cmds ",\n"
          hdr = colhdr[f]                        
          gsub(/^[[:space:]]+|[[:space:]]+$/,"",hdr)  
          printf("set ylabel '"'%s'"';\n",hdr)
          printf("set output '"'"$dir/Post_processing"/%s.png'"'\n",hdr)
          cmds = cmds "plot ""\"" FILENAME "\" using 1:" f " with lines" ","
          #print "plot " FILENAME using 1:" f " with lines" ",""
          cmds=cmds"'"'Original_folder/cat.txt'"' using 1:" f " with lines"        

        }      
       delete wanted  
    }
    END{              
       print cmds     
    }
    ' "$dir"/cat.txt>"$dir"/plot.gpl

   gnuplot "$dir"/plot.gpl
done

電流輸出是這樣的

reset
set terminal pngcairo size 1024,768
set encoding utf8
set termoption dash
set termopt enhanced
set xlabel 'Time[s]';
set ylabel 'H2_GasOut';
set output 'folderCD/Post_processing/H2_GasOut.png'
set ylabel 'O2_GasOut';
set output 'folderGK/Post_processing/O2_GasOut.png'
set ylabel 'H2O_GasOut';
set output 'folderFE/Post_processing/H2O_GasOut.png'
plot "folderCD/cat.txt" using 1:28 with lines,'Original_folder/cat.txt' using 1:28 with lines,
plot "folderGK/cat.txt" using 1:29 with lines,'Original_folder/cat.txt' using 1:29 with lines,
plot "folderGK/cat.txt" using 1:30 with lines,'Original_folder/cat.txt' using 1:30 with lines

所需輸出

reset
set terminal pngcairo size 1024,768
set encoding utf8
set termoption dash
set termopt enhanced
set xlabel 'Time[s]';
set ylabel 'H2_GasOut';
set output 'folderCD/Post_processing/H2_GasOut.png'
plot "folderCD/cat.txt" using 1:28 with lines,'Original_RedKinMec/cat.txt' using 1:28 with lines,
set ylabel 'O2_GasOut';
set output 'folderGK/Post_processing/O2_GasOut.png'
plot "folderGK/cat.txt" using 1:29 with lines,'Original_folder/cat.txt' using 1:29 with lines,
set ylabel 'H2O_GasOut';
set output 'folderFE/Post_processing/H2O_GasOut.png'
plot "folderGK/cat.txt" using 1:30 with lines,'Original_folder/cat.txt' using 1:30 with lines

有這樣的輸出也很好

set terminal pngcairo size 1024,768
   set encoding utf8
   set termopt dash
   set termopt enhanced
   set key top right
   set xlabel "Time[s]"
   set ylabel "O2_GasOut"
   set output "Post_processing/O2_GasOut.png"
   plot "folder1/cat.txt" using 1:22 with lines,\
   plot "folder2/cat.txt" using 1:22 with lines,\
   plot "folder3/cat.txt" using 1:22 with lines,\
   plot "folder4/cat.txt" using 1:22 with lines
   set ylabel "H2O_GasOut"
   set output "Post_processing/H2O_GasOut.png"
   plot "folder1/cat.txt" using 1:23 with lines,\
   plot "folder2/cat.txt" using 1:23 with lines,\
   plot "folder3/cat.txt" using 1:23 with lines,
   plot "folder4/cat.txt" using 1:23 with lines
   set ylabel "H2_GasOut"
   set output "Post_processing/H2_GasOut.png"
   plot "folder1/cat.txt" using 1:24 with lines,\
   plot "folder2/cat.txt" using 1:24 with lines,\
   plot "folder3/cat.txt" using 1:24 with lines,\
   plot "folder4/cat.txt" using 1:24 with lines




N.B: folder numbers are not fixed.
I added one of the cat.txt file for reference. https://1drv.ms/t/s!Aoomvi55MLAQh1wMmpnPGnliFmgg 

答案1

首先讓我們拆分腳本,以便我們有一個bash腳本和一個awk腳本檔案。這樣,您將需要在腳本中減少轉義,並且可以使用- 選項bash將變數轉送到其中。awk-v

for dir in folder* ; do
  echo "Preparing Post_processing files for ${dir}"
  mkdir "${dir}"/Post_processing
  gawk -f make_gpl.awk -v dirname="${dir}" "${dir}"/cat.txt > "${dir}"/plot.gpl
  gnuplot "${dir}"/plot.gpl
done

現在腳本bash相當簡單。

腳本中的一些更正和簡化awk- 我希望評論能解釋得足夠好:

#inserted field separator definition into script
BEGIN { FS="\t" }
/_GasOut/{
   for(f=1;f<=NF;f++){
      # $a ~ "B" matches if string B is part of field $a
      # only these elements are taken to array colhdr
      if ($f ~ "_GasOut") colhdr[f]=$f
   }
}
ENDFILE{
      #split prints with newlines into separate splits for readability
      print "set terminal pngcairo size 1024,768
      print "set encoding utf8"
      print "set termopt dash"
      print "set termopt enhanced"  
      print "set key top right"                                                                                         
      print "set xlabel \"Time[s]\""
      #for loop only matches if element of array colhdr is set
      for(f in colhdr){
        #it looks like there are only preceding spaces
        gsub(/^ +/,"",colhdr[f])
        #printing strings only - no printf needed
        #escaping quotes if they need to be printed
        #removed semicolons and commas at end of plot command - newline will do
        print("set ylabel \""colhdr[f]"\"")
        print("set output \""dirname"/Post_processing/"colhdr[f]".png\"")
        print("plot \""FILENAME"\" using 1:"f" with lines")
      }      
}    

使用腳本和您建立的範例繪圖檔案cat.txt

set terminal pngcairo size 1024,768
set encoding utf8
set termopt dash
set termopt enhanced
set key top right
set xlabel "Time[s]"
set ylabel "O2_GasOut"
set output "folder1/Post_processing/O2_GasOut.png"
plot "folder1/cat.txt" using 1:22 with lines
set ylabel "H2O_GasOut"
set output "folder1/Post_processing/H2O_GasOut.png"
plot "folder1/cat.txt" using 1:23 with lines
set ylabel "H2_GasOut"
set output "folder1/Post_processing/H2_GasOut.png"
plot "folder1/cat.txt" using 1:24 with lines
set ylabel "N2_GasOut"
set output "folder1/Post_processing/N2_GasOut.png"
plot "folder1/cat.txt" using 1:25 with lines
set ylabel "NO_GasOut"
set output "folder1/Post_processing/NO_GasOut.png"
plot "folder1/cat.txt" using 1:26 with lines
set ylabel "NO2_GasOut"
set output "folder1/Post_processing/NO2_GasOut.png"
plot "folder1/cat.txt" using 1:27 with lines
set ylabel "N2O_GasOut"
set output "folder1/Post_processing/N2O_GasOut.png"
plot "folder1/cat.txt" using 1:28 with lines

請注意,圖中 y 標籤的格式可能是錯誤的,但我不確定所需的格式。_將使以下字元成為下標termopt enhanced。要使更多字元成為下標,請使用括號,例如C_6H_{12}O_6糖的分子式。

相關內容