Skip to content

Commit

Permalink
First commit for file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Eitan Weinstein committed Aug 5, 2024
1 parent 0407976 commit 3f3a974
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 13 deletions.
72 changes: 72 additions & 0 deletions src/DataLib/fendl32B_retrofit/file_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Import packages
from pathlib import Path

def get_isotope(stem):
"""
Extract the element name and mass number from a given filename.
Arguments:
stem (str): Stem of a an ENDF (TENDL) and/or PENDF file, formatted
as f'{element}{mass_number}.ext'
Returns:
element (str): Chemical symbol of the isotope whose data is contained
in the file.
A (str): Mass number of the isotope, potentially including the letter
"m" at the end if the isotope is in a metastable state.
"""

isomer_id = ''

upper_case_letters = [char for char in stem if char.isupper()]
lower_case_letters = [char for char in stem if char.islower()]
numbers = [str(char) for char in stem if char.isdigit()]

if len(lower_case_letters) == 0:
lower_case_letters = ''
elif len(lower_case_letters) > 1:
isomer_id = lower_case_letters[-1]
lower_case_letters = lower_case_letters[:-1]

element = f'{upper_case_letters[0]}{lower_case_letters[0]}'
A = ''.join(numbers) + isomer_id

return element, A

def search_for_files(directory = '.'):
"""
Search through a directory for all pairs of ENDF (TENDL) and PENDF files
that have matching stems. If so, save the paths and the isotopic
information to a dictionary.
Arguments:
directory (str, optional): Path to the directory in which to search
for ENDF and PENDF files.
Defaults to the present working directory (".").
Returns:
file_info (dict): Dictionary containing the chemical symbol, mass
number, and paths to the ENDF and PENDF files for a given isotope.
The dictionary is formatted as such:
{f'{element}{mass_number}' :
{'Element'} : Isotope's chemical symbol,
{'Mass Number'} : Isotope's mass number,
{'File Paths'} : (endf_path, pendf_path)
}
"""

directory = Path(directory)

file_info = {}
for file in directory.iterdir():
if file.is_file():
if file.suffix == '.endf' or file.suffix == '.tendl':
endf_file = file
pendf_file = Path(f'{endf_file.stem}.pendf')
if pendf_file.is_file():
element, A = get_isotope(file.stem)
file_info[f'{element}{A}'] = {
'Element' : element,
'Mass Number' : A,
'File Paths' : (endf_file, pendf_file)
}

return file_info
39 changes: 26 additions & 13 deletions src/DataLib/fendl32B_retrofit/process_fendl3.2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,43 @@
import reaction_data as rxd
import tendl_processing as tp
import groupr_tools
import file_handling as fh
from pandas import concat

def main():
"""
Main method when run as a command line script.
"""

TAPE20 = 'tape20'
TAPE21 = 'tape21'

mt_dict = rxd.process_mt_data(rxd.load_mt_table('mt_table.csv'))
cumulative_data = tp.cumulative_data

endf_path = 'tape20'
pendf_path = 'tape21'
for isotope, file_properties in fh.search_for_files().items():
element = file_properties['Element']
A = file_properties['Mass Number']
endf_path, pendf_path = file_properties['File Paths']
endf_path.rename(TAPE20)
pendf_path.rename(TAPE21)

material_id, MTs, endftk_file_obj = tp.extract_endf_specs(endf_path)
njoy_input = groupr_tools.fill_input_template(material_id, MTs, 'Fe', 56, mt_dict)
groupr_tools.write_njoy_input_file(njoy_input)
gendf_path = groupr_tools.run_njoy('Fe', 56, material_id)
material_id, MTs, endftk_file_obj = tp.extract_endf_specs(TAPE20)
njoy_input = groupr_tools.fill_input_template(material_id, MTs,
element, A, mt_dict)
groupr_tools.write_njoy_input_file(njoy_input)
gendf_path = groupr_tools.run_njoy(element, A, material_id)

pKZA = tp.extract_gendf_pkza(gendf_path)
# Extract MT values again from GENDF file as there may be some difference
# from the original MT values in the ENDF/PENDF files
material_id, MTs, endftk_file_obj = tp.extract_endf_specs(gendf_path)
gendf_data = tp.iterate_MTs(MTs, endftk_file_obj, mt_dict, pKZA)
gendf_data.to_csv('gendf_data.csv')
groupr_tools.cleanup_njoy_files()
pKZA = tp.extract_gendf_pkza(gendf_path)
# Extract MT values again from GENDF file as there may be some
# difference from the original MT values in the ENDF/PENDF files
material_id, MTs, endftk_file_obj = tp.extract_endf_specs(gendf_path)
gendf_data = tp.iterate_MTs(MTs, endftk_file_obj, mt_dict, pKZA)
cumulative_data = concat([cumulative_data, gendf_data],
ignore_index=True)
groupr_tools.cleanup_njoy_files()

cumulative_data.to_csv('cumulative_gendf_data.csv')

if __name__ == '__main__':
main()
9 changes: 9 additions & 0 deletions src/DataLib/fendl32B_retrofit/tendl_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
import ENDFtk
import pandas as pd

# Initialize Data Frame in which to store all extracted data
cumulative_data = pd.DataFrame({
'Parent KZA' : [],
'Daughter KZA' : [],
'Emitted Particles' : [],
'Non-Zero Groups' : [],
'Cross Sections' : []
})

def extract_endf_specs(path):
"""
Extract the material ID and MT numbers from an ENDF file.
Expand Down

0 comments on commit 3f3a974

Please sign in to comment.