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」キーワードがある列だけでなく、最初の列もプロットしたいと思います。 (このキーワードを含むヘッダーの数は不明です。各列に別々のチャートをしたいです。)また、Original_folderのグラフィック結果は、フォルダCD、フォルダGK、フォルダFE ...などのすべてのプロットに対して同じチャートになります。プロットする必要があります。

そのチャートは、列ヘッダーと同じタイトルでそのフォルダに保存する必要があります。各図には2つの凡例が必要です。 1つは「original_folder」、もう1つは「folderCD / folderGK / ...」です。

Original_folderのすべての出力描画コマンドを1つの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砂糖の分子式などの括弧を使用してください。

おすすめ記事