ADDA > tool > average_Mueller_180114.py > v0.1, v0.2 > average for Mueller matrix elements for various values of the ADDA parameters

### About

A Python code named "average_Mueller_180114.py" is used to obtain the average values of the ADDA runs, in terms of the [Mueller matrix elements].

The code is used, for example, to obtain random orientation averaging of the elements.
(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

The code will read [mueller] files in the different directories and will take the average.

```average_Mueller_180114.py
import numpy as np
import glob

'''
v0.1 Jan. 14, 2018
  - save with title (1st line of the file)
  - average for files for Row=(181 and 361)
'''

INP_FILE = 'mueller'
OUT_FILE = 'avg_mueller'

# 1. set number of rows
SIZE_ROW = 181  # Scattering angle [0,..180]

# 2. initialization
cnt = 0
mueller = [[0.0] for idx in range(SIZE_ROW)]

# 3. average
afile = None
for elem in glob.glob("run*/" + INP_FILE):
    afile = elem
    dat = np.genfromtxt(elem, skip_header=1)
    mueller += dat[:SIZE_ROW]
    cnt += 1
mueller /= cnt

# 4. read title (1st line of the file)
with open(afile) as fd:
    title = fd.readline().rstrip('\n')
title = np.array(title).reshape(1,)
print(title)

# 5. save
with open(OUT_FILE, 'wb+') as fd:
    np.savetxt(fd, title, fmt='%s')
    np.savetxt(fd, mueller, fmt='%f')

```

### Run example

The code needs to be executed at the directory, where the ADDA result directories are found as shown below.
(e.g. run42500_sphere_g16_m1.5)

```txt:run
$ ls -F | grep "/"
...
run42522_sphere_g16_m1.5/
run42523_sphere_g16_m1.5/
run42524_sphere_g16_m1.5/
run42525_sphere_g16_m1.5/
run42526_sphere_g16_m1.5/
run42527_sphere_g16_m1.5/
run42528_sphere_g16_m1.5/
run42529_sphere_g16_m1.5/
```

Then, execute the code:

```txt:run
$ python3 CODE_171216_Euler/average_Mueller_180114.py 
['theta s11 s12 s13 s14 s21 s22 s23 s24 s31 s32 s33 s34 s41 s42 s43 s44']
```

The file [avg_mueller] will be produced.


```txt:mueller(excerpt)
theta s11 s12 s13 s14 s21 s22 s23 s24 s31 s32 s33 s34 s41 s42 s43 s44
0.000000 141.266663 0.085046 0.053983 0.001126 0.085140 141.263490 0.000252 -0.050307 0.053968 -0.000010 141.265491 -0.105743 0.001143 0.050316 0.105674 141.262323
1.000000 141.119789 0.084314 0.049436 0.001088 0.084415 141.116600 0.000418 -0.046058 0.049423 -0.000202 141.118626 -0.092219 0.001071 0.046068 0.092148 141.115441
2.000000 140.678630 0.073451 0.044840 0.001049 0.073558 140.675416 0.000587 -0.041680 0.044829 -0.000394 140.677470 -0.056483 0.000989 0.041692 0.056409 140.674262
3.000000 139.945718 0.052687 0.040218 0.001009 0.052800 139.942472 0.000761 -0.037200 0.040209 -0.000586 139.944538 0.001115 0.000900 0.037214 -0.001191 139.941299
4.000000 138.925249 0.022423 0.035594 0.000968 0.022541 138.921963 0.000938 -0.032645 0.035587 -0.000779 138.923995 0.080006 0.000808 0.032660 -0.080085 138.920716
5.000000 137.623045 -0.016777 0.030991 0.000923 -0.016654 137.619714 0.001120 -0.028039 0.030987 -0.000976 137.621620 0.179411 0.000713 0.028057 -0.179493 137.618296
6.000000 136.046508 -0.064197 0.026431 0.000874 -0.064069 136.043123 0.001306 -0.023412 0.026430 -0.001177 136.044762 0.298347 0.000619 0.023432 -0.298431 136.041386
...
```

### code v0.2

The v0.1 code can only handle [mueller] file with [theta] steps of 1.0.
This version can handle [theta] steps of any value.

```average_Mueller_180114.py
import numpy as np
import glob
import sys

'''
v0.2 Jan. 20, 2018
  - treat [theta]s with non 1.0 step
    + add check_row_numbers()
v0.1 Jan. 14, 2018
  - save with title (1st line of the file)
  - average for files for Row=(181 and 361)
'''

INP_FILE = 'mueller'
OUT_FILE = 'avg_mueller'
DIR_PREFIX = "run"


def check_row_numbers(filepath, theta_LE):
    # find number of rows where [THETA] <= [theta_LE]
    dat = np.genfromtxt(filepath, skip_header=1)
    firstColTr = (dat.transpose() <= theta_LE)[0]
    cnt = np.count_nonzero(firstColTr)
    return cnt


# 1. set number of rows
afile = glob.glob(DIR_PREFIX + "*/" + INP_FILE)[0]
size_row = check_row_numbers(afile, theta_LE=180.0)

# 2. initialization
cnt = 0
mueller = [[0.0] for idx in range(size_row)]

# 3. average
afile = None
for elem in glob.glob(DIR_PREFIX + "*/" + INP_FILE):
    afile = elem
    dat = np.genfromtxt(elem, skip_header=1)
    mueller += dat[:size_row]
    cnt += 1
mueller /= cnt

# 4. read title (1st line of the file)
with open(afile) as fd:
    title = fd.readline().rstrip('\n')
title = np.array(title).reshape(1,)
print(title)

# 5. save
with open(OUT_FILE, 'wb+') as fd:
    np.savetxt(fd, title, fmt='%s')
    np.savetxt(fd, mueller, fmt='%f')

print('[%s] is produced' % OUT_FILE)

``` 

```txt:run
$ python3 CODE_171216_Euler/average_Mueller_180114.py 
['theta s11 s12 s13 s14 s21 s22 s23 s24 s31 s32 s33 s34 s41 s42 s43 s44']
[avg_mueller] is produced
```



### Related

- [ADDA > tool > showMueller_180113.ipynb > v0.1-v0.4 > to show Mueller Matrix elements from [mueller] file](https://qiita.com/7of9/items/2392a0f4fc23069cb7a5)
- [ADDA > tool > average_Qxxx_171216.py > v0.1,v0.2 > average for Qext and Qabs for various values of the ADDA parameters](https://qiita.com/7of9/items/62151891b11b53857b46)