LoginSignup
0
1

More than 5 years have passed since last update.

Python > fileIO > Fortran出力のファイルからキーワードの値を取得する for bisphere.f

Last updated at Posted at 2018-01-03
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6

関連: Superposition T-matrix codes > bisphere.f > 2つの球の光散乱計算コード (analytical orientation averaging)
関連: bisphere.f > tool > calc_qext_bisphere_180104.py > v0.1-v0.3 > calculate Qext from the result (R1, R2, CEXT)

処理概要

下記のFortranコード出力ファイルからキーワード(e.g. R1, R2, CEXT)の値を読取る。

result.txt(一部)
This test result was computed on an IBM RISC model 397 workstation
for the current code setting.

R1= .5000D+01  R2= .5000D+01  R12= .2000D+02  LAM= .6283D+01
X12= .2000D+02  NODRT= 33
X1= .500000D+01 N1= .150000D+01 K1= .500000D-02 NODR(1)= 14
X2= .500000D+01 N2= .150000D+01 K2= .500000D-02 NODR(2)= 14
 CEXT= .589017D+03  CSCA= .567076D+03    W= .962749D+00  <COS>= .717082D+00
 TEST OF VAN DER MEE & HOVENIER IS SATISFIED

  S      ALPHA1      ALPHA2      ALPHA3      ALPHA4       BETA1       BETA2
  0     1.00000      .00000      .00000      .94160      .00000      .00000
  1     2.15125      .00000      .00000     2.21375      .00000      .00000
  2     2.99437     4.00361     3.83857     2.89376     -.13647      .11745
  3     3.13877     3.55605     3.63214     3.20301     -.11917      .00216
  4     3.32190     3.77498     3.68288     3.26877     -.15255      .10368
...

code v0.1

calc_qext_180104.py
import numpy as np

'''
v0.1 Jan. 04, 2018
  - add read_param_fromfile
'''


def read_param_fromfile(akey):
    # 1. find line
    keyline = []
    with open('result.txt') as fd:
        lines = fd.readlines()
        for aline in lines:
            if not akey + "=" in aline:
                continue
            keyline += [aline]
    if len(keyline) == 0:
        return None
    # 2. extract value next to the keyword
    found = False
    for elem in keyline[0].split(' '):
        if found:
            return elem
        if akey in elem:
            found = True
    return None  # just in case


res = read_param_fromfile("QEXT")
print(res)
res = read_param_fromfile("R1")
print(res)
res = read_param_fromfile("R2")
print(res)
res = read_param_fromfile("R12")
print(res)
res = read_param_fromfile("LAM")
print(res)
print('---')
res = read_param_fromfile("CEXT")
print(res)

run
$ python3 calc_qext_180104.py 
None
.5000D+01
.5000D+01
.2000D+02
.6283D+01

---
.589017D+03
  • キーワードが見つからない値はNoneとしている
  • 行末の値の場合、改行マークがついてくる
    • 今は対応不要なので、このままとする
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1