|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Download TENDL data from PSI and convert it to a HDF5 library for |
| 5 | +use with OpenMC. |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import ssl |
| 10 | +from multiprocessing import Pool |
| 11 | +from pathlib import Path |
| 12 | +from urllib.parse import urljoin |
| 13 | + |
| 14 | +import openmc.data |
| 15 | +from openmc_data import download, extract, process_neutron, state_download_size, all_release_details |
| 16 | + |
| 17 | + |
| 18 | +class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, |
| 19 | + argparse.RawDescriptionHelpFormatter): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +parser = argparse.ArgumentParser( |
| 24 | + description=__doc__, |
| 25 | + formatter_class=CustomFormatter |
| 26 | +) |
| 27 | +parser.add_argument('-d', '--destination', type=Path, default=None, |
| 28 | + help='Directory to create new library in') |
| 29 | +parser.add_argument('--download', action='store_true', |
| 30 | + help='Download files') |
| 31 | +parser.add_argument('--no-download', dest='download', action='store_false', |
| 32 | + help='Do not download files') |
| 33 | +parser.add_argument('--extract', action='store_true', |
| 34 | + help='Extract tar/zip files') |
| 35 | +parser.add_argument('--no-extract', dest='extract', action='store_false', |
| 36 | + help='Do not extract tar/zip files') |
| 37 | +parser.add_argument('--libver', choices=['earliest', 'latest'], |
| 38 | + default='latest', help="Output HDF5 versioning. Use " |
| 39 | + "'earliest' for backwards compatibility or 'latest' for " |
| 40 | + "performance") |
| 41 | +parser.add_argument('-r', '--release', choices=["2023"], default="2023", |
| 42 | + help="The nuclear data library release version. " |
| 43 | + "The options currently supported are 2023") |
| 44 | +parser.add_argument('--cleanup', action='store_true', |
| 45 | + help="Remove download directories when data has " |
| 46 | + "been processed") |
| 47 | +parser.add_argument('--no-cleanup', dest='cleanup', action='store_false', |
| 48 | + help="Do not remove download directories when data has " |
| 49 | + "been processed") |
| 50 | +parser.set_defaults(download=True, extract=True, cleanup=False) |
| 51 | +args = parser.parse_args() |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + |
| 56 | + library_name = 'tendl' |
| 57 | + |
| 58 | + cwd = Path.cwd() |
| 59 | + |
| 60 | + endf_files_dir = cwd.joinpath('-'.join([library_name, args.release, 'endf'])) |
| 61 | + download_path = cwd.joinpath('-'.join([library_name, args.release, 'download'])) |
| 62 | + # the destination is decided after the release is known |
| 63 | + # to avoid putting the release in a folder with a misleading name |
| 64 | + if args.destination is None: |
| 65 | + args.destination = Path('-'.join([library_name, args.release, 'hdf5'])) |
| 66 | + |
| 67 | + # This dictionary contains all the unique information about each release. |
| 68 | + # This can be exstened to accommodated new releases |
| 69 | + details = all_release_details[library_name][args.release]['neutron']['endf'] |
| 70 | + |
| 71 | + # ============================================================================== |
| 72 | + # DOWNLOAD FILES FROM WEBSITE |
| 73 | + |
| 74 | + if args.download: |
| 75 | + state_download_size(details['compressed_file_size'], details['uncompressed_file_size'], 'GB') |
| 76 | + for f in details['compressed_files']: |
| 77 | + # Establish connection to URL |
| 78 | + download( |
| 79 | + urljoin(details['base_url'], f), |
| 80 | + context=ssl._create_unverified_context(), |
| 81 | + output_path=download_path, |
| 82 | + as_browser=True |
| 83 | + ) |
| 84 | + |
| 85 | + # ============================================================================== |
| 86 | + # EXTRACT FILES FROM TGZ |
| 87 | + if args.extract: |
| 88 | + extract( |
| 89 | + compressed_files=[download_path/ f for f in details['compressed_files']], |
| 90 | + extraction_dir=endf_files_dir, |
| 91 | + del_compressed_file=args.cleanup |
| 92 | + ) |
| 93 | + |
| 94 | + # ============================================================================== |
| 95 | + # GENERATE HDF5 LIBRARY -- NEUTRON FILES |
| 96 | + |
| 97 | + # Get a list of all ENDF files |
| 98 | + neutron_files = endf_files_dir.glob('*.tendl') |
| 99 | + |
| 100 | + # Create output directory if it doesn't exist |
| 101 | + args.destination.mkdir(parents=True, exist_ok=True) |
| 102 | + |
| 103 | + library = openmc.data.DataLibrary() |
| 104 | + |
| 105 | + with Pool() as pool: |
| 106 | + results = [] |
| 107 | + for filename in sorted(neutron_files): |
| 108 | + func_args = (filename, args.destination, args.libver) |
| 109 | + r = pool.apply_async(process_neutron, func_args) |
| 110 | + results.append(r) |
| 111 | + |
| 112 | + for r in results: |
| 113 | + r.wait() |
| 114 | + |
| 115 | + # Register with library |
| 116 | + for p in sorted((args.destination).glob('*.h5')): |
| 117 | + library.register_file(p) |
| 118 | + |
| 119 | + # Write cross_sections.xml |
| 120 | + library.export_to_xml(args.destination / 'cross_sections.xml') |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + main() |
0 commit comments