Parsing a setup ASCII file

Hi everyone,

I have a large code that has 100+ setup options (strings, logical, integer, float, arrays with variable number of elements (strings or integers) that are stored in an ASCII file and loaded as a namelist (so the variables that are not set in the file are automatically set to their default values). In the output, I have to print all these values together with their values. I do it simply in one line by writing the namelist.

First problem - the output is unformatted, ugly and difficult to easily read. Second problem, I would also like to flush the entire list of the setup options as attributes to an h5 file.

It seems like an elegant solution to keep all the options stored in a structure. The only way I can think of is to do it half-manually, but that seems like a primitive solution.

On the other hand, going for xml/json seems a bit like an overkill and I’d prefer to keep it as simple as possible.

Is there a library/module freely available that could do the job?

Many thanks for your time and replies!

Perhaps something like this may be of interest to you:

I have seen it in the packages section, but didn’t get how can I define which variables are to be read from the input file. Can I have them all in a structure?

I am not sure what the format of the files is you are trying to read, but I made a module to deal with INI files that handles this type of things in an almost automatic way. See

Thanks, Arjen.

Three questions:

  1. Can one have groups of variables as in the namelists? For example, from the first group I read a parameter that tells me how many elements has array that I have to allocate and fill with the data from another group.
  2. Would it be easy to change the comment character from “#” to “!”? (mainly for back-compatibility issues.
  3. Would it require much changes to work with structure? I mean to fill the results into a structure?

Well, changing the comment character is almost trivial :). So that answers your second question.

As for the third question: I have never tried it, but I do not see there is a difference between a scalar x and a component x%y - you should be able to specify both. What you cannot do (at least not without providing some user-defined code) is specifying a derived type. One reason: Fortran does not offer introspection. Hence you cannot get a list of the components and then read the values for each component in a general way. That would definitely require some specific code that “knows” about the structure.

Your first question: how about reading the file twice? First get the sizes of the arrays, then allocate the arrays after which (with a different list of variables) you fill them.

Note: this is merely speculating on how this could be done - I have not worked out the details yet :slight_smile: Some extension of the format is possibly required, especially if you have arrays of a derived type.

1 Like

I recently had to do a refactoring for an ASCII input file of an in-house code,
tried namelists, and stored my experience here (which comes from other source , properly linked) : Working nicely with Fortran Namelists · The COOP Blog
Maybe this could help you.