11
22
33import os
4+ import warnings
45import xml .etree .ElementTree as ET
56from pathlib import Path
6- from typing import List , Union , Optional
7+ from typing import List , Optional , Union
78from urllib .parse import urlparse
89from urllib .request import urlopen
910
1011import pandas as pd
1112
12- from openmc_data_downloader import (
13- NATURAL_ABUNDANCE ,
14- LIB_OPTIONS ,
15- PARTICLE_OPTIONS ,
16- SAB_OPTIONS ,
17- ISOTOPE_OPTIONS ,
18- xs_info ,
19- sab_info
20- )
13+ from openmc_data_downloader import (ISOTOPE_OPTIONS , LIB_OPTIONS ,
14+ NATURAL_ABUNDANCE , PARTICLE_OPTIONS ,
15+ SAB_OPTIONS , sab_info , xs_info )
2116
2217_BLOCK_SIZE = 16384
2318
@@ -37,12 +32,19 @@ def set_enviromental_varible(cross_section_xml_path: Union[Path, str]) -> None:
3732
3833
3934def expand_materials_to_isotopes (materials : list ):
35+
36+ if isinstance (materials , list ):
37+ if len (materials ) == 0 :
38+ return []
39+
4040 try :
4141 import openmc
4242 except ImportError :
43- print ('openmc python package was not imported, '
44- 'expand_materials_to_isotopes can not be performed.' )
45- return None
43+ msg = (
44+ 'import openmc failed. openmc python package could not be found '
45+ 'and was not imported, the expand_materials_to_isotopes '
46+ 'opperation can not be performed ithout openmc' )
47+ raise ImportError (msg )
4648
4749 if isinstance (materials , openmc .Materials ):
4850 iterable_of_materials = materials
@@ -72,12 +74,19 @@ def expand_materials_to_isotopes(materials: list):
7274
7375
7476def expand_materials_to_sabs (materials : list ):
77+
78+ if isinstance (materials , list ):
79+ if len (materials ) == 0 :
80+ return []
81+
7582 try :
7683 import openmc
7784 except ImportError :
78- print ('openmc python package was not imported, '
79- 'expand_materials_to_isotopes can not be performed.' )
80- return None
85+ msg = (
86+ 'import openmc failed. openmc python package could not be found '
87+ 'and was not imported, the expand_materials_to_sabs '
88+ 'opperation can not be performed ithout openmc' )
89+ raise ImportError (msg )
8190
8291 if isinstance (materials , openmc .Materials ):
8392 iterable_of_materials = materials
@@ -113,15 +122,15 @@ def just_in_time_library_generator(
113122 sab : List [str ] = [],
114123 destination : Union [str , Path ] = None ,
115124 materials_xml : List [Union [str , Path ]] = [],
116- materials : list = [],
125+ materials : list = [], # also accepts a single openmc.Material
117126 particles : Optional [List [str ]] = ['neutron' , 'photon' ],
118127 set_OPENMC_CROSS_SECTIONS : bool = True ,
119128) -> str :
120129
121130 # expands elements, materials xml into list of isotopes
122- if len ( elements ) > 0 :
123- isotopes_from_elements = expand_elements_to_isotopes (elements )
124- isotopes = list (set (isotopes + isotopes_from_elements ))
131+
132+ isotopes_from_elements = expand_elements_to_isotopes (elements )
133+ isotopes = list (set (isotopes + isotopes_from_elements ))
125134
126135 isotopes_from_material_xml = expand_materials_xml_to_isotopes (
127136 materials_xml )
@@ -156,7 +165,10 @@ def just_in_time_library_generator(
156165 cross_section_xml_path = create_cross_sections_xml (dataframe , destination )
157166
158167 if set_OPENMC_CROSS_SECTIONS is True :
159- set_enviromental_varible (cross_section_xml_path )
168+ # making the cross section xml requires openmc and returns None if
169+ # openmc is not found.
170+ if cross_section_xml_path is not None :
171+ set_enviromental_varible (cross_section_xml_path )
160172 else :
161173 print ('Set your $OPENMC_CROSS_SECTIONS enviromental varible to {} to '
162174 'use this custom library' .format (cross_section_xml_path ))
@@ -239,8 +251,11 @@ def create_cross_sections_xml(dataframe, destination: Union[str, Path]) -> str:
239251 try :
240252 import openmc
241253 except ImportError :
242- print ('openmc python package was was found, cross_sections.xml can '
243- 'not be made.' )
254+ msg = (
255+ 'import openmc failed. openmc python package could not be found '
256+ 'and was not imported, cross sections.xml can not be made'
257+ 'without openmc' )
258+ warnings .warn (msg )
244259 return None
245260
246261 library = openmc .data .DataLibrary ()
0 commit comments