とある文以降のデータを取得する ( retrieveData__afterStatement.py )¶
やりたいこと¶
定められた様式で出力結果がテキスト出力 された際、そこからデータを抜き出したい.
様式が決まっているため、データ格納位置が、 ”とあるキーフレーズ” からわかるとする.
正規表現で判別 し、一致する範囲のデータを抜き取る.
使い方と引数について¶
使い方¶
関数をimportし、以下引数を与える.
imort nkUtilities.retrieveData__afterStatement as ras inpFile = "dat/sample.inp" expr_from = r"#\s*e-lower" expr_to = r"^\s*$" ret = ras.retrieveData__afterStatement( inpFile=inpFile, expr_from=expr_from, expr_to=expr_to )
引数¶
Items |
Type |
Description |
---|---|---|
inpFile |
string |
入力ファイル名 |
outFile |
string |
出力ファイル名 |
expr_from |
(raw) string |
データ開始位置( の1行前:@sendline=1 )の正規表現. |
expr_to |
(raw) string |
データ終了位置の正規表現. |
sendline |
integer |
開始位置からの送り行数.default=1(次の行から読み取り開始). |
fmt |
string |
outFileに記載時の桁数フォーマット (default='%15.8e'). |
names |
list of string |
各列の変数名.(default=None) |
コードと実行結果¶
コード ( retrieveData__afterStatement.py )¶
import os, sys, re
import numpy as np
# ========================================================= #
# === retrieveData__afterStatement.py === #
# ========================================================= #
def retrieveData__afterStatement( inpFile=None, outFile=None, expr_from=None, expr_to=None, \
sendline=1, fmt="%15.8e", names=None ):
x_, y_, z_ = 0, 1, 2
# ------------------------------------------------- #
# --- [1] arguments check --- #
# ------------------------------------------------- #
if ( inpFile is None ): sys.exit( "[retrieveData__afterStatement.py] inpFile == ???" )
if ( expr_from is None ): sys.exit( "[retrieveData__afterStatement.py] expr_from == ???" )
# ------------------------------------------------- #
# --- [2] open file and read lines --- #
# ------------------------------------------------- #
with open( inpFile, "r" ) as f:
lines = f.readlines()
# ------------------------------------------------- #
# --- [3] retrive start & end line --- #
# ------------------------------------------------- #
found = False
for ik,line in enumerate(lines):
if ( found is False ):
match = re.match( expr_from, line )
if ( match ):
iFrom = ik+sendline
iTo = len( lines )
found = True
elif ( expr_to is not None ):
match = re.match( expr_to , line )
if ( match ):
iTo = ik+1
break
else:
pass # to last line of the file.
if ( found is False ):
print( "[retrieveData__afterStatement.py] Cannot find reg-exp {} in this file... ".format( expr_from ) )
sys.exit()
# ------------------------------------------------- #
# --- [4] extract data --- #
# ------------------------------------------------- #
Data = np.loadtxt( lines[ iFrom:iTo ] )
# ------------------------------------------------- #
# --- [5] save in a file & return --- #
# ------------------------------------------------- #
if ( outFile is not None ):
with open( outFile, "w" ) as f:
if ( names is None ):
names = [ "x{0}".format(ik+1) for ik in range( Data.shape[1] ) ]
f.write( "# " + " ".join( names ) + "\n" )
f.write( "# " + " ".join( [ str(val) for val in Data.shape ] ) + "\n" )
f.write( "# " + " ".join( [ str(val) for val in Data.shape ] ) + "\n" )
np.savetxt( f, Data, fmt=fmt )
print( "[retrieveData__afterStatement.py] save in a file :: {}".format( outFile ) )
return( Data )
# ========================================================= #
# === Execution of Pragram === #
# ========================================================= #
if ( __name__=="__main__" ):
inpFile = "test/retrieveData__afterStatement/sample.inp"
outFile = "test/retrieveData__afterStatement/sample.out"
expr_from = r"#\s*e-lower"
expr_to = r"^\s*$"
ret = retrieveData__afterStatement( inpFile=inpFile, outFile=outFile, \
expr_from=expr_from, expr_to=expr_to )
サンプル入力ファイル ( e.g. PHITSの出力ファイル )¶
PHITSの出力ファイル
データを抜き出したいが、色んな情報が入っている.
とある行以降、空白行までを抜き出す.
[ T-Track ]
title = [t-track] in region mesh
mesh = reg # mesh type is region-wise
reg = 341
volume # combined, lattice or level structure
non reg vol # reg definition
1 341 1.0000E+00 # 341
e-type = 2 # e-mesh is linear given by emin, emax and ne
emin = 0.000000 # minimum value of e-mesh points
emax = 50.00000 # maximum value of e-mesh points
# edel = 0.5000000 # mesh width of e-mesh points
ne = 100 # number of e-mesh points
# data = ( e(i), i = 1, ne + 1 )
# 0.00000E+00 5.00000E-01 1.00000E+00 1.50000E+00 2.00000E+00
# 2.50000E+00 3.00000E+00 3.50000E+00 4.00000E+00 4.50000E+00
# 5.00000E+00 5.50000E+00 6.00000E+00 6.50000E+00 7.00000E+00
# 7.50000E+00 8.00000E+00 8.50000E+00 9.00000E+00 9.50000E+00
# 1.00000E+01 1.05000E+01 1.10000E+01 1.15000E+01 1.20000E+01
# 1.25000E+01 1.30000E+01 1.35000E+01 1.40000E+01 1.45000E+01
# 1.50000E+01 1.55000E+01 1.60000E+01 1.65000E+01 1.70000E+01
# 1.75000E+01 1.80000E+01 1.85000E+01 1.90000E+01 1.95000E+01
# 2.00000E+01 2.05000E+01 2.10000E+01 2.15000E+01 2.20000E+01
# 2.25000E+01 2.30000E+01 2.35000E+01 2.40000E+01 2.45000E+01
# 2.50000E+01 2.55000E+01 2.60000E+01 2.65000E+01 2.70000E+01
# 2.75000E+01 2.80000E+01 2.85000E+01 2.90000E+01 2.95000E+01
# 3.00000E+01 3.05000E+01 3.10000E+01 3.15000E+01 3.20000E+01
# 3.25000E+01 3.30000E+01 3.35000E+01 3.40000E+01 3.45000E+01
# 3.50000E+01 3.55000E+01 3.60000E+01 3.65000E+01 3.70000E+01
# 3.75000E+01 3.80000E+01 3.85000E+01 3.90000E+01 3.95000E+01
# 4.00000E+01 4.05000E+01 4.10000E+01 4.15000E+01 4.20000E+01
# 4.25000E+01 4.30000E+01 4.35000E+01 4.40000E+01 4.45000E+01
# 4.50000E+01 4.55000E+01 4.60000E+01 4.65000E+01 4.70000E+01
# 4.75000E+01 4.80000E+01 4.85000E+01 4.90000E+01 4.95000E+01
# 5.00000E+01
unit = 2 # unit is [1/cm^2/MeV/source]
material = all # (D=all) number of specific material
axis = eng # axis of output
file = out/fluence_energy.dat # file name of output for the above axis
part = photon
# kf/name : 22
x-txt = energy [MeV]
y-txt = track length [photons m/MeV/s]
epsout = 1 # (D=0) generate eps file by ANGEL
# used : main ( %) temp ( %) total ( %)
# memory : 109 ( 0) 0 ( 0) 109 ( 0)
#------------------------------------------------------------------------------
#newpage:
# no. = 1 reg = 341
x: energy [MeV]
y: track length [photons m/MeV/s]
p: xlin ylog afac(0.8) form(0.9)
h: n x y(photon ),hh0l n
# e-lower e-upper photon r.err
0.0000E+00 5.0000E-01 1.7541E+08 0.0038
5.0000E-01 1.0000E+00 1.9372E+08 0.0027
1.0000E+00 1.5000E+00 1.2231E+08 0.0033
1.5000E+00 2.0000E+00 8.7562E+07 0.0038
2.0000E+00 2.5000E+00 6.8225E+07 0.0043
2.5000E+00 3.0000E+00 5.4746E+07 0.0048
3.0000E+00 3.5000E+00 4.5100E+07 0.0053
3.5000E+00 4.0000E+00 3.9068E+07 0.0057
4.0000E+00 4.5000E+00 3.3838E+07 0.0069
4.5000E+00 5.0000E+00 2.9659E+07 0.0065
5.0000E+00 5.5000E+00 2.6662E+07 0.0069
5.5000E+00 6.0000E+00 2.3594E+07 0.0073
6.0000E+00 6.5000E+00 2.1392E+07 0.0076
6.5000E+00 7.0000E+00 1.9599E+07 0.0080
7.0000E+00 7.5000E+00 1.7951E+07 0.0083
7.5000E+00 8.0000E+00 1.6421E+07 0.0087
8.0000E+00 8.5000E+00 1.5214E+07 0.0091
8.5000E+00 9.0000E+00 1.4158E+07 0.0094
9.0000E+00 9.5000E+00 1.3199E+07 0.0097
9.5000E+00 1.0000E+01 1.2306E+07 0.0100
1.0000E+01 1.0500E+01 1.1437E+07 0.0104
1.0500E+01 1.1000E+01 1.0585E+07 0.0108
1.1000E+01 1.1500E+01 1.0092E+07 0.0111
1.1500E+01 1.2000E+01 9.4285E+06 0.0115
1.2000E+01 1.2500E+01 8.9805E+06 0.0118
1.2500E+01 1.3000E+01 8.6547E+06 0.0120
1.3000E+01 1.3500E+01 8.1341E+06 0.0124
1.3500E+01 1.4000E+01 7.7061E+06 0.0127
1.4000E+01 1.4500E+01 7.3546E+06 0.0130
1.4500E+01 1.5000E+01 6.8672E+06 0.0134
1.5000E+01 1.5500E+01 6.5886E+06 0.0137
1.5500E+01 1.6000E+01 6.2887E+06 0.0140
1.6000E+01 1.6500E+01 5.9878E+06 0.0144
1.6500E+01 1.7000E+01 5.9499E+06 0.0144
1.7000E+01 1.7500E+01 5.4845E+06 0.0150
1.7500E+01 1.8000E+01 5.2465E+06 0.0154
1.8000E+01 1.8500E+01 4.9685E+06 0.0158
1.8500E+01 1.9000E+01 4.7009E+06 0.0162
1.9000E+01 1.9500E+01 4.6726E+06 0.0163
1.9500E+01 2.0000E+01 4.5842E+06 0.0165
2.0000E+01 2.0500E+01 4.2835E+06 0.0170
2.0500E+01 2.1000E+01 4.2751E+06 0.0170
2.1000E+01 2.1500E+01 3.9268E+06 0.0178
2.1500E+01 2.2000E+01 3.7662E+06 0.0181
2.2000E+01 2.2500E+01 3.8050E+06 0.0181
2.2500E+01 2.3000E+01 3.6298E+06 0.0185
2.3000E+01 2.3500E+01 3.4209E+06 0.0190
2.3500E+01 2.4000E+01 3.3696E+06 0.0192
2.4000E+01 2.4500E+01 3.2558E+06 0.0195
2.4500E+01 2.5000E+01 3.0976E+06 0.0200
2.5000E+01 2.5500E+01 2.9256E+06 0.0206
2.5500E+01 2.6000E+01 2.8365E+06 0.0209
2.6000E+01 2.6500E+01 2.8261E+06 0.0210
2.6500E+01 2.7000E+01 2.7153E+06 0.0214
2.7000E+01 2.7500E+01 2.5634E+06 0.0220
2.7500E+01 2.8000E+01 2.5946E+06 0.0219
2.8000E+01 2.8500E+01 2.4797E+06 0.0224
2.8500E+01 2.9000E+01 2.2986E+06 0.0232
2.9000E+01 2.9500E+01 2.2794E+06 0.0233
2.9500E+01 3.0000E+01 2.2563E+06 0.0235
3.0000E+01 3.0500E+01 2.0940E+06 0.0243
3.0500E+01 3.1000E+01 2.0134E+06 0.0248
3.1000E+01 3.1500E+01 2.0208E+06 0.0248
3.1500E+01 3.2000E+01 1.9577E+06 0.0252
3.2000E+01 3.2500E+01 1.9208E+06 0.0254
3.2500E+01 3.3000E+01 1.7793E+06 0.0264
3.3000E+01 3.3500E+01 1.8017E+06 0.0263
3.3500E+01 3.4000E+01 1.6350E+06 0.0276
3.4000E+01 3.4500E+01 1.6176E+06 0.0277
3.4500E+01 3.5000E+01 1.5155E+06 0.0286
3.5000E+01 3.5500E+01 1.4521E+06 0.0292
3.5500E+01 3.6000E+01 1.4162E+06 0.0296
3.6000E+01 3.6500E+01 1.4010E+06 0.0298
3.6500E+01 3.7000E+01 1.2451E+06 0.0316
3.7000E+01 3.7500E+01 1.1945E+06 0.0322
3.7500E+01 3.8000E+01 1.1806E+06 0.0324
3.8000E+01 3.8500E+01 1.0848E+06 0.0338
3.8500E+01 3.9000E+01 1.0040E+06 0.0352
3.9000E+01 3.9500E+01 9.5567E+05 0.0360
3.9500E+01 4.0000E+01 8.9961E+05 0.0372
4.0000E+01 4.0500E+01 8.3856E+05 0.0385
4.0500E+01 4.1000E+01 7.5149E+05 0.0406
4.1000E+01 4.1500E+01 6.6715E+05 0.0431
4.1500E+01 4.2000E+01 5.7767E+05 0.0464
4.2000E+01 4.2500E+01 4.9806E+05 0.0499
4.2500E+01 4.3000E+01 4.2218E+05 0.0542
4.3000E+01 4.3500E+01 3.1028E+05 0.0632
4.3500E+01 4.4000E+01 2.1593E+05 0.0758
4.4000E+01 4.4500E+01 1.3146E+05 0.0971
4.4500E+01 4.5000E+01 2.4793E+04 0.2236
4.5000E+01 4.5500E+01 0.0000E+00 0.0000
4.5500E+01 4.6000E+01 0.0000E+00 0.0000
4.6000E+01 4.6500E+01 0.0000E+00 0.0000
4.6500E+01 4.7000E+01 0.0000E+00 0.0000
4.7000E+01 4.7500E+01 0.0000E+00 0.0000
4.7500E+01 4.8000E+01 0.0000E+00 0.0000
4.8000E+01 4.8500E+01 0.0000E+00 0.0000
4.8500E+01 4.9000E+01 0.0000E+00 0.0000
4.9000E+01 4.9500E+01 0.0000E+00 0.0000
4.9500E+01 5.0000E+01 0.0000E+00 0.0000
# sum over 1.2711E+07
'no. = 1, reg = 341'
msuc: {\huge [t-track] in region mesh}
msdr: {\it plotted by \ANGEL \version}
msdl: {\it calculated by \PHITS 3.28}
wt: s(0.7)
\vspace{-3}
vol &=& 1.0000E+00 [cm^3]
e:
# Information for Restart Calculation
# This calculation was newly started
# istdev = 2 # 1:Batch variance, 2:History variance
# resc2 = 8.00999999997049369E-10 # Total source weight or Total source weight / maxcas
# resc3 = 1.00000000000000000E+06 # Total history number or Total batch number
# maxcas = 100000 # History / Batch, only used for istdev=1
# bitrseed = 0101101000001110011101111011110101001001010001111010111000101101 # bit data of rseed
実行結果 ( dat/sample.out )¶
# x1 x2 x3 x4
# 100 4
# 100 4
0.00000000e+00 5.00000000e-01 1.75410000e+08 3.80000000e-03
5.00000000e-01 1.00000000e+00 1.93720000e+08 2.70000000e-03
1.00000000e+00 1.50000000e+00 1.22310000e+08 3.30000000e-03
1.50000000e+00 2.00000000e+00 8.75620000e+07 3.80000000e-03
2.00000000e+00 2.50000000e+00 6.82250000e+07 4.30000000e-03
2.50000000e+00 3.00000000e+00 5.47460000e+07 4.80000000e-03
3.00000000e+00 3.50000000e+00 4.51000000e+07 5.30000000e-03
3.50000000e+00 4.00000000e+00 3.90680000e+07 5.70000000e-03
4.00000000e+00 4.50000000e+00 3.38380000e+07 6.90000000e-03
4.50000000e+00 5.00000000e+00 2.96590000e+07 6.50000000e-03
5.00000000e+00 5.50000000e+00 2.66620000e+07 6.90000000e-03
5.50000000e+00 6.00000000e+00 2.35940000e+07 7.30000000e-03
6.00000000e+00 6.50000000e+00 2.13920000e+07 7.60000000e-03
6.50000000e+00 7.00000000e+00 1.95990000e+07 8.00000000e-03
7.00000000e+00 7.50000000e+00 1.79510000e+07 8.30000000e-03
7.50000000e+00 8.00000000e+00 1.64210000e+07 8.70000000e-03
8.00000000e+00 8.50000000e+00 1.52140000e+07 9.10000000e-03
8.50000000e+00 9.00000000e+00 1.41580000e+07 9.40000000e-03
9.00000000e+00 9.50000000e+00 1.31990000e+07 9.70000000e-03
9.50000000e+00 1.00000000e+01 1.23060000e+07 1.00000000e-02
1.00000000e+01 1.05000000e+01 1.14370000e+07 1.04000000e-02
1.05000000e+01 1.10000000e+01 1.05850000e+07 1.08000000e-02
1.10000000e+01 1.15000000e+01 1.00920000e+07 1.11000000e-02
1.15000000e+01 1.20000000e+01 9.42850000e+06 1.15000000e-02
1.20000000e+01 1.25000000e+01 8.98050000e+06 1.18000000e-02
1.25000000e+01 1.30000000e+01 8.65470000e+06 1.20000000e-02
1.30000000e+01 1.35000000e+01 8.13410000e+06 1.24000000e-02
1.35000000e+01 1.40000000e+01 7.70610000e+06 1.27000000e-02
1.40000000e+01 1.45000000e+01 7.35460000e+06 1.30000000e-02
1.45000000e+01 1.50000000e+01 6.86720000e+06 1.34000000e-02
1.50000000e+01 1.55000000e+01 6.58860000e+06 1.37000000e-02
1.55000000e+01 1.60000000e+01 6.28870000e+06 1.40000000e-02
1.60000000e+01 1.65000000e+01 5.98780000e+06 1.44000000e-02
1.65000000e+01 1.70000000e+01 5.94990000e+06 1.44000000e-02
1.70000000e+01 1.75000000e+01 5.48450000e+06 1.50000000e-02
1.75000000e+01 1.80000000e+01 5.24650000e+06 1.54000000e-02
1.80000000e+01 1.85000000e+01 4.96850000e+06 1.58000000e-02
1.85000000e+01 1.90000000e+01 4.70090000e+06 1.62000000e-02
1.90000000e+01 1.95000000e+01 4.67260000e+06 1.63000000e-02
1.95000000e+01 2.00000000e+01 4.58420000e+06 1.65000000e-02
2.00000000e+01 2.05000000e+01 4.28350000e+06 1.70000000e-02
2.05000000e+01 2.10000000e+01 4.27510000e+06 1.70000000e-02
2.10000000e+01 2.15000000e+01 3.92680000e+06 1.78000000e-02
2.15000000e+01 2.20000000e+01 3.76620000e+06 1.81000000e-02
2.20000000e+01 2.25000000e+01 3.80500000e+06 1.81000000e-02
2.25000000e+01 2.30000000e+01 3.62980000e+06 1.85000000e-02
2.30000000e+01 2.35000000e+01 3.42090000e+06 1.90000000e-02
2.35000000e+01 2.40000000e+01 3.36960000e+06 1.92000000e-02
2.40000000e+01 2.45000000e+01 3.25580000e+06 1.95000000e-02
2.45000000e+01 2.50000000e+01 3.09760000e+06 2.00000000e-02
2.50000000e+01 2.55000000e+01 2.92560000e+06 2.06000000e-02
2.55000000e+01 2.60000000e+01 2.83650000e+06 2.09000000e-02
2.60000000e+01 2.65000000e+01 2.82610000e+06 2.10000000e-02
2.65000000e+01 2.70000000e+01 2.71530000e+06 2.14000000e-02
2.70000000e+01 2.75000000e+01 2.56340000e+06 2.20000000e-02
2.75000000e+01 2.80000000e+01 2.59460000e+06 2.19000000e-02
2.80000000e+01 2.85000000e+01 2.47970000e+06 2.24000000e-02
2.85000000e+01 2.90000000e+01 2.29860000e+06 2.32000000e-02
2.90000000e+01 2.95000000e+01 2.27940000e+06 2.33000000e-02
2.95000000e+01 3.00000000e+01 2.25630000e+06 2.35000000e-02
3.00000000e+01 3.05000000e+01 2.09400000e+06 2.43000000e-02
3.05000000e+01 3.10000000e+01 2.01340000e+06 2.48000000e-02
3.10000000e+01 3.15000000e+01 2.02080000e+06 2.48000000e-02
3.15000000e+01 3.20000000e+01 1.95770000e+06 2.52000000e-02
3.20000000e+01 3.25000000e+01 1.92080000e+06 2.54000000e-02
3.25000000e+01 3.30000000e+01 1.77930000e+06 2.64000000e-02
3.30000000e+01 3.35000000e+01 1.80170000e+06 2.63000000e-02
3.35000000e+01 3.40000000e+01 1.63500000e+06 2.76000000e-02
3.40000000e+01 3.45000000e+01 1.61760000e+06 2.77000000e-02
3.45000000e+01 3.50000000e+01 1.51550000e+06 2.86000000e-02
3.50000000e+01 3.55000000e+01 1.45210000e+06 2.92000000e-02
3.55000000e+01 3.60000000e+01 1.41620000e+06 2.96000000e-02
3.60000000e+01 3.65000000e+01 1.40100000e+06 2.98000000e-02
3.65000000e+01 3.70000000e+01 1.24510000e+06 3.16000000e-02
3.70000000e+01 3.75000000e+01 1.19450000e+06 3.22000000e-02
3.75000000e+01 3.80000000e+01 1.18060000e+06 3.24000000e-02
3.80000000e+01 3.85000000e+01 1.08480000e+06 3.38000000e-02
3.85000000e+01 3.90000000e+01 1.00400000e+06 3.52000000e-02
3.90000000e+01 3.95000000e+01 9.55670000e+05 3.60000000e-02
3.95000000e+01 4.00000000e+01 8.99610000e+05 3.72000000e-02
4.00000000e+01 4.05000000e+01 8.38560000e+05 3.85000000e-02
4.05000000e+01 4.10000000e+01 7.51490000e+05 4.06000000e-02
4.10000000e+01 4.15000000e+01 6.67150000e+05 4.31000000e-02
4.15000000e+01 4.20000000e+01 5.77670000e+05 4.64000000e-02
4.20000000e+01 4.25000000e+01 4.98060000e+05 4.99000000e-02
4.25000000e+01 4.30000000e+01 4.22180000e+05 5.42000000e-02
4.30000000e+01 4.35000000e+01 3.10280000e+05 6.32000000e-02
4.35000000e+01 4.40000000e+01 2.15930000e+05 7.58000000e-02
4.40000000e+01 4.45000000e+01 1.31460000e+05 9.71000000e-02
4.45000000e+01 4.50000000e+01 2.47930000e+04 2.23600000e-01
4.50000000e+01 4.55000000e+01 0.00000000e+00 0.00000000e+00
4.55000000e+01 4.60000000e+01 0.00000000e+00 0.00000000e+00
4.60000000e+01 4.65000000e+01 0.00000000e+00 0.00000000e+00
4.65000000e+01 4.70000000e+01 0.00000000e+00 0.00000000e+00
4.70000000e+01 4.75000000e+01 0.00000000e+00 0.00000000e+00
4.75000000e+01 4.80000000e+01 0.00000000e+00 0.00000000e+00
4.80000000e+01 4.85000000e+01 0.00000000e+00 0.00000000e+00
4.85000000e+01 4.90000000e+01 0.00000000e+00 0.00000000e+00
4.90000000e+01 4.95000000e+01 0.00000000e+00 0.00000000e+00
4.95000000e+01 5.00000000e+01 0.00000000e+00 0.00000000e+00