ADDA > tool > average_Qxxx_171216.py > v0.1,v0.2 > average for Qext and Qabs for various values of the ADDA parameters

```txt:動作環境
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
```

This article is related to [ADDA (light scattering simulator based on the discrete dipole approximation)](https://github.com/adda-team/adda/).


### About

A Python code named "average_Qxxx_171216.py" is used to obtain the average values of the ADDA runs, in terms of the [Qext] and [Qabs].

The code is used, for example, to obtain random orientation averaging of Qext and Qabs.
(e.g. [ ADDA > tool > loop_beta_gamma_171216.py > v0.1 > run sequentially ADDA for different values of [beta] and [gamma] for orientations by Euler angles ([alpha] is fixed as 0.0) ]( https://qiita.com/7of9/items/5c52189aba265d4c9d09 )


### code v0.1

```average_Qxxx_171216.py
import numpy as np
import subprocess as sb
import sys
import glob

'''
v0.1 Dec. 16, 2017
  - add get_Qxxx()
  - add average_Qxxx()
'''

# coding rule: PEP8
# tested on: Python v3.5.2

# be executed at the directory where the ADDA result direcroties
#   are found (e.g. "run6144_sphere_g16_m1.5")


kTargetKeys = 'Qext', 'Qabs'  # 'Qsca' is obtained by 'Qext - Qabs'


def get_Qxxx(infile, atargetKey):
    with open(infile, 'rt') as fp:
        lines = fp.readlines()
        for aline in lines:
            if atargetKey in aline:  # e.g. Qext    = 3.788800597
                # print(aline)
                res = aline.split(" ")
                return np.array(res[1]).astype(float)


def average_Qxxx():
    res = glob.glob("run*/CrossSec-*")
    sum, cnt = 0.0, 0
    for atargetKey in kTargetKeys:
        for elem in res:
            qext = get_Qxxx(elem, atargetKey)
            sum += qext
            cnt += 1
        msg = '{1:.7f}  # {0}(avg)'.format(atargetKey, sum/cnt)
        print(msg)


if __name__ == '__main__':
    average_Qxxx()

````

### code v0.2 > fix bug (incorrect initialization)
(Updated Dec. 24, 2017)

```average_Qxxx_171216.py
import numpy as np
import subprocess as sb
import sys
import glob

'''
v0.2 Dec. 24, 2017
  - fix bug: [sum] and [cnt] was initialized incorrectly
v0.1 Dec. 16, 2017
  - add get_Qxxx()
  - add average_Qxxx()
'''

# coding rule: PEP8
# tested on: Python v3.5.2

# be executed at the directory where the ADDA result direcroties
#   are found (e.g. "run6144_sphere_g16_m1.5")


kTargetKeys = 'Qext', 'Qabs'  # 'Qsca' is obtained by 'Qext - Qabs'


def get_Qxxx(infile, atargetKey):
    with open(infile, 'rt') as fp:
        lines = fp.readlines()
        for aline in lines:
            if atargetKey in aline:  # e.g. Qext    = 3.788800597
                # print(aline)
                res = aline.split(" ")
                return np.array(res[1]).astype(float)


def average_Qxxx():
    res = glob.glob("run*/CrossSec-*")
    for atargetKey in kTargetKeys:
        sum, cnt = 0.0, 0
        for elem in res:
            qext = get_Qxxx(elem, atargetKey)
            sum += qext
            cnt += 1
        msg = '{1:.7f}  # {0}(avg)'.format(atargetKey, sum/cnt)
        print(msg)


if __name__ == '__main__':
    average_Qxxx()

```

### Run example

The code is needed to be executed at the directory, where the ADDA result directories are found as shown below.
(e.g. run6144_sphere_g16_m1)

```bash
$ ls -F | grep "/"
...
run6144_sphere_g16_m1.5/
run6145_sphere_g16_m1.5/
run6146_sphere_g16_m1.5/
run6147_sphere_g16_m1.5/
run6148_sphere_g16_m1.5/
```

Then, execute the code.


```bash
$ python3 CODE_171216_Euler/average_Qxxx_171216.py 
3.7929064  # Qext(avg)
0.0000000  # Qabs(avg)
```