6

I am using GNU Fortran (GCC) 4.8.2

I want to read allocatable arrays from a namelist. But I don't know in advance how many elements have to be read into the allocatable array, so I cannot allocate it before to read the namelist.

This is my namelist: namelist.nml:

&SECTION_1
    intList = 5,6,7
/ ! End of SECTION_1

And this is my program: namelist.f08:

program namelist
    implicit none

    integer, allocatable    :: intList(:)
    integer                 :: U    ! Unit to read the namelist file

    namelist /SECTION_1/ intList

    !allocate(intList(3)) ! <-- If I uncomment this, the program works.
    open(NEWUNIT=U, file="namelist.nml", status='OLD', recl=80, delim='APOSTROPHE')
    rewind(U)
    read(U, nml=SECTION_1)
    close(U)

    write (*,*) intList
end program namelist

If I uncomment the labeled line, the program works, but, as I said before, I cannot allocate before to read the namelist. Does anybody know how to accomplish this?.

Antonio Serrano
  • 385
  • 2
  • 15
  • Are you happy to allocate to larger than required, read in, then resize (based on null values)? Essentially, whenever you read into an allocatable object it has to be allocated. – francescalus Dec 02 '16 at 15:17
  • Instead of basing the resize on null values you could include a second parameter in the namelist which is just an integer which defines the size -- this has the advantage of not making any value special but has the downside of being more fragile (have to change two things if you want to add an element). – d_1999 Dec 02 '16 at 15:54
  • 1
    To follow up -- if you're happy with the fragility of an extra input that defines the length then you could just put that in a second namelist, which is read first and used to allocate the array appropriately. – d_1999 Dec 02 '16 at 15:57
  • 1
    Note the namelist input is missing the terminating slash. – IanH Dec 02 '16 at 23:54
  • Thank you all. I think that I'll feel happier if I write a function that pre-processes the namelist in the sense that it counts the number of elements of a given parameter in a given section. @IanH, is it not enough to use the '&END' termination?. – Antonio Serrano Dec 05 '16 at 09:00
  • 2
    `&END` indicates the **start** of namelist input for a namelist named END. You use a slash character to terminate namelist input. I am a little surprised that the processor doesn't complain at runtime about that. Note that you can read in arbitrary length namelist input using Fortran 2003's UDDTIO feature, but that would require gfortran >= 7.0. – IanH Dec 05 '16 at 09:48

1 Answers1

1

There are informal calls for proposals for automatic allocation when reading for the future Fortran standards. At least for character data to be able to read lines of text of unknown length https://github.com/j3-fortran/fortran_proposals/issues/9 An actual proposal exists for automatic allocation for IOMSG or ERRMSG specifiers in I/O statements https://j3-fortran.org/doc/year/19/19-252.txt

But nothing is finished, AFAIK. There is no automatic solution in current Fortran.

You must allocate to a large enough size before reading. There is no other way. There are other ways how to read data and there are custom data formats and custom parsers. Namelists are not everything even if often convenient.