-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_dir_compare.py
129 lines (120 loc) · 4.8 KB
/
table_dir_compare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
############################################################
#
# Author: Georg Schnabel
# Email: [email protected]
# Date: 2021/06/01
# Institution: IAEA
#
# Utility functions to retrieve the information of ENDF
# files stored in their header or to retrieve the same
# information from a html website with a table of a particle
# sublibrary, and functions to compare these differences.
#
#
# Usage:
# <command-call>
#
# <parameter>: <description>
#
############################################################
from utils import rename_endf, endf_metadata
from bs4 import BeautifulSoup
import html5lib
import re
import os
import argparse
def get_fendl_sublib_table_from_dir(endf_dir):
file_list = []
for root, dirs, files in os.walk(endf_dir):
for file in files:
file_list.append(os.path.join(root, file))
isolist = {}
for file in file_list:
meta_data = endf_metadata.get_endf_metadata(file)
if file.endswith('_.txt'):
continue
if meta_data is None:
print('problem with ' + str(file))
else:
isolist[meta_data['MAT']] = {
'FILE': os.path.basename(file),
'MAT': meta_data['MAT'],
'ZSYMAM': meta_data['ZSYMAM'],
'ALAB': meta_data['ALAB'],
'EDATE': meta_data['EDATE'],
'AUTH': meta_data['AUTH'].replace(' and ',',').replace(' ',''),
'HSUB_LIB': meta_data['HSUB_LIB'],
'EMAX': '{:e}'.format(float(meta_data['EMAX']))
}
return isolist
def get_fendl_sublib_table_from_htmlfile(endf_table_file):
with open(endf_table_file) as f:
content = f.read()
soup = BeautifulSoup(content, 'html5lib')
tbody_el = soup.find('tbody')
tr_els = tbody_el.find_all('tr', {'bgcolor': re.compile(r'.*')})
isolist = {}
for tr_el in tr_els:
td_els = tr_el.find_all('td')
cur_info = {}
for idx, td_el in enumerate(td_els):
if idx == 1:
cur_info['MAT'] = td_el.text.strip()
elif idx == 2:
cur_info['ZSYMAM'] = td_el.text.strip()
elif idx == 3:
cur_info['ALAB'] = td_el.text.strip()
elif idx == 4:
cur_info['EDATE'] = td_el.text.strip()
elif idx == 5:
cur_info['AUTH'] = td_el.text.strip().replace(' and ',',').replace(' ','')
elif idx == 6:
cur_info['HSUB_LIB'] = td_el.text.strip()
elif idx == 7:
cur_info['EMAX'] = '{:e}'.format(float(td_el.text.strip()))
isolist[cur_info['MAT']] = cur_info
return isolist
def compare_table(table1, table2):
for curiso in table1:
iso1 = table1[curiso]
if curiso in table2:
iso2 = table2[curiso]
# do the comparison
printed_header = False
found_diffs = False
for keyname in iso2:
if iso1[keyname] != iso2[keyname]:
found_diffs = True
if not printed_header:
print('Difference in ' + str(curiso) +
' (' + iso1['ZSYMAM'] + ')')
#print('[filename: ' + str(iso1['FILE']) + ']')
printed_header = True
print(keyname + ' differs: (file) ' +
str(iso1[keyname]) + ' != ' + str(iso2[keyname]) +
' (table)')
if found_diffs:
print('--------------------------')
print('########################################')
print(' MAT numbers only in #1 ')
print('########################################')
for curiso in table1:
iso1 = table1[curiso]
if curiso not in table2:
print('MAT: ' + curiso + ' - ' + iso1['ZSYMAM'] + ' - ' + iso1['EDATE'] +
' - ' + iso1['ALAB'] + ' - ' + iso1['AUTH'] + ' - ' + iso1['HSUB_LIB'])
print('########################################')
print(' MAT numbers only in #2 ')
print('########################################')
for curiso in table2:
iso2 = table2[curiso]
if curiso not in table1:
print('MAT: ' + curiso + ' - ' + iso2['ZSYMAM'] + ' - ' + iso2['EDATE'] +
' - ' + iso2['ALAB'] + ' - ' + iso2['AUTH'] + ' - ' + iso2['HSUB_LIB'])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Compare ENDF directories and sublib tables')
parser.add_argument('dir1', help='directory with ENDF file or path to html file with table', type=str)
parser.add_argument('dir2', help='directory with ENDF file or path to html file with table', type=str)
args = parser.parse_args()
dir1 = args.dir1
dir2 = args.dir2