Skip to content

Commit

Permalink
Merge pull request pyne#8 from paulromano/add-jeff33
Browse files Browse the repository at this point in the history
Add script to convert JEFF 3.3 data from OECD/NEA
  • Loading branch information
shimwell authored Aug 9, 2019
2 parents 7c4fdaa + 83c4713 commit cfd74f3
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 36 deletions.
70 changes: 34 additions & 36 deletions convert_jeff32.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import argparse
import glob
import os
import sys
import tarfile
import zipfile
from collections import defaultdict
from string import digits
from urllib.parse import urljoin

import openmc.data
from openmc._utils import download
Expand All @@ -23,8 +23,6 @@
processing the data may require as much as 40 GB of additional free disk
space. Note that if you don't need all 11 temperatures, you can modify the
'files' list in the script to download only the data you want.
Are you sure you want to continue? ([y]/n)
"""

class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
Expand All @@ -35,19 +33,24 @@ class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
description=description,
formatter_class=CustomFormatter
)
parser.add_argument('-b', '--batch', action='store_true',
help='supresses standard in')
parser.add_argument('-d', '--destination', default='jeff-3.2-hdf5',
parser.add_argument('-d', '--destination', default='jeff32_hdf5',
help='Directory to create new library in')
parser.add_argument('--download', action='store_true',
help='Download files from OECD-NEA')
parser.add_argument('--no-download', dest='download', action='store_false',
help='Do not download files from OECD-NEA')
parser.add_argument('--extract', action='store_true',
help='Extract tar/zip files')
parser.add_argument('--no-extract', dest='extract', action='store_false',
help='Do not extract tar/zip files')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='latest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
parser.set_defaults(download=True, extract=True)
args = parser.parse_args()

response = input(download_warning) if not args.batch else 'y'
if response.lower().startswith('n'):
sys.exit()
print(download_warning)

base_url = 'https://www.oecd-nea.org/dbforms/data/eva/evatapes/jeff_32/Processed/'
files = ['JEFF32-ACE-293K.tar.gz',
Expand All @@ -66,37 +69,32 @@ class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
# ==============================================================================
# DOWNLOAD FILES FROM OECD SITE

files_complete = []
for f in files:
# Establish connection to URL
url = base_url + f
downloaded_file = download(url)
files_complete.append(f)
if args.download:
for f in files:
download(urljoin(base_url, f))

# ==============================================================================
# EXTRACT FILES FROM TGZ

for f in files:
if f not in files_complete:
continue

# Extract files
if f.endswith('.zip'):
with zipfile.ZipFile(f, 'r') as zipf:
print('Extracting {}...'.format(f))
zipf.extractall('jeff-3.2')

else:
suffix = 'ACEs_293K' if '293' in f else ''
with tarfile.open(f, 'r') as tgz:
print('Extracting {}...'.format(f))
tgz.extractall(os.path.join('jeff-3.2', suffix))

# Remove thermal scattering tables from 293K data since they are
# redundant
if '293' in f:
for path in glob.glob(os.path.join('jeff-3.2', 'ACEs_293K', '*-293.ACE')):
os.remove(path)
if args.extract:
for f in files:
# Extract files
if f.endswith('.zip'):
with zipfile.ZipFile(f, 'r') as zipf:
print('Extracting {}...'.format(f))
zipf.extractall('jeff-3.2')

else:
suffix = 'ACEs_293K' if '293' in f else ''
with tarfile.open(f, 'r') as tgz:
print('Extracting {}...'.format(f))
tgz.extractall(os.path.join('jeff-3.2', suffix))

# Remove thermal scattering tables from 293K data since they are
# redundant
if '293' in f:
for path in glob.glob(os.path.join('jeff-3.2', 'ACEs_293K', '*-293.ACE')):
os.remove(path)

# ==============================================================================
# CHANGE ZAID FOR METASTABLES
Expand Down
83 changes: 83 additions & 0 deletions convert_jeff33.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python

import argparse
from pathlib import Path
import tarfile
import sys

import openmc.data
from openmc._utils import download


# Make sure Python version is sufficient
assert sys.version_info >= (3, 6), "Python 3.6+ is required"

description = """
Convert JEFF 3.3 ACE data distributed by OECD/NEA into an HDF5 library
that can be used by OpenMC. It will download an 1.3 GB archive containing
all the ACE files, extract it, convert them, and write HDF5 files into a
destination directory.
"""

class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
pass


parser = argparse.ArgumentParser(
description=description,
formatter_class=CustomFormatter
)
parser.add_argument('-d', '--destination', type=Path, default=Path('jeff33_hdf5'),
help='Directory to create new library in')
parser.add_argument('--download', action='store_true',
help='Download tarball from OECD-NEA')
parser.add_argument('--no-download', dest='download', action='store_false',
help='Do not download tarball from OECD-NEA')
parser.add_argument('--extract', action='store_true',
help='Extract zip files')
parser.add_argument('--no-extract', dest='extract', action='store_false',
help='Do not extract .tgz file if it has already been extracted')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='earliest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
parser.set_defaults(download=True, extract=True)
args = parser.parse_args()

# Download JEFF 3.3 library
filename = 'JEFF33-n_tsl-ace.tgz'
url = f'http://www.oecd-nea.org/dbdata/jeff/jeff33/downloads/{filename}'
if args.download:
download(url)

# Extract tar file
if args.extract:
with tarfile.open(filename, 'r') as tgz:
print(f'Extracting {filename}...')
tgz.extractall()

# Create output directory if it doesn't exist
args.destination.mkdir(parents=True, exist_ok=True)

# Get a list of all ACE files
paths = sorted(Path('ace').glob('*.[Aa][Cc][Ee]'))

lib = openmc.data.DataLibrary()
for p in sorted(paths):
print(f'Converting: {p}')
if 'jeff33' in str(p):
data = openmc.data.IncidentNeutron.from_ace(p)
if 'm.' in str(p):
# Correct metastable
data.metastable = 1
data.name += '_m1'
else:
data = openmc.data.ThermalScattering.from_ace(p)

h5_file = args.destination / f'{data.name}.h5'
data.export_to_hdf5(h5_file, 'w', libver=args.libver)
lib.register_file(h5_file)

lib.export_to_xml(args.destination / 'cross_sections.xml')

0 comments on commit cfd74f3

Please sign in to comment.