CIME.namelist

Module containing tools for dealing with Fortran namelists.

The public interface consists of the following functions: - character_literal_to_string - compress_literal_list - expand_literal_list - fortran_namelist_base_value - is_valid_fortran_name - is_valid_fortran_namelist_literal - literal_to_python_value - merge_literal_lists - parse - string_to_character_literal

In addition, the Namelist class represents a namelist held in memory.

For the moment, only a subset of namelist syntax is supported; specifically, we assume that only variables of intrinsic type are used, and indexing/co-indexing of arrays to set a portion of a variable is not supported. (However, null values and repeated values may be used to set or fill a variable as indexing would.)

We also always assume that a period (“.”) is the decimal separator, not a comma (“,”). We also assume that the file encoding is UTF-8 or some compatible format (e.g. ASCII).

Otherwise, most Fortran syntax rules implemented here are compatible with Fortran 2008 (which is largely the same as previous standards, and will be similar to Fortran 2015). The only exceptions should be cases where (a) we deliberately prohibit “troublesome” behavior that would be allowed by the standard, or (b) we rely on conventions shared by all major compilers.

One important convention is that newline characters can be used to denote the end of a record. This makes them equivalent to spaces at most locations in a Fortran namelist, except that newlines also end comments, and they are ignored entirely within strings.

While the treatment of comments in this module is standard, it may be somewhat surprising. Namelist comments are only allowed in two situations:

  1. As the only thing on a line (aside from optional indentation with spaces).

(2) Immediately after a “value separator” (the space, newline, comma, or slash after a value).

This implies that all lines except for the last are syntax errors, in this example:

` &group_name! This is not a valid comment because it's after the group name. foo ! Neither is this, because it's between a name and an equals sign. = 2 ! Nor this, because it comes between the value and the following comma. , bar = ! Nor this, because it's between an equals sign and a value. 2! Nor this, because it should be separated from the value by a comma or space. bazz = 3 ! Nor this, because it comes between the value and the following slash. /! This is fine, but technically it is outside the namelist, not a comment. `

However, the above would actually be valid if all the “comments” were removed. The Fortran standard is not clear about whether whitespace is allowed after inline comments and before subsequent non-whitespace text (!), but this module allows such whitespace, to preserve the sanity of both implementors and users.

The Fortran standard only applies to the interior of namelist groups, and not to text between one namelist group and the next. This module assumes that namelist groups are separated by (optional) whitespace and comments, and nothing else.

Functions

character_literal_to_string

Convert a Fortran character literal to a Python string.

compress_literal_list

Uses repetition syntax to shorten a literal list.

convert_bool

expand_literal_list

Expands a list of literal values to get rid of repetition syntax.

fortran_namelist_base_value

Strip off whitespace and repetition syntax from a namelist value.

get_fortran_name_only

remove array section if any and return only the variable name >>> get_fortran_name_only('foo') 'foo' >>> get_fortran_name_only('foo(3)') 'foo' >>> get_fortran_name_only('foo(::)') 'foo' >>> get_fortran_name_only('foo(1::)') 'foo' >>> get_fortran_name_only('foo(:+2:)') 'foo' >>> get_fortran_name_only('foo(::-3)') 'foo' >>> get_fortran_name_only('foo(::)') 'foo'

get_fortran_variable_indices

get indices from a fortran namelist variable as a triplet of minindex, maxindex and step

is_valid_fortran_name

Check that a variable name is allowed in Fortran.

is_valid_fortran_namelist_literal

Determine whether a literal is valid in a Fortran namelist.

literal_to_python_value

Convert a Fortran literal string to a Python value.

merge_literal_lists

Merge two lists of literal value strings.

parse

Parse a Fortran namelist.

string_to_character_literal

Convert a Python string to a Fortran character literal.

Classes

Namelist

Class representing a Fortran namelist.