Is there a way to specify or standardize the way a namelist is written to output?
For example, if I have:
namelist /stuff/ stuff1,stuff2,stuff3%more1,...
and then I want to write it to a file or standard out, such as:
write (9,stuff)
What is writes looks very different depending on the compiler and all character arrays are printed with all their characters even if they are blank.
Therefore the output is awful to look at, and impossible to parse in a portable way due to the changes.
Is there any way the Fortran language could standardize and/or allow formatting templates for namelist writes?
Or, failing that, does anyone know of a simple Fortran library that can write out a namelist in a portable sane way?
Is your purpose to have the namelist output as an easy means to read the values back in again in another Fortran program (or to restart the current one) or do you want to get output that can be used in a completely different context?
In the latter case, by far the easiest way is to use formatted output, so that you have almost full control over the layout and other details. Namelists are not meant for this type of control.
The remarkable thing is that Intel Fortran strips the delimiters from the strings but gfortran requires them. So Intel Fortran can read the file produced by gfortran but not vice versa.
The standard says that the default for DELIM= is “NONE”. This applies to both list-directed and namelist-directed output. The standard then says, for namelist output:
Namelist output records produced with a DELIM= specifier with a value of NONE and which contain
a character sequence might not be acceptable as namelist input records.
For namelist input, a lack of delimiters is fine for character input, as long as there are no value separators in the string read. For gfortran to give an error in this case would be improper. I hope you are misremembering what it does. If there are no delimiters, then it is standard-conforming to read a character value up until the next value separator (whitespace, comma, slash).
@marshallward , thanks for the quote on input. I was mistaken in saying that it is standard-conforming for a NAMELIST character sequence to not be delimited; that Intel accepts this is an extension (documented as such.) So gfortran is correct to reject such input.
It would have been best if, when NAMELIST was formally added to the standard in F90, it had been specified that delimiters were always output for NAMELIST. I don’t know if it was considered (though one would then need a separate way to specify WHICH delimiters were to be used.)
The motivation is for the code to spit out ALL parameters (since reading the input file as a namelist allows the user to supply a subset of parameters) so that the user can know what exact parameters were used for the run. Since there are hundreds of parameters and the code is under constant development, we do not want to have to fix the formats for each one hard-coded.
The namelist gives great flexibility, but reading the output of writing the namelist is pretty bad.
In our case, we have many 256 long character arrays that “can” be specified, so in the namelist output there are TONS of blank space.
I suppose we could write the namelist and then re-read it and strip out the spaces but it seems to me that the namelist in Fortran has such potential and seems to not be discussed or used often anymore?
Some kind of standard write output format (or preferably some high-level format options - such as “do not print trailing spaces in strings”) would be really helpful.
I agree with your sentiment re: NAMELIST entirely. NAMELIST can be so useful but unfortunately it seems to get less attention than it deserves. And with a little bit of extra work on it in the language standard, it can be JSON-like (or JSON-lite) and it can provide great benefits in simple and convenient exchange of information (data) between users and programs and also among programs.
Please see this link at the GitHub site for Fortran proposals where @marshallward above had started a related thread:
You may want to consider posting your own proposal(s), or collaborating on others such as those by @marshallward , or lending your support and comments / feedback to them.
Is there any “classification” to these “hundreds of parameters” and are they grouped together in any manner, say with object-oriented design in terms of “classes” iaka as derived types in Fortran?
By some such organization, you may find it easier to handle the input and run data communication with users.