EndfParser
- class endf_parserpy.EndfParser(ignore_number_mismatch=False, ignore_zero_mismatch=True, ignore_varspec_mismatch=True, fuzzy_matching=True, abuse_signpos=False, skip_intzero=False, prefer_noexp=False, accept_spaces=True, ignore_blank_lines=False, ignore_send_records=False, ignore_missing_tpid=False, keep_E=False, preserve_value_strings=False, include_linenum=True, width=11, check_arrays=True, strict_datatypes=False, array_type='dict', explain_missing_variable=True, cache_dir=None, print_cache_info=True, endf_format='endf6-ext', recipes=None, parsing_funs=None, loglevel=30)[source]
Bases:
objectClass for parsing and writing ENDF-6 formatted data.
This class provides functions for (1) parsing ENDF-6 formatted data, and (2) converting data given in a
dict-like object into the ENDF-6 format. The ENDF-6 formatted data may be given in a text file, a string, or a list of strings containing separate lines. The essential methods of this class areparsefile()andwritefile().Initializaton of options for parsing and writing ENDF-6 data.
The process of parsing can be influenced by many options that determine how inconsistencies in ENDF-6 formatted data should be handled and the degree of flexibility in accepting unusual number representations. Parameters pertaining to the parsing process are indicated by (parsing) in their description. The reverse process, writing data into the ENDF-6 format, can also be influenced by a variety of options, e.g., impacting the output precision. Parameters related to converting data into the ENDF-6 format are marked by (writing).
- Parameters:
ignore_number_mismatch (bool) – Tolerate mismatches between numbers in ENDF-6 formatted data and the expected numbers according to the ENDF-6 recipes. (parsing)
ignore_zero_mismatch (bool) – Tolerate non-zero numbers in ENDF-6 formatted data that are required to be zero according to ENDF-6 recipes. (parsing)
ignore_varspec_mismatch (bool) – Tolerate distinct numbers that are supposed to be mapped to the same symbol name. For the time being, even with this option enabled, possible inconsistent variable assignment have to be marked with a queston mark in the ENDF-6 recipe. (parsing)
fuzzy_matching (bool) – Tolerate small inconsistencies between fields when they are linked by a mathematical relationship. (parsing)
abuse_signpos (bool) – Permit positive numbers to start in the first character slot of an ENDF-6 field, which is usually reserved for the sign, to enhance numerical precision. (writing)
skip_intzero (bool) – For numbers written out in decimal notation, eliminate the integer part if zero, e.g. 0.12 becomes .12 to increase attainable precision. (writing)
prefer_noexp (bool) – Switch to decimal representation (i.e. non-scientific) if it leads to an increase of accuracy, e.g. 1.234567-1 becomes 0.12345678. (writing)
accept_spaces (bool) – Eliminate spaces in a number before trying to parse it, e.g. 1.234 +8 is transformed to 1.234+8. (parsing)
ignore_blank_lines (bool) – If
True, skip blank lines in ENDF-6 formatted input without complaining. Otherwise, blank lines will lead to parsing failure. (parsing)ignore_send_records (bool) – If
True, the correct positioning of SEND/FEND/MEND/TEND to indicate the end of a section is not checked. (parsing)ignore_missing_tpid (bool) – If
True, the parser will tolerate a missing TPID record at the beginning of the file. (parsing)keep_E (bool) – If
True, include the e character in scientific notation, e.g. 1.23e-8 instead of 1.23-8. The inclusion establishes compatibility with programming languages different from Fortran while the omission enhances numerical precision. (writing)preserve_value_strings (bool) – If
True, also the string representations of float numbers will be recorded during the parsing process (via theEndfFloatclass). These string representations when available will be used verbatim for outputting ENDF-6 formatted data, overruling any of the other options provided for controling the output format of floats. (parsing, writing)include_linenum (bool) – Controls whether the 5-digit line number should be included at the end of each line. (writing)
width (int) – The number of character slots in an ENDF-6 field. The ENDF-6 format requires 11 but the user may opt for a different width for their storage/application needs. (parsing, writing)
check_arrays (bool) – Ensures that index ranges provided in the Python
dictpassed as argument to thewrite()andwritefile()method are consistent with the values of counter variables. IfTrue, adictcontaining larger index ranges than expected by counter variables will lead to failure, otherwise the presence of additional indices will be ignored. (writing)strict_datatypes (bool) – Strict data type checking will lead to failure if a float needs to be cast to an int. If false, the writing process will only fail if the a value in the float cannot be perfectly represented by an int. (writing)
array_type (str) – The Python datatype to use for representing arrays read from ENDF-6 files. The two options are
"dict"(default) and"list". (parsing)explain_missing_variable (bool) – If the
write()orwritefile()method fail because a variable is missing in the dictionary, print available explanation if this argument isTrue.cache_dir – Directory to store the parsing trees associated with ENDF-6 recipes If None, the directory will be automatically determined relying on the appdirs package. If false, no cache directory will be used and ENDF-6 recipes will be compiled on the fly whenever this class is instantiated. Finally, the user can provide a custom directory as a string.
print_cache_info (bool) – If
True, print out a message regarding the location of the cache directory if it was automatically determined.endf_format (str) – Allow the user to pick specific ENDF format flavors. The default endf6-ext tolerates deviations from the ENDF-6 format encountered in some nuclear data libraries. Other choices are endf6 for strict compliance with the ENDF-6 formats manual and jendl with JENDL specific conventions, which are also implemented in endf6-ext.
recipes (dict_like) – The default ENDF-6 recipes can be overrided by providing a nested dictionary with custom recipes. Inspect the default recipe dictionary to see the required structure (from endf_parserpy.endf_recipes import endf_recipe_dictionary)
loglevel (int) – Controls the level of detail for logging output. Default is logging.WARN. Many ENDF-6 files in nuclear data libraries contain auxiliary information in unused fields (expected to be zero), which will trigger warnings. Use logging.ERROR to suppress these warnings (you will need to import logging).
- explain(varpath, stdout=True)[source]
Explain the meaning of a variable.
ENDF-6 recipes can contain the descriptions of variables, which are automatically read while calling the
parsefile(),parse(),write()andwritefile()method. Given the path to a variable, this function can output the associated description.- Parameters:
- Returns:
If
stdout=TruereturnNone, otherwise the description as astr.- Return type:
- parse(lines, exclude=None, include=None, nofail=False)[source]
Parse ENDF-6 formatted data.
- Parameters:
lines (Union[str, list[str]]) – The lines of text containing the ENDF-6 formatted data. This argument can be either a list of strings with each string storing a single line, or a string containing all ENDF-6 formatted data including linebreaks.
exclude (Union[None, tuple[Union[int, tuple[int, int]]]]) – See explanation of parameter
excludeinparsefile()for details.include (Union[None, tuple[Union[int, tuple[int, int]]]]) – See explanation of parameter
includeinparsefile()for details.nofail (bool) – See explanation of parameter
nofailinparsefile()for details.
- parsefile(filename, exclude=None, include=None, nofail=False)[source]
Parse ENDF-6 formatted data stored in a file.
- Parameters:
filename (str) – Path to the ENDF-6 file
exclude (Union[None, tuple[Union[int, tuple[int, int]]]]) – MF/MT sections to exclude in the parsing process. Excluded sections will only be available as a list of strings. The default None indicates that nothing should be excluded. If a tuple is provided, integers in that tuple denote the MF sections to be excluded. In addition to integers, also tuples composed of two integers can be provided to indicate the MF/MT combinations that should be excluded. For instance,
(3,)would exclude MF section 3 and(3, (2, 151))would exclude MF section 3 and additionally MF=2/MT=151. Useful to speed up the parsing process or to ensure a verbatim copy of data in a read/write sequence.include (Union[None, tuple[Union[int, tuple[int, int]]]]) – The MF/MT section to include in the parsing. All the other sections will be only present as a list of strings. This argument is only active if
exclude=None. The MF and MF/MT sections are specified exactly in the same way as for theexcludeargument.nofail (bool) – If this argument is
True, the parser will attempt to parse all MF/MT sections desired by the user, irrespective of failure in the parsing of any section. Sections where parsing failed will only be available as list of strings. On the other hand,nofail=falseinstructs the parser to abort immediately upon the first parsing failure.
- Returns:
A nested dictionary. The keys of the first level are MF numbers and of the second level MT numbers. The structure of the
dictstored under a specific MF/MT combination is determined by the corresponding ENDF recipe.- Return type:
- write(endf_dic, exclude=None, include=None, zero_as_blank=False)[source]
Convert data into the ENDF-6 format.
All parameters are explained in the description of
writefile().
- writefile(filename, endf_dic, exclude=None, include=None, zero_as_blank=False, overwrite=False)[source]
Write data to an ENDF-6 file.
- Parameters:
filename (str) – Path of the file to be created.
endf_dic (dict) – Nested dictionary with nuclear data. Keys of first level are MF numbers and the keys of second level MT numbers. The structure of the
dictstored under an MF/MT combination depends on the corresponding ENDF recipe.exclude (Union[None, tuple[Union[int, tuple[int, int]]]]) – A section will only be written to the file if not excluded. For an explanation of how to specify MF/MT sections to be excluded, see the
excludeargument ofparsefile().include (Union[None, tuple[Union[int, tuple[int, int]]]]) – This argument is only considered if
exclude=None. If this argument isNone, all sections will be written to the file. Otherwise, only the indicated sections will be written to the file. For an explanation of how to define which sections to include, see theincludeargument ofparsefile().overwrite (bool) – Existing files will only be overwritten if this argument is
True, otherwise this function will abort.